blob: 58fb117a4ada3cd16f59cb2e5c4ab58a67b78065 [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;
Mario Bertschler817afa32017-03-15 11:56:47 -070028import android.content.pm.ApplicationInfo;
Sunny Goyal3e9be432017-01-05 15:22:41 -080029import android.content.pm.LauncherActivityInfo;
Mario Bertschler817afa32017-03-15 11:56:47 -070030import android.content.pm.PackageInstaller;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.net.Uri;
Joe Onorato36115782010-06-17 13:28:48 -040032import android.os.Handler;
33import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070034import android.os.Looper;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040036import android.os.SystemClock;
Sunny Goyale26d1002016-06-20 14:52:14 -070037import android.os.Trace;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080038import android.os.UserHandle;
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;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070081import com.android.launcher3.util.Preconditions;
Sunny Goyal2bcbe132016-11-16 09:23:42 -080082import com.android.launcher3.util.Provider;
Adam Cohen091440a2015-03-18 14:16:05 -070083import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070084import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080085
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -080086import java.io.FileDescriptor;
87import java.io.PrintWriter;
Michael Jurkac2f801e2011-07-12 14:19:46 -070088import java.lang.ref.WeakReference;
Michael Jurkac2f801e2011-07-12 14:19:46 -070089import java.util.ArrayList;
90import java.util.Collections;
91import java.util.Comparator;
92import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070093import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070094import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070095import java.util.List;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070096import java.util.Map;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070097import java.util.Set;
Sunny Goyaldd96a5e2017-02-17 11:22:34 -080098import java.util.concurrent.CancellationException;
Sunny Goyal527c7d32015-08-28 15:19:36 -070099import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -0700100
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101/**
102 * Maintains in-memory state of the Launcher. It is expected that there should be only one
103 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700104 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800105 */
Kenny Guyed131872014-04-30 03:02:21 +0100106public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +0100107 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800108 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -0400109 private static final boolean DEBUG_RECEIVER = false;
Chris Wrenb358f812014-04-16 13:37:00 -0400110
Joe Onorato9c1289c2009-08-17 11:03:03 -0400111 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700112
Joe Onorato36115782010-06-17 13:28:48 -0400113 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500114 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800115
Sunny Goyalb265ba72017-02-14 15:03:45 -0800116 private final MainThreadExecutor mUiExecutor = new MainThreadExecutor();
Adam Cohen091440a2015-03-18 14:16:05 -0700117 @Thunk final LauncherAppState mApp;
118 @Thunk final Object mLock = new Object();
Adam Cohen091440a2015-03-18 14:16:05 -0700119 @Thunk LoaderTask mLoaderTask;
120 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700121 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122
Adam Cohen091440a2015-03-18 14:16:05 -0700123 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700124 static {
125 sWorkerThread.start();
126 }
Adam Cohen091440a2015-03-18 14:16:05 -0700127 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700128
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800129 // Indicates whether the current model data is valid or not.
130 // We start off with everything not loaded. After that, we assume that
Joe Onoratocc67f472010-06-08 10:54:30 -0700131 // our monitoring of the package manager provides all updates and we never
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800132 // need to do a requery. This is only ever touched from the loader thread.
133 private boolean mModelLoaded;
Hyunyoung Song6aa37292017-02-06 10:46:24 -0800134 public boolean isModelLoaded() {
135 synchronized (mLock) {
136 return mModelLoaded && mLoaderTask == null;
137 }
138 }
Joe Onoratocc67f472010-06-08 10:54:30 -0700139
Sunny Goyal756a28a2015-04-23 17:07:55 -0700140 /**
141 * Set of runnables to be called on the background thread after the workspace binding
142 * is complete.
143 */
144 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
145
Adam Cohen091440a2015-03-18 14:16:05 -0700146 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700148 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800149 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700150 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800151 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800152
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700153 private boolean mHasShortcutHostPermission;
154 // Runnable to check if the shortcuts permission has changed.
155 private final Runnable mShortcutPermissionCheckRunnable = new Runnable() {
156 @Override
157 public void run() {
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800158 if (mModelLoaded) {
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530159 boolean hasShortcutHostPermission =
160 DeepShortcutManager.getInstance(mApp.getContext()).hasHostPermission();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700161 if (hasShortcutHostPermission != mHasShortcutHostPermission) {
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800162 forceReload();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700163 }
164 }
165 }
166 };
167
Sunny Goyale9956a72016-09-01 17:24:47 -0700168 /**
169 * All the static data should be accessed on the background thread, A lock should be acquired
170 * on this object when accessing any data from this model.
171 */
172 static final BgDataModel sBgDataModel = new BgDataModel();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700173
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700174 // </ only access in worker thread >
175
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700176 private final IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700178 private final LauncherAppsCompat mLauncherApps;
179 private final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100180
Joe Onorato9c1289c2009-08-17 11:03:03 -0400181 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700182 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400183 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700184 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400185 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700186 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
187 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700188 public void bindScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700189 public void finishFirstPageBind(ViewOnDrawExecutor executor);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800190 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400191 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200192 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700193 public void bindAppsAdded(ArrayList<Long> newScreens,
194 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700195 ArrayList<ItemInfo> addAnimated,
196 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200197 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700198 public void bindPromiseAppProgressUpdated(PromiseAppInfo app);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700199 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800200 ArrayList<ShortcutInfo> removed, UserHandle user);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700201 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700202 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700203 public void bindWorkspaceComponentsRemoved(
204 HashSet<String> packageNames, HashSet<ComponentName> components,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800205 UserHandle user);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700206 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800207 public void notifyWidgetProvidersChanged();
Sunny Goyald164b7f2016-10-12 20:49:31 -0700208 public void bindAllWidgets(MultiHashMap<PackageItemInfo, WidgetItem> widgets);
Adam Cohen1462de32012-07-24 22:34:36 -0700209 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700210 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700211 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMap);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400212 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530214 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800215 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400216 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100217 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Sunny Goyald164b7f2016-10-12 20:49:31 -0700218 mBgWidgetsModel = new WidgetsModel(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800219 mIconCache = iconCache;
220
Kenny Guyed131872014-04-30 03:02:21 +0100221 mLauncherApps = LauncherAppsCompat.getInstance(context);
222 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800223 }
224
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700225 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
226 * posted on the worker thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700227 private static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700228 if (sWorkerThread.getThreadId() == Process.myTid()) {
229 r.run();
230 } else {
231 // If we are not on the worker thread, then post to the worker handler
232 sWorker.post(r);
233 }
234 }
235
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700236 public void setPackageState(PackageInstallInfo installInfo) {
237 enqueueModelUpdateTask(new PackageInstallStateChangedTask(installInfo));
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500238 }
239
Sunny Goyal756adbc2015-04-16 15:20:51 -0700240 /**
241 * Updates the icons and label of all pending icons for the provided package name.
242 */
243 public void updateSessionDisplayInfo(final String packageName) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700244 HashSet<String> packages = new HashSet<>();
245 packages.add(packageName);
246 enqueueModelUpdateTask(new CacheDataUpdatedTask(
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800247 CacheDataUpdatedTask.OP_SESSION_UPDATE, Process.myUserHandle(), packages));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800248 }
249
250 /**
251 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800252 */
Sunny Goyal2bcbe132016-11-16 09:23:42 -0800253 public void addAndBindAddedWorkspaceItems(List<ItemInfo> workspaceApps) {
254 addAndBindAddedWorkspaceItems(Provider.of(workspaceApps));
255 }
256
257 /**
258 * Adds the provided items to the workspace.
259 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700260 public void addAndBindAddedWorkspaceItems(
Sunny Goyal2bcbe132016-11-16 09:23:42 -0800261 Provider<List<ItemInfo>> appsProvider) {
262 enqueueModelUpdateTask(new AddWorkspaceItemsTask(appsProvider));
Winson Chung64359a52013-07-08 17:17:08 -0700263 }
264
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800265 public ModelWriter getWriter(boolean hasVerticalHotseat) {
266 return new ModelWriter(mApp.getContext(), sBgDataModel, hasVerticalHotseat);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 }
268
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700269 static void checkItemInfoLocked(
270 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
Sunny Goyale9956a72016-09-01 17:24:47 -0700271 ItemInfo modelItem = sBgDataModel.itemsIdMap.get(itemId);
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700272 if (modelItem != null && item != modelItem) {
273 // check all the data is consistent
274 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
275 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
276 ShortcutInfo shortcut = (ShortcutInfo) item;
277 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
278 modelShortcut.intent.filterEquals(shortcut.intent) &&
279 modelShortcut.id == shortcut.id &&
280 modelShortcut.itemType == shortcut.itemType &&
281 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700282 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700283 modelShortcut.cellX == shortcut.cellX &&
284 modelShortcut.cellY == shortcut.cellY &&
285 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700286 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700287 // For all intents and purposes, this is the same object
288 return;
289 }
290 }
291
292 // the modelItem needs to match up perfectly with item if our model is
293 // to be consistent with the database-- for now, just require
294 // modelItem == item or the equality check above
295 String msg = "item: " + ((item != null) ? item.toString() : "null") +
296 "modelItem: " +
297 ((modelItem != null) ? modelItem.toString() : "null") +
298 "Error: ItemInfo passed to checkItemInfo doesn't match original";
299 RuntimeException e = new RuntimeException(msg);
300 if (stackTrace != null) {
301 e.setStackTrace(stackTrace);
302 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800303 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700304 }
305 }
306
Michael Jurka816474f2012-06-25 14:49:02 -0700307 static void checkItemInfo(final ItemInfo item) {
308 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
309 final long itemId = item.id;
310 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700311 public void run() {
Sunny Goyale9956a72016-09-01 17:24:47 -0700312 synchronized (sBgDataModel) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700313 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700314 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700315 }
316 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700317 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700318 }
319
Joe Onorato9c1289c2009-08-17 11:03:03 -0400320 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700321 * Update the order of the workspace screens in the database. The array list contains
322 * a list of screen ids in the order that they should appear.
323 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700324 public static void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -0700325 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700326 final ContentResolver cr = context.getContentResolver();
327 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
328
329 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700330 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700331 while (iter.hasNext()) {
332 long id = iter.next();
333 if (id < 0) {
334 iter.remove();
335 }
336 }
337
338 Runnable r = new Runnable() {
339 @Override
340 public void run() {
Yura085c8532014-02-11 15:15:29 +0000341 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700342 // Clear the table
Yura085c8532014-02-11 15:15:29 +0000343 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -0700344 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700345 for (int i = 0; i < count; i++) {
346 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700347 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700348 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
349 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +0000350 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -0700351 }
Yura085c8532014-02-11 15:15:29 +0000352
353 try {
354 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
355 } catch (Exception ex) {
356 throw new RuntimeException(ex);
357 }
Winson Chung9e6a0a22013-08-27 11:58:12 -0700358
Sunny Goyale9956a72016-09-01 17:24:47 -0700359 synchronized (sBgDataModel) {
360 sBgDataModel.workspaceScreens.clear();
361 sBgDataModel.workspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -0700362 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700363 }
364 };
365 runOnWorkerThread(r);
366 }
367
368 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400369 * Set this as the current Launcher activity object for the loader.
370 */
371 public void initialize(Callbacks callbacks) {
372 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -0700373 Preconditions.assertUIThread();
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -0700374 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400375 }
376 }
377
Kenny Guyed131872014-04-30 03:02:21 +0100378 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800379 public void onPackageChanged(String packageName, UserHandle user) {
Kenny Guyed131872014-04-30 03:02:21 +0100380 int op = PackageUpdatedTask.OP_UPDATE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700381 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100382 }
383
384 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800385 public void onPackageRemoved(String packageName, UserHandle user) {
Sunny Goyalc2936bc2016-09-01 15:50:36 -0700386 onPackagesRemoved(user, packageName);
387 }
388
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800389 public void onPackagesRemoved(UserHandle user, String... packages) {
Kenny Guyed131872014-04-30 03:02:21 +0100390 int op = PackageUpdatedTask.OP_REMOVE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700391 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packages));
Kenny Guyed131872014-04-30 03:02:21 +0100392 }
393
394 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800395 public void onPackageAdded(String packageName, UserHandle user) {
Kenny Guyed131872014-04-30 03:02:21 +0100396 int op = PackageUpdatedTask.OP_ADD;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700397 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100398 }
399
400 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800401 public void onPackagesAvailable(String[] packageNames, UserHandle user,
Kenny Guyed131872014-04-30 03:02:21 +0100402 boolean replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700403 enqueueModelUpdateTask(
404 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100405 }
406
407 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800408 public void onPackagesUnavailable(String[] packageNames, UserHandle user,
Kenny Guyed131872014-04-30 03:02:21 +0100409 boolean replacing) {
410 if (!replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700411 enqueueModelUpdateTask(new PackageUpdatedTask(
412 PackageUpdatedTask.OP_UNAVAILABLE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100413 }
Kenny Guyed131872014-04-30 03:02:21 +0100414 }
415
Kenny Guy44cba692016-01-21 19:50:02 +0000416 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800417 public void onPackagesSuspended(String[] packageNames, UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700418 enqueueModelUpdateTask(new PackageUpdatedTask(
419 PackageUpdatedTask.OP_SUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000420 }
421
422 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800423 public void onPackagesUnsuspended(String[] packageNames, UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700424 enqueueModelUpdateTask(new PackageUpdatedTask(
425 PackageUpdatedTask.OP_UNSUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000426 }
427
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700428 @Override
429 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800430 UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700431 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, true));
Sunny Goyal50941fb2016-08-04 12:03:52 -0700432 }
433
434 public void updatePinnedShortcuts(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800435 UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700436 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, false));
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700437 }
438
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700439 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400440 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
441 * ACTION_PACKAGE_CHANGED.
442 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100443 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400444 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -0400445 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700446
Joe Onorato36115782010-06-17 13:28:48 -0400447 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +0100448 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -0700449 // If we have changed locale we need to clear out the labels in all apps/workspace.
450 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700451 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
452 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -0700453 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -0700454 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700455 } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
456 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
457 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800458 UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER);
Sunny Goyalda891c12016-03-18 18:29:24 -0700459 if (user != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700460 if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
461 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700462 enqueueModelUpdateTask(new PackageUpdatedTask(
463 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE, user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700464 }
465
466 // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so
467 // we need to run the state change task again.
468 if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
469 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700470 enqueueModelUpdateTask(new UserLockStateChangedTask(user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700471 }
Sunny Goyalda891c12016-03-18 18:29:24 -0700472 }
Tony Wickham827cef22016-03-17 15:39:39 -0700473 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
474 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700475 }
476 }
477
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800478 /**
479 * Reloads the workspace items from the DB and re-binds the workspace. This should generally
480 * not be called as DB updates are automatically followed by UI update
481 */
482 public void forceReload() {
483 synchronized (mLock) {
484 // Stop any existing loaders first, so they don't set mModelLoaded to true later
485 stopLoaderLocked();
486 mModelLoaded = false;
487 }
Winson Chungf0c6ae02012-03-21 16:10:31 -0700488
Reena Lee93f824a2011-09-23 17:20:28 -0700489 // Do this here because if the launcher activity is running it will be restarted.
490 // If it's not running startLoaderFromBackground will merely tell it that it needs
491 // to reload.
492 startLoaderFromBackground();
493 }
494
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700495 /**
496 * When the launcher is in the background, it's possible for it to miss paired
497 * configuration changes. So whenever we trigger the loader from the background
498 * tell the launcher that it needs to re-run the loader when it comes back instead
499 * of doing it now.
500 */
501 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800502 Callbacks callbacks = getCallback();
503 if (callbacks != null) {
504 // Only actually run the loader if they're not paused.
505 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -0700506 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700507 }
508 }
Joe Onorato36115782010-06-17 13:28:48 -0400509 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400510
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700511 /**
512 * If there is already a loader task running, tell it to stop.
513 */
514 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -0700515 LoaderTask oldTask = mLoaderTask;
516 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -0700517 oldTask.stopLocked();
518 }
Reena Lee93f824a2011-09-23 17:20:28 -0700519 }
520
Adam Cohen1a85c582014-09-30 09:48:49 -0700521 public boolean isCurrentCallbacks(Callbacks callbacks) {
522 return (mCallbacks != null && mCallbacks.get() == callbacks);
523 }
524
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700525 /**
526 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
527 * @return true if the page could be bound synchronously.
528 */
529 public boolean startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700530 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
531 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -0400532 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -0400533 // Don't bother to start the thread if we know it's not going to do anything
534 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -0700535 final Callbacks oldCallbacks = mCallbacks.get();
536 // Clear any pending bind-runnables from the synchronized load process.
Sunny Goyalb265ba72017-02-14 15:03:45 -0800537 mUiExecutor.execute(new Runnable() {
538 public void run() {
539 oldCallbacks.clearPendingBinds();
540 }
541 });
Sunny Goyal527c7d32015-08-28 15:19:36 -0700542
Joe Onorato36115782010-06-17 13:28:48 -0400543 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700544 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700545 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800546 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
547 && mModelLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700548 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700549 return true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700550 } else {
551 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
552 sWorker.post(mLoaderTask);
553 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400554 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400555 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700556 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400557 }
558
Joe Onorato36115782010-06-17 13:28:48 -0400559 public void stopLoader() {
560 synchronized (mLock) {
561 if (mLoaderTask != null) {
562 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400563 }
564 }
Joe Onorato36115782010-06-17 13:28:48 -0400565 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400566
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800567 /**
568 * Loads the workspace screen ids in an ordered list.
569 */
Sunny Goyale5bb7052015-07-27 14:36:07 -0700570 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -0700571 final ContentResolver contentResolver = context.getContentResolver();
572 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -0700573
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800574 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -0700575 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
576 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -0700577 }
578
Mario Bertschler817afa32017-03-15 11:56:47 -0700579 public void onInstallSessionCreated(final PackageInstallInfo sessionInfo) {
580 enqueueModelUpdateTask(new ExtendedModelTask() {
581 @Override
582 public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
583 apps.addPromiseApp(app.getContext(), sessionInfo);
584 if (!apps.added.isEmpty()) {
585 final ArrayList<AppInfo> arrayList = new ArrayList<>(apps.added);
586 apps.added.clear();
587 scheduleCallbackTask(new CallbackTask() {
588 @Override
589 public void execute(Callbacks callbacks) {
590 callbacks.bindAppsAdded(null, null, null, arrayList);
591 }
592 });
593 }
594 }
595 });
596 }
597
Joe Onorato36115782010-06-17 13:28:48 -0400598 /**
599 * Runnable for the thread that loads the contents of the launcher:
600 * - workspace icons
601 * - widgets
602 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700603 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -0400604 */
605 private class LoaderTask implements Runnable {
606 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -0700607 private int mPageToBindFirst;
608
Adam Cohen091440a2015-03-18 14:16:05 -0700609 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -0400610 private boolean mStopped;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700611
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700612 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -0400613 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -0700614 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400615 }
616
Joe Onorato36115782010-06-17 13:28:48 -0400617 private void waitForIdle() {
618 // Wait until the either we're stopped or the other threads are done.
619 // This way we don't start loading all apps until the workspace has settled
620 // down.
621 synchronized (LoaderTask.this) {
Sunny Goyalb265ba72017-02-14 15:03:45 -0800622 LooperIdleLock idleLock = new LooperIdleLock(this, Looper.getMainLooper());
623 // Just in case mFlushingWorkerThread changes but we aren't woken up,
624 // wait no longer than 1sec at a time
625 while (!mStopped && idleLock.awaitLocked(1000));
Daniel Sandler843e8602010-06-07 14:59:01 -0400626 }
Joe Onorato36115782010-06-17 13:28:48 -0400627 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400628
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700629 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -0500630 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700631 // Ensure that we have a valid page index to load synchronously
632 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
633 "valid page index");
634 }
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800635 if (!mModelLoaded) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700636 // Ensure that we don't try and bind a specified page when the pages have not been
637 // loaded already (we should load everything asynchronously in that case)
638 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
639 }
640 synchronized (mLock) {
641 if (mIsLoaderTaskRunning) {
642 // Ensure that we are never running the background loading at this point since
643 // we also touch the background collections
644 throw new RuntimeException("Error! Background loading is already running");
645 }
646 }
647
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700648 // Divide the set of loaded items into those that we are binding synchronously, and
649 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800650 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700651 // XXX: For now, continue posting the binding of AllApps as there are other issues that
652 // arise from that.
653 onlyBindAllApps();
Tony Wickham80f57872016-06-29 18:12:15 -0700654
655 bindDeepShortcuts();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700656 }
657
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800658 private void verifyNotStopped() throws CancellationException {
659 synchronized (LoaderTask.this) {
660 if (mStopped) {
661 throw new CancellationException("Loader stopped");
662 }
663 }
664 }
665
Joe Onorato36115782010-06-17 13:28:48 -0400666 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700667 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -0700668 if (mStopped) {
669 return;
670 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700671 mIsLoaderTaskRunning = true;
672 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400673
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800674 try {
Sunny Goyalb265ba72017-02-14 15:03:45 -0800675 long now = 0;
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800676 if (DEBUG_LOADERS) Log.d(TAG, "step 1.1: loading workspace");
677 // Set to false in bindWorkspace()
678 mIsLoadingAndBindingWorkspace = true;
679 loadWorkspace();
Joe Onorato36115782010-06-17 13:28:48 -0400680
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800681 verifyNotStopped();
682 if (DEBUG_LOADERS) Log.d(TAG, "step 1.2: bind workspace workspace");
683 bindWorkspace(mPageToBindFirst);
684
685 // Take a break
Sunny Goyalb265ba72017-02-14 15:03:45 -0800686 if (DEBUG_LOADERS) {
687 Log.d(TAG, "step 1 completed, wait for idle");
688 now = SystemClock.uptimeMillis();
689 }
Joe Onorato36115782010-06-17 13:28:48 -0400690 waitForIdle();
Sunny Goyalb265ba72017-02-14 15:03:45 -0800691 if (DEBUG_LOADERS) Log.d(TAG, "Waited " + (SystemClock.uptimeMillis() - now) + "ms");
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800692 verifyNotStopped();
Daniel Sandler843e8602010-06-07 14:59:01 -0400693
694 // second step
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800695 if (DEBUG_LOADERS) Log.d(TAG, "step 2.1: loading all apps");
696 loadAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700697
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800698 verifyNotStopped();
699 if (DEBUG_LOADERS) Log.d(TAG, "step 2.2: Update icon cache");
700 updateIconCache();
701
702 // Take a break
Sunny Goyalb265ba72017-02-14 15:03:45 -0800703 if (DEBUG_LOADERS) {
704 Log.d(TAG, "step 2 completed, wait for idle");
705 now = SystemClock.uptimeMillis();
706 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700707 waitForIdle();
Sunny Goyalb265ba72017-02-14 15:03:45 -0800708 if (DEBUG_LOADERS) Log.d(TAG, "Waited " + (SystemClock.uptimeMillis() - now) + "ms");
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800709 verifyNotStopped();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700710
711 // third step
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800712 if (DEBUG_LOADERS) Log.d(TAG, "step 3.1: loading deep shortcuts");
713 loadDeepShortcuts();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400714
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800715 verifyNotStopped();
716 if (DEBUG_LOADERS) Log.d(TAG, "step 3.2: bind deep shortcuts");
717 bindDeepShortcuts();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400718
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800719 synchronized (mLock) {
720 // Everything loaded bind the data.
721 mModelLoaded = true;
722 mHasLoaderCompletedOnce = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400723 }
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800724 } catch (CancellationException e) {
725 // Loader stopped, ignore
726 } finally {
727 // Clear out this reference, otherwise we end up holding it until all of the
728 // callback runnables are done.
729 mContext = null;
730
731 synchronized (mLock) {
732 // If we are still the last one to be scheduled, remove ourselves.
733 if (mLoaderTask == this) {
734 mLoaderTask = null;
735 }
736 mIsLoaderTaskRunning = false;
737 }
Joe Onorato36115782010-06-17 13:28:48 -0400738 }
Joe Onorato36115782010-06-17 13:28:48 -0400739 }
740
741 public void stopLocked() {
742 synchronized (LoaderTask.this) {
743 mStopped = true;
744 this.notify();
745 }
746 }
747
748 /**
749 * Gets the callbacks object. If we've been stopped, or if the launcher object
750 * has somehow been garbage collected, return null instead. Pass in the Callbacks
751 * object that was around when the deferred message was scheduled, and if there's
752 * a new Callbacks object around then also return null. This will save us from
753 * calling onto it with data that will be ignored.
754 */
755 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
756 synchronized (mLock) {
757 if (mStopped) {
758 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400759 }
Joe Onorato36115782010-06-17 13:28:48 -0400760
761 if (mCallbacks == null) {
762 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400763 }
Joe Onorato36115782010-06-17 13:28:48 -0400764
765 final Callbacks callbacks = mCallbacks.get();
766 if (callbacks != oldCallbacks) {
767 return null;
768 }
769 if (callbacks == null) {
770 Log.w(TAG, "no mCallbacks");
771 return null;
772 }
773
774 return callbacks;
775 }
776 }
777
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800778 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -0700779 if (LauncherAppState.PROFILE_STARTUP) {
780 Trace.beginSection("Loading Workspace");
781 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400782
Joe Onorato36115782010-06-17 13:28:48 -0400783 final Context context = mContext;
784 final ContentResolver contentResolver = context.getContentResolver();
Sunny Goyal342e4662017-02-02 15:21:08 -0800785 final PackageManagerHelper pmHelper = new PackageManagerHelper(context);
786 final boolean isSafeMode = pmHelper.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700787 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530788 final DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -0700789 final boolean isSdCardReady = Utilities.isBootCompleted();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800790 final MultiHashMap<UserHandle, String> pendingPackages = new MultiHashMap<>();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400791
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700792 boolean clearDb = false;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -0700793 try {
794 ImportDataTask.performImportIfPossible(context);
795 } catch (Exception e) {
796 // Migration failed. Clear workspace.
797 clearDb = true;
798 }
799
800 if (!clearDb && GridSizeMigrationTask.ENABLED &&
Sunny Goyalf076eae2016-01-11 12:25:10 -0800801 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
802 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700803 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -0700804 }
805
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700806 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -0700807 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -0700808 LauncherSettings.Settings.call(contentResolver,
Sunny Goyale05b08f2017-02-23 18:30:22 -0800809 LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -0500810 }
811
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700812 Log.d(TAG, "loadWorkspace: loading default favorites");
813 LauncherSettings.Settings.call(contentResolver,
814 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -0700815
Sunny Goyale9956a72016-09-01 17:24:47 -0700816 synchronized (sBgDataModel) {
817 sBgDataModel.clear();
818
Sunny Goyal756adbc2015-04-16 15:20:51 -0700819 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -0700820 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyale9956a72016-09-01 17:24:47 -0700821 sBgDataModel.workspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -0800822
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700823 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800824 final LoaderCursor c = new LoaderCursor(contentResolver.query(
825 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null), mApp);
Daniel Sandler8802e962010-05-26 16:28:16 -0400826
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800827 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400828
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700829 try {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700830 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
831 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -0400832 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
833 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700834 final int spanXIndex = c.getColumnIndexOrThrow
835 (LauncherSettings.Favorites.SPANX);
836 final int spanYIndex = c.getColumnIndexOrThrow(
837 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800838 final int rankIndex = c.getColumnIndexOrThrow(
839 LauncherSettings.Favorites.RANK);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700840 final int optionsIndex = c.getColumnIndexOrThrow(
841 LauncherSettings.Favorites.OPTIONS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400842
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800843 final LongSparseArray<UserHandle> allUsers = c.allUsers;
Kenny Guyff05f432016-01-22 17:48:29 +0000844 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700845 final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800846 for (UserHandle user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +0000847 long serialNo = mUserManager.getSerialNumberForUser(user);
848 allUsers.put(serialNo, user);
849 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700850
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700851 boolean userUnlocked = mUserManager.isUserUnlocked(user);
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700852
853 // We can only query for shortcuts when the user is unlocked.
854 if (userUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -0700855 List<ShortcutInfoCompat> pinnedShortcuts =
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530856 shortcutManager.queryForPinnedShortcuts(null, user);
857 if (shortcutManager.wasLastCallSuccess()) {
Sunny Goyal49f4f032016-08-01 15:45:49 -0700858 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
859 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
860 shortcut);
861 }
862 } else {
863 // Shortcut manager can fail due to some race condition when the
864 // lock state changes too frequently. For the purpose of the loading
865 // shortcuts, consider the user is still locked.
866 userUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700867 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700868 }
Sunny Goyal49f4f032016-08-01 15:45:49 -0700869 unlockedUsers.put(serialNo, userUnlocked);
Sunny Goyal7f834d22015-04-21 10:10:23 -0700870 }
871
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700872 ShortcutInfo info;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700873 LauncherAppWidgetInfo appWidgetInfo;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700874 Intent intent;
Sunny Goyal81e44912017-01-14 15:05:14 -0800875 String targetPkg;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400876
Jon Miranda529af302017-02-28 14:18:54 -0800877 FolderIconPreviewVerifier verifier =
878 new FolderIconPreviewVerifier(mApp.getInvariantDeviceProfile());
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700879 while (!mStopped && c.moveToNext()) {
880 try {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800881 if (c.user == null) {
882 // User has been deleted, remove the item.
883 c.markDeleted("User has been deleted");
884 continue;
885 }
886
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700887 boolean allowMissingTarget = false;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800888 switch (c.itemType) {
Sunny Goyal81e44912017-01-14 15:05:14 -0800889 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700890 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700891 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Sunny Goyal81e44912017-01-14 15:05:14 -0800892 intent = c.parseIntent();
893 if (intent == null) {
894 c.markDeleted("Invalid or null intent");
895 continue;
896 }
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800897
Sunny Goyal81e44912017-01-14 15:05:14 -0800898 int disabledState = quietMode.get(c.serialNumber) ?
899 ShortcutInfo.FLAG_DISABLED_QUIET_USER : 0;
900 ComponentName cn = intent.getComponent();
901 targetPkg = cn == null ? intent.getPackage() : cn.getPackageName();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700902
Sunny Goyal81e44912017-01-14 15:05:14 -0800903 if (!Process.myUserHandle().equals(c.user)) {
904 if (c.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
905 c.markDeleted("Legacy shortcuts are only allowed for default user");
906 continue;
907 } else if (c.restoreFlag != 0) {
908 // Don't restore items for other profiles.
909 c.markDeleted("Restore from managed profile not supported");
910 continue;
911 }
912 }
913 if (TextUtils.isEmpty(targetPkg) &&
914 c.itemType != LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
915 c.markDeleted("Only legacy shortcuts can have null package");
916 continue;
917 }
Sunny Goyal34942622014-08-29 17:20:55 -0700918
Sunny Goyal81e44912017-01-14 15:05:14 -0800919 // If there is no target package, its an implicit intent
920 // (legacy shortcut) which is always valid
921 boolean validTarget = TextUtils.isEmpty(targetPkg) ||
922 launcherApps.isPackageEnabledForProfile(targetPkg, c.user);
Sunny Goyal94485362014-09-18 16:13:58 -0700923
Sunny Goyal81e44912017-01-14 15:05:14 -0800924 if (cn != null && validTarget) {
925 // If the apk is present and the shortcut points to a specific
926 // component.
927
928 // If the component is already present
929 if (launcherApps.isActivityEnabledForProfile(cn, c.user)) {
930 // no special handling necessary for this item
931 c.markRestored();
932 } else {
933 if (c.hasRestoreFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
934 // We allow auto install apps to have their intent
935 // updated after an install.
Sunny Goyal342e4662017-02-02 15:21:08 -0800936 intent = pmHelper.getAppLaunchIntent(targetPkg, c.user);
Sunny Goyal81e44912017-01-14 15:05:14 -0800937 if (intent != null) {
938 c.restoreFlag = 0;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800939 c.updater().put(
Sunny Goyal81e44912017-01-14 15:05:14 -0800940 LauncherSettings.Favorites.INTENT,
941 intent.toUri(0)).commit();
942 cn = intent.getComponent();
Sunny Goyalb05a00a2016-08-29 10:06:57 -0700943 } else {
Sunny Goyal81e44912017-01-14 15:05:14 -0800944 c.markDeleted("Unable to find a launch target");
Sunny Goyal94485362014-09-18 16:13:58 -0700945 continue;
946 }
Sunny Goyal1a745e82014-10-02 15:58:31 -0700947 } else {
Sunny Goyal81e44912017-01-14 15:05:14 -0800948 // The app is installed but the component is no
949 // longer available.
950 c.markDeleted("Invalid component removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -0700951 continue;
Winson Chungee055712013-07-30 14:46:24 -0700952 }
Winson Chungee055712013-07-30 14:46:24 -0700953 }
Sunny Goyal81e44912017-01-14 15:05:14 -0800954 }
955 // else if cn == null => can't infer much, leave it
956 // else if !validPkg => could be restored icon or missing sd-card
957
958 if (!TextUtils.isEmpty(targetPkg) && !validTarget) {
959 // Points to a valid app (superset of cn != null) but the apk
960 // is not available.
961
962 if (c.restoreFlag != 0) {
963 // Package is not yet available but might be
964 // installed later.
965 FileLog.d(TAG, "package not yet restored: " + targetPkg);
966
967 if (c.hasRestoreFlag(ShortcutInfo.FLAG_RESTORE_STARTED)) {
968 // Restore has started once.
969 } else if (installingPkgs.containsKey(targetPkg)) {
970 // App restore has started. Update the flag
971 c.restoreFlag |= ShortcutInfo.FLAG_RESTORE_STARTED;
972 c.updater().commit();
973 } else {
974 c.markDeleted("Unrestored app removed: " + targetPkg);
975 continue;
976 }
Sunny Goyalad2e91a2017-02-28 15:14:58 -0800977 } else if (pmHelper.isAppOnSdcard(targetPkg, c.user)) {
Sunny Goyal81e44912017-01-14 15:05:14 -0800978 // Package is present but not available.
979 disabledState |= ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
980 // Add the icon on the workspace anyway.
981 allowMissingTarget = true;
982 } else if (!isSdCardReady) {
983 // SdCard is not ready yet. Package might get available,
984 // once it is ready.
985 Log.d(TAG, "Missing pkg, will check later: " + targetPkg);
986 pendingPackages.addToList(c.user, targetPkg);
987 // Add the icon on the workspace anyway.
988 allowMissingTarget = true;
989 } else {
990 // Do not wait for external media load anymore.
991 c.markDeleted("Invalid package removed: " + targetPkg);
992 continue;
993 }
994 }
995
996 if (validTarget) {
997 // The shortcut points to a valid target (either no target
998 // or something which is ready to be used)
999 c.markRestored();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001000 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001001
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001002 boolean useLowResIcon = !c.isOnWorkspaceOrHotseat() &&
Jon Miranda529af302017-02-28 14:18:54 -08001003 !verifier.isItemInPreview(c.getInt(rankIndex));
Sunny Goyal34b65272015-03-11 16:56:52 -07001004
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001005 if (c.restoreFlag != 0) {
Sunny Goyal81e44912017-01-14 15:05:14 -08001006 // Already verified above that user is same as default user
1007 info = c.getRestoredItemInfo(intent);
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001008 } else if (c.itemType ==
Chris Wrenf4d08112014-01-16 18:13:56 -05001009 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001010 info = c.getAppShortcutInfo(
1011 intent, allowMissingTarget, useLowResIcon);
1012 } else if (c.itemType ==
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001013 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001014
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001015 ShortcutKey key = ShortcutKey.fromIntent(intent, c.user);
1016 if (unlockedUsers.get(c.serialNumber)) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001017 ShortcutInfoCompat pinnedShortcut =
1018 shortcutKeyToPinnedShortcuts.get(key);
1019 if (pinnedShortcut == null) {
1020 // The shortcut is no longer valid.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001021 c.markDeleted("Pinned shortcut not found");
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001022 continue;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001023 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001024 info = new ShortcutInfo(pinnedShortcut, context);
Sunny Goyal1b072632017-01-18 11:30:23 -08001025 info.iconBitmap = LauncherIcons
1026 .createShortcutIcon(pinnedShortcut, context);
Charles He3ff90472017-02-15 12:22:56 +00001027 if (pmHelper.isAppSuspended(
Sunny Goyalad2e91a2017-02-28 15:14:58 -08001028 pinnedShortcut.getPackage(), info.user)) {
Charles He3ff90472017-02-15 12:22:56 +00001029 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1030 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001031 intent = info.intent;
1032 } else {
1033 // Create a shortcut info in disabled mode for now.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001034 info = c.loadSimpleShortcut();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001035 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001036 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001037 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001038 info = c.loadSimpleShortcut();
Michael Jurka96879562012-03-22 05:54:33 -07001039
Sunny Goyald09c3702016-04-06 16:18:20 -07001040 // Shortcuts are only available on the primary profile
Sunny Goyalad2e91a2017-02-28 15:14:58 -08001041 if (pmHelper.isAppSuspended(targetPkg, c.user)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001042 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1043 }
1044
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001045 // App shortcuts that used to be automatically added to Launcher
1046 // didn't always have the correct intent flags set, so do that
1047 // here
1048 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001049 intent.getCategories() != null &&
1050 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001051 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001052 intent.addFlags(
1053 Intent.FLAG_ACTIVITY_NEW_TASK |
1054 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1055 }
Michael Jurka96879562012-03-22 05:54:33 -07001056 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001057
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001058 if (info != null) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001059 c.applyCommonProperties(info);
1060
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001061 info.intent = intent;
Sunny Goyal08f72612015-01-05 13:41:43 -08001062 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001063 info.spanX = 1;
1064 info.spanY = 1;
Sunny Goyald09c3702016-04-06 16:18:20 -07001065 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001066 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1067 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1068 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001069
Sunny Goyal81e44912017-01-14 15:05:14 -08001070 if (c.restoreFlag != 0 && !TextUtils.isEmpty(targetPkg)) {
1071 Integer progress = installingPkgs.get(targetPkg);
1072 if (progress != null) {
1073 info.setInstallProgress(progress);
1074 } else {
1075 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001076 }
1077 }
1078
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001079 c.checkAndAddItem(info, sBgDataModel);
Winson Chung1323b482013-08-05 12:41:55 -07001080 } else {
1081 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001082 }
1083 break;
1084
1085 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001086 FolderInfo folderInfo = sBgDataModel.findOrMakeFolder(c.id);
1087 c.applyCommonProperties(folderInfo);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001088
Sunny Goyala508e4f2015-05-21 09:33:57 -07001089 // Do not trim the folder label, as is was set by the user.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001090 folderInfo.title = c.getString(c.titleIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001091 folderInfo.spanX = 1;
1092 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001093 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001094
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001095 // no special handling required for restored folders
1096 c.markRestored();
Chris Wrenf4d08112014-01-16 18:13:56 -05001097
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001098 c.checkAndAddItem(folderInfo, sBgDataModel);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001099 break;
1100
1101 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08001102 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001103 // Read all Launcher-specific widget details
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001104 boolean customWidget = c.itemType ==
Adam Cohen59400422014-03-05 18:07:04 -08001105 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
1106
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001107 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001108 String savedProvider = c.getString(appWidgetProviderIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001109
Sunny Goyalff572272014-07-23 13:58:07 -07001110 final ComponentName component =
1111 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04001112
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001113 final boolean isIdValid = !c.hasRestoreFlag(
1114 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID);
1115 final boolean wasProviderReady = !c.hasRestoreFlag(
1116 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY);
Sunny Goyal651077b2014-06-30 14:15:31 -07001117
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001118 if (widgetProvidersMap == null) {
1119 widgetProvidersMap = AppWidgetManagerCompat
1120 .getInstance(mContext).getAllProvidersMap();
1121 }
1122 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
1123 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00001124 ComponentName.unflattenFromString(savedProvider),
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001125 c.user));
Sunny Goyalff572272014-07-23 13:58:07 -07001126
1127 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08001128 if (!isSafeMode && !customWidget &&
1129 wasProviderReady && !isProviderReady) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001130 c.markDeleted(
1131 "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07001132 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001133 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07001134 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07001135 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1136 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08001137
Sunny Goyal53f96722015-07-13 19:54:53 -07001138 // The provider is available. So the widget is either
1139 // available or not available. We do not need to track
1140 // any future restore updates.
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001141 int status = c.restoreFlag &
Sunny Goyal53f96722015-07-13 19:54:53 -07001142 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07001143 if (!wasProviderReady) {
1144 // If provider was not previously ready, update the
1145 // status and UI flag.
1146
1147 // Id would be valid only if the widget restore broadcast was received.
1148 if (isIdValid) {
Sunny Goyal86df1382016-08-10 15:03:22 -07001149 status |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07001150 } else {
1151 status &= ~LauncherAppWidgetInfo
1152 .FLAG_PROVIDER_NOT_READY;
1153 }
1154 }
1155 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07001156 } else {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001157 Log.v(TAG, "Widget restore pending id=" + c.id
Sunny Goyal651077b2014-06-30 14:15:31 -07001158 + " appWidgetId=" + appWidgetId
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001159 + " status =" + c.restoreFlag);
Sunny Goyal651077b2014-06-30 14:15:31 -07001160 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07001161 component);
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001162 appWidgetInfo.restoreStatus = c.restoreFlag;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001163 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07001164
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001165 if (c.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_RESTORE_STARTED)) {
Sunny Goyal94485362014-09-18 16:13:58 -07001166 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001167 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07001168 // App restore has started. Update the flag
1169 appWidgetInfo.restoreStatus |=
1170 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001171 } else if (!isSafeMode) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001172 c.markDeleted("Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07001173 continue;
1174 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07001175
1176 appWidgetInfo.installProgress =
1177 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07001178 }
Sunny Goyal86df1382016-08-10 15:03:22 -07001179 if (appWidgetInfo.hasRestoreFlag(
1180 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG)) {
Sunny Goyal81e44912017-01-14 15:05:14 -08001181 appWidgetInfo.bindOptions = c.parseIntent();
Sunny Goyal86df1382016-08-10 15:03:22 -07001182 }
Sunny Goyalff572272014-07-23 13:58:07 -07001183
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001184 c.applyCommonProperties(appWidgetInfo);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001185 appWidgetInfo.spanX = c.getInt(spanXIndex);
1186 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001187 appWidgetInfo.user = c.user;
Joe Onorato36115782010-06-17 13:28:48 -04001188
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001189 if (!c.isOnWorkspaceOrHotseat()) {
1190 c.markDeleted("Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001191 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001192 continue;
1193 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001194
Adam Cohen59400422014-03-05 18:07:04 -08001195 if (!customWidget) {
1196 String providerName =
1197 appWidgetInfo.providerName.flattenToString();
1198 if (!providerName.equals(savedProvider) ||
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001199 (appWidgetInfo.restoreStatus != c.restoreFlag)) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001200 c.updater()
1201 .put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1202 providerName)
1203 .put(LauncherSettings.Favorites.RESTORED,
1204 appWidgetInfo.restoreStatus)
1205 .commit();
Adam Cohen59400422014-03-05 18:07:04 -08001206 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001207 }
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001208 c.checkAndAddItem(appWidgetInfo, sBgDataModel);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001209 }
Joe Onorato36115782010-06-17 13:28:48 -04001210 break;
1211 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001212 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001213 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001214 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001215 }
1216 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001217 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001218 }
1219
Winson Chungba9c37f2013-08-30 14:11:37 -07001220 // Break early if we've stopped loading
1221 if (mStopped) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001222 sBgDataModel.clear();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001223 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07001224 }
1225
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001226 // Remove dead items
1227 if (c.commitDeleted()) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001228 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07001229 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
1230 .call(contentResolver,
1231 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
1232 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
1233 for (long folderId : deletedFolderIds) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001234 sBgDataModel.workspaceItems.remove(sBgDataModel.folders.get(folderId));
1235 sBgDataModel.folders.remove(folderId);
1236 sBgDataModel.itemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001237 }
1238 }
1239
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001240 // Unpin shortcuts that don't exist on the workspace.
Sunny Goyalf75baa92016-11-22 03:23:51 +05301241 HashSet<ShortcutKey> pendingShortcuts =
1242 InstallShortcutReceiver.getPendingShortcuts(context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001243 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001244 MutableInt numTimesPinned = sBgDataModel.pinnedShortcutCounts.get(key);
Sunny Goyalf75baa92016-11-22 03:23:51 +05301245 if ((numTimesPinned == null || numTimesPinned.value == 0)
1246 && !pendingShortcuts.contains(key)) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001247 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301248 shortcutManager.unpinShortcut(key);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001249 }
1250 }
1251
Jon Miranda655ec422017-02-07 11:40:22 -08001252 FolderIconPreviewVerifier verifier =
1253 new FolderIconPreviewVerifier(mApp.getInvariantDeviceProfile());
1254 // Sort the folder items and make sure all items in the preview are high resolution.
Sunny Goyale9956a72016-09-01 17:24:47 -07001255 for (FolderInfo folder : sBgDataModel.folders) {
Sunny Goyal317698b2015-07-29 11:45:41 -07001256 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
Jon Miranda655ec422017-02-07 11:40:22 -08001257 verifier.setFolderInfo(folder);
1258
1259 int numItemsInPreview = 0;
Sunny Goyal317698b2015-07-29 11:45:41 -07001260 for (ShortcutInfo info : folder.contents) {
Jon Miranda655ec422017-02-07 11:40:22 -08001261 if (info.usingLowResIcon
1262 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION
1263 && verifier.isItemInPreview(info.rank)) {
Sunny Goyal3fe4a142016-12-15 17:40:07 -08001264 mIconCache.getTitleAndIcon(info, false);
Jon Miranda655ec422017-02-07 11:40:22 -08001265 numItemsInPreview++;
Sunny Goyal317698b2015-07-29 11:45:41 -07001266 }
Jon Miranda655ec422017-02-07 11:40:22 -08001267
1268 if (numItemsInPreview >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
Sunny Goyal317698b2015-07-29 11:45:41 -07001269 break;
1270 }
1271 }
1272 }
1273
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001274 c.commitRestoredItems();
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001275 if (!isSdCardReady && !pendingPackages.isEmpty()) {
1276 context.registerReceiver(
1277 new SdCardAvailableReceiver(
1278 LauncherModel.this, mContext, pendingPackages),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001279 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001280 null,
1281 sWorker);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001282 }
1283
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001284 // Remove any empty screens
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001285 ArrayList<Long> unusedScreens = new ArrayList<>(sBgDataModel.workspaceScreens);
Sunny Goyale9956a72016-09-01 17:24:47 -07001286 for (ItemInfo item: sBgDataModel.itemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001287 long screenId = item.screenId;
1288 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1289 unusedScreens.contains(screenId)) {
1290 unusedScreens.remove(screenId);
1291 }
1292 }
1293
1294 // If there are any empty screens remove them, and update.
1295 if (unusedScreens.size() != 0) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001296 sBgDataModel.workspaceScreens.removeAll(unusedScreens);
1297 updateWorkspaceScreenOrder(context, sBgDataModel.workspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001298 }
Joe Onorato36115782010-06-17 13:28:48 -04001299 }
Sunny Goyale26d1002016-06-20 14:52:14 -07001300 if (LauncherAppState.PROFILE_STARTUP) {
1301 Trace.endSection();
1302 }
Adam Cohene25af792013-06-06 23:08:25 -07001303 }
1304
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001305 /** Filters the set of items who are directly or indirectly (via another container) on the
1306 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08001307 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001308 ArrayList<ItemInfo> allWorkspaceItems,
1309 ArrayList<ItemInfo> currentScreenItems,
1310 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001311 // Purge any null ItemInfos
1312 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
1313 while (iter.hasNext()) {
1314 ItemInfo i = iter.next();
1315 if (i == null) {
1316 iter.remove();
1317 }
1318 }
1319
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001320 // Order the set of items by their containers first, this allows use to walk through the
1321 // list sequentially, build up a list of containers that are in the specified screen,
1322 // as well as all items in those containers.
1323 Set<Long> itemsOnScreen = new HashSet<Long>();
1324 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
1325 @Override
1326 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07001327 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001328 }
1329 });
1330 for (ItemInfo info : allWorkspaceItems) {
1331 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001332 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001333 currentScreenItems.add(info);
1334 itemsOnScreen.add(info.id);
1335 } else {
1336 otherScreenItems.add(info);
1337 }
1338 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1339 currentScreenItems.add(info);
1340 itemsOnScreen.add(info.id);
1341 } else {
1342 if (itemsOnScreen.contains(info.container)) {
1343 currentScreenItems.add(info);
1344 itemsOnScreen.add(info.id);
1345 } else {
1346 otherScreenItems.add(info);
1347 }
1348 }
1349 }
1350 }
1351
1352 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08001353 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001354 ArrayList<LauncherAppWidgetInfo> appWidgets,
1355 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
1356 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001357
1358 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001359 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001360 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08001361 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001362 currentScreenWidgets.add(widget);
1363 } else {
1364 otherScreenWidgets.add(widget);
1365 }
1366 }
1367 }
1368
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001369 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
1370 * right) */
1371 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Sunny Goyal87f784c2017-01-11 10:48:34 -08001372 final InvariantDeviceProfile profile = mApp.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07001373 final int screenCols = profile.numColumns;
1374 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001375 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07001376 @Override
1377 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07001378 if (lhs.container == rhs.container) {
1379 // Within containers, order by their spatial position in that container
1380 switch ((int) lhs.container) {
1381 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
1382 long lr = (lhs.screenId * screenCellCount +
1383 lhs.cellY * screenCols + lhs.cellX);
1384 long rr = (rhs.screenId * screenCellCount +
1385 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07001386 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07001387 }
1388 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
1389 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07001390 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07001391 }
1392 default:
Sunny Goyal3d706ad2017-03-06 16:56:39 -08001393 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07001394 throw new RuntimeException("Unexpected container type when " +
1395 "sorting workspace items.");
1396 }
1397 return 0;
1398 }
1399 } else {
1400 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07001401 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07001402 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001403 }
1404 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001405 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001406
Adam Cohendcd297f2013-06-18 13:13:40 -07001407 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
1408 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001409 final Runnable r = new Runnable() {
1410 @Override
1411 public void run() {
1412 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1413 if (callbacks != null) {
1414 callbacks.bindScreens(orderedScreens);
1415 }
1416 }
1417 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001418 mUiExecutor.execute(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07001419 }
1420
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001421 private void bindWorkspaceItems(final Callbacks oldCallbacks,
1422 final ArrayList<ItemInfo> workspaceItems,
1423 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07001424 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001425
1426 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07001427 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001428 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04001429 final int start = i;
1430 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001431 final Runnable r = new Runnable() {
1432 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001433 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001434 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001435 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07001436 callbacks.bindItems(workspaceItems, start, start+chunkSize,
1437 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001438 }
1439 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001440 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001441 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04001442 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001443
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001444 // Bind the widgets, one at a time
1445 N = appWidgets.size();
1446 for (int i = 0; i < N; i++) {
1447 final LauncherAppWidgetInfo widget = appWidgets.get(i);
1448 final Runnable r = new Runnable() {
1449 public void run() {
1450 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1451 if (callbacks != null) {
1452 callbacks.bindAppWidget(widget);
1453 }
1454 }
1455 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001456 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001457 }
1458 }
1459
1460 /**
1461 * Binds all loaded data to actual views on the main thread.
1462 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001463 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001464 final long t = SystemClock.uptimeMillis();
1465 Runnable r;
1466
1467 // Don't use these two variables in any of the callback runnables.
1468 // Otherwise we hold a reference to them.
1469 final Callbacks oldCallbacks = mCallbacks.get();
1470 if (oldCallbacks == null) {
1471 // This launcher has exited and nobody bothered to tell us. Just bail.
1472 Log.w(TAG, "LoaderTask running with no launcher");
1473 return;
1474 }
1475
Winson Chung9b9fb962013-11-15 15:39:34 -08001476 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07001477 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
1478 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
1479 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07001480
Sunny Goyale9956a72016-09-01 17:24:47 -07001481 synchronized (sBgDataModel) {
1482 workspaceItems.addAll(sBgDataModel.workspaceItems);
1483 appWidgets.addAll(sBgDataModel.appWidgets);
1484 orderedScreenIds.addAll(sBgDataModel.workspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001485 }
1486
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001487 final int currentScreen;
1488 {
1489 int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE
1490 ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen();
1491 if (currScreen >= orderedScreenIds.size()) {
1492 // There may be no workspace screens (just hotseat items and an empty page).
1493 currScreen = PagedView.INVALID_RESTORE_PAGE;
1494 }
1495 currentScreen = currScreen;
Winson Chung9b9fb962013-11-15 15:39:34 -08001496 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001497 final boolean validFirstPage = currentScreen >= 0;
1498 final long currentScreenId =
1499 validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID;
Winson Chung9b9fb962013-11-15 15:39:34 -08001500
Winson Chung9b9fb962013-11-15 15:39:34 -08001501 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07001502 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
1503 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
1504 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
1505 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001506
Winson Chung9b9fb962013-11-15 15:39:34 -08001507 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001508 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08001509 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001510 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001511 sortWorkspaceItemsSpatially(currentWorkspaceItems);
1512 sortWorkspaceItemsSpatially(otherWorkspaceItems);
1513
1514 // Tell the workspace that we're about to start binding items
1515 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001516 public void run() {
1517 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1518 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001519 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001520 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04001521 }
1522 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001523 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001524 mUiExecutor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001525
Adam Cohendcd297f2013-06-18 13:13:40 -07001526 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
1527
Sunny Goyalb265ba72017-02-14 15:03:45 -08001528 Executor mainExecutor = mUiExecutor;
Sunny Goyal527c7d32015-08-28 15:19:36 -07001529 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07001530 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001531
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001532 // In case of validFirstPage, only bind the first screen, and defer binding the
1533 // remaining screens after first onDraw (and an optional the fade animation whichever
1534 // happens later).
1535 // This ensures that the first screen is immediately visible (eg. during rotation)
1536 // In case of !validFirstPage, bind all pages one after other.
1537 final Executor deferredExecutor =
Sunny Goyalb265ba72017-02-14 15:03:45 -08001538 validFirstPage ? new ViewOnDrawExecutor(mUiExecutor) : mainExecutor;
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001539
1540 mainExecutor.execute(new Runnable() {
1541 @Override
1542 public void run() {
1543 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1544 if (callbacks != null) {
1545 callbacks.finishFirstPageBind(
1546 validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null);
1547 }
1548 }
1549 });
Sunny Goyal527c7d32015-08-28 15:19:36 -07001550
Sunny Goyal44c06432016-04-02 10:56:02 -07001551 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001552
1553 // Tell the workspace that we're done binding items
1554 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001555 public void run() {
1556 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1557 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001558 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04001559 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001560
Sunny Goyal639e9062015-08-19 19:17:06 -07001561 mIsLoadingAndBindingWorkspace = false;
1562
1563 // Run all the bind complete runnables after workspace is bound.
1564 if (!mBindCompleteRunnables.isEmpty()) {
1565 synchronized (mBindCompleteRunnables) {
1566 for (final Runnable r : mBindCompleteRunnables) {
1567 runOnWorkerThread(r);
1568 }
1569 mBindCompleteRunnables.clear();
1570 }
1571 }
1572
Winson Chung98e030b2012-05-07 16:01:11 -07001573 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04001574 if (DEBUG_LOADERS) {
1575 Log.d(TAG, "bound workspace in "
1576 + (SystemClock.uptimeMillis()-t) + "ms");
1577 }
Winson Chung36a62fe2012-05-06 18:04:42 -07001578
Joe Onorato36115782010-06-17 13:28:48 -04001579 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001580 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001581 deferredExecutor.execute(r);
1582
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001583 if (validFirstPage) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001584 r = new Runnable() {
1585 public void run() {
1586 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1587 if (callbacks != null) {
1588 // We are loading synchronously, which means, some of the pages will be
1589 // bound after first draw. Inform the callbacks that page binding is
1590 // not complete, and schedule the remaining pages.
1591 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
1592 callbacks.onPageBoundSynchronously(currentScreen);
1593 }
1594 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
1595 }
1596 }
1597 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001598 mUiExecutor.execute(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07001599 }
Joe Onorato36115782010-06-17 13:28:48 -04001600 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001601
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001602 private void updateIconCache() {
1603 // Ignore packages which have a promise icon.
1604 HashSet<String> packagesToIgnore = new HashSet<>();
Sunny Goyale9956a72016-09-01 17:24:47 -07001605 synchronized (sBgDataModel) {
1606 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001607 if (info instanceof ShortcutInfo) {
1608 ShortcutInfo si = (ShortcutInfo) info;
1609 if (si.isPromise() && si.getTargetComponent() != null) {
1610 packagesToIgnore.add(si.getTargetComponent().getPackageName());
1611 }
1612 } else if (info instanceof LauncherAppWidgetInfo) {
1613 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
1614 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
1615 packagesToIgnore.add(lawi.providerName.getPackageName());
1616 }
1617 }
1618 }
1619 }
1620 mIconCache.updateDbIcons(packagesToIgnore);
1621 }
1622
Joe Onorato36115782010-06-17 13:28:48 -04001623 private void onlyBindAllApps() {
1624 final Callbacks oldCallbacks = mCallbacks.get();
1625 if (oldCallbacks == null) {
1626 // This launcher has exited and nobody bothered to tell us. Just bail.
1627 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1628 return;
1629 }
1630
1631 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07001632 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001633 final ArrayList<AppInfo> list
1634 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07001635 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001636 public void run() {
1637 final long t = SystemClock.uptimeMillis();
1638 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1639 if (callbacks != null) {
1640 callbacks.bindAllApplications(list);
1641 }
1642 if (DEBUG_LOADERS) {
1643 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08001644 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04001645 }
1646 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07001647 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001648 mUiExecutor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04001649 }
1650
Winson Chung64359a52013-07-08 17:17:08 -07001651 private void loadAllApps() {
1652 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04001653
Joe Onorato36115782010-06-17 13:28:48 -04001654 final Callbacks oldCallbacks = mCallbacks.get();
1655 if (oldCallbacks == null) {
1656 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07001657 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04001658 return;
1659 }
1660
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001661 final List<UserHandle> profiles = mUserManager.getUserProfiles();
Kenny Guyed131872014-04-30 03:02:21 +01001662
Winson Chung64359a52013-07-08 17:17:08 -07001663 // Clear the list of apps
1664 mBgAllAppsList.clear();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001665 for (UserHandle user : profiles) {
Kenny Guyed131872014-04-30 03:02:21 +01001666 // Query for the set of apps
1667 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal3e9be432017-01-05 15:22:41 -08001668 final List<LauncherActivityInfo> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01001669 if (DEBUG_LOADERS) {
1670 Log.d(TAG, "getActivityList took "
1671 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
1672 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
1673 }
1674 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08001675 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01001676 if (apps == null || apps.isEmpty()) {
1677 return;
1678 }
Kenny Guyff05f432016-01-22 17:48:29 +00001679 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01001680 // Create the ApplicationInfos
1681 for (int i = 0; i < apps.size(); i++) {
Sunny Goyal3e9be432017-01-05 15:22:41 -08001682 LauncherActivityInfo app = apps.get(i);
Kenny Guyed131872014-04-30 03:02:21 +01001683 // This builds the icon bitmaps.
Sunny Goyal24bb66a2017-03-21 15:12:01 -07001684 mBgAllAppsList.add(new AppInfo(app, user, quietMode), app);
Kenny Guyed131872014-04-30 03:02:21 +01001685 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08001686
Sunny Goyal756a28a2015-04-23 17:07:55 -07001687 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
1688 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07001689 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07001690
1691 @Override
1692 public void run() {
1693 heuristic.processUserApps(apps);
1694 }
Sunny Goyal639e9062015-08-19 19:17:06 -07001695 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001696 mUiExecutor.execute(new Runnable() {
Sunny Goyal639e9062015-08-19 19:17:06 -07001697
Sunny Goyalb265ba72017-02-14 15:03:45 -08001698 @Override
1699 public void run() {
1700 // Check isLoadingWorkspace on the UI thread, as it is updated on
1701 // the UI thread.
1702 if (mIsLoadingAndBindingWorkspace) {
1703 synchronized (mBindCompleteRunnables) {
1704 mBindCompleteRunnables.add(r);
1705 }
1706 } else {
1707 runOnWorkerThread(r);
1708 }
1709 }
1710 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08001711 }
Winson Chung64359a52013-07-08 17:17:08 -07001712 }
Mario Bertschler817afa32017-03-15 11:56:47 -07001713
1714 if (FeatureFlags.LAUNCHER3_PROMISE_APPS_IN_ALL_APPS) {
1715 // get all active sessions and add them to the all apps list
1716 PackageInstallerCompat installer = PackageInstallerCompat.getInstance(mContext);
1717 for (PackageInstaller.SessionInfo info : installer.getAllVerifiedSessions()) {
1718 mBgAllAppsList.addPromiseApp(mContext,
1719 PackageInstallInfo.fromInstallingState(info));
1720 }
1721 }
1722
Bjorn Bringert85f418d2013-09-06 12:50:05 +01001723 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001724 final ArrayList<AppInfo> added = mBgAllAppsList.added;
1725 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07001726
Mario Bertschler817afa32017-03-15 11:56:47 -07001727
Winson Chung64359a52013-07-08 17:17:08 -07001728 // Post callback on main thread
Sunny Goyalb265ba72017-02-14 15:03:45 -08001729 mUiExecutor.execute(new Runnable() {
Winson Chung64359a52013-07-08 17:17:08 -07001730 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07001731
Winson Chung64359a52013-07-08 17:17:08 -07001732 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07001733 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07001734 if (callbacks != null) {
1735 callbacks.bindAllApplications(added);
1736 if (DEBUG_LOADERS) {
1737 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001738 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07001739 }
1740 } else {
1741 Log.i(TAG, "not binding apps: no Launcher activity");
1742 }
1743 }
1744 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07001745 // Cleanup any data stored for a deleted user.
1746 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04001747 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07001748 Log.d(TAG, "Icons processed in "
1749 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08001750 }
1751 }
1752
Sunny Goyaldd96a5e2017-02-17 11:22:34 -08001753 private void loadDeepShortcuts() {
1754 if (!mModelLoaded) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07001755 sBgDataModel.deepShortcutMap.clear();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301756 DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(mContext);
1757 mHasShortcutHostPermission = shortcutManager.hasHostPermission();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001758 if (mHasShortcutHostPermission) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001759 for (UserHandle user : mUserManager.getUserProfiles()) {
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001760 if (mUserManager.isUserUnlocked(user)) {
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301761 List<ShortcutInfoCompat> shortcuts =
1762 shortcutManager.queryForAllShortcuts(user);
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07001763 sBgDataModel.updateDeepShortcutMap(null, user, shortcuts);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001764 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001765 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001766 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001767 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001768 }
Joe Onorato36115782010-06-17 13:28:48 -04001769 }
1770
Tony Wickham80f57872016-06-29 18:12:15 -07001771 public void bindDeepShortcuts() {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07001772 final MultiHashMap<ComponentKey, String> shortcutMapCopy =
1773 sBgDataModel.deepShortcutMap.clone();
Tony Wickham80f57872016-06-29 18:12:15 -07001774 Runnable r = new Runnable() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001775 @Override
1776 public void run() {
1777 Callbacks callbacks = getCallback();
1778 if (callbacks != null) {
1779 callbacks.bindDeepShortcutMap(shortcutMapCopy);
1780 }
1781 }
Tony Wickham80f57872016-06-29 18:12:15 -07001782 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001783 mUiExecutor.execute(r);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001784 }
1785
Sunny Goyal75b0f552015-05-20 21:57:06 -07001786 /**
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001787 * Refreshes the cached shortcuts if the shortcut permission has changed.
1788 * Current implementation simply reloads the workspace, but it can be optimized to
1789 * use partial updates similar to {@link UserManagerCompat}
1790 */
1791 public void refreshShortcutsIfRequired() {
Sunny Goyalf5e37442016-11-02 10:31:24 -07001792 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001793 sWorker.removeCallbacks(mShortcutPermissionCheckRunnable);
1794 sWorker.post(mShortcutPermissionCheckRunnable);
1795 }
1796 }
1797
1798 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -07001799 * Called when the icons for packages have been updated in the icon cache.
1800 */
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001801 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandle user) {
Sunny Goyal75b0f552015-05-20 21:57:06 -07001802 // If any package icon has changed (app was updated while launcher was dead),
1803 // update the corresponding shortcuts.
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001804 enqueueModelUpdateTask(new CacheDataUpdatedTask(
1805 CacheDataUpdatedTask.OP_CACHE_UPDATE, user, updatedPackages));
Sunny Goyal75b0f552015-05-20 21:57:06 -07001806 }
1807
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001808 void enqueueModelUpdateTask(BaseModelUpdateTask task) {
Jon Mirandaf28629a2017-02-23 15:12:42 -08001809 if (!mModelLoaded && mLoaderTask == null) {
1810 if (DEBUG_LOADERS) {
1811 Log.d(TAG, "enqueueModelUpdateTask Ignoring task since loader is pending=" + task);
1812 }
1813 return;
1814 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001815 task.init(this);
1816 runOnWorkerThread(task);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001817 }
1818
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001819 /**
1820 * A task to be executed on the current callbacks on the UI thread.
1821 * If there is no current callbacks, the task is ignored.
1822 */
1823 public interface CallbackTask {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001824
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001825 void execute(Callbacks callbacks);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001826 }
1827
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001828 /**
1829 * A runnable which changes/updates the data model of the launcher based on certain events.
1830 */
1831 public static abstract class BaseModelUpdateTask implements Runnable {
Joe Onorato36115782010-06-17 13:28:48 -04001832
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001833 private LauncherModel mModel;
Sunny Goyalb265ba72017-02-14 15:03:45 -08001834 private Executor mUiExecutor;
Joe Onorato36115782010-06-17 13:28:48 -04001835
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001836 /* package private */
1837 void init(LauncherModel model) {
1838 mModel = model;
Sunny Goyalb265ba72017-02-14 15:03:45 -08001839 mUiExecutor = mModel.mUiExecutor;
Joe Onorato36115782010-06-17 13:28:48 -04001840 }
1841
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001842 @Override
Joe Onorato36115782010-06-17 13:28:48 -04001843 public void run() {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001844 if (!mModel.mHasLoaderCompletedOnce) {
Sunny Goyalc905efc2015-05-06 09:54:53 -07001845 // Loader has not yet run.
1846 return;
1847 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001848 execute(mModel.mApp, sBgDataModel, mModel.mBgAllAppsList);
1849 }
Joe Onorato36115782010-06-17 13:28:48 -04001850
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001851 /**
1852 * Execute the actual task. Called on the worker thread.
1853 */
1854 public abstract void execute(
1855 LauncherAppState app, BgDataModel dataModel, AllAppsList apps);
Sunny Goyale0f58d72014-11-10 18:05:31 -08001856
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001857 /**
1858 * Schedules a {@param task} to be executed on the current callbacks.
1859 */
1860 public final void scheduleCallbackTask(final CallbackTask task) {
1861 final Callbacks callbacks = mModel.getCallback();
Sunny Goyalb265ba72017-02-14 15:03:45 -08001862 mUiExecutor.execute(new Runnable() {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001863 public void run() {
1864 Callbacks cb = mModel.getCallback();
1865 if (callbacks == cb && cb != null) {
1866 task.execute(callbacks);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07001867 }
1868 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001869 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001870 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001871
1872 public ModelWriter getModelWriter() {
1873 // Updates from model task, do not deal with icon position in hotseat.
1874 return mModel.getWriter(false /* hasVerticalHotseat */);
1875 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001876 }
1877
Sunny Goyal1b072632017-01-18 11:30:23 -08001878 public void updateAndBindShortcutInfo(final ShortcutInfo si, final ShortcutInfoCompat info) {
1879 updateAndBindShortcutInfo(new Provider<ShortcutInfo>() {
1880 @Override
1881 public ShortcutInfo get() {
1882 si.updateFromDeepShortcutInfo(info, mApp.getContext());
1883 si.iconBitmap = LauncherIcons.createShortcutIcon(info, mApp.getContext());
1884 return si;
1885 }
1886 });
1887 }
1888
Sunny Goyal10923b32016-07-20 15:42:44 -07001889 /**
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001890 * Utility method to update a shortcut on the background thread.
Sunny Goyal10923b32016-07-20 15:42:44 -07001891 */
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001892 public void updateAndBindShortcutInfo(final Provider<ShortcutInfo> shortcutProvider) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001893 enqueueModelUpdateTask(new ExtendedModelTask() {
Sunny Goyal10923b32016-07-20 15:42:44 -07001894 @Override
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001895 public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001896 ShortcutInfo info = shortcutProvider.get();
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001897 ArrayList<ShortcutInfo> update = new ArrayList<>();
Sunny Goyal10923b32016-07-20 15:42:44 -07001898 update.add(info);
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001899 bindUpdatedShortcuts(update, info.user);
Sunny Goyal10923b32016-07-20 15:42:44 -07001900 }
1901 });
1902 }
1903
Sunny Goyald164b7f2016-10-12 20:49:31 -07001904 private void bindWidgetsModel(final Callbacks callbacks) {
1905 final MultiHashMap<PackageItemInfo, WidgetItem> widgets
1906 = mBgWidgetsModel.getWidgetsMap().clone();
Sunny Goyalb265ba72017-02-14 15:03:45 -08001907 mUiExecutor.execute(new Runnable() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001908 @Override
1909 public void run() {
1910 Callbacks cb = getCallback();
1911 if (callbacks == cb && cb != null) {
Sunny Goyald164b7f2016-10-12 20:49:31 -07001912 callbacks.bindAllWidgets(widgets);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001913 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07001914 }
1915 });
1916 }
1917
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001918 public void refreshAndBindWidgetsAndShortcuts(
1919 final Callbacks callbacks, final boolean bindFirst) {
1920 runOnWorkerThread(new Runnable() {
1921 @Override
1922 public void run() {
1923 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
Sunny Goyald164b7f2016-10-12 20:49:31 -07001924 bindWidgetsModel(callbacks);
Sunny Goyal31860582015-09-18 08:38:57 -07001925 }
Sunny Goyald164b7f2016-10-12 20:49:31 -07001926 ArrayList<WidgetItem> allWidgets = mBgWidgetsModel.update(mApp.getContext());
1927 bindWidgetsModel(callbacks);
1928
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001929 // update the Widget entries inside DB on the worker thread.
Sunny Goyal87f784c2017-01-11 10:48:34 -08001930 mApp.getWidgetCache().removeObsoletePreviews(allWidgets);
Sunny Goyal31860582015-09-18 08:38:57 -07001931 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001932 });
Michael Jurkac402cd92013-05-20 15:49:32 +02001933 }
1934
Sunny Goyal651077b2014-06-30 14:15:31 -07001935 static boolean isValidProvider(AppWidgetProviderInfo provider) {
1936 return (provider != null) && (provider.provider != null)
1937 && (provider.provider.getPackageName() != null);
1938 }
1939
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001940 public void dumpState(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
1941 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
1942 writer.println(prefix + "All apps list: size=" + mBgAllAppsList.data.size());
1943 for (AppInfo info : mBgAllAppsList.data) {
1944 writer.println(prefix + " title=\"" + info.title + "\" iconBitmap=" + info.iconBitmap
1945 + " componentName=" + info.componentName.getPackageName());
1946 }
Joe Onorato36115782010-06-17 13:28:48 -04001947 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001948 sBgDataModel.dump(prefix, fd, writer, args);
Joe Onoratobe386092009-11-17 17:32:16 -08001949 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08001950
1951 public Callbacks getCallback() {
1952 return mCallbacks != null ? mCallbacks.get() : null;
1953 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07001954
1955 /**
1956 * @return {@link FolderInfo} if its already loaded.
1957 */
1958 public FolderInfo findFolderById(Long folderId) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001959 synchronized (sBgDataModel) {
1960 return sBgDataModel.folders.get(folderId);
Sunny Goyal18bf8e22015-04-08 18:13:46 -07001961 }
1962 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07001963
1964 /**
1965 * @return the looper for the worker thread which can be used to start background tasks.
1966 */
1967 public static Looper getWorkerLooper() {
1968 return sWorkerThread.getLooper();
1969 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001970}