blob: 35811d38a233e144814a4246101184161e03494c [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Romain Guy629de3e2010-01-13 12:20:59 -080019import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070020import android.content.BroadcastReceiver;
21import android.content.ComponentName;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070022import android.content.ContentProviderOperation;
23import android.content.ContentResolver;
24import android.content.ContentValues;
25import android.content.Context;
26import android.content.Intent;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070027import android.content.IntentFilter;
Sunny Goyal3e9be432017-01-05 15:22:41 -080028import android.content.pm.LauncherActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.net.Uri;
Joe Onorato36115782010-06-17 13:28:48 -040030import android.os.Handler;
31import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070032import android.os.Looper;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040034import android.os.SystemClock;
Sunny Goyale26d1002016-06-20 14:52:14 -070035import android.os.Trace;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080036import android.os.UserHandle;
Winson Chunga90303b2013-11-15 13:05:06 -080037import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070038import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080039import android.util.LongSparseArray;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070040import android.util.MutableInt;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010041
Sunny Goyalffe83f12014-08-14 17:39:34 -070042import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010043import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070044import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070045import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010046import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyal3d706ad2017-03-06 16:56:39 -080047import com.android.launcher3.config.FeatureFlags;
Tony Wickham827cef22016-03-17 15:39:39 -070048import com.android.launcher3.dynamicui.ExtractionUtils;
Sunny Goyal26119432016-02-18 22:09:23 +000049import com.android.launcher3.folder.Folder;
50import com.android.launcher3.folder.FolderIcon;
Jon Miranda655ec422017-02-07 11:40:22 -080051import com.android.launcher3.folder.FolderIconPreviewVerifier;
Sunny Goyal1b072632017-01-18 11:30:23 -080052import com.android.launcher3.graphics.LauncherIcons;
Sunny Goyal1acb9e92016-05-16 12:41:09 -070053import com.android.launcher3.logging.FileLog;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070054import com.android.launcher3.model.AddWorkspaceItemsTask;
Sunny Goyale9956a72016-09-01 17:24:47 -070055import com.android.launcher3.model.BgDataModel;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070056import com.android.launcher3.model.CacheDataUpdatedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080057import com.android.launcher3.model.ExtendedModelTask;
Sunny Goyalf862a262015-12-14 14:27:38 -080058import com.android.launcher3.model.GridSizeMigrationTask;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -080059import com.android.launcher3.model.LoaderCursor;
Sunny Goyal43bf11d2017-02-02 13:52:53 -080060import com.android.launcher3.model.ModelWriter;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070061import com.android.launcher3.model.PackageInstallStateChangedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080062import com.android.launcher3.model.PackageItemInfo;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070063import com.android.launcher3.model.PackageUpdatedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080064import com.android.launcher3.model.SdCardAvailableReceiver;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070065import com.android.launcher3.model.ShortcutsChangedTask;
66import com.android.launcher3.model.UserLockStateChangedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080067import com.android.launcher3.model.WidgetItem;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070068import com.android.launcher3.model.WidgetsModel;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -070069import com.android.launcher3.provider.ImportDataTask;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070070import com.android.launcher3.provider.LauncherDbUtils;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070071import com.android.launcher3.shortcuts.DeepShortcutManager;
72import com.android.launcher3.shortcuts.ShortcutInfoCompat;
73import com.android.launcher3.shortcuts.ShortcutKey;
Robin Lee26ace122015-03-16 19:41:43 +000074import com.android.launcher3.util.ComponentKey;
Sunny Goyalb265ba72017-02-14 15:03:45 -080075import com.android.launcher3.util.LooperIdleLock;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070076import com.android.launcher3.util.ManagedProfileHeuristic;
Tony Wickhamd82a39d2016-07-01 15:44:13 -070077import com.android.launcher3.util.MultiHashMap;
Sunny Goyald09c3702016-04-06 16:18:20 -070078import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070079import com.android.launcher3.util.Preconditions;
Sunny Goyal2bcbe132016-11-16 09:23:42 -080080import com.android.launcher3.util.Provider;
Adam Cohen091440a2015-03-18 14:16:05 -070081import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070082import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080083
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -080084import java.io.FileDescriptor;
85import java.io.PrintWriter;
Michael Jurkac2f801e2011-07-12 14:19:46 -070086import java.lang.ref.WeakReference;
Michael Jurkac2f801e2011-07-12 14:19:46 -070087import java.util.ArrayList;
88import java.util.Collections;
89import java.util.Comparator;
90import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070091import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070092import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070093import java.util.List;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070094import java.util.Map;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070095import java.util.Set;
Sunny Goyaldd96a5e2017-02-17 11:22:34 -080096import java.util.concurrent.CancellationException;
Sunny Goyal527c7d32015-08-28 15:19:36 -070097import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -070098
The Android Open Source Project31dd5032009-03-03 19:32:27 -080099/**
100 * Maintains in-memory state of the Launcher. It is expected that there should be only one
101 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700102 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800103 */
Kenny Guyed131872014-04-30 03:02:21 +0100104public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +0100105 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800106 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -0400107 private static final boolean DEBUG_RECEIVER = false;
Chris Wrenb358f812014-04-16 13:37:00 -0400108
Joe Onorato9c1289c2009-08-17 11:03:03 -0400109 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700110
Joe Onorato36115782010-06-17 13:28:48 -0400111 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500112 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800113
Sunny Goyalb265ba72017-02-14 15:03:45 -0800114 private final MainThreadExecutor mUiExecutor = new MainThreadExecutor();
Adam Cohen091440a2015-03-18 14:16:05 -0700115 @Thunk final LauncherAppState mApp;
116 @Thunk final Object mLock = new Object();
Adam Cohen091440a2015-03-18 14:16:05 -0700117 @Thunk LoaderTask mLoaderTask;
118 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700119 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120
Adam Cohen091440a2015-03-18 14:16:05 -0700121 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700122 static {
123 sWorkerThread.start();
124 }
Adam Cohen091440a2015-03-18 14:16:05 -0700125 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700126
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800127 // Indicates whether the current model data is valid or not.
128 // We start off with everything not loaded. After that, we assume that
Joe Onoratocc67f472010-06-08 10:54:30 -0700129 // our monitoring of the package manager provides all updates and we never
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800130 // need to do a requery. This is only ever touched from the loader thread.
131 private boolean mModelLoaded;
Hyunyoung Song6aa37292017-02-06 10:46:24 -0800132 public boolean isModelLoaded() {
133 synchronized (mLock) {
134 return mModelLoaded && mLoaderTask == null;
135 }
136 }
Joe Onoratocc67f472010-06-08 10:54:30 -0700137
Sunny Goyal756a28a2015-04-23 17:07:55 -0700138 /**
139 * Set of runnables to be called on the background thread after the workspace binding
140 * is complete.
141 */
142 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
143
Adam Cohen091440a2015-03-18 14:16:05 -0700144 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700146 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800147 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700148 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800149 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800150
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700151 private boolean mHasShortcutHostPermission;
152 // Runnable to check if the shortcuts permission has changed.
153 private final Runnable mShortcutPermissionCheckRunnable = new Runnable() {
154 @Override
155 public void run() {
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800156 if (mModelLoaded) {
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530157 boolean hasShortcutHostPermission =
158 DeepShortcutManager.getInstance(mApp.getContext()).hasHostPermission();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700159 if (hasShortcutHostPermission != mHasShortcutHostPermission) {
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800160 forceReload();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700161 }
162 }
163 }
164 };
165
Sunny Goyale9956a72016-09-01 17:24:47 -0700166 /**
167 * All the static data should be accessed on the background thread, A lock should be acquired
168 * on this object when accessing any data from this model.
169 */
170 static final BgDataModel sBgDataModel = new BgDataModel();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700171
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700172 // </ only access in worker thread >
173
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700174 private final IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700176 private final LauncherAppsCompat mLauncherApps;
177 private final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100178
Joe Onorato9c1289c2009-08-17 11:03:03 -0400179 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700180 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400181 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700182 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400183 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700184 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
185 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700186 public void bindScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700187 public void finishFirstPageBind(ViewOnDrawExecutor executor);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800188 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400189 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200190 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700191 public void bindAppsAdded(ArrayList<Long> newScreens,
192 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700193 ArrayList<ItemInfo> addAnimated,
194 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200195 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700196 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800197 ArrayList<ShortcutInfo> removed, UserHandle user);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700198 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700199 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700200 public void bindWorkspaceComponentsRemoved(
201 HashSet<String> packageNames, HashSet<ComponentName> components,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800202 UserHandle user);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700203 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800204 public void notifyWidgetProvidersChanged();
Sunny Goyald164b7f2016-10-12 20:49:31 -0700205 public void bindAllWidgets(MultiHashMap<PackageItemInfo, WidgetItem> widgets);
Adam Cohen1462de32012-07-24 22:34:36 -0700206 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700207 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700208 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMap);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400209 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530211 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800212 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400213 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100214 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Sunny Goyald164b7f2016-10-12 20:49:31 -0700215 mBgWidgetsModel = new WidgetsModel(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800216 mIconCache = iconCache;
217
Kenny Guyed131872014-04-30 03:02:21 +0100218 mLauncherApps = LauncherAppsCompat.getInstance(context);
219 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800220 }
221
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700222 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
223 * posted on the worker thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700224 private static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700225 if (sWorkerThread.getThreadId() == Process.myTid()) {
226 r.run();
227 } else {
228 // If we are not on the worker thread, then post to the worker handler
229 sWorker.post(r);
230 }
231 }
232
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700233 public void setPackageState(PackageInstallInfo installInfo) {
234 enqueueModelUpdateTask(new PackageInstallStateChangedTask(installInfo));
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500235 }
236
Sunny Goyal756adbc2015-04-16 15:20:51 -0700237 /**
238 * Updates the icons and label of all pending icons for the provided package name.
239 */
240 public void updateSessionDisplayInfo(final String packageName) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700241 HashSet<String> packages = new HashSet<>();
242 packages.add(packageName);
243 enqueueModelUpdateTask(new CacheDataUpdatedTask(
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800244 CacheDataUpdatedTask.OP_SESSION_UPDATE, Process.myUserHandle(), packages));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800245 }
246
247 /**
248 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800249 */
Sunny Goyal2bcbe132016-11-16 09:23:42 -0800250 public void addAndBindAddedWorkspaceItems(List<ItemInfo> workspaceApps) {
251 addAndBindAddedWorkspaceItems(Provider.of(workspaceApps));
252 }
253
254 /**
255 * Adds the provided items to the workspace.
256 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700257 public void addAndBindAddedWorkspaceItems(
Sunny Goyal2bcbe132016-11-16 09:23:42 -0800258 Provider<List<ItemInfo>> appsProvider) {
259 enqueueModelUpdateTask(new AddWorkspaceItemsTask(appsProvider));
Winson Chung64359a52013-07-08 17:17:08 -0700260 }
261
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800262 public ModelWriter getWriter(boolean hasVerticalHotseat) {
263 return new ModelWriter(mApp.getContext(), sBgDataModel, hasVerticalHotseat);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 }
265
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700266 static void checkItemInfoLocked(
267 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
Sunny Goyale9956a72016-09-01 17:24:47 -0700268 ItemInfo modelItem = sBgDataModel.itemsIdMap.get(itemId);
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700269 if (modelItem != null && item != modelItem) {
270 // check all the data is consistent
271 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
272 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
273 ShortcutInfo shortcut = (ShortcutInfo) item;
274 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
275 modelShortcut.intent.filterEquals(shortcut.intent) &&
276 modelShortcut.id == shortcut.id &&
277 modelShortcut.itemType == shortcut.itemType &&
278 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700279 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700280 modelShortcut.cellX == shortcut.cellX &&
281 modelShortcut.cellY == shortcut.cellY &&
282 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700283 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700284 // For all intents and purposes, this is the same object
285 return;
286 }
287 }
288
289 // the modelItem needs to match up perfectly with item if our model is
290 // to be consistent with the database-- for now, just require
291 // modelItem == item or the equality check above
292 String msg = "item: " + ((item != null) ? item.toString() : "null") +
293 "modelItem: " +
294 ((modelItem != null) ? modelItem.toString() : "null") +
295 "Error: ItemInfo passed to checkItemInfo doesn't match original";
296 RuntimeException e = new RuntimeException(msg);
297 if (stackTrace != null) {
298 e.setStackTrace(stackTrace);
299 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800300 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700301 }
302 }
303
Michael Jurka816474f2012-06-25 14:49:02 -0700304 static void checkItemInfo(final ItemInfo item) {
305 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
306 final long itemId = item.id;
307 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700308 public void run() {
Sunny Goyale9956a72016-09-01 17:24:47 -0700309 synchronized (sBgDataModel) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700310 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700311 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700312 }
313 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700314 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700315 }
316
Joe Onorato9c1289c2009-08-17 11:03:03 -0400317 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700318 * Update the order of the workspace screens in the database. The array list contains
319 * a list of screen ids in the order that they should appear.
320 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700321 public static void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -0700322 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700323 final ContentResolver cr = context.getContentResolver();
324 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
325
326 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700327 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700328 while (iter.hasNext()) {
329 long id = iter.next();
330 if (id < 0) {
331 iter.remove();
332 }
333 }
334
335 Runnable r = new Runnable() {
336 @Override
337 public void run() {
Yura085c8532014-02-11 15:15:29 +0000338 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700339 // Clear the table
Yura085c8532014-02-11 15:15:29 +0000340 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -0700341 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700342 for (int i = 0; i < count; i++) {
343 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700344 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700345 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
346 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +0000347 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -0700348 }
Yura085c8532014-02-11 15:15:29 +0000349
350 try {
351 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
352 } catch (Exception ex) {
353 throw new RuntimeException(ex);
354 }
Winson Chung9e6a0a22013-08-27 11:58:12 -0700355
Sunny Goyale9956a72016-09-01 17:24:47 -0700356 synchronized (sBgDataModel) {
357 sBgDataModel.workspaceScreens.clear();
358 sBgDataModel.workspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -0700359 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700360 }
361 };
362 runOnWorkerThread(r);
363 }
364
365 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400366 * Set this as the current Launcher activity object for the loader.
367 */
368 public void initialize(Callbacks callbacks) {
369 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -0700370 Preconditions.assertUIThread();
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -0700371 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400372 }
373 }
374
Kenny Guyed131872014-04-30 03:02:21 +0100375 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800376 public void onPackageChanged(String packageName, UserHandle user) {
Kenny Guyed131872014-04-30 03:02:21 +0100377 int op = PackageUpdatedTask.OP_UPDATE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700378 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100379 }
380
381 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800382 public void onPackageRemoved(String packageName, UserHandle user) {
Sunny Goyalc2936bc2016-09-01 15:50:36 -0700383 onPackagesRemoved(user, packageName);
384 }
385
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800386 public void onPackagesRemoved(UserHandle user, String... packages) {
Kenny Guyed131872014-04-30 03:02:21 +0100387 int op = PackageUpdatedTask.OP_REMOVE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700388 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packages));
Kenny Guyed131872014-04-30 03:02:21 +0100389 }
390
391 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800392 public void onPackageAdded(String packageName, UserHandle user) {
Kenny Guyed131872014-04-30 03:02:21 +0100393 int op = PackageUpdatedTask.OP_ADD;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700394 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100395 }
396
397 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800398 public void onPackagesAvailable(String[] packageNames, UserHandle user,
Kenny Guyed131872014-04-30 03:02:21 +0100399 boolean replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700400 enqueueModelUpdateTask(
401 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100402 }
403
404 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800405 public void onPackagesUnavailable(String[] packageNames, UserHandle user,
Kenny Guyed131872014-04-30 03:02:21 +0100406 boolean replacing) {
407 if (!replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700408 enqueueModelUpdateTask(new PackageUpdatedTask(
409 PackageUpdatedTask.OP_UNAVAILABLE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100410 }
Kenny Guyed131872014-04-30 03:02:21 +0100411 }
412
Kenny Guy44cba692016-01-21 19:50:02 +0000413 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800414 public void onPackagesSuspended(String[] packageNames, UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700415 enqueueModelUpdateTask(new PackageUpdatedTask(
416 PackageUpdatedTask.OP_SUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000417 }
418
419 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800420 public void onPackagesUnsuspended(String[] packageNames, UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700421 enqueueModelUpdateTask(new PackageUpdatedTask(
422 PackageUpdatedTask.OP_UNSUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000423 }
424
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700425 @Override
426 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800427 UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700428 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, true));
Sunny Goyal50941fb2016-08-04 12:03:52 -0700429 }
430
431 public void updatePinnedShortcuts(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800432 UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700433 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, false));
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700434 }
435
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700436 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400437 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
438 * ACTION_PACKAGE_CHANGED.
439 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100440 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400441 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -0400442 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700443
Joe Onorato36115782010-06-17 13:28:48 -0400444 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +0100445 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -0700446 // If we have changed locale we need to clear out the labels in all apps/workspace.
447 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700448 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
449 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -0700450 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -0700451 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700452 } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
453 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
454 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800455 UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER);
Sunny Goyalda891c12016-03-18 18:29:24 -0700456 if (user != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700457 if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
458 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700459 enqueueModelUpdateTask(new PackageUpdatedTask(
460 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE, user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700461 }
462
463 // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so
464 // we need to run the state change task again.
465 if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
466 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700467 enqueueModelUpdateTask(new UserLockStateChangedTask(user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700468 }
Sunny Goyalda891c12016-03-18 18:29:24 -0700469 }
Tony Wickham827cef22016-03-17 15:39:39 -0700470 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
471 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700472 }
473 }
474
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800475 /**
476 * Reloads the workspace items from the DB and re-binds the workspace. This should generally
477 * not be called as DB updates are automatically followed by UI update
478 */
479 public void forceReload() {
480 synchronized (mLock) {
481 // Stop any existing loaders first, so they don't set mModelLoaded to true later
482 stopLoaderLocked();
483 mModelLoaded = false;
484 }
Winson Chungf0c6ae02012-03-21 16:10:31 -0700485
Reena Lee93f824a2011-09-23 17:20:28 -0700486 // Do this here because if the launcher activity is running it will be restarted.
487 // If it's not running startLoaderFromBackground will merely tell it that it needs
488 // to reload.
489 startLoaderFromBackground();
490 }
491
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700492 /**
493 * When the launcher is in the background, it's possible for it to miss paired
494 * configuration changes. So whenever we trigger the loader from the background
495 * tell the launcher that it needs to re-run the loader when it comes back instead
496 * of doing it now.
497 */
498 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800499 Callbacks callbacks = getCallback();
500 if (callbacks != null) {
501 // Only actually run the loader if they're not paused.
502 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -0700503 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700504 }
505 }
Joe Onorato36115782010-06-17 13:28:48 -0400506 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400507
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700508 /**
509 * If there is already a loader task running, tell it to stop.
510 */
511 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -0700512 LoaderTask oldTask = mLoaderTask;
513 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -0700514 oldTask.stopLocked();
515 }
Reena Lee93f824a2011-09-23 17:20:28 -0700516 }
517
Adam Cohen1a85c582014-09-30 09:48:49 -0700518 public boolean isCurrentCallbacks(Callbacks callbacks) {
519 return (mCallbacks != null && mCallbacks.get() == callbacks);
520 }
521
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700522 /**
523 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
524 * @return true if the page could be bound synchronously.
525 */
526 public boolean startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700527 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
528 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -0400529 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -0400530 // Don't bother to start the thread if we know it's not going to do anything
531 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -0700532 final Callbacks oldCallbacks = mCallbacks.get();
533 // Clear any pending bind-runnables from the synchronized load process.
Sunny Goyalb265ba72017-02-14 15:03:45 -0800534 mUiExecutor.execute(new Runnable() {
535 public void run() {
536 oldCallbacks.clearPendingBinds();
537 }
538 });
Sunny Goyal527c7d32015-08-28 15:19:36 -0700539
Joe Onorato36115782010-06-17 13:28:48 -0400540 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700541 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700542 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800543 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
544 && mModelLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700545 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700546 return true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700547 } else {
548 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
549 sWorker.post(mLoaderTask);
550 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400551 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400552 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700553 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400554 }
555
Joe Onorato36115782010-06-17 13:28:48 -0400556 public void stopLoader() {
557 synchronized (mLock) {
558 if (mLoaderTask != null) {
559 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400560 }
561 }
Joe Onorato36115782010-06-17 13:28:48 -0400562 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400563
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800564 /**
565 * Loads the workspace screen ids in an ordered list.
566 */
Sunny Goyale5bb7052015-07-27 14:36:07 -0700567 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -0700568 final ContentResolver contentResolver = context.getContentResolver();
569 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -0700570
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800571 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -0700572 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
573 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -0700574 }
575
Joe Onorato36115782010-06-17 13:28:48 -0400576 /**
577 * Runnable for the thread that loads the contents of the launcher:
578 * - workspace icons
579 * - widgets
580 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700581 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -0400582 */
583 private class LoaderTask implements Runnable {
584 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -0700585 private int mPageToBindFirst;
586
Adam Cohen091440a2015-03-18 14:16:05 -0700587 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -0400588 private boolean mStopped;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700589
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700590 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -0400591 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -0700592 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400593 }
594
Joe Onorato36115782010-06-17 13:28:48 -0400595 private void waitForIdle() {
596 // Wait until the either we're stopped or the other threads are done.
597 // This way we don't start loading all apps until the workspace has settled
598 // down.
599 synchronized (LoaderTask.this) {
Sunny Goyalb265ba72017-02-14 15:03:45 -0800600 LooperIdleLock idleLock = new LooperIdleLock(this, Looper.getMainLooper());
601 // Just in case mFlushingWorkerThread changes but we aren't woken up,
602 // wait no longer than 1sec at a time
603 while (!mStopped && idleLock.awaitLocked(1000));
Daniel Sandler843e8602010-06-07 14:59:01 -0400604 }
Joe Onorato36115782010-06-17 13:28:48 -0400605 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400606
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700607 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -0500608 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700609 // Ensure that we have a valid page index to load synchronously
610 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
611 "valid page index");
612 }
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800613 if (!mModelLoaded) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700614 // Ensure that we don't try and bind a specified page when the pages have not been
615 // loaded already (we should load everything asynchronously in that case)
616 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
617 }
618 synchronized (mLock) {
619 if (mIsLoaderTaskRunning) {
620 // Ensure that we are never running the background loading at this point since
621 // we also touch the background collections
622 throw new RuntimeException("Error! Background loading is already running");
623 }
624 }
625
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700626 // Divide the set of loaded items into those that we are binding synchronously, and
627 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800628 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700629 // XXX: For now, continue posting the binding of AllApps as there are other issues that
630 // arise from that.
631 onlyBindAllApps();
Tony Wickham80f57872016-06-29 18:12:15 -0700632
633 bindDeepShortcuts();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700634 }
635
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800636 private void verifyNotStopped() throws CancellationException {
637 synchronized (LoaderTask.this) {
638 if (mStopped) {
639 throw new CancellationException("Loader stopped");
640 }
641 }
642 }
643
Joe Onorato36115782010-06-17 13:28:48 -0400644 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700645 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -0700646 if (mStopped) {
647 return;
648 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700649 mIsLoaderTaskRunning = true;
650 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400651
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800652 try {
Sunny Goyalb265ba72017-02-14 15:03:45 -0800653 long now = 0;
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800654 if (DEBUG_LOADERS) Log.d(TAG, "step 1.1: loading workspace");
655 // Set to false in bindWorkspace()
656 mIsLoadingAndBindingWorkspace = true;
657 loadWorkspace();
Joe Onorato36115782010-06-17 13:28:48 -0400658
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800659 verifyNotStopped();
660 if (DEBUG_LOADERS) Log.d(TAG, "step 1.2: bind workspace workspace");
661 bindWorkspace(mPageToBindFirst);
662
663 // Take a break
Sunny Goyalb265ba72017-02-14 15:03:45 -0800664 if (DEBUG_LOADERS) {
665 Log.d(TAG, "step 1 completed, wait for idle");
666 now = SystemClock.uptimeMillis();
667 }
Joe Onorato36115782010-06-17 13:28:48 -0400668 waitForIdle();
Sunny Goyalb265ba72017-02-14 15:03:45 -0800669 if (DEBUG_LOADERS) Log.d(TAG, "Waited " + (SystemClock.uptimeMillis() - now) + "ms");
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800670 verifyNotStopped();
Daniel Sandler843e8602010-06-07 14:59:01 -0400671
672 // second step
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800673 if (DEBUG_LOADERS) Log.d(TAG, "step 2.1: loading all apps");
674 loadAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700675
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800676 verifyNotStopped();
677 if (DEBUG_LOADERS) Log.d(TAG, "step 2.2: Update icon cache");
678 updateIconCache();
679
680 // Take a break
Sunny Goyalb265ba72017-02-14 15:03:45 -0800681 if (DEBUG_LOADERS) {
682 Log.d(TAG, "step 2 completed, wait for idle");
683 now = SystemClock.uptimeMillis();
684 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700685 waitForIdle();
Sunny Goyalb265ba72017-02-14 15:03:45 -0800686 if (DEBUG_LOADERS) Log.d(TAG, "Waited " + (SystemClock.uptimeMillis() - now) + "ms");
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800687 verifyNotStopped();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700688
689 // third step
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800690 if (DEBUG_LOADERS) Log.d(TAG, "step 3.1: loading deep shortcuts");
691 loadDeepShortcuts();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400692
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800693 verifyNotStopped();
694 if (DEBUG_LOADERS) Log.d(TAG, "step 3.2: bind deep shortcuts");
695 bindDeepShortcuts();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400696
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800697 synchronized (mLock) {
698 // Everything loaded bind the data.
699 mModelLoaded = true;
700 mHasLoaderCompletedOnce = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400701 }
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800702 } catch (CancellationException e) {
703 // Loader stopped, ignore
704 } finally {
705 // Clear out this reference, otherwise we end up holding it until all of the
706 // callback runnables are done.
707 mContext = null;
708
709 synchronized (mLock) {
710 // If we are still the last one to be scheduled, remove ourselves.
711 if (mLoaderTask == this) {
712 mLoaderTask = null;
713 }
714 mIsLoaderTaskRunning = false;
715 }
Joe Onorato36115782010-06-17 13:28:48 -0400716 }
Joe Onorato36115782010-06-17 13:28:48 -0400717 }
718
719 public void stopLocked() {
720 synchronized (LoaderTask.this) {
721 mStopped = true;
722 this.notify();
723 }
724 }
725
726 /**
727 * Gets the callbacks object. If we've been stopped, or if the launcher object
728 * has somehow been garbage collected, return null instead. Pass in the Callbacks
729 * object that was around when the deferred message was scheduled, and if there's
730 * a new Callbacks object around then also return null. This will save us from
731 * calling onto it with data that will be ignored.
732 */
733 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
734 synchronized (mLock) {
735 if (mStopped) {
736 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400737 }
Joe Onorato36115782010-06-17 13:28:48 -0400738
739 if (mCallbacks == null) {
740 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400741 }
Joe Onorato36115782010-06-17 13:28:48 -0400742
743 final Callbacks callbacks = mCallbacks.get();
744 if (callbacks != oldCallbacks) {
745 return null;
746 }
747 if (callbacks == null) {
748 Log.w(TAG, "no mCallbacks");
749 return null;
750 }
751
752 return callbacks;
753 }
754 }
755
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800756 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -0700757 if (LauncherAppState.PROFILE_STARTUP) {
758 Trace.beginSection("Loading Workspace");
759 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400760
Joe Onorato36115782010-06-17 13:28:48 -0400761 final Context context = mContext;
762 final ContentResolver contentResolver = context.getContentResolver();
Sunny Goyal342e4662017-02-02 15:21:08 -0800763 final PackageManagerHelper pmHelper = new PackageManagerHelper(context);
764 final boolean isSafeMode = pmHelper.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700765 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530766 final DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -0700767 final boolean isSdCardReady = Utilities.isBootCompleted();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800768 final MultiHashMap<UserHandle, String> pendingPackages = new MultiHashMap<>();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400769
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700770 boolean clearDb = false;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -0700771 try {
772 ImportDataTask.performImportIfPossible(context);
773 } catch (Exception e) {
774 // Migration failed. Clear workspace.
775 clearDb = true;
776 }
777
778 if (!clearDb && GridSizeMigrationTask.ENABLED &&
Sunny Goyalf076eae2016-01-11 12:25:10 -0800779 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
780 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700781 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -0700782 }
783
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700784 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -0700785 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -0700786 LauncherSettings.Settings.call(contentResolver,
Sunny Goyale05b08f2017-02-23 18:30:22 -0800787 LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -0500788 }
789
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700790 Log.d(TAG, "loadWorkspace: loading default favorites");
791 LauncherSettings.Settings.call(contentResolver,
792 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -0700793
Sunny Goyale9956a72016-09-01 17:24:47 -0700794 synchronized (sBgDataModel) {
795 sBgDataModel.clear();
796
Sunny Goyal756adbc2015-04-16 15:20:51 -0700797 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -0700798 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyale9956a72016-09-01 17:24:47 -0700799 sBgDataModel.workspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -0800800
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700801 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800802 final LoaderCursor c = new LoaderCursor(contentResolver.query(
803 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null), mApp);
Daniel Sandler8802e962010-05-26 16:28:16 -0400804
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800805 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400806
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700807 try {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700808 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
809 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -0400810 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
811 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700812 final int spanXIndex = c.getColumnIndexOrThrow
813 (LauncherSettings.Favorites.SPANX);
814 final int spanYIndex = c.getColumnIndexOrThrow(
815 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800816 final int rankIndex = c.getColumnIndexOrThrow(
817 LauncherSettings.Favorites.RANK);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700818 final int optionsIndex = c.getColumnIndexOrThrow(
819 LauncherSettings.Favorites.OPTIONS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400820
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800821 final LongSparseArray<UserHandle> allUsers = c.allUsers;
Kenny Guyff05f432016-01-22 17:48:29 +0000822 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700823 final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800824 for (UserHandle user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +0000825 long serialNo = mUserManager.getSerialNumberForUser(user);
826 allUsers.put(serialNo, user);
827 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700828
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700829 boolean userUnlocked = mUserManager.isUserUnlocked(user);
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700830
831 // We can only query for shortcuts when the user is unlocked.
832 if (userUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -0700833 List<ShortcutInfoCompat> pinnedShortcuts =
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530834 shortcutManager.queryForPinnedShortcuts(null, user);
835 if (shortcutManager.wasLastCallSuccess()) {
Sunny Goyal49f4f032016-08-01 15:45:49 -0700836 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
837 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
838 shortcut);
839 }
840 } else {
841 // Shortcut manager can fail due to some race condition when the
842 // lock state changes too frequently. For the purpose of the loading
843 // shortcuts, consider the user is still locked.
844 userUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700845 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700846 }
Sunny Goyal49f4f032016-08-01 15:45:49 -0700847 unlockedUsers.put(serialNo, userUnlocked);
Sunny Goyal7f834d22015-04-21 10:10:23 -0700848 }
849
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700850 ShortcutInfo info;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700851 LauncherAppWidgetInfo appWidgetInfo;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700852 Intent intent;
Sunny Goyal81e44912017-01-14 15:05:14 -0800853 String targetPkg;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400854
Jon Miranda529af302017-02-28 14:18:54 -0800855 FolderIconPreviewVerifier verifier =
856 new FolderIconPreviewVerifier(mApp.getInvariantDeviceProfile());
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700857 while (!mStopped && c.moveToNext()) {
858 try {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800859 if (c.user == null) {
860 // User has been deleted, remove the item.
861 c.markDeleted("User has been deleted");
862 continue;
863 }
864
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700865 boolean allowMissingTarget = false;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800866 switch (c.itemType) {
Sunny Goyal81e44912017-01-14 15:05:14 -0800867 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700868 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700869 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Sunny Goyal81e44912017-01-14 15:05:14 -0800870 intent = c.parseIntent();
871 if (intent == null) {
872 c.markDeleted("Invalid or null intent");
873 continue;
874 }
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800875
Sunny Goyal81e44912017-01-14 15:05:14 -0800876 int disabledState = quietMode.get(c.serialNumber) ?
877 ShortcutInfo.FLAG_DISABLED_QUIET_USER : 0;
878 ComponentName cn = intent.getComponent();
879 targetPkg = cn == null ? intent.getPackage() : cn.getPackageName();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700880
Sunny Goyal81e44912017-01-14 15:05:14 -0800881 if (!Process.myUserHandle().equals(c.user)) {
882 if (c.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
883 c.markDeleted("Legacy shortcuts are only allowed for default user");
884 continue;
885 } else if (c.restoreFlag != 0) {
886 // Don't restore items for other profiles.
887 c.markDeleted("Restore from managed profile not supported");
888 continue;
889 }
890 }
891 if (TextUtils.isEmpty(targetPkg) &&
892 c.itemType != LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
893 c.markDeleted("Only legacy shortcuts can have null package");
894 continue;
895 }
Sunny Goyal34942622014-08-29 17:20:55 -0700896
Sunny Goyal81e44912017-01-14 15:05:14 -0800897 // If there is no target package, its an implicit intent
898 // (legacy shortcut) which is always valid
899 boolean validTarget = TextUtils.isEmpty(targetPkg) ||
900 launcherApps.isPackageEnabledForProfile(targetPkg, c.user);
Sunny Goyal94485362014-09-18 16:13:58 -0700901
Sunny Goyal81e44912017-01-14 15:05:14 -0800902 if (cn != null && validTarget) {
903 // If the apk is present and the shortcut points to a specific
904 // component.
905
906 // If the component is already present
907 if (launcherApps.isActivityEnabledForProfile(cn, c.user)) {
908 // no special handling necessary for this item
909 c.markRestored();
910 } else {
911 if (c.hasRestoreFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
912 // We allow auto install apps to have their intent
913 // updated after an install.
Sunny Goyal342e4662017-02-02 15:21:08 -0800914 intent = pmHelper.getAppLaunchIntent(targetPkg, c.user);
Sunny Goyal81e44912017-01-14 15:05:14 -0800915 if (intent != null) {
916 c.restoreFlag = 0;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800917 c.updater().put(
Sunny Goyal81e44912017-01-14 15:05:14 -0800918 LauncherSettings.Favorites.INTENT,
919 intent.toUri(0)).commit();
920 cn = intent.getComponent();
Sunny Goyalb05a00a2016-08-29 10:06:57 -0700921 } else {
Sunny Goyal81e44912017-01-14 15:05:14 -0800922 c.markDeleted("Unable to find a launch target");
Sunny Goyal94485362014-09-18 16:13:58 -0700923 continue;
924 }
Sunny Goyal1a745e82014-10-02 15:58:31 -0700925 } else {
Sunny Goyal81e44912017-01-14 15:05:14 -0800926 // The app is installed but the component is no
927 // longer available.
928 c.markDeleted("Invalid component removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -0700929 continue;
Winson Chungee055712013-07-30 14:46:24 -0700930 }
Winson Chungee055712013-07-30 14:46:24 -0700931 }
Sunny Goyal81e44912017-01-14 15:05:14 -0800932 }
933 // else if cn == null => can't infer much, leave it
934 // else if !validPkg => could be restored icon or missing sd-card
935
936 if (!TextUtils.isEmpty(targetPkg) && !validTarget) {
937 // Points to a valid app (superset of cn != null) but the apk
938 // is not available.
939
940 if (c.restoreFlag != 0) {
941 // Package is not yet available but might be
942 // installed later.
943 FileLog.d(TAG, "package not yet restored: " + targetPkg);
944
945 if (c.hasRestoreFlag(ShortcutInfo.FLAG_RESTORE_STARTED)) {
946 // Restore has started once.
947 } else if (installingPkgs.containsKey(targetPkg)) {
948 // App restore has started. Update the flag
949 c.restoreFlag |= ShortcutInfo.FLAG_RESTORE_STARTED;
950 c.updater().commit();
951 } else {
952 c.markDeleted("Unrestored app removed: " + targetPkg);
953 continue;
954 }
Sunny Goyalad2e91a2017-02-28 15:14:58 -0800955 } else if (pmHelper.isAppOnSdcard(targetPkg, c.user)) {
Sunny Goyal81e44912017-01-14 15:05:14 -0800956 // Package is present but not available.
957 disabledState |= ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
958 // Add the icon on the workspace anyway.
959 allowMissingTarget = true;
960 } else if (!isSdCardReady) {
961 // SdCard is not ready yet. Package might get available,
962 // once it is ready.
963 Log.d(TAG, "Missing pkg, will check later: " + targetPkg);
964 pendingPackages.addToList(c.user, targetPkg);
965 // Add the icon on the workspace anyway.
966 allowMissingTarget = true;
967 } else {
968 // Do not wait for external media load anymore.
969 c.markDeleted("Invalid package removed: " + targetPkg);
970 continue;
971 }
972 }
973
974 if (validTarget) {
975 // The shortcut points to a valid target (either no target
976 // or something which is ready to be used)
977 c.markRestored();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700978 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400979
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800980 boolean useLowResIcon = !c.isOnWorkspaceOrHotseat() &&
Jon Miranda529af302017-02-28 14:18:54 -0800981 !verifier.isItemInPreview(c.getInt(rankIndex));
Sunny Goyal34b65272015-03-11 16:56:52 -0700982
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -0800983 if (c.restoreFlag != 0) {
Sunny Goyal81e44912017-01-14 15:05:14 -0800984 // Already verified above that user is same as default user
985 info = c.getRestoredItemInfo(intent);
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800986 } else if (c.itemType ==
Chris Wrenf4d08112014-01-16 18:13:56 -0500987 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800988 info = c.getAppShortcutInfo(
989 intent, allowMissingTarget, useLowResIcon);
990 } else if (c.itemType ==
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700991 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700992
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800993 ShortcutKey key = ShortcutKey.fromIntent(intent, c.user);
994 if (unlockedUsers.get(c.serialNumber)) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700995 ShortcutInfoCompat pinnedShortcut =
996 shortcutKeyToPinnedShortcuts.get(key);
997 if (pinnedShortcut == null) {
998 // The shortcut is no longer valid.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800999 c.markDeleted("Pinned shortcut not found");
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001000 continue;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001001 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001002 info = new ShortcutInfo(pinnedShortcut, context);
Sunny Goyal1b072632017-01-18 11:30:23 -08001003 info.iconBitmap = LauncherIcons
1004 .createShortcutIcon(pinnedShortcut, context);
Charles He3ff90472017-02-15 12:22:56 +00001005 if (pmHelper.isAppSuspended(
Sunny Goyalad2e91a2017-02-28 15:14:58 -08001006 pinnedShortcut.getPackage(), info.user)) {
Charles He3ff90472017-02-15 12:22:56 +00001007 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1008 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001009 intent = info.intent;
1010 } else {
1011 // Create a shortcut info in disabled mode for now.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001012 info = c.loadSimpleShortcut();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001013 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001014 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001015 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001016 info = c.loadSimpleShortcut();
Michael Jurka96879562012-03-22 05:54:33 -07001017
Sunny Goyald09c3702016-04-06 16:18:20 -07001018 // Shortcuts are only available on the primary profile
Sunny Goyalad2e91a2017-02-28 15:14:58 -08001019 if (pmHelper.isAppSuspended(targetPkg, c.user)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001020 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1021 }
1022
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001023 // App shortcuts that used to be automatically added to Launcher
1024 // didn't always have the correct intent flags set, so do that
1025 // here
1026 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001027 intent.getCategories() != null &&
1028 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001029 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001030 intent.addFlags(
1031 Intent.FLAG_ACTIVITY_NEW_TASK |
1032 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1033 }
Michael Jurka96879562012-03-22 05:54:33 -07001034 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001035
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001036 if (info != null) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001037 c.applyCommonProperties(info);
1038
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001039 info.intent = intent;
Sunny Goyal08f72612015-01-05 13:41:43 -08001040 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001041 info.spanX = 1;
1042 info.spanY = 1;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001043 // TODO: Remove this extra. Instead we should be using
1044 // itemInfo#user.
1045 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, c.serialNumber);
Sunny Goyald09c3702016-04-06 16:18:20 -07001046 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001047 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1048 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1049 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001050
Sunny Goyal81e44912017-01-14 15:05:14 -08001051 if (c.restoreFlag != 0 && !TextUtils.isEmpty(targetPkg)) {
1052 Integer progress = installingPkgs.get(targetPkg);
1053 if (progress != null) {
1054 info.setInstallProgress(progress);
1055 } else {
1056 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001057 }
1058 }
1059
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001060 c.checkAndAddItem(info, sBgDataModel);
Winson Chung1323b482013-08-05 12:41:55 -07001061 } else {
1062 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001063 }
1064 break;
1065
1066 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001067 FolderInfo folderInfo = sBgDataModel.findOrMakeFolder(c.id);
1068 c.applyCommonProperties(folderInfo);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001069
Sunny Goyala508e4f2015-05-21 09:33:57 -07001070 // Do not trim the folder label, as is was set by the user.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001071 folderInfo.title = c.getString(c.titleIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001072 folderInfo.spanX = 1;
1073 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001074 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001075
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001076 // no special handling required for restored folders
1077 c.markRestored();
Chris Wrenf4d08112014-01-16 18:13:56 -05001078
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001079 c.checkAndAddItem(folderInfo, sBgDataModel);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001080 break;
1081
1082 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08001083 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001084 // Read all Launcher-specific widget details
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001085 boolean customWidget = c.itemType ==
Adam Cohen59400422014-03-05 18:07:04 -08001086 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
1087
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001088 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001089 String savedProvider = c.getString(appWidgetProviderIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001090
Sunny Goyalff572272014-07-23 13:58:07 -07001091 final ComponentName component =
1092 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04001093
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001094 final boolean isIdValid = !c.hasRestoreFlag(
1095 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID);
1096 final boolean wasProviderReady = !c.hasRestoreFlag(
1097 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY);
Sunny Goyal651077b2014-06-30 14:15:31 -07001098
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001099 if (widgetProvidersMap == null) {
1100 widgetProvidersMap = AppWidgetManagerCompat
1101 .getInstance(mContext).getAllProvidersMap();
1102 }
1103 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
1104 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00001105 ComponentName.unflattenFromString(savedProvider),
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001106 c.user));
Sunny Goyalff572272014-07-23 13:58:07 -07001107
1108 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08001109 if (!isSafeMode && !customWidget &&
1110 wasProviderReady && !isProviderReady) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001111 c.markDeleted(
1112 "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07001113 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001114 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07001115 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07001116 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1117 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08001118
Sunny Goyal53f96722015-07-13 19:54:53 -07001119 // The provider is available. So the widget is either
1120 // available or not available. We do not need to track
1121 // any future restore updates.
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001122 int status = c.restoreFlag &
Sunny Goyal53f96722015-07-13 19:54:53 -07001123 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07001124 if (!wasProviderReady) {
1125 // If provider was not previously ready, update the
1126 // status and UI flag.
1127
1128 // Id would be valid only if the widget restore broadcast was received.
1129 if (isIdValid) {
Sunny Goyal86df1382016-08-10 15:03:22 -07001130 status |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07001131 } else {
1132 status &= ~LauncherAppWidgetInfo
1133 .FLAG_PROVIDER_NOT_READY;
1134 }
1135 }
1136 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07001137 } else {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001138 Log.v(TAG, "Widget restore pending id=" + c.id
Sunny Goyal651077b2014-06-30 14:15:31 -07001139 + " appWidgetId=" + appWidgetId
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001140 + " status =" + c.restoreFlag);
Sunny Goyal651077b2014-06-30 14:15:31 -07001141 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07001142 component);
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001143 appWidgetInfo.restoreStatus = c.restoreFlag;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001144 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07001145
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001146 if (c.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_RESTORE_STARTED)) {
Sunny Goyal94485362014-09-18 16:13:58 -07001147 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001148 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07001149 // App restore has started. Update the flag
1150 appWidgetInfo.restoreStatus |=
1151 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001152 } else if (!isSafeMode) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001153 c.markDeleted("Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07001154 continue;
1155 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07001156
1157 appWidgetInfo.installProgress =
1158 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07001159 }
Sunny Goyal86df1382016-08-10 15:03:22 -07001160 if (appWidgetInfo.hasRestoreFlag(
1161 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG)) {
Sunny Goyal81e44912017-01-14 15:05:14 -08001162 appWidgetInfo.bindOptions = c.parseIntent();
Sunny Goyal86df1382016-08-10 15:03:22 -07001163 }
Sunny Goyalff572272014-07-23 13:58:07 -07001164
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001165 c.applyCommonProperties(appWidgetInfo);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001166 appWidgetInfo.spanX = c.getInt(spanXIndex);
1167 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001168 appWidgetInfo.user = c.user;
Joe Onorato36115782010-06-17 13:28:48 -04001169
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001170 if (!c.isOnWorkspaceOrHotseat()) {
1171 c.markDeleted("Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001172 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001173 continue;
1174 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001175
Adam Cohen59400422014-03-05 18:07:04 -08001176 if (!customWidget) {
1177 String providerName =
1178 appWidgetInfo.providerName.flattenToString();
1179 if (!providerName.equals(savedProvider) ||
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001180 (appWidgetInfo.restoreStatus != c.restoreFlag)) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001181 c.updater()
1182 .put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1183 providerName)
1184 .put(LauncherSettings.Favorites.RESTORED,
1185 appWidgetInfo.restoreStatus)
1186 .commit();
Adam Cohen59400422014-03-05 18:07:04 -08001187 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001188 }
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001189 c.checkAndAddItem(appWidgetInfo, sBgDataModel);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001190 }
Joe Onorato36115782010-06-17 13:28:48 -04001191 break;
1192 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001193 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001194 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001195 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001196 }
1197 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001198 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001199 }
1200
Winson Chungba9c37f2013-08-30 14:11:37 -07001201 // Break early if we've stopped loading
1202 if (mStopped) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001203 sBgDataModel.clear();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001204 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07001205 }
1206
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001207 // Remove dead items
1208 if (c.commitDeleted()) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001209 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07001210 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
1211 .call(contentResolver,
1212 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
1213 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
1214 for (long folderId : deletedFolderIds) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001215 sBgDataModel.workspaceItems.remove(sBgDataModel.folders.get(folderId));
1216 sBgDataModel.folders.remove(folderId);
1217 sBgDataModel.itemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001218 }
1219 }
1220
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001221 // Unpin shortcuts that don't exist on the workspace.
Sunny Goyalf75baa92016-11-22 03:23:51 +05301222 HashSet<ShortcutKey> pendingShortcuts =
1223 InstallShortcutReceiver.getPendingShortcuts(context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001224 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001225 MutableInt numTimesPinned = sBgDataModel.pinnedShortcutCounts.get(key);
Sunny Goyalf75baa92016-11-22 03:23:51 +05301226 if ((numTimesPinned == null || numTimesPinned.value == 0)
1227 && !pendingShortcuts.contains(key)) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001228 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301229 shortcutManager.unpinShortcut(key);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001230 }
1231 }
1232
Jon Miranda655ec422017-02-07 11:40:22 -08001233 FolderIconPreviewVerifier verifier =
1234 new FolderIconPreviewVerifier(mApp.getInvariantDeviceProfile());
1235 // Sort the folder items and make sure all items in the preview are high resolution.
Sunny Goyale9956a72016-09-01 17:24:47 -07001236 for (FolderInfo folder : sBgDataModel.folders) {
Sunny Goyal317698b2015-07-29 11:45:41 -07001237 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
Jon Miranda655ec422017-02-07 11:40:22 -08001238 verifier.setFolderInfo(folder);
1239
1240 int numItemsInPreview = 0;
Sunny Goyal317698b2015-07-29 11:45:41 -07001241 for (ShortcutInfo info : folder.contents) {
Jon Miranda655ec422017-02-07 11:40:22 -08001242 if (info.usingLowResIcon
1243 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION
1244 && verifier.isItemInPreview(info.rank)) {
Sunny Goyal3fe4a142016-12-15 17:40:07 -08001245 mIconCache.getTitleAndIcon(info, false);
Jon Miranda655ec422017-02-07 11:40:22 -08001246 numItemsInPreview++;
Sunny Goyal317698b2015-07-29 11:45:41 -07001247 }
Jon Miranda655ec422017-02-07 11:40:22 -08001248
1249 if (numItemsInPreview >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
Sunny Goyal317698b2015-07-29 11:45:41 -07001250 break;
1251 }
1252 }
1253 }
1254
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001255 c.commitRestoredItems();
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001256 if (!isSdCardReady && !pendingPackages.isEmpty()) {
1257 context.registerReceiver(
1258 new SdCardAvailableReceiver(
1259 LauncherModel.this, mContext, pendingPackages),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001260 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001261 null,
1262 sWorker);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001263 }
1264
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001265 // Remove any empty screens
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001266 ArrayList<Long> unusedScreens = new ArrayList<>(sBgDataModel.workspaceScreens);
Sunny Goyale9956a72016-09-01 17:24:47 -07001267 for (ItemInfo item: sBgDataModel.itemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001268 long screenId = item.screenId;
1269 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1270 unusedScreens.contains(screenId)) {
1271 unusedScreens.remove(screenId);
1272 }
1273 }
1274
1275 // If there are any empty screens remove them, and update.
1276 if (unusedScreens.size() != 0) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001277 sBgDataModel.workspaceScreens.removeAll(unusedScreens);
1278 updateWorkspaceScreenOrder(context, sBgDataModel.workspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001279 }
Joe Onorato36115782010-06-17 13:28:48 -04001280 }
Sunny Goyale26d1002016-06-20 14:52:14 -07001281 if (LauncherAppState.PROFILE_STARTUP) {
1282 Trace.endSection();
1283 }
Adam Cohene25af792013-06-06 23:08:25 -07001284 }
1285
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001286 /** Filters the set of items who are directly or indirectly (via another container) on the
1287 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08001288 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001289 ArrayList<ItemInfo> allWorkspaceItems,
1290 ArrayList<ItemInfo> currentScreenItems,
1291 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001292 // Purge any null ItemInfos
1293 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
1294 while (iter.hasNext()) {
1295 ItemInfo i = iter.next();
1296 if (i == null) {
1297 iter.remove();
1298 }
1299 }
1300
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001301 // Order the set of items by their containers first, this allows use to walk through the
1302 // list sequentially, build up a list of containers that are in the specified screen,
1303 // as well as all items in those containers.
1304 Set<Long> itemsOnScreen = new HashSet<Long>();
1305 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
1306 @Override
1307 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07001308 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001309 }
1310 });
1311 for (ItemInfo info : allWorkspaceItems) {
1312 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001313 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001314 currentScreenItems.add(info);
1315 itemsOnScreen.add(info.id);
1316 } else {
1317 otherScreenItems.add(info);
1318 }
1319 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1320 currentScreenItems.add(info);
1321 itemsOnScreen.add(info.id);
1322 } else {
1323 if (itemsOnScreen.contains(info.container)) {
1324 currentScreenItems.add(info);
1325 itemsOnScreen.add(info.id);
1326 } else {
1327 otherScreenItems.add(info);
1328 }
1329 }
1330 }
1331 }
1332
1333 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08001334 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001335 ArrayList<LauncherAppWidgetInfo> appWidgets,
1336 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
1337 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001338
1339 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001340 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001341 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08001342 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001343 currentScreenWidgets.add(widget);
1344 } else {
1345 otherScreenWidgets.add(widget);
1346 }
1347 }
1348 }
1349
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001350 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
1351 * right) */
1352 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Sunny Goyal87f784c2017-01-11 10:48:34 -08001353 final InvariantDeviceProfile profile = mApp.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07001354 final int screenCols = profile.numColumns;
1355 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001356 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07001357 @Override
1358 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07001359 if (lhs.container == rhs.container) {
1360 // Within containers, order by their spatial position in that container
1361 switch ((int) lhs.container) {
1362 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
1363 long lr = (lhs.screenId * screenCellCount +
1364 lhs.cellY * screenCols + lhs.cellX);
1365 long rr = (rhs.screenId * screenCellCount +
1366 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07001367 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07001368 }
1369 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
1370 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07001371 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07001372 }
1373 default:
Sunny Goyal3d706ad2017-03-06 16:56:39 -08001374 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07001375 throw new RuntimeException("Unexpected container type when " +
1376 "sorting workspace items.");
1377 }
1378 return 0;
1379 }
1380 } else {
1381 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07001382 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07001383 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001384 }
1385 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001386 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001387
Adam Cohendcd297f2013-06-18 13:13:40 -07001388 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
1389 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001390 final Runnable r = new Runnable() {
1391 @Override
1392 public void run() {
1393 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1394 if (callbacks != null) {
1395 callbacks.bindScreens(orderedScreens);
1396 }
1397 }
1398 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001399 mUiExecutor.execute(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07001400 }
1401
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001402 private void bindWorkspaceItems(final Callbacks oldCallbacks,
1403 final ArrayList<ItemInfo> workspaceItems,
1404 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07001405 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001406
1407 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07001408 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001409 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04001410 final int start = i;
1411 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001412 final Runnable r = new Runnable() {
1413 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001414 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001415 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001416 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07001417 callbacks.bindItems(workspaceItems, start, start+chunkSize,
1418 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001419 }
1420 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001421 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001422 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04001423 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001424
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001425 // Bind the widgets, one at a time
1426 N = appWidgets.size();
1427 for (int i = 0; i < N; i++) {
1428 final LauncherAppWidgetInfo widget = appWidgets.get(i);
1429 final Runnable r = new Runnable() {
1430 public void run() {
1431 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1432 if (callbacks != null) {
1433 callbacks.bindAppWidget(widget);
1434 }
1435 }
1436 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001437 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001438 }
1439 }
1440
1441 /**
1442 * Binds all loaded data to actual views on the main thread.
1443 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001444 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001445 final long t = SystemClock.uptimeMillis();
1446 Runnable r;
1447
1448 // Don't use these two variables in any of the callback runnables.
1449 // Otherwise we hold a reference to them.
1450 final Callbacks oldCallbacks = mCallbacks.get();
1451 if (oldCallbacks == null) {
1452 // This launcher has exited and nobody bothered to tell us. Just bail.
1453 Log.w(TAG, "LoaderTask running with no launcher");
1454 return;
1455 }
1456
Winson Chung9b9fb962013-11-15 15:39:34 -08001457 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07001458 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
1459 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
1460 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07001461
Sunny Goyale9956a72016-09-01 17:24:47 -07001462 synchronized (sBgDataModel) {
1463 workspaceItems.addAll(sBgDataModel.workspaceItems);
1464 appWidgets.addAll(sBgDataModel.appWidgets);
1465 orderedScreenIds.addAll(sBgDataModel.workspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001466 }
1467
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001468 final int currentScreen;
1469 {
1470 int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE
1471 ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen();
1472 if (currScreen >= orderedScreenIds.size()) {
1473 // There may be no workspace screens (just hotseat items and an empty page).
1474 currScreen = PagedView.INVALID_RESTORE_PAGE;
1475 }
1476 currentScreen = currScreen;
Winson Chung9b9fb962013-11-15 15:39:34 -08001477 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001478 final boolean validFirstPage = currentScreen >= 0;
1479 final long currentScreenId =
1480 validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID;
Winson Chung9b9fb962013-11-15 15:39:34 -08001481
Winson Chung9b9fb962013-11-15 15:39:34 -08001482 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07001483 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
1484 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
1485 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
1486 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001487
Winson Chung9b9fb962013-11-15 15:39:34 -08001488 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001489 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08001490 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001491 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001492 sortWorkspaceItemsSpatially(currentWorkspaceItems);
1493 sortWorkspaceItemsSpatially(otherWorkspaceItems);
1494
1495 // Tell the workspace that we're about to start binding items
1496 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001497 public void run() {
1498 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1499 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001500 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001501 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04001502 }
1503 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001504 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001505 mUiExecutor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001506
Adam Cohendcd297f2013-06-18 13:13:40 -07001507 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
1508
Sunny Goyalb265ba72017-02-14 15:03:45 -08001509 Executor mainExecutor = mUiExecutor;
Sunny Goyal527c7d32015-08-28 15:19:36 -07001510 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07001511 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001512
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001513 // In case of validFirstPage, only bind the first screen, and defer binding the
1514 // remaining screens after first onDraw (and an optional the fade animation whichever
1515 // happens later).
1516 // This ensures that the first screen is immediately visible (eg. during rotation)
1517 // In case of !validFirstPage, bind all pages one after other.
1518 final Executor deferredExecutor =
Sunny Goyalb265ba72017-02-14 15:03:45 -08001519 validFirstPage ? new ViewOnDrawExecutor(mUiExecutor) : mainExecutor;
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001520
1521 mainExecutor.execute(new Runnable() {
1522 @Override
1523 public void run() {
1524 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1525 if (callbacks != null) {
1526 callbacks.finishFirstPageBind(
1527 validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null);
1528 }
1529 }
1530 });
Sunny Goyal527c7d32015-08-28 15:19:36 -07001531
Sunny Goyal44c06432016-04-02 10:56:02 -07001532 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001533
1534 // Tell the workspace that we're done binding items
1535 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001536 public void run() {
1537 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1538 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001539 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04001540 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001541
Sunny Goyal639e9062015-08-19 19:17:06 -07001542 mIsLoadingAndBindingWorkspace = false;
1543
1544 // Run all the bind complete runnables after workspace is bound.
1545 if (!mBindCompleteRunnables.isEmpty()) {
1546 synchronized (mBindCompleteRunnables) {
1547 for (final Runnable r : mBindCompleteRunnables) {
1548 runOnWorkerThread(r);
1549 }
1550 mBindCompleteRunnables.clear();
1551 }
1552 }
1553
Winson Chung98e030b2012-05-07 16:01:11 -07001554 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04001555 if (DEBUG_LOADERS) {
1556 Log.d(TAG, "bound workspace in "
1557 + (SystemClock.uptimeMillis()-t) + "ms");
1558 }
Winson Chung36a62fe2012-05-06 18:04:42 -07001559
Joe Onorato36115782010-06-17 13:28:48 -04001560 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001561 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001562 deferredExecutor.execute(r);
1563
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001564 if (validFirstPage) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001565 r = new Runnable() {
1566 public void run() {
1567 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1568 if (callbacks != null) {
1569 // We are loading synchronously, which means, some of the pages will be
1570 // bound after first draw. Inform the callbacks that page binding is
1571 // not complete, and schedule the remaining pages.
1572 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
1573 callbacks.onPageBoundSynchronously(currentScreen);
1574 }
1575 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
1576 }
1577 }
1578 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001579 mUiExecutor.execute(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07001580 }
Joe Onorato36115782010-06-17 13:28:48 -04001581 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001582
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001583 private void updateIconCache() {
1584 // Ignore packages which have a promise icon.
1585 HashSet<String> packagesToIgnore = new HashSet<>();
Sunny Goyale9956a72016-09-01 17:24:47 -07001586 synchronized (sBgDataModel) {
1587 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001588 if (info instanceof ShortcutInfo) {
1589 ShortcutInfo si = (ShortcutInfo) info;
1590 if (si.isPromise() && si.getTargetComponent() != null) {
1591 packagesToIgnore.add(si.getTargetComponent().getPackageName());
1592 }
1593 } else if (info instanceof LauncherAppWidgetInfo) {
1594 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
1595 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
1596 packagesToIgnore.add(lawi.providerName.getPackageName());
1597 }
1598 }
1599 }
1600 }
1601 mIconCache.updateDbIcons(packagesToIgnore);
1602 }
1603
Joe Onorato36115782010-06-17 13:28:48 -04001604 private void onlyBindAllApps() {
1605 final Callbacks oldCallbacks = mCallbacks.get();
1606 if (oldCallbacks == null) {
1607 // This launcher has exited and nobody bothered to tell us. Just bail.
1608 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1609 return;
1610 }
1611
1612 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07001613 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001614 final ArrayList<AppInfo> list
1615 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07001616 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001617 public void run() {
1618 final long t = SystemClock.uptimeMillis();
1619 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1620 if (callbacks != null) {
1621 callbacks.bindAllApplications(list);
1622 }
1623 if (DEBUG_LOADERS) {
1624 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08001625 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04001626 }
1627 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07001628 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001629 mUiExecutor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04001630 }
1631
Winson Chung64359a52013-07-08 17:17:08 -07001632 private void loadAllApps() {
1633 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04001634
Joe Onorato36115782010-06-17 13:28:48 -04001635 final Callbacks oldCallbacks = mCallbacks.get();
1636 if (oldCallbacks == null) {
1637 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07001638 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04001639 return;
1640 }
1641
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001642 final List<UserHandle> profiles = mUserManager.getUserProfiles();
Kenny Guyed131872014-04-30 03:02:21 +01001643
Winson Chung64359a52013-07-08 17:17:08 -07001644 // Clear the list of apps
1645 mBgAllAppsList.clear();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001646 for (UserHandle user : profiles) {
Kenny Guyed131872014-04-30 03:02:21 +01001647 // Query for the set of apps
1648 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal3e9be432017-01-05 15:22:41 -08001649 final List<LauncherActivityInfo> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01001650 if (DEBUG_LOADERS) {
1651 Log.d(TAG, "getActivityList took "
1652 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
1653 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
1654 }
1655 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08001656 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01001657 if (apps == null || apps.isEmpty()) {
1658 return;
1659 }
Kenny Guyff05f432016-01-22 17:48:29 +00001660 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01001661 // Create the ApplicationInfos
1662 for (int i = 0; i < apps.size(); i++) {
Sunny Goyal3e9be432017-01-05 15:22:41 -08001663 LauncherActivityInfo app = apps.get(i);
Kenny Guyed131872014-04-30 03:02:21 +01001664 // This builds the icon bitmaps.
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001665 mBgAllAppsList.add(new AppInfo(mContext, app, user, quietMode), app);
Kenny Guyed131872014-04-30 03:02:21 +01001666 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08001667
Sunny Goyal756a28a2015-04-23 17:07:55 -07001668 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
1669 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07001670 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07001671
1672 @Override
1673 public void run() {
1674 heuristic.processUserApps(apps);
1675 }
Sunny Goyal639e9062015-08-19 19:17:06 -07001676 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001677 mUiExecutor.execute(new Runnable() {
Sunny Goyal639e9062015-08-19 19:17:06 -07001678
Sunny Goyalb265ba72017-02-14 15:03:45 -08001679 @Override
1680 public void run() {
1681 // Check isLoadingWorkspace on the UI thread, as it is updated on
1682 // the UI thread.
1683 if (mIsLoadingAndBindingWorkspace) {
1684 synchronized (mBindCompleteRunnables) {
1685 mBindCompleteRunnables.add(r);
1686 }
1687 } else {
1688 runOnWorkerThread(r);
1689 }
1690 }
1691 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08001692 }
Winson Chung64359a52013-07-08 17:17:08 -07001693 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01001694 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001695 final ArrayList<AppInfo> added = mBgAllAppsList.added;
1696 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07001697
1698 // Post callback on main thread
Sunny Goyalb265ba72017-02-14 15:03:45 -08001699 mUiExecutor.execute(new Runnable() {
Winson Chung64359a52013-07-08 17:17:08 -07001700 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07001701
Winson Chung64359a52013-07-08 17:17:08 -07001702 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07001703 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07001704 if (callbacks != null) {
1705 callbacks.bindAllApplications(added);
1706 if (DEBUG_LOADERS) {
1707 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001708 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07001709 }
1710 } else {
1711 Log.i(TAG, "not binding apps: no Launcher activity");
1712 }
1713 }
1714 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07001715 // Cleanup any data stored for a deleted user.
1716 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04001717 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07001718 Log.d(TAG, "Icons processed in "
1719 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08001720 }
1721 }
1722
Sunny Goyaldd96a5e2017-02-17 11:22:34 -08001723 private void loadDeepShortcuts() {
1724 if (!mModelLoaded) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07001725 sBgDataModel.deepShortcutMap.clear();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301726 DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(mContext);
1727 mHasShortcutHostPermission = shortcutManager.hasHostPermission();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001728 if (mHasShortcutHostPermission) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001729 for (UserHandle user : mUserManager.getUserProfiles()) {
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001730 if (mUserManager.isUserUnlocked(user)) {
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301731 List<ShortcutInfoCompat> shortcuts =
1732 shortcutManager.queryForAllShortcuts(user);
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07001733 sBgDataModel.updateDeepShortcutMap(null, user, shortcuts);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001734 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001735 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001736 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001737 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001738 }
Joe Onorato36115782010-06-17 13:28:48 -04001739 }
1740
Tony Wickham80f57872016-06-29 18:12:15 -07001741 public void bindDeepShortcuts() {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07001742 final MultiHashMap<ComponentKey, String> shortcutMapCopy =
1743 sBgDataModel.deepShortcutMap.clone();
Tony Wickham80f57872016-06-29 18:12:15 -07001744 Runnable r = new Runnable() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001745 @Override
1746 public void run() {
1747 Callbacks callbacks = getCallback();
1748 if (callbacks != null) {
1749 callbacks.bindDeepShortcutMap(shortcutMapCopy);
1750 }
1751 }
Tony Wickham80f57872016-06-29 18:12:15 -07001752 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001753 mUiExecutor.execute(r);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001754 }
1755
Sunny Goyal75b0f552015-05-20 21:57:06 -07001756 /**
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001757 * Refreshes the cached shortcuts if the shortcut permission has changed.
1758 * Current implementation simply reloads the workspace, but it can be optimized to
1759 * use partial updates similar to {@link UserManagerCompat}
1760 */
1761 public void refreshShortcutsIfRequired() {
Sunny Goyalf5e37442016-11-02 10:31:24 -07001762 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001763 sWorker.removeCallbacks(mShortcutPermissionCheckRunnable);
1764 sWorker.post(mShortcutPermissionCheckRunnable);
1765 }
1766 }
1767
1768 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -07001769 * Called when the icons for packages have been updated in the icon cache.
1770 */
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001771 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandle user) {
Sunny Goyal75b0f552015-05-20 21:57:06 -07001772 // If any package icon has changed (app was updated while launcher was dead),
1773 // update the corresponding shortcuts.
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001774 enqueueModelUpdateTask(new CacheDataUpdatedTask(
1775 CacheDataUpdatedTask.OP_CACHE_UPDATE, user, updatedPackages));
Sunny Goyal75b0f552015-05-20 21:57:06 -07001776 }
1777
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001778 void enqueueModelUpdateTask(BaseModelUpdateTask task) {
Jon Mirandaf28629a2017-02-23 15:12:42 -08001779 if (!mModelLoaded && mLoaderTask == null) {
1780 if (DEBUG_LOADERS) {
1781 Log.d(TAG, "enqueueModelUpdateTask Ignoring task since loader is pending=" + task);
1782 }
1783 return;
1784 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001785 task.init(this);
1786 runOnWorkerThread(task);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001787 }
1788
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001789 /**
1790 * A task to be executed on the current callbacks on the UI thread.
1791 * If there is no current callbacks, the task is ignored.
1792 */
1793 public interface CallbackTask {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001794
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001795 void execute(Callbacks callbacks);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001796 }
1797
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001798 /**
1799 * A runnable which changes/updates the data model of the launcher based on certain events.
1800 */
1801 public static abstract class BaseModelUpdateTask implements Runnable {
Joe Onorato36115782010-06-17 13:28:48 -04001802
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001803 private LauncherModel mModel;
Sunny Goyalb265ba72017-02-14 15:03:45 -08001804 private Executor mUiExecutor;
Joe Onorato36115782010-06-17 13:28:48 -04001805
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001806 /* package private */
1807 void init(LauncherModel model) {
1808 mModel = model;
Sunny Goyalb265ba72017-02-14 15:03:45 -08001809 mUiExecutor = mModel.mUiExecutor;
Joe Onorato36115782010-06-17 13:28:48 -04001810 }
1811
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001812 @Override
Joe Onorato36115782010-06-17 13:28:48 -04001813 public void run() {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001814 if (!mModel.mHasLoaderCompletedOnce) {
Sunny Goyalc905efc2015-05-06 09:54:53 -07001815 // Loader has not yet run.
1816 return;
1817 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001818 execute(mModel.mApp, sBgDataModel, mModel.mBgAllAppsList);
1819 }
Joe Onorato36115782010-06-17 13:28:48 -04001820
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001821 /**
1822 * Execute the actual task. Called on the worker thread.
1823 */
1824 public abstract void execute(
1825 LauncherAppState app, BgDataModel dataModel, AllAppsList apps);
Sunny Goyale0f58d72014-11-10 18:05:31 -08001826
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001827 /**
1828 * Schedules a {@param task} to be executed on the current callbacks.
1829 */
1830 public final void scheduleCallbackTask(final CallbackTask task) {
1831 final Callbacks callbacks = mModel.getCallback();
Sunny Goyalb265ba72017-02-14 15:03:45 -08001832 mUiExecutor.execute(new Runnable() {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001833 public void run() {
1834 Callbacks cb = mModel.getCallback();
1835 if (callbacks == cb && cb != null) {
1836 task.execute(callbacks);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07001837 }
1838 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001839 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001840 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001841
1842 public ModelWriter getModelWriter() {
1843 // Updates from model task, do not deal with icon position in hotseat.
1844 return mModel.getWriter(false /* hasVerticalHotseat */);
1845 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001846 }
1847
Sunny Goyal1b072632017-01-18 11:30:23 -08001848 public void updateAndBindShortcutInfo(final ShortcutInfo si, final ShortcutInfoCompat info) {
1849 updateAndBindShortcutInfo(new Provider<ShortcutInfo>() {
1850 @Override
1851 public ShortcutInfo get() {
1852 si.updateFromDeepShortcutInfo(info, mApp.getContext());
1853 si.iconBitmap = LauncherIcons.createShortcutIcon(info, mApp.getContext());
1854 return si;
1855 }
1856 });
1857 }
1858
Sunny Goyal10923b32016-07-20 15:42:44 -07001859 /**
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001860 * Utility method to update a shortcut on the background thread.
Sunny Goyal10923b32016-07-20 15:42:44 -07001861 */
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001862 public void updateAndBindShortcutInfo(final Provider<ShortcutInfo> shortcutProvider) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001863 enqueueModelUpdateTask(new ExtendedModelTask() {
Sunny Goyal10923b32016-07-20 15:42:44 -07001864 @Override
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001865 public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001866 ShortcutInfo info = shortcutProvider.get();
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001867 ArrayList<ShortcutInfo> update = new ArrayList<>();
Sunny Goyal10923b32016-07-20 15:42:44 -07001868 update.add(info);
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001869 bindUpdatedShortcuts(update, info.user);
Sunny Goyal10923b32016-07-20 15:42:44 -07001870 }
1871 });
1872 }
1873
Sunny Goyald164b7f2016-10-12 20:49:31 -07001874 private void bindWidgetsModel(final Callbacks callbacks) {
1875 final MultiHashMap<PackageItemInfo, WidgetItem> widgets
1876 = mBgWidgetsModel.getWidgetsMap().clone();
Sunny Goyalb265ba72017-02-14 15:03:45 -08001877 mUiExecutor.execute(new Runnable() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001878 @Override
1879 public void run() {
1880 Callbacks cb = getCallback();
1881 if (callbacks == cb && cb != null) {
Sunny Goyald164b7f2016-10-12 20:49:31 -07001882 callbacks.bindAllWidgets(widgets);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001883 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07001884 }
1885 });
1886 }
1887
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001888 public void refreshAndBindWidgetsAndShortcuts(
1889 final Callbacks callbacks, final boolean bindFirst) {
1890 runOnWorkerThread(new Runnable() {
1891 @Override
1892 public void run() {
1893 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
Sunny Goyald164b7f2016-10-12 20:49:31 -07001894 bindWidgetsModel(callbacks);
Sunny Goyal31860582015-09-18 08:38:57 -07001895 }
Sunny Goyald164b7f2016-10-12 20:49:31 -07001896 ArrayList<WidgetItem> allWidgets = mBgWidgetsModel.update(mApp.getContext());
1897 bindWidgetsModel(callbacks);
1898
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001899 // update the Widget entries inside DB on the worker thread.
Sunny Goyal87f784c2017-01-11 10:48:34 -08001900 mApp.getWidgetCache().removeObsoletePreviews(allWidgets);
Sunny Goyal31860582015-09-18 08:38:57 -07001901 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001902 });
Michael Jurkac402cd92013-05-20 15:49:32 +02001903 }
1904
Sunny Goyal651077b2014-06-30 14:15:31 -07001905 static boolean isValidProvider(AppWidgetProviderInfo provider) {
1906 return (provider != null) && (provider.provider != null)
1907 && (provider.provider.getPackageName() != null);
1908 }
1909
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001910 public void dumpState(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
1911 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
1912 writer.println(prefix + "All apps list: size=" + mBgAllAppsList.data.size());
1913 for (AppInfo info : mBgAllAppsList.data) {
1914 writer.println(prefix + " title=\"" + info.title + "\" iconBitmap=" + info.iconBitmap
1915 + " componentName=" + info.componentName.getPackageName());
1916 }
Joe Onorato36115782010-06-17 13:28:48 -04001917 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001918 sBgDataModel.dump(prefix, fd, writer, args);
Joe Onoratobe386092009-11-17 17:32:16 -08001919 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08001920
1921 public Callbacks getCallback() {
1922 return mCallbacks != null ? mCallbacks.get() : null;
1923 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07001924
1925 /**
1926 * @return {@link FolderInfo} if its already loaded.
1927 */
1928 public FolderInfo findFolderById(Long folderId) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001929 synchronized (sBgDataModel) {
1930 return sBgDataModel.folders.get(folderId);
Sunny Goyal18bf8e22015-04-08 18:13:46 -07001931 }
1932 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07001933
1934 /**
1935 * @return the looper for the worker thread which can be used to start background tasks.
1936 */
1937 public static Looper getWorkerLooper() {
1938 return sWorkerThread.getLooper();
1939 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940}