blob: ca9f3120d618904b999c39d4255c40a27cc6c285 [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;
Mario Bertschler817afa32017-03-15 11:56:47 -070029import android.content.pm.PackageInstaller;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.net.Uri;
Joe Onorato36115782010-06-17 13:28:48 -040031import android.os.Handler;
32import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070033import android.os.Looper;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040035import android.os.SystemClock;
Sunny Goyale26d1002016-06-20 14:52:14 -070036import android.os.Trace;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080037import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070038import android.support.annotation.Nullable;
Winson Chunga90303b2013-11-15 13:05:06 -080039import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070040import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080041import android.util.LongSparseArray;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070042import android.util.MutableInt;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010043
Sunny Goyalffe83f12014-08-14 17:39:34 -070044import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010045import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070046import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070047import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010048import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyal3d706ad2017-03-06 16:56:39 -080049import com.android.launcher3.config.FeatureFlags;
Tony Wickham827cef22016-03-17 15:39:39 -070050import com.android.launcher3.dynamicui.ExtractionUtils;
Sunny Goyal26119432016-02-18 22:09:23 +000051import com.android.launcher3.folder.Folder;
52import com.android.launcher3.folder.FolderIcon;
Jon Miranda655ec422017-02-07 11:40:22 -080053import com.android.launcher3.folder.FolderIconPreviewVerifier;
Sunny Goyal1b072632017-01-18 11:30:23 -080054import com.android.launcher3.graphics.LauncherIcons;
Sunny Goyal1acb9e92016-05-16 12:41:09 -070055import com.android.launcher3.logging.FileLog;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070056import com.android.launcher3.model.AddWorkspaceItemsTask;
Sunny Goyale9956a72016-09-01 17:24:47 -070057import com.android.launcher3.model.BgDataModel;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070058import com.android.launcher3.model.CacheDataUpdatedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080059import com.android.launcher3.model.ExtendedModelTask;
Sunny Goyalf862a262015-12-14 14:27:38 -080060import com.android.launcher3.model.GridSizeMigrationTask;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -080061import com.android.launcher3.model.LoaderCursor;
Sunny Goyal43bf11d2017-02-02 13:52:53 -080062import com.android.launcher3.model.ModelWriter;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070063import com.android.launcher3.model.PackageInstallStateChangedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080064import com.android.launcher3.model.PackageItemInfo;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070065import com.android.launcher3.model.PackageUpdatedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080066import com.android.launcher3.model.SdCardAvailableReceiver;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070067import com.android.launcher3.model.ShortcutsChangedTask;
68import com.android.launcher3.model.UserLockStateChangedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080069import com.android.launcher3.model.WidgetItem;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070070import com.android.launcher3.model.WidgetsModel;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -070071import com.android.launcher3.provider.ImportDataTask;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070072import com.android.launcher3.provider.LauncherDbUtils;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070073import com.android.launcher3.shortcuts.DeepShortcutManager;
74import com.android.launcher3.shortcuts.ShortcutInfoCompat;
75import com.android.launcher3.shortcuts.ShortcutKey;
Robin Lee26ace122015-03-16 19:41:43 +000076import com.android.launcher3.util.ComponentKey;
Sunny Goyalb265ba72017-02-14 15:03:45 -080077import com.android.launcher3.util.LooperIdleLock;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070078import com.android.launcher3.util.ManagedProfileHeuristic;
Tony Wickhamd82a39d2016-07-01 15:44:13 -070079import com.android.launcher3.util.MultiHashMap;
Sunny Goyald09c3702016-04-06 16:18:20 -070080import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham86222d22017-03-29 15:30:43 -070081import com.android.launcher3.util.PackageUserKey;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070082import com.android.launcher3.util.Preconditions;
Sunny Goyal2bcbe132016-11-16 09:23:42 -080083import com.android.launcher3.util.Provider;
Adam Cohen091440a2015-03-18 14:16:05 -070084import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070085import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080086
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -080087import java.io.FileDescriptor;
88import java.io.PrintWriter;
Michael Jurkac2f801e2011-07-12 14:19:46 -070089import java.lang.ref.WeakReference;
Michael Jurkac2f801e2011-07-12 14:19:46 -070090import java.util.ArrayList;
91import java.util.Collections;
92import java.util.Comparator;
93import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070094import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070095import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070096import java.util.List;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070097import java.util.Map;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070098import java.util.Set;
Sunny Goyaldd96a5e2017-02-17 11:22:34 -080099import java.util.concurrent.CancellationException;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700100import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -0700101
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102/**
103 * Maintains in-memory state of the Launcher. It is expected that there should be only one
104 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700105 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106 */
Kenny Guyed131872014-04-30 03:02:21 +0100107public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +0100108 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800109 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -0400110 private static final boolean DEBUG_RECEIVER = false;
Chris Wrenb358f812014-04-16 13:37:00 -0400111
Joe Onorato9c1289c2009-08-17 11:03:03 -0400112 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700113
Joe Onorato36115782010-06-17 13:28:48 -0400114 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500115 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800116
Sunny Goyalb265ba72017-02-14 15:03:45 -0800117 private final MainThreadExecutor mUiExecutor = new MainThreadExecutor();
Adam Cohen091440a2015-03-18 14:16:05 -0700118 @Thunk final LauncherAppState mApp;
119 @Thunk final Object mLock = new Object();
Adam Cohen091440a2015-03-18 14:16:05 -0700120 @Thunk LoaderTask mLoaderTask;
121 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700122 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Adam Cohen091440a2015-03-18 14:16:05 -0700124 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700125 static {
126 sWorkerThread.start();
127 }
Adam Cohen091440a2015-03-18 14:16:05 -0700128 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700129
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800130 // Indicates whether the current model data is valid or not.
131 // We start off with everything not loaded. After that, we assume that
Joe Onoratocc67f472010-06-08 10:54:30 -0700132 // our monitoring of the package manager provides all updates and we never
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800133 // need to do a requery. This is only ever touched from the loader thread.
134 private boolean mModelLoaded;
Hyunyoung Song6aa37292017-02-06 10:46:24 -0800135 public boolean isModelLoaded() {
136 synchronized (mLock) {
137 return mModelLoaded && mLoaderTask == null;
138 }
139 }
Joe Onoratocc67f472010-06-08 10:54:30 -0700140
Sunny Goyal756a28a2015-04-23 17:07:55 -0700141 /**
142 * Set of runnables to be called on the background thread after the workspace binding
143 * is complete.
144 */
145 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
146
Adam Cohen091440a2015-03-18 14:16:05 -0700147 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700149 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800150 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700151 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800152 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800153
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700154 private boolean mHasShortcutHostPermission;
155 // Runnable to check if the shortcuts permission has changed.
156 private final Runnable mShortcutPermissionCheckRunnable = new Runnable() {
157 @Override
158 public void run() {
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800159 if (mModelLoaded) {
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530160 boolean hasShortcutHostPermission =
161 DeepShortcutManager.getInstance(mApp.getContext()).hasHostPermission();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700162 if (hasShortcutHostPermission != mHasShortcutHostPermission) {
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800163 forceReload();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700164 }
165 }
166 }
167 };
168
Sunny Goyale9956a72016-09-01 17:24:47 -0700169 /**
170 * All the static data should be accessed on the background thread, A lock should be acquired
171 * on this object when accessing any data from this model.
172 */
173 static final BgDataModel sBgDataModel = new BgDataModel();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700174
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700175 // </ only access in worker thread >
176
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700177 private final IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700179 private final LauncherAppsCompat mLauncherApps;
180 private final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100181
Joe Onorato9c1289c2009-08-17 11:03:03 -0400182 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700183 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400184 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700185 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400186 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700187 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
188 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700189 public void bindScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700190 public void finishFirstPageBind(ViewOnDrawExecutor executor);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800191 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400192 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200193 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700194 public void bindAppsAdded(ArrayList<Long> newScreens,
195 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700196 ArrayList<ItemInfo> addAnimated,
197 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200198 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700199 public void bindPromiseAppProgressUpdated(PromiseAppInfo app);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700200 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800201 ArrayList<ShortcutInfo> removed, UserHandle user);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700202 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700203 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700204 public void bindWorkspaceComponentsRemoved(
205 HashSet<String> packageNames, HashSet<ComponentName> components,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800206 UserHandle user);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700207 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800208 public void notifyWidgetProvidersChanged();
Sunny Goyald164b7f2016-10-12 20:49:31 -0700209 public void bindAllWidgets(MultiHashMap<PackageItemInfo, WidgetItem> widgets);
Adam Cohen1462de32012-07-24 22:34:36 -0700210 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700211 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700212 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMap);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400213 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530215 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800216 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400217 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100218 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Sunny Goyald164b7f2016-10-12 20:49:31 -0700219 mBgWidgetsModel = new WidgetsModel(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800220 mIconCache = iconCache;
221
Kenny Guyed131872014-04-30 03:02:21 +0100222 mLauncherApps = LauncherAppsCompat.getInstance(context);
223 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800224 }
225
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700226 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
227 * posted on the worker thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700228 private static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700229 if (sWorkerThread.getThreadId() == Process.myTid()) {
230 r.run();
231 } else {
232 // If we are not on the worker thread, then post to the worker handler
233 sWorker.post(r);
234 }
235 }
236
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700237 public void setPackageState(PackageInstallInfo installInfo) {
238 enqueueModelUpdateTask(new PackageInstallStateChangedTask(installInfo));
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500239 }
240
Sunny Goyal756adbc2015-04-16 15:20:51 -0700241 /**
242 * Updates the icons and label of all pending icons for the provided package name.
243 */
244 public void updateSessionDisplayInfo(final String packageName) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700245 HashSet<String> packages = new HashSet<>();
246 packages.add(packageName);
247 enqueueModelUpdateTask(new CacheDataUpdatedTask(
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800248 CacheDataUpdatedTask.OP_SESSION_UPDATE, Process.myUserHandle(), packages));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800249 }
250
251 /**
252 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800253 */
Sunny Goyal2bcbe132016-11-16 09:23:42 -0800254 public void addAndBindAddedWorkspaceItems(List<ItemInfo> workspaceApps) {
255 addAndBindAddedWorkspaceItems(Provider.of(workspaceApps));
256 }
257
258 /**
259 * Adds the provided items to the workspace.
260 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700261 public void addAndBindAddedWorkspaceItems(
Sunny Goyal2bcbe132016-11-16 09:23:42 -0800262 Provider<List<ItemInfo>> appsProvider) {
263 enqueueModelUpdateTask(new AddWorkspaceItemsTask(appsProvider));
Winson Chung64359a52013-07-08 17:17:08 -0700264 }
265
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800266 public ModelWriter getWriter(boolean hasVerticalHotseat) {
267 return new ModelWriter(mApp.getContext(), sBgDataModel, hasVerticalHotseat);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268 }
269
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700270 static void checkItemInfoLocked(
271 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
Sunny Goyale9956a72016-09-01 17:24:47 -0700272 ItemInfo modelItem = sBgDataModel.itemsIdMap.get(itemId);
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700273 if (modelItem != null && item != modelItem) {
274 // check all the data is consistent
275 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
276 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
277 ShortcutInfo shortcut = (ShortcutInfo) item;
278 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
279 modelShortcut.intent.filterEquals(shortcut.intent) &&
280 modelShortcut.id == shortcut.id &&
281 modelShortcut.itemType == shortcut.itemType &&
282 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700283 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700284 modelShortcut.cellX == shortcut.cellX &&
285 modelShortcut.cellY == shortcut.cellY &&
286 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700287 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700288 // For all intents and purposes, this is the same object
289 return;
290 }
291 }
292
293 // the modelItem needs to match up perfectly with item if our model is
294 // to be consistent with the database-- for now, just require
295 // modelItem == item or the equality check above
296 String msg = "item: " + ((item != null) ? item.toString() : "null") +
297 "modelItem: " +
298 ((modelItem != null) ? modelItem.toString() : "null") +
299 "Error: ItemInfo passed to checkItemInfo doesn't match original";
300 RuntimeException e = new RuntimeException(msg);
301 if (stackTrace != null) {
302 e.setStackTrace(stackTrace);
303 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800304 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700305 }
306 }
307
Michael Jurka816474f2012-06-25 14:49:02 -0700308 static void checkItemInfo(final ItemInfo item) {
309 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
310 final long itemId = item.id;
311 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700312 public void run() {
Sunny Goyale9956a72016-09-01 17:24:47 -0700313 synchronized (sBgDataModel) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700314 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700315 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700316 }
317 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700318 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700319 }
320
Joe Onorato9c1289c2009-08-17 11:03:03 -0400321 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700322 * Update the order of the workspace screens in the database. The array list contains
323 * a list of screen ids in the order that they should appear.
324 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700325 public static void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -0700326 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700327 final ContentResolver cr = context.getContentResolver();
328 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
329
330 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700331 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700332 while (iter.hasNext()) {
333 long id = iter.next();
334 if (id < 0) {
335 iter.remove();
336 }
337 }
338
339 Runnable r = new Runnable() {
340 @Override
341 public void run() {
Yura085c8532014-02-11 15:15:29 +0000342 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700343 // Clear the table
Yura085c8532014-02-11 15:15:29 +0000344 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -0700345 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700346 for (int i = 0; i < count; i++) {
347 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700348 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700349 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
350 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +0000351 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -0700352 }
Yura085c8532014-02-11 15:15:29 +0000353
354 try {
355 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
356 } catch (Exception ex) {
357 throw new RuntimeException(ex);
358 }
Winson Chung9e6a0a22013-08-27 11:58:12 -0700359
Sunny Goyale9956a72016-09-01 17:24:47 -0700360 synchronized (sBgDataModel) {
361 sBgDataModel.workspaceScreens.clear();
362 sBgDataModel.workspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -0700363 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700364 }
365 };
366 runOnWorkerThread(r);
367 }
368
369 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400370 * Set this as the current Launcher activity object for the loader.
371 */
372 public void initialize(Callbacks callbacks) {
373 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -0700374 Preconditions.assertUIThread();
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -0700375 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400376 }
377 }
378
Kenny Guyed131872014-04-30 03:02:21 +0100379 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800380 public void onPackageChanged(String packageName, UserHandle user) {
Kenny Guyed131872014-04-30 03:02:21 +0100381 int op = PackageUpdatedTask.OP_UPDATE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700382 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100383 }
384
385 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800386 public void onPackageRemoved(String packageName, UserHandle user) {
Sunny Goyalc2936bc2016-09-01 15:50:36 -0700387 onPackagesRemoved(user, packageName);
388 }
389
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800390 public void onPackagesRemoved(UserHandle user, String... packages) {
Kenny Guyed131872014-04-30 03:02:21 +0100391 int op = PackageUpdatedTask.OP_REMOVE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700392 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packages));
Kenny Guyed131872014-04-30 03:02:21 +0100393 }
394
395 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800396 public void onPackageAdded(String packageName, UserHandle user) {
Kenny Guyed131872014-04-30 03:02:21 +0100397 int op = PackageUpdatedTask.OP_ADD;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700398 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100399 }
400
401 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800402 public void onPackagesAvailable(String[] packageNames, UserHandle user,
Kenny Guyed131872014-04-30 03:02:21 +0100403 boolean replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700404 enqueueModelUpdateTask(
405 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100406 }
407
408 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800409 public void onPackagesUnavailable(String[] packageNames, UserHandle user,
Kenny Guyed131872014-04-30 03:02:21 +0100410 boolean replacing) {
411 if (!replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700412 enqueueModelUpdateTask(new PackageUpdatedTask(
413 PackageUpdatedTask.OP_UNAVAILABLE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100414 }
Kenny Guyed131872014-04-30 03:02:21 +0100415 }
416
Kenny Guy44cba692016-01-21 19:50:02 +0000417 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800418 public void onPackagesSuspended(String[] packageNames, UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700419 enqueueModelUpdateTask(new PackageUpdatedTask(
420 PackageUpdatedTask.OP_SUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000421 }
422
423 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800424 public void onPackagesUnsuspended(String[] packageNames, UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700425 enqueueModelUpdateTask(new PackageUpdatedTask(
426 PackageUpdatedTask.OP_UNSUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000427 }
428
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700429 @Override
430 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800431 UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700432 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, true));
Sunny Goyal50941fb2016-08-04 12:03:52 -0700433 }
434
435 public void updatePinnedShortcuts(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800436 UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700437 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, false));
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700438 }
439
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700440 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400441 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
442 * ACTION_PACKAGE_CHANGED.
443 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100444 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400445 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -0400446 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700447
Joe Onorato36115782010-06-17 13:28:48 -0400448 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +0100449 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -0700450 // If we have changed locale we need to clear out the labels in all apps/workspace.
451 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700452 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
453 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -0700454 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -0700455 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700456 } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
457 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
458 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800459 UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER);
Sunny Goyalda891c12016-03-18 18:29:24 -0700460 if (user != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700461 if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
462 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700463 enqueueModelUpdateTask(new PackageUpdatedTask(
464 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE, user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700465 }
466
467 // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so
468 // we need to run the state change task again.
469 if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
470 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700471 enqueueModelUpdateTask(new UserLockStateChangedTask(user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700472 }
Sunny Goyalda891c12016-03-18 18:29:24 -0700473 }
Tony Wickham827cef22016-03-17 15:39:39 -0700474 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
475 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700476 }
477 }
478
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800479 /**
480 * Reloads the workspace items from the DB and re-binds the workspace. This should generally
481 * not be called as DB updates are automatically followed by UI update
482 */
483 public void forceReload() {
484 synchronized (mLock) {
485 // Stop any existing loaders first, so they don't set mModelLoaded to true later
486 stopLoaderLocked();
487 mModelLoaded = false;
488 }
Winson Chungf0c6ae02012-03-21 16:10:31 -0700489
Reena Lee93f824a2011-09-23 17:20:28 -0700490 // Do this here because if the launcher activity is running it will be restarted.
491 // If it's not running startLoaderFromBackground will merely tell it that it needs
492 // to reload.
493 startLoaderFromBackground();
494 }
495
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700496 /**
497 * When the launcher is in the background, it's possible for it to miss paired
498 * configuration changes. So whenever we trigger the loader from the background
499 * tell the launcher that it needs to re-run the loader when it comes back instead
500 * of doing it now.
501 */
502 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800503 Callbacks callbacks = getCallback();
504 if (callbacks != null) {
505 // Only actually run the loader if they're not paused.
506 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -0700507 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700508 }
509 }
Joe Onorato36115782010-06-17 13:28:48 -0400510 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400511
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700512 /**
513 * If there is already a loader task running, tell it to stop.
514 */
515 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -0700516 LoaderTask oldTask = mLoaderTask;
517 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -0700518 oldTask.stopLocked();
519 }
Reena Lee93f824a2011-09-23 17:20:28 -0700520 }
521
Adam Cohen1a85c582014-09-30 09:48:49 -0700522 public boolean isCurrentCallbacks(Callbacks callbacks) {
523 return (mCallbacks != null && mCallbacks.get() == callbacks);
524 }
525
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700526 /**
527 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
528 * @return true if the page could be bound synchronously.
529 */
530 public boolean startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700531 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
532 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -0400533 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -0400534 // Don't bother to start the thread if we know it's not going to do anything
535 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -0700536 final Callbacks oldCallbacks = mCallbacks.get();
537 // Clear any pending bind-runnables from the synchronized load process.
Sunny Goyalb265ba72017-02-14 15:03:45 -0800538 mUiExecutor.execute(new Runnable() {
539 public void run() {
540 oldCallbacks.clearPendingBinds();
541 }
542 });
Sunny Goyal527c7d32015-08-28 15:19:36 -0700543
Joe Onorato36115782010-06-17 13:28:48 -0400544 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700545 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700546 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800547 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
548 && mModelLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700549 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700550 return true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700551 } else {
552 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
553 sWorker.post(mLoaderTask);
554 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400555 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400556 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700557 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400558 }
559
Joe Onorato36115782010-06-17 13:28:48 -0400560 public void stopLoader() {
561 synchronized (mLock) {
562 if (mLoaderTask != null) {
563 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400564 }
565 }
Joe Onorato36115782010-06-17 13:28:48 -0400566 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400567
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800568 /**
569 * Loads the workspace screen ids in an ordered list.
570 */
Sunny Goyale5bb7052015-07-27 14:36:07 -0700571 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -0700572 final ContentResolver contentResolver = context.getContentResolver();
573 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -0700574
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800575 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -0700576 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
577 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -0700578 }
579
Mario Bertschler817afa32017-03-15 11:56:47 -0700580 public void onInstallSessionCreated(final PackageInstallInfo sessionInfo) {
581 enqueueModelUpdateTask(new ExtendedModelTask() {
582 @Override
583 public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
584 apps.addPromiseApp(app.getContext(), sessionInfo);
585 if (!apps.added.isEmpty()) {
586 final ArrayList<AppInfo> arrayList = new ArrayList<>(apps.added);
587 apps.added.clear();
588 scheduleCallbackTask(new CallbackTask() {
589 @Override
590 public void execute(Callbacks callbacks) {
591 callbacks.bindAppsAdded(null, null, null, arrayList);
592 }
593 });
594 }
595 }
596 });
597 }
598
Joe Onorato36115782010-06-17 13:28:48 -0400599 /**
600 * Runnable for the thread that loads the contents of the launcher:
601 * - workspace icons
602 * - widgets
603 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700604 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -0400605 */
606 private class LoaderTask implements Runnable {
607 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -0700608 private int mPageToBindFirst;
609
Adam Cohen091440a2015-03-18 14:16:05 -0700610 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -0400611 private boolean mStopped;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700612
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700613 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -0400614 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -0700615 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400616 }
617
Joe Onorato36115782010-06-17 13:28:48 -0400618 private void waitForIdle() {
619 // Wait until the either we're stopped or the other threads are done.
620 // This way we don't start loading all apps until the workspace has settled
621 // down.
622 synchronized (LoaderTask.this) {
Sunny Goyalb265ba72017-02-14 15:03:45 -0800623 LooperIdleLock idleLock = new LooperIdleLock(this, Looper.getMainLooper());
624 // Just in case mFlushingWorkerThread changes but we aren't woken up,
625 // wait no longer than 1sec at a time
626 while (!mStopped && idleLock.awaitLocked(1000));
Daniel Sandler843e8602010-06-07 14:59:01 -0400627 }
Joe Onorato36115782010-06-17 13:28:48 -0400628 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400629
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700630 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -0500631 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700632 // Ensure that we have a valid page index to load synchronously
633 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
634 "valid page index");
635 }
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800636 if (!mModelLoaded) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700637 // Ensure that we don't try and bind a specified page when the pages have not been
638 // loaded already (we should load everything asynchronously in that case)
639 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
640 }
641 synchronized (mLock) {
642 if (mIsLoaderTaskRunning) {
643 // Ensure that we are never running the background loading at this point since
644 // we also touch the background collections
645 throw new RuntimeException("Error! Background loading is already running");
646 }
647 }
648
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700649 // Divide the set of loaded items into those that we are binding synchronously, and
650 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800651 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700652 // XXX: For now, continue posting the binding of AllApps as there are other issues that
653 // arise from that.
654 onlyBindAllApps();
Tony Wickham80f57872016-06-29 18:12:15 -0700655
656 bindDeepShortcuts();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700657 }
658
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800659 private void verifyNotStopped() throws CancellationException {
660 synchronized (LoaderTask.this) {
661 if (mStopped) {
662 throw new CancellationException("Loader stopped");
663 }
664 }
665 }
666
Joe Onorato36115782010-06-17 13:28:48 -0400667 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700668 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -0700669 if (mStopped) {
670 return;
671 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700672 mIsLoaderTaskRunning = true;
673 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400674
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800675 try {
Sunny Goyalb265ba72017-02-14 15:03:45 -0800676 long now = 0;
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800677 if (DEBUG_LOADERS) Log.d(TAG, "step 1.1: loading workspace");
678 // Set to false in bindWorkspace()
679 mIsLoadingAndBindingWorkspace = true;
680 loadWorkspace();
Joe Onorato36115782010-06-17 13:28:48 -0400681
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800682 verifyNotStopped();
683 if (DEBUG_LOADERS) Log.d(TAG, "step 1.2: bind workspace workspace");
684 bindWorkspace(mPageToBindFirst);
685
686 // Take a break
Sunny Goyalb265ba72017-02-14 15:03:45 -0800687 if (DEBUG_LOADERS) {
688 Log.d(TAG, "step 1 completed, wait for idle");
689 now = SystemClock.uptimeMillis();
690 }
Joe Onorato36115782010-06-17 13:28:48 -0400691 waitForIdle();
Sunny Goyalb265ba72017-02-14 15:03:45 -0800692 if (DEBUG_LOADERS) Log.d(TAG, "Waited " + (SystemClock.uptimeMillis() - now) + "ms");
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800693 verifyNotStopped();
Daniel Sandler843e8602010-06-07 14:59:01 -0400694
695 // second step
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800696 if (DEBUG_LOADERS) Log.d(TAG, "step 2.1: loading all apps");
697 loadAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700698
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800699 verifyNotStopped();
700 if (DEBUG_LOADERS) Log.d(TAG, "step 2.2: Update icon cache");
701 updateIconCache();
702
703 // Take a break
Sunny Goyalb265ba72017-02-14 15:03:45 -0800704 if (DEBUG_LOADERS) {
705 Log.d(TAG, "step 2 completed, wait for idle");
706 now = SystemClock.uptimeMillis();
707 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700708 waitForIdle();
Sunny Goyalb265ba72017-02-14 15:03:45 -0800709 if (DEBUG_LOADERS) Log.d(TAG, "Waited " + (SystemClock.uptimeMillis() - now) + "ms");
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800710 verifyNotStopped();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700711
712 // third step
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800713 if (DEBUG_LOADERS) Log.d(TAG, "step 3.1: loading deep shortcuts");
714 loadDeepShortcuts();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400715
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800716 verifyNotStopped();
717 if (DEBUG_LOADERS) Log.d(TAG, "step 3.2: bind deep shortcuts");
718 bindDeepShortcuts();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400719
Tony Wickhama7e1c1c2017-04-13 16:24:38 -0700720 // Take a break
721 if (DEBUG_LOADERS) Log.d(TAG, "step 3 completed, wait for idle");
722 waitForIdle();
723 verifyNotStopped();
724
725 // fourth step
726 if (DEBUG_LOADERS) Log.d(TAG, "step 4.1: loading widgets");
727 refreshAndBindWidgetsAndShortcuts(getCallback(), false /* bindFirst */,
728 null /* packageUser */);
729
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800730 synchronized (mLock) {
731 // Everything loaded bind the data.
732 mModelLoaded = true;
733 mHasLoaderCompletedOnce = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400734 }
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800735 } catch (CancellationException e) {
736 // Loader stopped, ignore
737 } finally {
738 // Clear out this reference, otherwise we end up holding it until all of the
739 // callback runnables are done.
740 mContext = null;
741
742 synchronized (mLock) {
743 // If we are still the last one to be scheduled, remove ourselves.
744 if (mLoaderTask == this) {
745 mLoaderTask = null;
746 }
747 mIsLoaderTaskRunning = false;
748 }
Joe Onorato36115782010-06-17 13:28:48 -0400749 }
Joe Onorato36115782010-06-17 13:28:48 -0400750 }
751
752 public void stopLocked() {
753 synchronized (LoaderTask.this) {
754 mStopped = true;
755 this.notify();
756 }
757 }
758
759 /**
760 * Gets the callbacks object. If we've been stopped, or if the launcher object
761 * has somehow been garbage collected, return null instead. Pass in the Callbacks
762 * object that was around when the deferred message was scheduled, and if there's
763 * a new Callbacks object around then also return null. This will save us from
764 * calling onto it with data that will be ignored.
765 */
766 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
767 synchronized (mLock) {
768 if (mStopped) {
769 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400770 }
Joe Onorato36115782010-06-17 13:28:48 -0400771
772 if (mCallbacks == null) {
773 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400774 }
Joe Onorato36115782010-06-17 13:28:48 -0400775
776 final Callbacks callbacks = mCallbacks.get();
777 if (callbacks != oldCallbacks) {
778 return null;
779 }
780 if (callbacks == null) {
781 Log.w(TAG, "no mCallbacks");
782 return null;
783 }
784
785 return callbacks;
786 }
787 }
788
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800789 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -0700790 if (LauncherAppState.PROFILE_STARTUP) {
791 Trace.beginSection("Loading Workspace");
792 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400793
Joe Onorato36115782010-06-17 13:28:48 -0400794 final Context context = mContext;
795 final ContentResolver contentResolver = context.getContentResolver();
Sunny Goyal342e4662017-02-02 15:21:08 -0800796 final PackageManagerHelper pmHelper = new PackageManagerHelper(context);
797 final boolean isSafeMode = pmHelper.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700798 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530799 final DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -0700800 final boolean isSdCardReady = Utilities.isBootCompleted();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800801 final MultiHashMap<UserHandle, String> pendingPackages = new MultiHashMap<>();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400802
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700803 boolean clearDb = false;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -0700804 try {
805 ImportDataTask.performImportIfPossible(context);
806 } catch (Exception e) {
807 // Migration failed. Clear workspace.
808 clearDb = true;
809 }
810
811 if (!clearDb && GridSizeMigrationTask.ENABLED &&
Sunny Goyalf076eae2016-01-11 12:25:10 -0800812 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
813 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700814 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -0700815 }
816
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700817 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -0700818 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -0700819 LauncherSettings.Settings.call(contentResolver,
Sunny Goyale05b08f2017-02-23 18:30:22 -0800820 LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -0500821 }
822
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700823 Log.d(TAG, "loadWorkspace: loading default favorites");
824 LauncherSettings.Settings.call(contentResolver,
825 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -0700826
Sunny Goyale9956a72016-09-01 17:24:47 -0700827 synchronized (sBgDataModel) {
828 sBgDataModel.clear();
829
Sunny Goyal756adbc2015-04-16 15:20:51 -0700830 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -0700831 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyale9956a72016-09-01 17:24:47 -0700832 sBgDataModel.workspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -0800833
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700834 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800835 final LoaderCursor c = new LoaderCursor(contentResolver.query(
836 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null), mApp);
Daniel Sandler8802e962010-05-26 16:28:16 -0400837
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800838 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400839
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700840 try {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700841 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
842 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -0400843 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
844 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700845 final int spanXIndex = c.getColumnIndexOrThrow
846 (LauncherSettings.Favorites.SPANX);
847 final int spanYIndex = c.getColumnIndexOrThrow(
848 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800849 final int rankIndex = c.getColumnIndexOrThrow(
850 LauncherSettings.Favorites.RANK);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700851 final int optionsIndex = c.getColumnIndexOrThrow(
852 LauncherSettings.Favorites.OPTIONS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400853
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800854 final LongSparseArray<UserHandle> allUsers = c.allUsers;
Kenny Guyff05f432016-01-22 17:48:29 +0000855 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700856 final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800857 for (UserHandle user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +0000858 long serialNo = mUserManager.getSerialNumberForUser(user);
859 allUsers.put(serialNo, user);
860 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700861
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700862 boolean userUnlocked = mUserManager.isUserUnlocked(user);
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700863
864 // We can only query for shortcuts when the user is unlocked.
865 if (userUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -0700866 List<ShortcutInfoCompat> pinnedShortcuts =
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530867 shortcutManager.queryForPinnedShortcuts(null, user);
868 if (shortcutManager.wasLastCallSuccess()) {
Sunny Goyal49f4f032016-08-01 15:45:49 -0700869 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
870 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
871 shortcut);
872 }
873 } else {
874 // Shortcut manager can fail due to some race condition when the
875 // lock state changes too frequently. For the purpose of the loading
876 // shortcuts, consider the user is still locked.
877 userUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700878 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700879 }
Sunny Goyal49f4f032016-08-01 15:45:49 -0700880 unlockedUsers.put(serialNo, userUnlocked);
Sunny Goyal7f834d22015-04-21 10:10:23 -0700881 }
882
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700883 ShortcutInfo info;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700884 LauncherAppWidgetInfo appWidgetInfo;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700885 Intent intent;
Sunny Goyal81e44912017-01-14 15:05:14 -0800886 String targetPkg;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400887
Jon Miranda529af302017-02-28 14:18:54 -0800888 FolderIconPreviewVerifier verifier =
889 new FolderIconPreviewVerifier(mApp.getInvariantDeviceProfile());
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700890 while (!mStopped && c.moveToNext()) {
891 try {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800892 if (c.user == null) {
893 // User has been deleted, remove the item.
894 c.markDeleted("User has been deleted");
895 continue;
896 }
897
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700898 boolean allowMissingTarget = false;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800899 switch (c.itemType) {
Sunny Goyal81e44912017-01-14 15:05:14 -0800900 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700901 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700902 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Sunny Goyal81e44912017-01-14 15:05:14 -0800903 intent = c.parseIntent();
904 if (intent == null) {
905 c.markDeleted("Invalid or null intent");
906 continue;
907 }
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800908
Sunny Goyal81e44912017-01-14 15:05:14 -0800909 int disabledState = quietMode.get(c.serialNumber) ?
910 ShortcutInfo.FLAG_DISABLED_QUIET_USER : 0;
911 ComponentName cn = intent.getComponent();
912 targetPkg = cn == null ? intent.getPackage() : cn.getPackageName();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700913
Sunny Goyal81e44912017-01-14 15:05:14 -0800914 if (!Process.myUserHandle().equals(c.user)) {
915 if (c.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
916 c.markDeleted("Legacy shortcuts are only allowed for default user");
917 continue;
918 } else if (c.restoreFlag != 0) {
919 // Don't restore items for other profiles.
920 c.markDeleted("Restore from managed profile not supported");
921 continue;
922 }
923 }
924 if (TextUtils.isEmpty(targetPkg) &&
925 c.itemType != LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
926 c.markDeleted("Only legacy shortcuts can have null package");
927 continue;
928 }
Sunny Goyal34942622014-08-29 17:20:55 -0700929
Sunny Goyal81e44912017-01-14 15:05:14 -0800930 // If there is no target package, its an implicit intent
931 // (legacy shortcut) which is always valid
932 boolean validTarget = TextUtils.isEmpty(targetPkg) ||
933 launcherApps.isPackageEnabledForProfile(targetPkg, c.user);
Sunny Goyal94485362014-09-18 16:13:58 -0700934
Sunny Goyal81e44912017-01-14 15:05:14 -0800935 if (cn != null && validTarget) {
936 // If the apk is present and the shortcut points to a specific
937 // component.
938
939 // If the component is already present
940 if (launcherApps.isActivityEnabledForProfile(cn, c.user)) {
941 // no special handling necessary for this item
942 c.markRestored();
943 } else {
Mario Bertschlerc06af332017-03-28 12:23:22 -0700944 if (c.hasRestoreFlag(ShortcutInfo.FLAG_AUTOINSTALL_ICON)) {
Sunny Goyal81e44912017-01-14 15:05:14 -0800945 // We allow auto install apps to have their intent
946 // updated after an install.
Sunny Goyal342e4662017-02-02 15:21:08 -0800947 intent = pmHelper.getAppLaunchIntent(targetPkg, c.user);
Sunny Goyal81e44912017-01-14 15:05:14 -0800948 if (intent != null) {
949 c.restoreFlag = 0;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800950 c.updater().put(
Sunny Goyal81e44912017-01-14 15:05:14 -0800951 LauncherSettings.Favorites.INTENT,
952 intent.toUri(0)).commit();
953 cn = intent.getComponent();
Sunny Goyalb05a00a2016-08-29 10:06:57 -0700954 } else {
Sunny Goyal81e44912017-01-14 15:05:14 -0800955 c.markDeleted("Unable to find a launch target");
Sunny Goyal94485362014-09-18 16:13:58 -0700956 continue;
957 }
Sunny Goyal1a745e82014-10-02 15:58:31 -0700958 } else {
Sunny Goyal81e44912017-01-14 15:05:14 -0800959 // The app is installed but the component is no
960 // longer available.
961 c.markDeleted("Invalid component removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -0700962 continue;
Winson Chungee055712013-07-30 14:46:24 -0700963 }
Winson Chungee055712013-07-30 14:46:24 -0700964 }
Sunny Goyal81e44912017-01-14 15:05:14 -0800965 }
966 // else if cn == null => can't infer much, leave it
967 // else if !validPkg => could be restored icon or missing sd-card
968
969 if (!TextUtils.isEmpty(targetPkg) && !validTarget) {
970 // Points to a valid app (superset of cn != null) but the apk
971 // is not available.
972
973 if (c.restoreFlag != 0) {
974 // Package is not yet available but might be
975 // installed later.
976 FileLog.d(TAG, "package not yet restored: " + targetPkg);
977
978 if (c.hasRestoreFlag(ShortcutInfo.FLAG_RESTORE_STARTED)) {
979 // Restore has started once.
980 } else if (installingPkgs.containsKey(targetPkg)) {
981 // App restore has started. Update the flag
982 c.restoreFlag |= ShortcutInfo.FLAG_RESTORE_STARTED;
983 c.updater().commit();
984 } else {
985 c.markDeleted("Unrestored app removed: " + targetPkg);
986 continue;
987 }
Sunny Goyalad2e91a2017-02-28 15:14:58 -0800988 } else if (pmHelper.isAppOnSdcard(targetPkg, c.user)) {
Sunny Goyal81e44912017-01-14 15:05:14 -0800989 // Package is present but not available.
990 disabledState |= ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
991 // Add the icon on the workspace anyway.
992 allowMissingTarget = true;
993 } else if (!isSdCardReady) {
994 // SdCard is not ready yet. Package might get available,
995 // once it is ready.
996 Log.d(TAG, "Missing pkg, will check later: " + targetPkg);
997 pendingPackages.addToList(c.user, targetPkg);
998 // Add the icon on the workspace anyway.
999 allowMissingTarget = true;
1000 } else {
1001 // Do not wait for external media load anymore.
1002 c.markDeleted("Invalid package removed: " + targetPkg);
1003 continue;
1004 }
1005 }
1006
1007 if (validTarget) {
1008 // The shortcut points to a valid target (either no target
1009 // or something which is ready to be used)
1010 c.markRestored();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001011 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001012
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001013 boolean useLowResIcon = !c.isOnWorkspaceOrHotseat() &&
Jon Miranda529af302017-02-28 14:18:54 -08001014 !verifier.isItemInPreview(c.getInt(rankIndex));
Sunny Goyal34b65272015-03-11 16:56:52 -07001015
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001016 if (c.restoreFlag != 0) {
Sunny Goyal81e44912017-01-14 15:05:14 -08001017 // Already verified above that user is same as default user
1018 info = c.getRestoredItemInfo(intent);
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001019 } else if (c.itemType ==
Chris Wrenf4d08112014-01-16 18:13:56 -05001020 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001021 info = c.getAppShortcutInfo(
1022 intent, allowMissingTarget, useLowResIcon);
1023 } else if (c.itemType ==
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001024 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001025
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001026 ShortcutKey key = ShortcutKey.fromIntent(intent, c.user);
1027 if (unlockedUsers.get(c.serialNumber)) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001028 ShortcutInfoCompat pinnedShortcut =
1029 shortcutKeyToPinnedShortcuts.get(key);
1030 if (pinnedShortcut == null) {
1031 // The shortcut is no longer valid.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001032 c.markDeleted("Pinned shortcut not found");
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001033 continue;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001034 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001035 info = new ShortcutInfo(pinnedShortcut, context);
Sunny Goyal1b072632017-01-18 11:30:23 -08001036 info.iconBitmap = LauncherIcons
1037 .createShortcutIcon(pinnedShortcut, context);
Charles He3ff90472017-02-15 12:22:56 +00001038 if (pmHelper.isAppSuspended(
Sunny Goyalad2e91a2017-02-28 15:14:58 -08001039 pinnedShortcut.getPackage(), info.user)) {
Charles He3ff90472017-02-15 12:22:56 +00001040 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1041 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001042 intent = info.intent;
1043 } else {
1044 // Create a shortcut info in disabled mode for now.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001045 info = c.loadSimpleShortcut();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001046 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001047 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001048 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001049 info = c.loadSimpleShortcut();
Michael Jurka96879562012-03-22 05:54:33 -07001050
Sunny Goyald09c3702016-04-06 16:18:20 -07001051 // Shortcuts are only available on the primary profile
Tony Wickham431a7132017-05-04 15:39:43 -07001052 if (!TextUtils.isEmpty(targetPkg)
1053 && pmHelper.isAppSuspended(targetPkg, c.user)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001054 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1055 }
1056
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001057 // App shortcuts that used to be automatically added to Launcher
1058 // didn't always have the correct intent flags set, so do that
1059 // here
1060 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001061 intent.getCategories() != null &&
1062 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001063 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001064 intent.addFlags(
1065 Intent.FLAG_ACTIVITY_NEW_TASK |
1066 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1067 }
Michael Jurka96879562012-03-22 05:54:33 -07001068 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001069
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001070 if (info != null) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001071 c.applyCommonProperties(info);
1072
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001073 info.intent = intent;
Sunny Goyal08f72612015-01-05 13:41:43 -08001074 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001075 info.spanX = 1;
1076 info.spanY = 1;
Sunny Goyald09c3702016-04-06 16:18:20 -07001077 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001078 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1079 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1080 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001081
Sunny Goyal81e44912017-01-14 15:05:14 -08001082 if (c.restoreFlag != 0 && !TextUtils.isEmpty(targetPkg)) {
1083 Integer progress = installingPkgs.get(targetPkg);
1084 if (progress != null) {
1085 info.setInstallProgress(progress);
1086 } else {
1087 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001088 }
1089 }
1090
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001091 c.checkAndAddItem(info, sBgDataModel);
Winson Chung1323b482013-08-05 12:41:55 -07001092 } else {
1093 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001094 }
1095 break;
1096
1097 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001098 FolderInfo folderInfo = sBgDataModel.findOrMakeFolder(c.id);
1099 c.applyCommonProperties(folderInfo);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001100
Sunny Goyala508e4f2015-05-21 09:33:57 -07001101 // Do not trim the folder label, as is was set by the user.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001102 folderInfo.title = c.getString(c.titleIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001103 folderInfo.spanX = 1;
1104 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001105 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001106
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001107 // no special handling required for restored folders
1108 c.markRestored();
Chris Wrenf4d08112014-01-16 18:13:56 -05001109
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001110 c.checkAndAddItem(folderInfo, sBgDataModel);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001111 break;
1112
1113 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08001114 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001115 // Read all Launcher-specific widget details
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001116 boolean customWidget = c.itemType ==
Adam Cohen59400422014-03-05 18:07:04 -08001117 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
1118
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001119 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001120 String savedProvider = c.getString(appWidgetProviderIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001121
Sunny Goyalff572272014-07-23 13:58:07 -07001122 final ComponentName component =
1123 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04001124
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001125 final boolean isIdValid = !c.hasRestoreFlag(
1126 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID);
1127 final boolean wasProviderReady = !c.hasRestoreFlag(
1128 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY);
Sunny Goyal651077b2014-06-30 14:15:31 -07001129
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001130 if (widgetProvidersMap == null) {
1131 widgetProvidersMap = AppWidgetManagerCompat
1132 .getInstance(mContext).getAllProvidersMap();
1133 }
1134 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
1135 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00001136 ComponentName.unflattenFromString(savedProvider),
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001137 c.user));
Sunny Goyalff572272014-07-23 13:58:07 -07001138
1139 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08001140 if (!isSafeMode && !customWidget &&
1141 wasProviderReady && !isProviderReady) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001142 c.markDeleted(
1143 "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07001144 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001145 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07001146 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07001147 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1148 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08001149
Sunny Goyal53f96722015-07-13 19:54:53 -07001150 // The provider is available. So the widget is either
1151 // available or not available. We do not need to track
1152 // any future restore updates.
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001153 int status = c.restoreFlag &
Sunny Goyal53f96722015-07-13 19:54:53 -07001154 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07001155 if (!wasProviderReady) {
1156 // If provider was not previously ready, update the
1157 // status and UI flag.
1158
1159 // Id would be valid only if the widget restore broadcast was received.
1160 if (isIdValid) {
Sunny Goyal86df1382016-08-10 15:03:22 -07001161 status |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07001162 } else {
1163 status &= ~LauncherAppWidgetInfo
1164 .FLAG_PROVIDER_NOT_READY;
1165 }
1166 }
1167 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07001168 } else {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001169 Log.v(TAG, "Widget restore pending id=" + c.id
Sunny Goyal651077b2014-06-30 14:15:31 -07001170 + " appWidgetId=" + appWidgetId
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001171 + " status =" + c.restoreFlag);
Sunny Goyal651077b2014-06-30 14:15:31 -07001172 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07001173 component);
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001174 appWidgetInfo.restoreStatus = c.restoreFlag;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001175 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07001176
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001177 if (c.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_RESTORE_STARTED)) {
Sunny Goyal94485362014-09-18 16:13:58 -07001178 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001179 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07001180 // App restore has started. Update the flag
1181 appWidgetInfo.restoreStatus |=
1182 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001183 } else if (!isSafeMode) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001184 c.markDeleted("Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07001185 continue;
1186 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07001187
1188 appWidgetInfo.installProgress =
1189 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07001190 }
Sunny Goyal86df1382016-08-10 15:03:22 -07001191 if (appWidgetInfo.hasRestoreFlag(
1192 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG)) {
Sunny Goyal81e44912017-01-14 15:05:14 -08001193 appWidgetInfo.bindOptions = c.parseIntent();
Sunny Goyal86df1382016-08-10 15:03:22 -07001194 }
Sunny Goyalff572272014-07-23 13:58:07 -07001195
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001196 c.applyCommonProperties(appWidgetInfo);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001197 appWidgetInfo.spanX = c.getInt(spanXIndex);
1198 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001199 appWidgetInfo.user = c.user;
Joe Onorato36115782010-06-17 13:28:48 -04001200
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001201 if (!c.isOnWorkspaceOrHotseat()) {
1202 c.markDeleted("Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001203 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001204 continue;
1205 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001206
Adam Cohen59400422014-03-05 18:07:04 -08001207 if (!customWidget) {
1208 String providerName =
1209 appWidgetInfo.providerName.flattenToString();
1210 if (!providerName.equals(savedProvider) ||
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001211 (appWidgetInfo.restoreStatus != c.restoreFlag)) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001212 c.updater()
1213 .put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1214 providerName)
1215 .put(LauncherSettings.Favorites.RESTORED,
1216 appWidgetInfo.restoreStatus)
1217 .commit();
Adam Cohen59400422014-03-05 18:07:04 -08001218 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001219 }
Jon Miranda2b823f42017-05-02 18:36:18 -07001220
1221 if (appWidgetInfo.restoreStatus !=
1222 LauncherAppWidgetInfo.RESTORE_COMPLETED) {
1223 String pkg = appWidgetInfo.providerName.getPackageName();
1224 appWidgetInfo.pendingItemInfo = new PackageItemInfo(pkg);
1225 appWidgetInfo.pendingItemInfo.user = appWidgetInfo.user;
1226 mIconCache.getTitleAndIconForApp(
1227 appWidgetInfo.pendingItemInfo, false);
1228 }
1229
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001230 c.checkAndAddItem(appWidgetInfo, sBgDataModel);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001231 }
Joe Onorato36115782010-06-17 13:28:48 -04001232 break;
1233 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001234 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001235 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001236 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001237 }
1238 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001239 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001240 }
1241
Winson Chungba9c37f2013-08-30 14:11:37 -07001242 // Break early if we've stopped loading
1243 if (mStopped) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001244 sBgDataModel.clear();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001245 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07001246 }
1247
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001248 // Remove dead items
1249 if (c.commitDeleted()) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001250 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07001251 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
1252 .call(contentResolver,
1253 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
1254 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
1255 for (long folderId : deletedFolderIds) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001256 sBgDataModel.workspaceItems.remove(sBgDataModel.folders.get(folderId));
1257 sBgDataModel.folders.remove(folderId);
1258 sBgDataModel.itemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001259 }
Sunny Goyal55fddc82017-04-06 15:02:52 -07001260
1261 // Remove any ghost widgets
1262 LauncherSettings.Settings.call(contentResolver,
1263 LauncherSettings.Settings.METHOD_REMOVE_GHOST_WIDGETS);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001264 }
1265
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001266 // Unpin shortcuts that don't exist on the workspace.
Sunny Goyalf75baa92016-11-22 03:23:51 +05301267 HashSet<ShortcutKey> pendingShortcuts =
1268 InstallShortcutReceiver.getPendingShortcuts(context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001269 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001270 MutableInt numTimesPinned = sBgDataModel.pinnedShortcutCounts.get(key);
Sunny Goyalf75baa92016-11-22 03:23:51 +05301271 if ((numTimesPinned == null || numTimesPinned.value == 0)
1272 && !pendingShortcuts.contains(key)) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001273 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301274 shortcutManager.unpinShortcut(key);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001275 }
1276 }
1277
Jon Miranda655ec422017-02-07 11:40:22 -08001278 FolderIconPreviewVerifier verifier =
1279 new FolderIconPreviewVerifier(mApp.getInvariantDeviceProfile());
1280 // Sort the folder items and make sure all items in the preview are high resolution.
Sunny Goyale9956a72016-09-01 17:24:47 -07001281 for (FolderInfo folder : sBgDataModel.folders) {
Sunny Goyal317698b2015-07-29 11:45:41 -07001282 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
Jon Miranda655ec422017-02-07 11:40:22 -08001283 verifier.setFolderInfo(folder);
1284
1285 int numItemsInPreview = 0;
Sunny Goyal317698b2015-07-29 11:45:41 -07001286 for (ShortcutInfo info : folder.contents) {
Jon Miranda655ec422017-02-07 11:40:22 -08001287 if (info.usingLowResIcon
1288 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION
1289 && verifier.isItemInPreview(info.rank)) {
Sunny Goyal3fe4a142016-12-15 17:40:07 -08001290 mIconCache.getTitleAndIcon(info, false);
Jon Miranda655ec422017-02-07 11:40:22 -08001291 numItemsInPreview++;
Sunny Goyal317698b2015-07-29 11:45:41 -07001292 }
Jon Miranda655ec422017-02-07 11:40:22 -08001293
1294 if (numItemsInPreview >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
Sunny Goyal317698b2015-07-29 11:45:41 -07001295 break;
1296 }
1297 }
1298 }
1299
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001300 c.commitRestoredItems();
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001301 if (!isSdCardReady && !pendingPackages.isEmpty()) {
1302 context.registerReceiver(
1303 new SdCardAvailableReceiver(
1304 LauncherModel.this, mContext, pendingPackages),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001305 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001306 null,
1307 sWorker);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001308 }
1309
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001310 // Remove any empty screens
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001311 ArrayList<Long> unusedScreens = new ArrayList<>(sBgDataModel.workspaceScreens);
Sunny Goyale9956a72016-09-01 17:24:47 -07001312 for (ItemInfo item: sBgDataModel.itemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001313 long screenId = item.screenId;
1314 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1315 unusedScreens.contains(screenId)) {
1316 unusedScreens.remove(screenId);
1317 }
1318 }
1319
1320 // If there are any empty screens remove them, and update.
1321 if (unusedScreens.size() != 0) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001322 sBgDataModel.workspaceScreens.removeAll(unusedScreens);
1323 updateWorkspaceScreenOrder(context, sBgDataModel.workspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001324 }
Joe Onorato36115782010-06-17 13:28:48 -04001325 }
Sunny Goyale26d1002016-06-20 14:52:14 -07001326 if (LauncherAppState.PROFILE_STARTUP) {
1327 Trace.endSection();
1328 }
Adam Cohene25af792013-06-06 23:08:25 -07001329 }
1330
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001331 /** Filters the set of items who are directly or indirectly (via another container) on the
1332 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08001333 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001334 ArrayList<ItemInfo> allWorkspaceItems,
1335 ArrayList<ItemInfo> currentScreenItems,
1336 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001337 // Purge any null ItemInfos
1338 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
1339 while (iter.hasNext()) {
1340 ItemInfo i = iter.next();
1341 if (i == null) {
1342 iter.remove();
1343 }
1344 }
1345
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001346 // Order the set of items by their containers first, this allows use to walk through the
1347 // list sequentially, build up a list of containers that are in the specified screen,
1348 // as well as all items in those containers.
1349 Set<Long> itemsOnScreen = new HashSet<Long>();
1350 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
1351 @Override
1352 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07001353 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001354 }
1355 });
1356 for (ItemInfo info : allWorkspaceItems) {
1357 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001358 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001359 currentScreenItems.add(info);
1360 itemsOnScreen.add(info.id);
1361 } else {
1362 otherScreenItems.add(info);
1363 }
1364 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1365 currentScreenItems.add(info);
1366 itemsOnScreen.add(info.id);
1367 } else {
1368 if (itemsOnScreen.contains(info.container)) {
1369 currentScreenItems.add(info);
1370 itemsOnScreen.add(info.id);
1371 } else {
1372 otherScreenItems.add(info);
1373 }
1374 }
1375 }
1376 }
1377
1378 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08001379 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001380 ArrayList<LauncherAppWidgetInfo> appWidgets,
1381 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
1382 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001383
1384 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001385 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001386 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08001387 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001388 currentScreenWidgets.add(widget);
1389 } else {
1390 otherScreenWidgets.add(widget);
1391 }
1392 }
1393 }
1394
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001395 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
1396 * right) */
1397 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Sunny Goyal87f784c2017-01-11 10:48:34 -08001398 final InvariantDeviceProfile profile = mApp.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07001399 final int screenCols = profile.numColumns;
1400 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001401 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07001402 @Override
1403 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07001404 if (lhs.container == rhs.container) {
1405 // Within containers, order by their spatial position in that container
1406 switch ((int) lhs.container) {
1407 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
1408 long lr = (lhs.screenId * screenCellCount +
1409 lhs.cellY * screenCols + lhs.cellX);
1410 long rr = (rhs.screenId * screenCellCount +
1411 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07001412 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07001413 }
1414 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
1415 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07001416 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07001417 }
1418 default:
Sunny Goyal3d706ad2017-03-06 16:56:39 -08001419 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07001420 throw new RuntimeException("Unexpected container type when " +
1421 "sorting workspace items.");
1422 }
1423 return 0;
1424 }
1425 } else {
1426 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07001427 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07001428 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001429 }
1430 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001431 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001432
Adam Cohendcd297f2013-06-18 13:13:40 -07001433 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
1434 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001435 final Runnable r = new Runnable() {
1436 @Override
1437 public void run() {
1438 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1439 if (callbacks != null) {
1440 callbacks.bindScreens(orderedScreens);
1441 }
1442 }
1443 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001444 mUiExecutor.execute(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07001445 }
1446
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001447 private void bindWorkspaceItems(final Callbacks oldCallbacks,
1448 final ArrayList<ItemInfo> workspaceItems,
1449 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07001450 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001451
1452 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07001453 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001454 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04001455 final int start = i;
1456 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001457 final Runnable r = new Runnable() {
1458 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001459 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001460 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001461 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07001462 callbacks.bindItems(workspaceItems, start, start+chunkSize,
1463 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001464 }
1465 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001466 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001467 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04001468 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001469
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001470 // Bind the widgets, one at a time
1471 N = appWidgets.size();
1472 for (int i = 0; i < N; i++) {
1473 final LauncherAppWidgetInfo widget = appWidgets.get(i);
1474 final Runnable r = new Runnable() {
1475 public void run() {
1476 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1477 if (callbacks != null) {
1478 callbacks.bindAppWidget(widget);
1479 }
1480 }
1481 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001482 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001483 }
1484 }
1485
1486 /**
1487 * Binds all loaded data to actual views on the main thread.
1488 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001489 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001490 final long t = SystemClock.uptimeMillis();
1491 Runnable r;
1492
1493 // Don't use these two variables in any of the callback runnables.
1494 // Otherwise we hold a reference to them.
1495 final Callbacks oldCallbacks = mCallbacks.get();
1496 if (oldCallbacks == null) {
1497 // This launcher has exited and nobody bothered to tell us. Just bail.
1498 Log.w(TAG, "LoaderTask running with no launcher");
1499 return;
1500 }
1501
Winson Chung9b9fb962013-11-15 15:39:34 -08001502 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07001503 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
1504 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
1505 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07001506
Sunny Goyale9956a72016-09-01 17:24:47 -07001507 synchronized (sBgDataModel) {
1508 workspaceItems.addAll(sBgDataModel.workspaceItems);
1509 appWidgets.addAll(sBgDataModel.appWidgets);
1510 orderedScreenIds.addAll(sBgDataModel.workspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001511 }
1512
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001513 final int currentScreen;
1514 {
1515 int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE
1516 ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen();
1517 if (currScreen >= orderedScreenIds.size()) {
1518 // There may be no workspace screens (just hotseat items and an empty page).
1519 currScreen = PagedView.INVALID_RESTORE_PAGE;
1520 }
1521 currentScreen = currScreen;
Winson Chung9b9fb962013-11-15 15:39:34 -08001522 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001523 final boolean validFirstPage = currentScreen >= 0;
1524 final long currentScreenId =
1525 validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID;
Winson Chung9b9fb962013-11-15 15:39:34 -08001526
Winson Chung9b9fb962013-11-15 15:39:34 -08001527 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07001528 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
1529 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
1530 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
1531 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001532
Winson Chung9b9fb962013-11-15 15:39:34 -08001533 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001534 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08001535 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001536 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001537 sortWorkspaceItemsSpatially(currentWorkspaceItems);
1538 sortWorkspaceItemsSpatially(otherWorkspaceItems);
1539
1540 // Tell the workspace that we're about to start binding items
1541 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001542 public void run() {
1543 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1544 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001545 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001546 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04001547 }
1548 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001549 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001550 mUiExecutor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001551
Adam Cohendcd297f2013-06-18 13:13:40 -07001552 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
1553
Sunny Goyalb265ba72017-02-14 15:03:45 -08001554 Executor mainExecutor = mUiExecutor;
Sunny Goyal527c7d32015-08-28 15:19:36 -07001555 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07001556 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001557
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001558 // In case of validFirstPage, only bind the first screen, and defer binding the
1559 // remaining screens after first onDraw (and an optional the fade animation whichever
1560 // happens later).
1561 // This ensures that the first screen is immediately visible (eg. during rotation)
1562 // In case of !validFirstPage, bind all pages one after other.
1563 final Executor deferredExecutor =
Sunny Goyalb265ba72017-02-14 15:03:45 -08001564 validFirstPage ? new ViewOnDrawExecutor(mUiExecutor) : mainExecutor;
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001565
1566 mainExecutor.execute(new Runnable() {
1567 @Override
1568 public void run() {
1569 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1570 if (callbacks != null) {
1571 callbacks.finishFirstPageBind(
1572 validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null);
1573 }
1574 }
1575 });
Sunny Goyal527c7d32015-08-28 15:19:36 -07001576
Sunny Goyal44c06432016-04-02 10:56:02 -07001577 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001578
1579 // Tell the workspace that we're done binding items
1580 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001581 public void run() {
1582 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1583 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001584 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04001585 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001586
Sunny Goyal639e9062015-08-19 19:17:06 -07001587 mIsLoadingAndBindingWorkspace = false;
1588
1589 // Run all the bind complete runnables after workspace is bound.
1590 if (!mBindCompleteRunnables.isEmpty()) {
1591 synchronized (mBindCompleteRunnables) {
1592 for (final Runnable r : mBindCompleteRunnables) {
1593 runOnWorkerThread(r);
1594 }
1595 mBindCompleteRunnables.clear();
1596 }
1597 }
1598
Winson Chung98e030b2012-05-07 16:01:11 -07001599 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04001600 if (DEBUG_LOADERS) {
1601 Log.d(TAG, "bound workspace in "
1602 + (SystemClock.uptimeMillis()-t) + "ms");
1603 }
Winson Chung36a62fe2012-05-06 18:04:42 -07001604
Joe Onorato36115782010-06-17 13:28:48 -04001605 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001606 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001607 deferredExecutor.execute(r);
1608
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001609 if (validFirstPage) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001610 r = new Runnable() {
1611 public void run() {
1612 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1613 if (callbacks != null) {
1614 // We are loading synchronously, which means, some of the pages will be
1615 // bound after first draw. Inform the callbacks that page binding is
1616 // not complete, and schedule the remaining pages.
1617 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
1618 callbacks.onPageBoundSynchronously(currentScreen);
1619 }
1620 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
1621 }
1622 }
1623 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001624 mUiExecutor.execute(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07001625 }
Joe Onorato36115782010-06-17 13:28:48 -04001626 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001627
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001628 private void updateIconCache() {
1629 // Ignore packages which have a promise icon.
1630 HashSet<String> packagesToIgnore = new HashSet<>();
Sunny Goyale9956a72016-09-01 17:24:47 -07001631 synchronized (sBgDataModel) {
1632 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001633 if (info instanceof ShortcutInfo) {
1634 ShortcutInfo si = (ShortcutInfo) info;
1635 if (si.isPromise() && si.getTargetComponent() != null) {
1636 packagesToIgnore.add(si.getTargetComponent().getPackageName());
1637 }
1638 } else if (info instanceof LauncherAppWidgetInfo) {
1639 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
1640 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
1641 packagesToIgnore.add(lawi.providerName.getPackageName());
1642 }
1643 }
1644 }
1645 }
1646 mIconCache.updateDbIcons(packagesToIgnore);
1647 }
1648
Joe Onorato36115782010-06-17 13:28:48 -04001649 private void onlyBindAllApps() {
1650 final Callbacks oldCallbacks = mCallbacks.get();
1651 if (oldCallbacks == null) {
1652 // This launcher has exited and nobody bothered to tell us. Just bail.
1653 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1654 return;
1655 }
1656
1657 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07001658 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001659 final ArrayList<AppInfo> list
1660 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07001661 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001662 public void run() {
1663 final long t = SystemClock.uptimeMillis();
1664 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1665 if (callbacks != null) {
1666 callbacks.bindAllApplications(list);
1667 }
1668 if (DEBUG_LOADERS) {
1669 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08001670 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04001671 }
1672 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07001673 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001674 mUiExecutor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04001675 }
1676
Winson Chung64359a52013-07-08 17:17:08 -07001677 private void loadAllApps() {
1678 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04001679
Joe Onorato36115782010-06-17 13:28:48 -04001680 final Callbacks oldCallbacks = mCallbacks.get();
1681 if (oldCallbacks == null) {
1682 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07001683 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04001684 return;
1685 }
1686
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001687 final List<UserHandle> profiles = mUserManager.getUserProfiles();
Kenny Guyed131872014-04-30 03:02:21 +01001688
Winson Chung64359a52013-07-08 17:17:08 -07001689 // Clear the list of apps
1690 mBgAllAppsList.clear();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001691 for (UserHandle user : profiles) {
Kenny Guyed131872014-04-30 03:02:21 +01001692 // Query for the set of apps
1693 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal3e9be432017-01-05 15:22:41 -08001694 final List<LauncherActivityInfo> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01001695 if (DEBUG_LOADERS) {
1696 Log.d(TAG, "getActivityList took "
1697 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
1698 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
1699 }
1700 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08001701 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01001702 if (apps == null || apps.isEmpty()) {
1703 return;
1704 }
Kenny Guyff05f432016-01-22 17:48:29 +00001705 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01001706 // Create the ApplicationInfos
1707 for (int i = 0; i < apps.size(); i++) {
Sunny Goyal3e9be432017-01-05 15:22:41 -08001708 LauncherActivityInfo app = apps.get(i);
Kenny Guyed131872014-04-30 03:02:21 +01001709 // This builds the icon bitmaps.
Sunny Goyal24bb66a2017-03-21 15:12:01 -07001710 mBgAllAppsList.add(new AppInfo(app, user, quietMode), app);
Kenny Guyed131872014-04-30 03:02:21 +01001711 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08001712
Sunny Goyal756a28a2015-04-23 17:07:55 -07001713 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
1714 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07001715 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07001716
1717 @Override
1718 public void run() {
1719 heuristic.processUserApps(apps);
1720 }
Sunny Goyal639e9062015-08-19 19:17:06 -07001721 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001722 mUiExecutor.execute(new Runnable() {
Sunny Goyal639e9062015-08-19 19:17:06 -07001723
Sunny Goyalb265ba72017-02-14 15:03:45 -08001724 @Override
1725 public void run() {
1726 // Check isLoadingWorkspace on the UI thread, as it is updated on
1727 // the UI thread.
1728 if (mIsLoadingAndBindingWorkspace) {
1729 synchronized (mBindCompleteRunnables) {
1730 mBindCompleteRunnables.add(r);
1731 }
1732 } else {
1733 runOnWorkerThread(r);
1734 }
1735 }
1736 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08001737 }
Winson Chung64359a52013-07-08 17:17:08 -07001738 }
Mario Bertschler817afa32017-03-15 11:56:47 -07001739
1740 if (FeatureFlags.LAUNCHER3_PROMISE_APPS_IN_ALL_APPS) {
1741 // get all active sessions and add them to the all apps list
1742 PackageInstallerCompat installer = PackageInstallerCompat.getInstance(mContext);
1743 for (PackageInstaller.SessionInfo info : installer.getAllVerifiedSessions()) {
1744 mBgAllAppsList.addPromiseApp(mContext,
1745 PackageInstallInfo.fromInstallingState(info));
1746 }
1747 }
1748
Bjorn Bringert85f418d2013-09-06 12:50:05 +01001749 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001750 final ArrayList<AppInfo> added = mBgAllAppsList.added;
1751 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07001752
Mario Bertschler817afa32017-03-15 11:56:47 -07001753
Winson Chung64359a52013-07-08 17:17:08 -07001754 // Post callback on main thread
Sunny Goyalb265ba72017-02-14 15:03:45 -08001755 mUiExecutor.execute(new Runnable() {
Winson Chung64359a52013-07-08 17:17:08 -07001756 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07001757
Winson Chung64359a52013-07-08 17:17:08 -07001758 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07001759 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07001760 if (callbacks != null) {
1761 callbacks.bindAllApplications(added);
1762 if (DEBUG_LOADERS) {
1763 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001764 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07001765 }
1766 } else {
1767 Log.i(TAG, "not binding apps: no Launcher activity");
1768 }
1769 }
1770 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07001771 // Cleanup any data stored for a deleted user.
1772 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04001773 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07001774 Log.d(TAG, "Icons processed in "
1775 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08001776 }
1777 }
1778
Sunny Goyaldd96a5e2017-02-17 11:22:34 -08001779 private void loadDeepShortcuts() {
1780 if (!mModelLoaded) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07001781 sBgDataModel.deepShortcutMap.clear();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301782 DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(mContext);
1783 mHasShortcutHostPermission = shortcutManager.hasHostPermission();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001784 if (mHasShortcutHostPermission) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001785 for (UserHandle user : mUserManager.getUserProfiles()) {
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001786 if (mUserManager.isUserUnlocked(user)) {
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301787 List<ShortcutInfoCompat> shortcuts =
1788 shortcutManager.queryForAllShortcuts(user);
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07001789 sBgDataModel.updateDeepShortcutMap(null, user, shortcuts);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001790 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001791 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001792 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001793 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001794 }
Joe Onorato36115782010-06-17 13:28:48 -04001795 }
1796
Tony Wickham80f57872016-06-29 18:12:15 -07001797 public void bindDeepShortcuts() {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07001798 final MultiHashMap<ComponentKey, String> shortcutMapCopy =
1799 sBgDataModel.deepShortcutMap.clone();
Tony Wickham80f57872016-06-29 18:12:15 -07001800 Runnable r = new Runnable() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001801 @Override
1802 public void run() {
1803 Callbacks callbacks = getCallback();
1804 if (callbacks != null) {
1805 callbacks.bindDeepShortcutMap(shortcutMapCopy);
1806 }
1807 }
Tony Wickham80f57872016-06-29 18:12:15 -07001808 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001809 mUiExecutor.execute(r);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001810 }
1811
Sunny Goyal75b0f552015-05-20 21:57:06 -07001812 /**
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001813 * Refreshes the cached shortcuts if the shortcut permission has changed.
1814 * Current implementation simply reloads the workspace, but it can be optimized to
1815 * use partial updates similar to {@link UserManagerCompat}
1816 */
1817 public void refreshShortcutsIfRequired() {
Sunny Goyalf5e37442016-11-02 10:31:24 -07001818 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001819 sWorker.removeCallbacks(mShortcutPermissionCheckRunnable);
1820 sWorker.post(mShortcutPermissionCheckRunnable);
1821 }
1822 }
1823
1824 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -07001825 * Called when the icons for packages have been updated in the icon cache.
1826 */
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001827 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandle user) {
Sunny Goyal75b0f552015-05-20 21:57:06 -07001828 // If any package icon has changed (app was updated while launcher was dead),
1829 // update the corresponding shortcuts.
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001830 enqueueModelUpdateTask(new CacheDataUpdatedTask(
1831 CacheDataUpdatedTask.OP_CACHE_UPDATE, user, updatedPackages));
Sunny Goyal75b0f552015-05-20 21:57:06 -07001832 }
1833
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001834 void enqueueModelUpdateTask(BaseModelUpdateTask task) {
Jon Mirandaf28629a2017-02-23 15:12:42 -08001835 if (!mModelLoaded && mLoaderTask == null) {
1836 if (DEBUG_LOADERS) {
1837 Log.d(TAG, "enqueueModelUpdateTask Ignoring task since loader is pending=" + task);
1838 }
1839 return;
1840 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001841 task.init(this);
1842 runOnWorkerThread(task);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001843 }
1844
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001845 /**
1846 * A task to be executed on the current callbacks on the UI thread.
1847 * If there is no current callbacks, the task is ignored.
1848 */
1849 public interface CallbackTask {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001850
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001851 void execute(Callbacks callbacks);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001852 }
1853
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001854 /**
1855 * A runnable which changes/updates the data model of the launcher based on certain events.
1856 */
1857 public static abstract class BaseModelUpdateTask implements Runnable {
Joe Onorato36115782010-06-17 13:28:48 -04001858
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001859 private LauncherModel mModel;
Sunny Goyalb265ba72017-02-14 15:03:45 -08001860 private Executor mUiExecutor;
Joe Onorato36115782010-06-17 13:28:48 -04001861
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001862 /* package private */
1863 void init(LauncherModel model) {
1864 mModel = model;
Sunny Goyalb265ba72017-02-14 15:03:45 -08001865 mUiExecutor = mModel.mUiExecutor;
Joe Onorato36115782010-06-17 13:28:48 -04001866 }
1867
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001868 @Override
Joe Onorato36115782010-06-17 13:28:48 -04001869 public void run() {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001870 if (!mModel.mHasLoaderCompletedOnce) {
Sunny Goyalc905efc2015-05-06 09:54:53 -07001871 // Loader has not yet run.
1872 return;
1873 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001874 execute(mModel.mApp, sBgDataModel, mModel.mBgAllAppsList);
1875 }
Joe Onorato36115782010-06-17 13:28:48 -04001876
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001877 /**
1878 * Execute the actual task. Called on the worker thread.
1879 */
1880 public abstract void execute(
1881 LauncherAppState app, BgDataModel dataModel, AllAppsList apps);
Sunny Goyale0f58d72014-11-10 18:05:31 -08001882
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001883 /**
1884 * Schedules a {@param task} to be executed on the current callbacks.
1885 */
1886 public final void scheduleCallbackTask(final CallbackTask task) {
1887 final Callbacks callbacks = mModel.getCallback();
Sunny Goyalb265ba72017-02-14 15:03:45 -08001888 mUiExecutor.execute(new Runnable() {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001889 public void run() {
1890 Callbacks cb = mModel.getCallback();
1891 if (callbacks == cb && cb != null) {
1892 task.execute(callbacks);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07001893 }
1894 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001895 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001896 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001897
1898 public ModelWriter getModelWriter() {
1899 // Updates from model task, do not deal with icon position in hotseat.
1900 return mModel.getWriter(false /* hasVerticalHotseat */);
1901 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001902 }
1903
Sunny Goyal1b072632017-01-18 11:30:23 -08001904 public void updateAndBindShortcutInfo(final ShortcutInfo si, final ShortcutInfoCompat info) {
1905 updateAndBindShortcutInfo(new Provider<ShortcutInfo>() {
1906 @Override
1907 public ShortcutInfo get() {
1908 si.updateFromDeepShortcutInfo(info, mApp.getContext());
1909 si.iconBitmap = LauncherIcons.createShortcutIcon(info, mApp.getContext());
1910 return si;
1911 }
1912 });
1913 }
1914
Sunny Goyal10923b32016-07-20 15:42:44 -07001915 /**
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001916 * Utility method to update a shortcut on the background thread.
Sunny Goyal10923b32016-07-20 15:42:44 -07001917 */
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001918 public void updateAndBindShortcutInfo(final Provider<ShortcutInfo> shortcutProvider) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001919 enqueueModelUpdateTask(new ExtendedModelTask() {
Sunny Goyal10923b32016-07-20 15:42:44 -07001920 @Override
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001921 public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001922 ShortcutInfo info = shortcutProvider.get();
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001923 ArrayList<ShortcutInfo> update = new ArrayList<>();
Sunny Goyal10923b32016-07-20 15:42:44 -07001924 update.add(info);
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001925 bindUpdatedShortcuts(update, info.user);
Sunny Goyal10923b32016-07-20 15:42:44 -07001926 }
1927 });
1928 }
1929
Sunny Goyald164b7f2016-10-12 20:49:31 -07001930 private void bindWidgetsModel(final Callbacks callbacks) {
1931 final MultiHashMap<PackageItemInfo, WidgetItem> widgets
1932 = mBgWidgetsModel.getWidgetsMap().clone();
Sunny Goyalb265ba72017-02-14 15:03:45 -08001933 mUiExecutor.execute(new Runnable() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001934 @Override
1935 public void run() {
1936 Callbacks cb = getCallback();
1937 if (callbacks == cb && cb != null) {
Sunny Goyald164b7f2016-10-12 20:49:31 -07001938 callbacks.bindAllWidgets(widgets);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001939 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07001940 }
1941 });
1942 }
1943
Tony Wickham86222d22017-03-29 15:30:43 -07001944 public void refreshAndBindWidgetsAndShortcuts(final Callbacks callbacks,
1945 final boolean bindFirst, @Nullable final PackageUserKey packageUser) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001946 runOnWorkerThread(new Runnable() {
1947 @Override
1948 public void run() {
1949 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
Sunny Goyald164b7f2016-10-12 20:49:31 -07001950 bindWidgetsModel(callbacks);
Sunny Goyal31860582015-09-18 08:38:57 -07001951 }
Tony Wickham86222d22017-03-29 15:30:43 -07001952 ArrayList<WidgetItem> widgets = mBgWidgetsModel.update(
1953 mApp.getContext(), packageUser);
Sunny Goyald164b7f2016-10-12 20:49:31 -07001954 bindWidgetsModel(callbacks);
1955
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001956 // update the Widget entries inside DB on the worker thread.
Tony Wickham86222d22017-03-29 15:30:43 -07001957 mApp.getWidgetCache().removeObsoletePreviews(widgets, packageUser);
Sunny Goyal31860582015-09-18 08:38:57 -07001958 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001959 });
Michael Jurkac402cd92013-05-20 15:49:32 +02001960 }
1961
Sunny Goyal651077b2014-06-30 14:15:31 -07001962 static boolean isValidProvider(AppWidgetProviderInfo provider) {
1963 return (provider != null) && (provider.provider != null)
1964 && (provider.provider.getPackageName() != null);
1965 }
1966
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001967 public void dumpState(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
1968 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
1969 writer.println(prefix + "All apps list: size=" + mBgAllAppsList.data.size());
1970 for (AppInfo info : mBgAllAppsList.data) {
1971 writer.println(prefix + " title=\"" + info.title + "\" iconBitmap=" + info.iconBitmap
1972 + " componentName=" + info.componentName.getPackageName());
1973 }
Joe Onorato36115782010-06-17 13:28:48 -04001974 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001975 sBgDataModel.dump(prefix, fd, writer, args);
Joe Onoratobe386092009-11-17 17:32:16 -08001976 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08001977
1978 public Callbacks getCallback() {
1979 return mCallbacks != null ? mCallbacks.get() : null;
1980 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07001981
1982 /**
1983 * @return {@link FolderInfo} if its already loaded.
1984 */
1985 public FolderInfo findFolderById(Long folderId) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001986 synchronized (sBgDataModel) {
1987 return sBgDataModel.folders.get(folderId);
Sunny Goyal18bf8e22015-04-08 18:13:46 -07001988 }
1989 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07001990
1991 /**
1992 * @return the looper for the worker thread which can be used to start background tasks.
1993 */
1994 public static Looper getWorkerLooper() {
1995 return sWorkerThread.getLooper();
1996 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997}