2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.launcher3;
19 import android.appwidget.AppWidgetProviderInfo;
20 import android.content.BroadcastReceiver;
21 import android.content.ComponentName;
22 import android.content.ContentProviderOperation;
23 import android.content.ContentResolver;
24 import android.content.ContentValues;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.content.IntentFilter;
28 import android.content.pm.LauncherActivityInfo;
29 import android.net.Uri;
30 import android.os.Handler;
31 import android.os.HandlerThread;
32 import android.os.Looper;
33 import android.os.Process;
34 import android.os.SystemClock;
35 import android.os.Trace;
36 import android.os.UserHandle;
37 import android.support.annotation.Nullable;
38 import android.text.TextUtils;
39 import android.util.Log;
40 import android.util.LongSparseArray;
41 import android.util.MutableInt;
43 import com.android.launcher3.compat.AppWidgetManagerCompat;
44 import com.android.launcher3.compat.LauncherAppsCompat;
45 import com.android.launcher3.compat.PackageInstallerCompat;
46 import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
47 import com.android.launcher3.compat.UserManagerCompat;
48 import com.android.launcher3.config.ProviderConfig;
49 import com.android.launcher3.dynamicui.ExtractionUtils;
50 import com.android.launcher3.folder.Folder;
51 import com.android.launcher3.folder.FolderIcon;
52 import com.android.launcher3.graphics.LauncherIcons;
53 import com.android.launcher3.logging.FileLog;
54 import com.android.launcher3.model.AddWorkspaceItemsTask;
55 import com.android.launcher3.model.BgDataModel;
56 import com.android.launcher3.model.CacheDataUpdatedTask;
57 import com.android.launcher3.model.ExtendedModelTask;
58 import com.android.launcher3.model.GridSizeMigrationTask;
59 import com.android.launcher3.model.LoaderCursor;
60 import com.android.launcher3.model.ModelWriter;
61 import com.android.launcher3.model.PackageInstallStateChangedTask;
62 import com.android.launcher3.model.PackageItemInfo;
63 import com.android.launcher3.model.PackageUpdatedTask;
64 import com.android.launcher3.model.SdCardAvailableReceiver;
65 import com.android.launcher3.model.ShortcutsChangedTask;
66 import com.android.launcher3.model.UserLockStateChangedTask;
67 import com.android.launcher3.model.WidgetItem;
68 import com.android.launcher3.model.WidgetsModel;
69 import com.android.launcher3.provider.ImportDataTask;
70 import com.android.launcher3.provider.LauncherDbUtils;
71 import com.android.launcher3.shortcuts.DeepShortcutManager;
72 import com.android.launcher3.shortcuts.ShortcutInfoCompat;
73 import com.android.launcher3.shortcuts.ShortcutKey;
74 import com.android.launcher3.util.ComponentKey;
75 import com.android.launcher3.util.ManagedProfileHeuristic;
76 import com.android.launcher3.util.MultiHashMap;
77 import com.android.launcher3.util.PackageManagerHelper;
78 import com.android.launcher3.util.PackageUserKey;
79 import com.android.launcher3.util.Preconditions;
80 import com.android.launcher3.util.Provider;
81 import com.android.launcher3.util.Thunk;
82 import com.android.launcher3.util.ViewOnDrawExecutor;
84 import java.io.FileDescriptor;
85 import java.io.PrintWriter;
86 import java.lang.ref.WeakReference;
87 import java.util.ArrayList;
88 import java.util.Collections;
89 import java.util.Comparator;
90 import java.util.HashMap;
91 import java.util.HashSet;
92 import java.util.Iterator;
93 import java.util.List;
96 import java.util.concurrent.CancellationException;
97 import java.util.concurrent.Executor;
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
104 public class LauncherModel extends BroadcastReceiver
105 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
106 static final boolean DEBUG_LOADERS = false;
107 private static final boolean DEBUG_RECEIVER = false;
109 static final String TAG = "Launcher.Model";
111 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
112 private static final long INVALID_SCREEN_ID = -1L;
114 @Thunk final LauncherAppState mApp;
115 @Thunk final Object mLock = new Object();
116 @Thunk DeferredHandler mHandler = new DeferredHandler();
117 @Thunk LoaderTask mLoaderTask;
118 @Thunk boolean mIsLoaderTaskRunning;
119 @Thunk boolean mHasLoaderCompletedOnce;
121 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
123 sWorkerThread.start();
125 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
127 // Indicates whether the current model data is valid or not.
128 // We start off with everything not loaded. After that, we assume that
129 // our monitoring of the package manager provides all updates and we never
130 // need to do a requery. This is only ever touched from the loader thread.
131 private boolean mModelLoaded;
132 public boolean isModelLoaded() {
133 synchronized (mLock) {
134 return mModelLoaded && mLoaderTask == null;
139 * Set of runnables to be called on the background thread after the workspace binding
142 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
144 @Thunk WeakReference<Callbacks> mCallbacks;
146 // < only access in worker thread >
147 private final AllAppsList mBgAllAppsList;
148 // Entire list of widgets.
149 private final WidgetsModel mBgWidgetsModel;
151 private boolean mHasShortcutHostPermission;
152 // Runnable to check if the shortcuts permission has changed.
153 private final Runnable mShortcutPermissionCheckRunnable = new Runnable() {
157 boolean hasShortcutHostPermission =
158 DeepShortcutManager.getInstance(mApp.getContext()).hasHostPermission();
159 if (hasShortcutHostPermission != mHasShortcutHostPermission) {
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.
170 static final BgDataModel sBgDataModel = new BgDataModel();
172 // </ only access in worker thread >
174 private final IconCache mIconCache;
176 private final LauncherAppsCompat mLauncherApps;
177 private final UserManagerCompat mUserManager;
179 public interface Callbacks {
180 public boolean setLoadOnResume();
181 public int getCurrentWorkspaceScreen();
182 public void clearPendingBinds();
183 public void startBinding();
184 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
185 boolean forceAnimateIcons);
186 public void bindScreens(ArrayList<Long> orderedScreenIds);
187 public void finishFirstPageBind(ViewOnDrawExecutor executor);
188 public void finishBindingItems();
189 public void bindAppWidget(LauncherAppWidgetInfo info);
190 public void bindAllApplications(ArrayList<AppInfo> apps);
191 public void bindAppsAdded(ArrayList<Long> newScreens,
192 ArrayList<ItemInfo> addNotAnimated,
193 ArrayList<ItemInfo> addAnimated,
194 ArrayList<AppInfo> addedApps);
195 public void bindAppsUpdated(ArrayList<AppInfo> apps);
196 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
197 ArrayList<ShortcutInfo> removed, UserHandle user);
198 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
199 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
200 public void bindWorkspaceComponentsRemoved(
201 HashSet<String> packageNames, HashSet<ComponentName> components,
203 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
204 public void notifyWidgetProvidersChanged();
205 public void bindAllWidgets(MultiHashMap<PackageItemInfo, WidgetItem> widgets);
206 public void onPageBoundSynchronously(int page);
207 public void executeOnNextDraw(ViewOnDrawExecutor executor);
208 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMap);
211 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
212 Context context = app.getContext();
214 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
215 mBgWidgetsModel = new WidgetsModel(iconCache, appFilter);
216 mIconCache = iconCache;
218 mLauncherApps = LauncherAppsCompat.getInstance(context);
219 mUserManager = UserManagerCompat.getInstance(context);
222 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
223 * posted on the main thread handler. */
224 private void runOnMainThread(Runnable r) {
225 if (sWorkerThread.getThreadId() == Process.myTid()) {
226 // If we are on the worker thread, post onto the main handler
233 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
234 * posted on the worker thread handler. */
235 private static void runOnWorkerThread(Runnable r) {
236 if (sWorkerThread.getThreadId() == Process.myTid()) {
239 // If we are not on the worker thread, then post to the worker handler
244 public void setPackageState(PackageInstallInfo installInfo) {
245 enqueueModelUpdateTask(new PackageInstallStateChangedTask(installInfo));
249 * Updates the icons and label of all pending icons for the provided package name.
251 public void updateSessionDisplayInfo(final String packageName) {
252 HashSet<String> packages = new HashSet<>();
253 packages.add(packageName);
254 enqueueModelUpdateTask(new CacheDataUpdatedTask(
255 CacheDataUpdatedTask.OP_SESSION_UPDATE, Process.myUserHandle(), packages));
259 * Adds the provided items to the workspace.
261 public void addAndBindAddedWorkspaceItems(List<ItemInfo> workspaceApps) {
262 addAndBindAddedWorkspaceItems(Provider.of(workspaceApps));
266 * Adds the provided items to the workspace.
268 public void addAndBindAddedWorkspaceItems(
269 Provider<List<ItemInfo>> appsProvider) {
270 enqueueModelUpdateTask(new AddWorkspaceItemsTask(appsProvider));
273 public ModelWriter getWriter(boolean hasVerticalHotseat) {
274 return new ModelWriter(mApp.getContext(), sBgDataModel, hasVerticalHotseat);
277 static void checkItemInfoLocked(
278 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
279 ItemInfo modelItem = sBgDataModel.itemsIdMap.get(itemId);
280 if (modelItem != null && item != modelItem) {
281 // check all the data is consistent
282 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
283 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
284 ShortcutInfo shortcut = (ShortcutInfo) item;
285 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
286 modelShortcut.intent.filterEquals(shortcut.intent) &&
287 modelShortcut.id == shortcut.id &&
288 modelShortcut.itemType == shortcut.itemType &&
289 modelShortcut.container == shortcut.container &&
290 modelShortcut.screenId == shortcut.screenId &&
291 modelShortcut.cellX == shortcut.cellX &&
292 modelShortcut.cellY == shortcut.cellY &&
293 modelShortcut.spanX == shortcut.spanX &&
294 modelShortcut.spanY == shortcut.spanY) {
295 // For all intents and purposes, this is the same object
300 // the modelItem needs to match up perfectly with item if our model is
301 // to be consistent with the database-- for now, just require
302 // modelItem == item or the equality check above
303 String msg = "item: " + ((item != null) ? item.toString() : "null") +
305 ((modelItem != null) ? modelItem.toString() : "null") +
306 "Error: ItemInfo passed to checkItemInfo doesn't match original";
307 RuntimeException e = new RuntimeException(msg);
308 if (stackTrace != null) {
309 e.setStackTrace(stackTrace);
315 static void checkItemInfo(final ItemInfo item) {
316 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
317 final long itemId = item.id;
318 Runnable r = new Runnable() {
320 synchronized (sBgDataModel) {
321 checkItemInfoLocked(itemId, item, stackTrace);
325 runOnWorkerThread(r);
329 * Update the order of the workspace screens in the database. The array list contains
330 * a list of screen ids in the order that they should appear.
332 public static void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
333 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
334 final ContentResolver cr = context.getContentResolver();
335 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
337 // Remove any negative screen ids -- these aren't persisted
338 Iterator<Long> iter = screensCopy.iterator();
339 while (iter.hasNext()) {
340 long id = iter.next();
346 Runnable r = new Runnable() {
349 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
351 ops.add(ContentProviderOperation.newDelete(uri).build());
352 int count = screensCopy.size();
353 for (int i = 0; i < count; i++) {
354 ContentValues v = new ContentValues();
355 long screenId = screensCopy.get(i);
356 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
357 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
358 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
362 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
363 } catch (Exception ex) {
364 throw new RuntimeException(ex);
367 synchronized (sBgDataModel) {
368 sBgDataModel.workspaceScreens.clear();
369 sBgDataModel.workspaceScreens.addAll(screensCopy);
373 runOnWorkerThread(r);
377 * Set this as the current Launcher activity object for the loader.
379 public void initialize(Callbacks callbacks) {
380 synchronized (mLock) {
381 Preconditions.assertUIThread();
382 // Remove any queued UI runnables
383 mHandler.cancelAll();
384 mCallbacks = new WeakReference<>(callbacks);
389 public void onPackageChanged(String packageName, UserHandle user) {
390 int op = PackageUpdatedTask.OP_UPDATE;
391 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
395 public void onPackageRemoved(String packageName, UserHandle user) {
396 onPackagesRemoved(user, packageName);
399 public void onPackagesRemoved(UserHandle user, String... packages) {
400 int op = PackageUpdatedTask.OP_REMOVE;
401 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packages));
405 public void onPackageAdded(String packageName, UserHandle user) {
406 int op = PackageUpdatedTask.OP_ADD;
407 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
411 public void onPackagesAvailable(String[] packageNames, UserHandle user,
413 enqueueModelUpdateTask(
414 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, user, packageNames));
418 public void onPackagesUnavailable(String[] packageNames, UserHandle user,
421 enqueueModelUpdateTask(new PackageUpdatedTask(
422 PackageUpdatedTask.OP_UNAVAILABLE, user, packageNames));
427 public void onPackagesSuspended(String[] packageNames, UserHandle user) {
428 enqueueModelUpdateTask(new PackageUpdatedTask(
429 PackageUpdatedTask.OP_SUSPEND, user, packageNames));
433 public void onPackagesUnsuspended(String[] packageNames, UserHandle user) {
434 enqueueModelUpdateTask(new PackageUpdatedTask(
435 PackageUpdatedTask.OP_UNSUSPEND, user, packageNames));
439 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
441 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, true));
444 public void updatePinnedShortcuts(String packageName, List<ShortcutInfoCompat> shortcuts,
446 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, false));
450 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
451 * ACTION_PACKAGE_CHANGED.
454 public void onReceive(Context context, Intent intent) {
455 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
457 final String action = intent.getAction();
458 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
459 // If we have changed locale we need to clear out the labels in all apps/workspace.
461 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
462 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
463 UserManagerCompat.getInstance(context).enableAndResetCache();
465 } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
466 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
467 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
468 UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER);
470 if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
471 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
472 enqueueModelUpdateTask(new PackageUpdatedTask(
473 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE, user));
476 // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so
477 // we need to run the state change task again.
478 if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
479 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
480 enqueueModelUpdateTask(new UserLockStateChangedTask(user));
483 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
484 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
489 * Reloads the workspace items from the DB and re-binds the workspace. This should generally
490 * not be called as DB updates are automatically followed by UI update
492 public void forceReload() {
493 synchronized (mLock) {
494 // Stop any existing loaders first, so they don't set mModelLoaded to true later
496 mModelLoaded = false;
499 // Do this here because if the launcher activity is running it will be restarted.
500 // If it's not running startLoaderFromBackground will merely tell it that it needs
502 startLoaderFromBackground();
506 * When the launcher is in the background, it's possible for it to miss paired
507 * configuration changes. So whenever we trigger the loader from the background
508 * tell the launcher that it needs to re-run the loader when it comes back instead
511 public void startLoaderFromBackground() {
512 Callbacks callbacks = getCallback();
513 if (callbacks != null) {
514 // Only actually run the loader if they're not paused.
515 if (!callbacks.setLoadOnResume()) {
516 startLoader(callbacks.getCurrentWorkspaceScreen());
522 * If there is already a loader task running, tell it to stop.
524 private void stopLoaderLocked() {
525 LoaderTask oldTask = mLoaderTask;
526 if (oldTask != null) {
527 oldTask.stopLocked();
531 public boolean isCurrentCallbacks(Callbacks callbacks) {
532 return (mCallbacks != null && mCallbacks.get() == callbacks);
536 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
537 * @return true if the page could be bound synchronously.
539 public boolean startLoader(int synchronousBindPage) {
540 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
541 InstallShortcutReceiver.enableInstallQueue();
542 synchronized (mLock) {
543 // Don't bother to start the thread if we know it's not going to do anything
544 if (mCallbacks != null && mCallbacks.get() != null) {
545 final Callbacks oldCallbacks = mCallbacks.get();
546 // Clear any pending bind-runnables from the synchronized load process.
547 runOnMainThread(new Runnable() {
549 oldCallbacks.clearPendingBinds();
553 // If there is already one running, tell it to stop.
555 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
556 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
557 && mModelLoaded && !mIsLoaderTaskRunning) {
558 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
561 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
562 sWorker.post(mLoaderTask);
569 public void stopLoader() {
570 synchronized (mLock) {
571 if (mLoaderTask != null) {
572 mLoaderTask.stopLocked();
578 * Loads the workspace screen ids in an ordered list.
580 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
581 final ContentResolver contentResolver = context.getContentResolver();
582 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
584 // Get screens ordered by rank.
585 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
586 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
590 * Runnable for the thread that loads the contents of the launcher:
594 * - deep shortcuts within apps
596 private class LoaderTask implements Runnable {
597 private Context mContext;
598 private int mPageToBindFirst;
600 @Thunk boolean mIsLoadingAndBindingWorkspace;
601 private boolean mStopped;
602 @Thunk boolean mLoadAndBindStepFinished;
604 LoaderTask(Context context, int pageToBindFirst) {
606 mPageToBindFirst = pageToBindFirst;
609 private void waitForIdle() {
610 // Wait until the either we're stopped or the other threads are done.
611 // This way we don't start loading all apps until the workspace has settled
613 synchronized (LoaderTask.this) {
614 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
616 mHandler.postIdle(new Runnable() {
618 synchronized (LoaderTask.this) {
619 mLoadAndBindStepFinished = true;
621 Log.d(TAG, "done with previous binding step");
623 LoaderTask.this.notify();
628 while (!mStopped && !mLoadAndBindStepFinished) {
630 // Just in case mFlushingWorkerThread changes but we aren't woken up,
631 // wait no longer than 1sec at a time
633 } catch (InterruptedException ex) {
639 + (SystemClock.uptimeMillis()-workspaceWaitTime)
640 + "ms for previous step to finish binding");
645 void runBindSynchronousPage(int synchronousBindPage) {
646 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
647 // Ensure that we have a valid page index to load synchronously
648 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
652 // Ensure that we don't try and bind a specified page when the pages have not been
653 // loaded already (we should load everything asynchronously in that case)
654 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
656 synchronized (mLock) {
657 if (mIsLoaderTaskRunning) {
658 // Ensure that we are never running the background loading at this point since
659 // we also touch the background collections
660 throw new RuntimeException("Error! Background loading is already running");
664 // XXX: Throw an exception if we are already loading (since we touch the worker thread
665 // data structures, we can't allow any other thread to touch that data, but because
666 // this call is synchronous, we can get away with not locking).
668 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
669 // operations from the previous activity. We need to ensure that all queued operations
670 // are executed before any synchronous binding work is done.
673 // Divide the set of loaded items into those that we are binding synchronously, and
674 // everything else that is to be bound normally (asynchronously).
675 bindWorkspace(synchronousBindPage);
676 // XXX: For now, continue posting the binding of AllApps as there are other issues that
683 private void verifyNotStopped() throws CancellationException {
684 synchronized (LoaderTask.this) {
686 throw new CancellationException("Loader stopped");
692 synchronized (mLock) {
696 mIsLoaderTaskRunning = true;
700 if (DEBUG_LOADERS) Log.d(TAG, "step 1.1: loading workspace");
701 // Set to false in bindWorkspace()
702 mIsLoadingAndBindingWorkspace = true;
706 if (DEBUG_LOADERS) Log.d(TAG, "step 1.2: bind workspace workspace");
707 bindWorkspace(mPageToBindFirst);
710 if (DEBUG_LOADERS) Log.d(TAG, "step 1 completed, wait for idle");
715 if (DEBUG_LOADERS) Log.d(TAG, "step 2.1: loading all apps");
719 if (DEBUG_LOADERS) Log.d(TAG, "step 2.2: Update icon cache");
723 if (DEBUG_LOADERS) Log.d(TAG, "step 2 completed, wait for idle");
728 if (DEBUG_LOADERS) Log.d(TAG, "step 3.1: loading deep shortcuts");
732 if (DEBUG_LOADERS) Log.d(TAG, "step 3.2: bind deep shortcuts");
736 if (DEBUG_LOADERS) Log.d(TAG, "step 3 completed, wait for idle");
741 if (DEBUG_LOADERS) Log.d(TAG, "step 4.1: loading widgets");
742 refreshAndBindWidgetsAndShortcuts(getCallback(), false /* bindFirst */,
743 null /* packageUser */);
745 synchronized (mLock) {
746 // Everything loaded bind the data.
748 mHasLoaderCompletedOnce = true;
750 } catch (CancellationException e) {
751 // Loader stopped, ignore
753 // Clear out this reference, otherwise we end up holding it until all of the
754 // callback runnables are done.
757 synchronized (mLock) {
758 // If we are still the last one to be scheduled, remove ourselves.
759 if (mLoaderTask == this) {
762 mIsLoaderTaskRunning = false;
767 public void stopLocked() {
768 synchronized (LoaderTask.this) {
775 * Gets the callbacks object. If we've been stopped, or if the launcher object
776 * has somehow been garbage collected, return null instead. Pass in the Callbacks
777 * object that was around when the deferred message was scheduled, and if there's
778 * a new Callbacks object around then also return null. This will save us from
779 * calling onto it with data that will be ignored.
781 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
782 synchronized (mLock) {
787 if (mCallbacks == null) {
791 final Callbacks callbacks = mCallbacks.get();
792 if (callbacks != oldCallbacks) {
795 if (callbacks == null) {
796 Log.w(TAG, "no mCallbacks");
804 private void loadWorkspace() {
805 if (LauncherAppState.PROFILE_STARTUP) {
806 Trace.beginSection("Loading Workspace");
809 final Context context = mContext;
810 final ContentResolver contentResolver = context.getContentResolver();
811 final PackageManagerHelper pmHelper = new PackageManagerHelper(context);
812 final boolean isSafeMode = pmHelper.isSafeMode();
813 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
814 final DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(context);
815 final boolean isSdCardReady = Utilities.isBootCompleted();
816 final MultiHashMap<UserHandle, String> pendingPackages = new MultiHashMap<>();
818 boolean clearDb = false;
820 ImportDataTask.performImportIfPossible(context);
821 } catch (Exception e) {
822 // Migration failed. Clear workspace.
826 if (!clearDb && GridSizeMigrationTask.ENABLED &&
827 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
828 // Migration failed. Clear workspace.
833 Log.d(TAG, "loadWorkspace: resetting launcher database");
834 LauncherSettings.Settings.call(contentResolver,
835 LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB);
838 Log.d(TAG, "loadWorkspace: loading default favorites");
839 LauncherSettings.Settings.call(contentResolver,
840 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
842 synchronized (sBgDataModel) {
843 sBgDataModel.clear();
845 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
846 .getInstance(mContext).updateAndGetActiveSessionCache();
847 sBgDataModel.workspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
849 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
850 final LoaderCursor c = new LoaderCursor(contentResolver.query(
851 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null), mApp);
853 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
856 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
857 LauncherSettings.Favorites.APPWIDGET_ID);
858 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
859 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
860 final int spanXIndex = c.getColumnIndexOrThrow
861 (LauncherSettings.Favorites.SPANX);
862 final int spanYIndex = c.getColumnIndexOrThrow(
863 LauncherSettings.Favorites.SPANY);
864 final int rankIndex = c.getColumnIndexOrThrow(
865 LauncherSettings.Favorites.RANK);
866 final int optionsIndex = c.getColumnIndexOrThrow(
867 LauncherSettings.Favorites.OPTIONS);
869 final LongSparseArray<UserHandle> allUsers = c.allUsers;
870 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
871 final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>();
872 for (UserHandle user : mUserManager.getUserProfiles()) {
873 long serialNo = mUserManager.getSerialNumberForUser(user);
874 allUsers.put(serialNo, user);
875 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
877 boolean userUnlocked = mUserManager.isUserUnlocked(user);
879 // We can only query for shortcuts when the user is unlocked.
881 List<ShortcutInfoCompat> pinnedShortcuts =
882 shortcutManager.queryForPinnedShortcuts(null, user);
883 if (shortcutManager.wasLastCallSuccess()) {
884 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
885 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
889 // Shortcut manager can fail due to some race condition when the
890 // lock state changes too frequently. For the purpose of the loading
891 // shortcuts, consider the user is still locked.
892 userUnlocked = false;
895 unlockedUsers.put(serialNo, userUnlocked);
899 LauncherAppWidgetInfo appWidgetInfo;
903 while (!mStopped && c.moveToNext()) {
905 if (c.user == null) {
906 // User has been deleted, remove the item.
907 c.markDeleted("User has been deleted");
911 boolean allowMissingTarget = false;
912 switch (c.itemType) {
913 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
914 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
915 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
916 intent = c.parseIntent();
917 if (intent == null) {
918 c.markDeleted("Invalid or null intent");
922 int disabledState = quietMode.get(c.serialNumber) ?
923 ShortcutInfo.FLAG_DISABLED_QUIET_USER : 0;
924 ComponentName cn = intent.getComponent();
925 targetPkg = cn == null ? intent.getPackage() : cn.getPackageName();
927 if (!Process.myUserHandle().equals(c.user)) {
928 if (c.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
929 c.markDeleted("Legacy shortcuts are only allowed for default user");
931 } else if (c.restoreFlag != 0) {
932 // Don't restore items for other profiles.
933 c.markDeleted("Restore from managed profile not supported");
937 if (TextUtils.isEmpty(targetPkg) &&
938 c.itemType != LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
939 c.markDeleted("Only legacy shortcuts can have null package");
943 // If there is no target package, its an implicit intent
944 // (legacy shortcut) which is always valid
945 boolean validTarget = TextUtils.isEmpty(targetPkg) ||
946 launcherApps.isPackageEnabledForProfile(targetPkg, c.user);
948 if (cn != null && validTarget) {
949 // If the apk is present and the shortcut points to a specific
952 // If the component is already present
953 if (launcherApps.isActivityEnabledForProfile(cn, c.user)) {
954 // no special handling necessary for this item
957 if (c.hasRestoreFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
958 // We allow auto install apps to have their intent
959 // updated after an install.
960 intent = pmHelper.getAppLaunchIntent(targetPkg, c.user);
961 if (intent != null) {
964 LauncherSettings.Favorites.INTENT,
965 intent.toUri(0)).commit();
966 cn = intent.getComponent();
968 c.markDeleted("Unable to find a launch target");
972 // The app is installed but the component is no
974 c.markDeleted("Invalid component removed: " + cn);
979 // else if cn == null => can't infer much, leave it
980 // else if !validPkg => could be restored icon or missing sd-card
982 if (!TextUtils.isEmpty(targetPkg) && !validTarget) {
983 // Points to a valid app (superset of cn != null) but the apk
986 if (c.restoreFlag != 0) {
987 // Package is not yet available but might be
989 FileLog.d(TAG, "package not yet restored: " + targetPkg);
991 if (c.hasRestoreFlag(ShortcutInfo.FLAG_RESTORE_STARTED)) {
992 // Restore has started once.
993 } else if (installingPkgs.containsKey(targetPkg)) {
994 // App restore has started. Update the flag
995 c.restoreFlag |= ShortcutInfo.FLAG_RESTORE_STARTED;
996 c.updater().commit();
998 c.markDeleted("Unrestored app removed: " + targetPkg);
1001 } else if (pmHelper.isAppOnSdcard(targetPkg, c.user)) {
1002 // Package is present but not available.
1003 disabledState |= ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1004 // Add the icon on the workspace anyway.
1005 allowMissingTarget = true;
1006 } else if (!isSdCardReady) {
1007 // SdCard is not ready yet. Package might get available,
1008 // once it is ready.
1009 Log.d(TAG, "Missing pkg, will check later: " + targetPkg);
1010 pendingPackages.addToList(c.user, targetPkg);
1011 // Add the icon on the workspace anyway.
1012 allowMissingTarget = true;
1014 // Do not wait for external media load anymore.
1015 c.markDeleted("Invalid package removed: " + targetPkg);
1021 // The shortcut points to a valid target (either no target
1022 // or something which is ready to be used)
1026 boolean useLowResIcon = !c.isOnWorkspaceOrHotseat() &&
1027 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1029 if (c.restoreFlag != 0) {
1030 // Already verified above that user is same as default user
1031 info = c.getRestoredItemInfo(intent);
1032 } else if (c.itemType ==
1033 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1034 info = c.getAppShortcutInfo(
1035 intent, allowMissingTarget, useLowResIcon);
1036 } else if (c.itemType ==
1037 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
1039 ShortcutKey key = ShortcutKey.fromIntent(intent, c.user);
1040 if (unlockedUsers.get(c.serialNumber)) {
1041 ShortcutInfoCompat pinnedShortcut =
1042 shortcutKeyToPinnedShortcuts.get(key);
1043 if (pinnedShortcut == null) {
1044 // The shortcut is no longer valid.
1045 c.markDeleted("Pinned shortcut not found");
1048 info = new ShortcutInfo(pinnedShortcut, context);
1049 info.iconBitmap = LauncherIcons
1050 .createShortcutIcon(pinnedShortcut, context);
1051 if (pmHelper.isAppSuspended(
1052 pinnedShortcut.getPackage(), info.user)) {
1053 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1055 intent = info.intent;
1057 // Create a shortcut info in disabled mode for now.
1058 info = c.loadSimpleShortcut();
1059 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
1061 } else { // item type == ITEM_TYPE_SHORTCUT
1062 info = c.loadSimpleShortcut();
1064 // Shortcuts are only available on the primary profile
1065 if (pmHelper.isAppSuspended(targetPkg, c.user)) {
1066 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1069 // App shortcuts that used to be automatically added to Launcher
1070 // didn't always have the correct intent flags set, so do that
1072 if (intent.getAction() != null &&
1073 intent.getCategories() != null &&
1074 intent.getAction().equals(Intent.ACTION_MAIN) &&
1075 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
1077 Intent.FLAG_ACTIVITY_NEW_TASK |
1078 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1083 c.applyCommonProperties(info);
1085 info.intent = intent;
1086 info.rank = c.getInt(rankIndex);
1089 info.isDisabled |= disabledState;
1090 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1091 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1094 if (c.restoreFlag != 0 && !TextUtils.isEmpty(targetPkg)) {
1095 Integer progress = installingPkgs.get(targetPkg);
1096 if (progress != null) {
1097 info.setInstallProgress(progress);
1099 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
1103 c.checkAndAddItem(info, sBgDataModel);
1105 throw new RuntimeException("Unexpected null ShortcutInfo");
1109 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1110 FolderInfo folderInfo = sBgDataModel.findOrMakeFolder(c.id);
1111 c.applyCommonProperties(folderInfo);
1113 // Do not trim the folder label, as is was set by the user.
1114 folderInfo.title = c.getString(c.titleIndex);
1115 folderInfo.spanX = 1;
1116 folderInfo.spanY = 1;
1117 folderInfo.options = c.getInt(optionsIndex);
1119 // no special handling required for restored folders
1122 c.checkAndAddItem(folderInfo, sBgDataModel);
1125 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1126 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
1127 // Read all Launcher-specific widget details
1128 boolean customWidget = c.itemType ==
1129 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
1131 int appWidgetId = c.getInt(appWidgetIdIndex);
1132 String savedProvider = c.getString(appWidgetProviderIndex);
1134 final ComponentName component =
1135 ComponentName.unflattenFromString(savedProvider);
1137 final boolean isIdValid = !c.hasRestoreFlag(
1138 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID);
1139 final boolean wasProviderReady = !c.hasRestoreFlag(
1140 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY);
1142 if (widgetProvidersMap == null) {
1143 widgetProvidersMap = AppWidgetManagerCompat
1144 .getInstance(mContext).getAllProvidersMap();
1146 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
1148 ComponentName.unflattenFromString(savedProvider),
1151 final boolean isProviderReady = isValidProvider(provider);
1152 if (!isSafeMode && !customWidget &&
1153 wasProviderReady && !isProviderReady) {
1155 "Deleting widget that isn't installed anymore: "
1158 if (isProviderReady) {
1159 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1162 // The provider is available. So the widget is either
1163 // available or not available. We do not need to track
1164 // any future restore updates.
1165 int status = c.restoreFlag &
1166 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
1167 if (!wasProviderReady) {
1168 // If provider was not previously ready, update the
1169 // status and UI flag.
1171 // Id would be valid only if the widget restore broadcast was received.
1173 status |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
1175 status &= ~LauncherAppWidgetInfo
1176 .FLAG_PROVIDER_NOT_READY;
1179 appWidgetInfo.restoreStatus = status;
1181 Log.v(TAG, "Widget restore pending id=" + c.id
1182 + " appWidgetId=" + appWidgetId
1183 + " status =" + c.restoreFlag);
1184 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1186 appWidgetInfo.restoreStatus = c.restoreFlag;
1187 Integer installProgress = installingPkgs.get(component.getPackageName());
1189 if (c.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_RESTORE_STARTED)) {
1190 // Restore has started once.
1191 } else if (installProgress != null) {
1192 // App restore has started. Update the flag
1193 appWidgetInfo.restoreStatus |=
1194 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
1195 } else if (!isSafeMode) {
1196 c.markDeleted("Unrestored widget removed: " + component);
1200 appWidgetInfo.installProgress =
1201 installProgress == null ? 0 : installProgress;
1203 if (appWidgetInfo.hasRestoreFlag(
1204 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG)) {
1205 appWidgetInfo.bindOptions = c.parseIntent();
1208 c.applyCommonProperties(appWidgetInfo);
1209 appWidgetInfo.spanX = c.getInt(spanXIndex);
1210 appWidgetInfo.spanY = c.getInt(spanYIndex);
1211 appWidgetInfo.user = c.user;
1213 if (!c.isOnWorkspaceOrHotseat()) {
1214 c.markDeleted("Widget found where container != " +
1215 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1219 if (!customWidget) {
1220 String providerName =
1221 appWidgetInfo.providerName.flattenToString();
1222 if (!providerName.equals(savedProvider) ||
1223 (appWidgetInfo.restoreStatus != c.restoreFlag)) {
1225 .put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1227 .put(LauncherSettings.Favorites.RESTORED,
1228 appWidgetInfo.restoreStatus)
1232 c.checkAndAddItem(appWidgetInfo, sBgDataModel);
1236 } catch (Exception e) {
1237 Log.e(TAG, "Desktop items loading interrupted", e);
1241 Utilities.closeSilently(c);
1244 // Break early if we've stopped loading
1246 sBgDataModel.clear();
1250 // Remove dead items
1251 if (c.commitDeleted()) {
1252 // Remove any empty folder
1253 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
1254 .call(contentResolver,
1255 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
1256 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
1257 for (long folderId : deletedFolderIds) {
1258 sBgDataModel.workspaceItems.remove(sBgDataModel.folders.get(folderId));
1259 sBgDataModel.folders.remove(folderId);
1260 sBgDataModel.itemsIdMap.remove(folderId);
1264 // Unpin shortcuts that don't exist on the workspace.
1265 HashSet<ShortcutKey> pendingShortcuts =
1266 InstallShortcutReceiver.getPendingShortcuts(context);
1267 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
1268 MutableInt numTimesPinned = sBgDataModel.pinnedShortcutCounts.get(key);
1269 if ((numTimesPinned == null || numTimesPinned.value == 0)
1270 && !pendingShortcuts.contains(key)) {
1271 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
1272 shortcutManager.unpinShortcut(key);
1276 // Sort all the folder items and make sure the first 3 items are high resolution.
1277 for (FolderInfo folder : sBgDataModel.folders) {
1278 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
1280 for (ShortcutInfo info : folder.contents) {
1281 if (info.usingLowResIcon &&
1282 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1283 mIconCache.getTitleAndIcon(info, false);
1286 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
1292 c.commitRestoredItems();
1293 if (!isSdCardReady && !pendingPackages.isEmpty()) {
1294 context.registerReceiver(
1295 new SdCardAvailableReceiver(
1296 LauncherModel.this, mContext, pendingPackages),
1297 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
1302 // Remove any empty screens
1303 ArrayList<Long> unusedScreens = new ArrayList<>(sBgDataModel.workspaceScreens);
1304 for (ItemInfo item: sBgDataModel.itemsIdMap) {
1305 long screenId = item.screenId;
1306 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1307 unusedScreens.contains(screenId)) {
1308 unusedScreens.remove(screenId);
1312 // If there are any empty screens remove them, and update.
1313 if (unusedScreens.size() != 0) {
1314 sBgDataModel.workspaceScreens.removeAll(unusedScreens);
1315 updateWorkspaceScreenOrder(context, sBgDataModel.workspaceScreens);
1318 if (LauncherAppState.PROFILE_STARTUP) {
1323 /** Filters the set of items who are directly or indirectly (via another container) on the
1324 * specified screen. */
1325 private void filterCurrentWorkspaceItems(long currentScreenId,
1326 ArrayList<ItemInfo> allWorkspaceItems,
1327 ArrayList<ItemInfo> currentScreenItems,
1328 ArrayList<ItemInfo> otherScreenItems) {
1329 // Purge any null ItemInfos
1330 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
1331 while (iter.hasNext()) {
1332 ItemInfo i = iter.next();
1338 // Order the set of items by their containers first, this allows use to walk through the
1339 // list sequentially, build up a list of containers that are in the specified screen,
1340 // as well as all items in those containers.
1341 Set<Long> itemsOnScreen = new HashSet<Long>();
1342 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
1344 public int compare(ItemInfo lhs, ItemInfo rhs) {
1345 return Utilities.longCompare(lhs.container, rhs.container);
1348 for (ItemInfo info : allWorkspaceItems) {
1349 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1350 if (info.screenId == currentScreenId) {
1351 currentScreenItems.add(info);
1352 itemsOnScreen.add(info.id);
1354 otherScreenItems.add(info);
1356 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1357 currentScreenItems.add(info);
1358 itemsOnScreen.add(info.id);
1360 if (itemsOnScreen.contains(info.container)) {
1361 currentScreenItems.add(info);
1362 itemsOnScreen.add(info.id);
1364 otherScreenItems.add(info);
1370 /** Filters the set of widgets which are on the specified screen. */
1371 private void filterCurrentAppWidgets(long currentScreenId,
1372 ArrayList<LauncherAppWidgetInfo> appWidgets,
1373 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
1374 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
1376 for (LauncherAppWidgetInfo widget : appWidgets) {
1377 if (widget == null) continue;
1378 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1379 widget.screenId == currentScreenId) {
1380 currentScreenWidgets.add(widget);
1382 otherScreenWidgets.add(widget);
1387 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
1389 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
1390 final InvariantDeviceProfile profile = mApp.getInvariantDeviceProfile();
1391 final int screenCols = profile.numColumns;
1392 final int screenCellCount = profile.numColumns * profile.numRows;
1393 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
1395 public int compare(ItemInfo lhs, ItemInfo rhs) {
1396 if (lhs.container == rhs.container) {
1397 // Within containers, order by their spatial position in that container
1398 switch ((int) lhs.container) {
1399 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
1400 long lr = (lhs.screenId * screenCellCount +
1401 lhs.cellY * screenCols + lhs.cellX);
1402 long rr = (rhs.screenId * screenCellCount +
1403 rhs.cellY * screenCols + rhs.cellX);
1404 return Utilities.longCompare(lr, rr);
1406 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
1407 // We currently use the screen id as the rank
1408 return Utilities.longCompare(lhs.screenId, rhs.screenId);
1411 if (ProviderConfig.IS_DOGFOOD_BUILD) {
1412 throw new RuntimeException("Unexpected container type when " +
1413 "sorting workspace items.");
1418 // Between containers, order by hotseat, desktop
1419 return Utilities.longCompare(lhs.container, rhs.container);
1425 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
1426 final ArrayList<Long> orderedScreens) {
1427 final Runnable r = new Runnable() {
1430 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1431 if (callbacks != null) {
1432 callbacks.bindScreens(orderedScreens);
1439 private void bindWorkspaceItems(final Callbacks oldCallbacks,
1440 final ArrayList<ItemInfo> workspaceItems,
1441 final ArrayList<LauncherAppWidgetInfo> appWidgets,
1442 final Executor executor) {
1444 // Bind the workspace items
1445 int N = workspaceItems.size();
1446 for (int i = 0; i < N; i += ITEMS_CHUNK) {
1447 final int start = i;
1448 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
1449 final Runnable r = new Runnable() {
1452 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1453 if (callbacks != null) {
1454 callbacks.bindItems(workspaceItems, start, start+chunkSize,
1459 executor.execute(r);
1462 // Bind the widgets, one at a time
1463 N = appWidgets.size();
1464 for (int i = 0; i < N; i++) {
1465 final LauncherAppWidgetInfo widget = appWidgets.get(i);
1466 final Runnable r = new Runnable() {
1468 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1469 if (callbacks != null) {
1470 callbacks.bindAppWidget(widget);
1474 executor.execute(r);
1479 * Binds all loaded data to actual views on the main thread.
1481 private void bindWorkspace(int synchronizeBindPage) {
1482 final long t = SystemClock.uptimeMillis();
1485 // Don't use these two variables in any of the callback runnables.
1486 // Otherwise we hold a reference to them.
1487 final Callbacks oldCallbacks = mCallbacks.get();
1488 if (oldCallbacks == null) {
1489 // This launcher has exited and nobody bothered to tell us. Just bail.
1490 Log.w(TAG, "LoaderTask running with no launcher");
1494 // Save a copy of all the bg-thread collections
1495 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
1496 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
1497 ArrayList<Long> orderedScreenIds = new ArrayList<>();
1499 synchronized (sBgDataModel) {
1500 workspaceItems.addAll(sBgDataModel.workspaceItems);
1501 appWidgets.addAll(sBgDataModel.appWidgets);
1502 orderedScreenIds.addAll(sBgDataModel.workspaceScreens);
1505 final int currentScreen;
1507 int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE
1508 ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen();
1509 if (currScreen >= orderedScreenIds.size()) {
1510 // There may be no workspace screens (just hotseat items and an empty page).
1511 currScreen = PagedView.INVALID_RESTORE_PAGE;
1513 currentScreen = currScreen;
1515 final boolean validFirstPage = currentScreen >= 0;
1516 final long currentScreenId =
1517 validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID;
1519 // Separate the items that are on the current screen, and all the other remaining items
1520 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
1521 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
1522 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
1523 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
1525 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
1526 otherWorkspaceItems);
1527 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
1529 sortWorkspaceItemsSpatially(currentWorkspaceItems);
1530 sortWorkspaceItemsSpatially(otherWorkspaceItems);
1532 // Tell the workspace that we're about to start binding items
1533 r = new Runnable() {
1535 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1536 if (callbacks != null) {
1537 callbacks.clearPendingBinds();
1538 callbacks.startBinding();
1544 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
1546 Executor mainExecutor = new DeferredMainThreadExecutor();
1547 // Load items on the current page.
1548 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
1550 // In case of validFirstPage, only bind the first screen, and defer binding the
1551 // remaining screens after first onDraw (and an optional the fade animation whichever
1553 // This ensures that the first screen is immediately visible (eg. during rotation)
1554 // In case of !validFirstPage, bind all pages one after other.
1555 final Executor deferredExecutor =
1556 validFirstPage ? new ViewOnDrawExecutor(mHandler) : mainExecutor;
1558 mainExecutor.execute(new Runnable() {
1561 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1562 if (callbacks != null) {
1563 callbacks.finishFirstPageBind(
1564 validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null);
1569 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
1571 // Tell the workspace that we're done binding items
1572 r = new Runnable() {
1574 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1575 if (callbacks != null) {
1576 callbacks.finishBindingItems();
1579 mIsLoadingAndBindingWorkspace = false;
1581 // Run all the bind complete runnables after workspace is bound.
1582 if (!mBindCompleteRunnables.isEmpty()) {
1583 synchronized (mBindCompleteRunnables) {
1584 for (final Runnable r : mBindCompleteRunnables) {
1585 runOnWorkerThread(r);
1587 mBindCompleteRunnables.clear();
1591 // If we're profiling, ensure this is the last thing in the queue.
1592 if (DEBUG_LOADERS) {
1593 Log.d(TAG, "bound workspace in "
1594 + (SystemClock.uptimeMillis()-t) + "ms");
1599 deferredExecutor.execute(r);
1601 if (validFirstPage) {
1602 r = new Runnable() {
1604 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1605 if (callbacks != null) {
1606 // We are loading synchronously, which means, some of the pages will be
1607 // bound after first draw. Inform the callbacks that page binding is
1608 // not complete, and schedule the remaining pages.
1609 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
1610 callbacks.onPageBoundSynchronously(currentScreen);
1612 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
1620 private void updateIconCache() {
1621 // Ignore packages which have a promise icon.
1622 HashSet<String> packagesToIgnore = new HashSet<>();
1623 synchronized (sBgDataModel) {
1624 for (ItemInfo info : sBgDataModel.itemsIdMap) {
1625 if (info instanceof ShortcutInfo) {
1626 ShortcutInfo si = (ShortcutInfo) info;
1627 if (si.isPromise() && si.getTargetComponent() != null) {
1628 packagesToIgnore.add(si.getTargetComponent().getPackageName());
1630 } else if (info instanceof LauncherAppWidgetInfo) {
1631 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
1632 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
1633 packagesToIgnore.add(lawi.providerName.getPackageName());
1638 mIconCache.updateDbIcons(packagesToIgnore);
1641 private void onlyBindAllApps() {
1642 final Callbacks oldCallbacks = mCallbacks.get();
1643 if (oldCallbacks == null) {
1644 // This launcher has exited and nobody bothered to tell us. Just bail.
1645 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1650 @SuppressWarnings("unchecked")
1651 final ArrayList<AppInfo> list
1652 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
1653 Runnable r = new Runnable() {
1655 final long t = SystemClock.uptimeMillis();
1656 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1657 if (callbacks != null) {
1658 callbacks.bindAllApplications(list);
1660 if (DEBUG_LOADERS) {
1661 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
1662 + (SystemClock.uptimeMillis() - t) + "ms");
1669 private void loadAllApps() {
1670 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1672 final Callbacks oldCallbacks = mCallbacks.get();
1673 if (oldCallbacks == null) {
1674 // This launcher has exited and nobody bothered to tell us. Just bail.
1675 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
1679 final List<UserHandle> profiles = mUserManager.getUserProfiles();
1681 // Clear the list of apps
1682 mBgAllAppsList.clear();
1683 for (UserHandle user : profiles) {
1684 // Query for the set of apps
1685 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
1686 final List<LauncherActivityInfo> apps = mLauncherApps.getActivityList(null, user);
1687 if (DEBUG_LOADERS) {
1688 Log.d(TAG, "getActivityList took "
1689 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
1690 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
1692 // Fail if we don't have any apps
1693 // TODO: Fix this. Only fail for the current user.
1694 if (apps == null || apps.isEmpty()) {
1697 boolean quietMode = mUserManager.isQuietModeEnabled(user);
1698 // Create the ApplicationInfos
1699 for (int i = 0; i < apps.size(); i++) {
1700 LauncherActivityInfo app = apps.get(i);
1701 // This builds the icon bitmaps.
1702 mBgAllAppsList.add(new AppInfo(app, user, quietMode), app);
1705 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
1706 if (heuristic != null) {
1707 final Runnable r = new Runnable() {
1711 heuristic.processUserApps(apps);
1714 runOnMainThread(new Runnable() {
1718 // Check isLoadingWorkspace on the UI thread, as it is updated on
1720 if (mIsLoadingAndBindingWorkspace) {
1721 synchronized (mBindCompleteRunnables) {
1722 mBindCompleteRunnables.add(r);
1725 runOnWorkerThread(r);
1731 // Huh? Shouldn't this be inside the Runnable below?
1732 final ArrayList<AppInfo> added = mBgAllAppsList.added;
1733 mBgAllAppsList.added = new ArrayList<AppInfo>();
1735 // Post callback on main thread
1736 mHandler.post(new Runnable() {
1739 final long bindTime = SystemClock.uptimeMillis();
1740 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1741 if (callbacks != null) {
1742 callbacks.bindAllApplications(added);
1743 if (DEBUG_LOADERS) {
1744 Log.d(TAG, "bound " + added.size() + " apps in "
1745 + (SystemClock.uptimeMillis() - bindTime) + "ms");
1748 Log.i(TAG, "not binding apps: no Launcher activity");
1752 // Cleanup any data stored for a deleted user.
1753 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
1754 if (DEBUG_LOADERS) {
1755 Log.d(TAG, "Icons processed in "
1756 + (SystemClock.uptimeMillis() - loadTime) + "ms");
1760 private void loadDeepShortcuts() {
1761 if (!mModelLoaded) {
1762 sBgDataModel.deepShortcutMap.clear();
1763 DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(mContext);
1764 mHasShortcutHostPermission = shortcutManager.hasHostPermission();
1765 if (mHasShortcutHostPermission) {
1766 for (UserHandle user : mUserManager.getUserProfiles()) {
1767 if (mUserManager.isUserUnlocked(user)) {
1768 List<ShortcutInfoCompat> shortcuts =
1769 shortcutManager.queryForAllShortcuts(user);
1770 sBgDataModel.updateDeepShortcutMap(null, user, shortcuts);
1778 public void bindDeepShortcuts() {
1779 final MultiHashMap<ComponentKey, String> shortcutMapCopy =
1780 sBgDataModel.deepShortcutMap.clone();
1781 Runnable r = new Runnable() {
1784 Callbacks callbacks = getCallback();
1785 if (callbacks != null) {
1786 callbacks.bindDeepShortcutMap(shortcutMapCopy);
1794 * Refreshes the cached shortcuts if the shortcut permission has changed.
1795 * Current implementation simply reloads the workspace, but it can be optimized to
1796 * use partial updates similar to {@link UserManagerCompat}
1798 public void refreshShortcutsIfRequired() {
1799 if (Utilities.ATLEAST_NOUGAT_MR1) {
1800 sWorker.removeCallbacks(mShortcutPermissionCheckRunnable);
1801 sWorker.post(mShortcutPermissionCheckRunnable);
1806 * Called when the icons for packages have been updated in the icon cache.
1808 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandle user) {
1809 // If any package icon has changed (app was updated while launcher was dead),
1810 // update the corresponding shortcuts.
1811 enqueueModelUpdateTask(new CacheDataUpdatedTask(
1812 CacheDataUpdatedTask.OP_CACHE_UPDATE, user, updatedPackages));
1815 void enqueueModelUpdateTask(BaseModelUpdateTask task) {
1816 if (!mModelLoaded && mLoaderTask == null) {
1817 if (DEBUG_LOADERS) {
1818 Log.d(TAG, "enqueueModelUpdateTask Ignoring task since loader is pending=" + task);
1823 runOnWorkerThread(task);
1827 * A task to be executed on the current callbacks on the UI thread.
1828 * If there is no current callbacks, the task is ignored.
1830 public interface CallbackTask {
1832 void execute(Callbacks callbacks);
1836 * A runnable which changes/updates the data model of the launcher based on certain events.
1838 public static abstract class BaseModelUpdateTask implements Runnable {
1840 private LauncherModel mModel;
1841 private DeferredHandler mUiHandler;
1843 /* package private */
1844 void init(LauncherModel model) {
1846 mUiHandler = mModel.mHandler;
1851 if (!mModel.mHasLoaderCompletedOnce) {
1852 // Loader has not yet run.
1855 execute(mModel.mApp, sBgDataModel, mModel.mBgAllAppsList);
1859 * Execute the actual task. Called on the worker thread.
1861 public abstract void execute(
1862 LauncherAppState app, BgDataModel dataModel, AllAppsList apps);
1865 * Schedules a {@param task} to be executed on the current callbacks.
1867 public final void scheduleCallbackTask(final CallbackTask task) {
1868 final Callbacks callbacks = mModel.getCallback();
1869 mUiHandler.post(new Runnable() {
1871 Callbacks cb = mModel.getCallback();
1872 if (callbacks == cb && cb != null) {
1873 task.execute(callbacks);
1879 public ModelWriter getModelWriter() {
1880 // Updates from model task, do not deal with icon position in hotseat.
1881 return mModel.getWriter(false /* hasVerticalHotseat */);
1885 public void updateAndBindShortcutInfo(final ShortcutInfo si, final ShortcutInfoCompat info) {
1886 updateAndBindShortcutInfo(new Provider<ShortcutInfo>() {
1888 public ShortcutInfo get() {
1889 si.updateFromDeepShortcutInfo(info, mApp.getContext());
1890 si.iconBitmap = LauncherIcons.createShortcutIcon(info, mApp.getContext());
1897 * Utility method to update a shortcut on the background thread.
1899 public void updateAndBindShortcutInfo(final Provider<ShortcutInfo> shortcutProvider) {
1900 enqueueModelUpdateTask(new ExtendedModelTask() {
1902 public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
1903 ShortcutInfo info = shortcutProvider.get();
1904 ArrayList<ShortcutInfo> update = new ArrayList<>();
1906 bindUpdatedShortcuts(update, info.user);
1911 private void bindWidgetsModel(final Callbacks callbacks) {
1912 final MultiHashMap<PackageItemInfo, WidgetItem> widgets
1913 = mBgWidgetsModel.getWidgetsMap().clone();
1914 mHandler.post(new Runnable() {
1917 Callbacks cb = getCallback();
1918 if (callbacks == cb && cb != null) {
1919 callbacks.bindAllWidgets(widgets);
1925 public void refreshAndBindWidgetsAndShortcuts(final Callbacks callbacks,
1926 final boolean bindFirst, @Nullable final PackageUserKey packageUser) {
1927 runOnWorkerThread(new Runnable() {
1930 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
1931 bindWidgetsModel(callbacks);
1933 ArrayList<WidgetItem> widgets = mBgWidgetsModel.update(
1934 mApp.getContext(), packageUser);
1935 bindWidgetsModel(callbacks);
1937 // update the Widget entries inside DB on the worker thread.
1938 mApp.getWidgetCache().removeObsoletePreviews(widgets, packageUser);
1943 static boolean isValidProvider(AppWidgetProviderInfo provider) {
1944 return (provider != null) && (provider.provider != null)
1945 && (provider.provider.getPackageName() != null);
1948 public void dumpState(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
1949 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
1950 writer.println(prefix + "All apps list: size=" + mBgAllAppsList.data.size());
1951 for (AppInfo info : mBgAllAppsList.data) {
1952 writer.println(prefix + " title=\"" + info.title + "\" iconBitmap=" + info.iconBitmap
1953 + " componentName=" + info.componentName.getPackageName());
1956 sBgDataModel.dump(prefix, fd, writer, args);
1959 public Callbacks getCallback() {
1960 return mCallbacks != null ? mCallbacks.get() : null;
1964 * @return {@link FolderInfo} if its already loaded.
1966 public FolderInfo findFolderById(Long folderId) {
1967 synchronized (sBgDataModel) {
1968 return sBgDataModel.folders.get(folderId);
1972 @Thunk class DeferredMainThreadExecutor implements Executor {
1975 public void execute(Runnable command) {
1976 runOnMainThread(command);
1981 * @return the looper for the worker thread which can be used to start background tasks.
1983 public static Looper getWorkerLooper() {
1984 return sWorkerThread.getLooper();