blob: 707ec8642d146be85d2e73fd997dd2b55923fd0c [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);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700198 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800199 ArrayList<ShortcutInfo> removed, UserHandle user);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700200 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700201 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700202 public void bindWorkspaceComponentsRemoved(
203 HashSet<String> packageNames, HashSet<ComponentName> components,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800204 UserHandle user);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700205 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800206 public void notifyWidgetProvidersChanged();
Sunny Goyald164b7f2016-10-12 20:49:31 -0700207 public void bindAllWidgets(MultiHashMap<PackageItemInfo, WidgetItem> widgets);
Adam Cohen1462de32012-07-24 22:34:36 -0700208 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700209 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700210 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMap);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400211 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530213 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800214 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400215 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100216 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Sunny Goyald164b7f2016-10-12 20:49:31 -0700217 mBgWidgetsModel = new WidgetsModel(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800218 mIconCache = iconCache;
219
Kenny Guyed131872014-04-30 03:02:21 +0100220 mLauncherApps = LauncherAppsCompat.getInstance(context);
221 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800222 }
223
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700224 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
225 * posted on the worker thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700226 private static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700227 if (sWorkerThread.getThreadId() == Process.myTid()) {
228 r.run();
229 } else {
230 // If we are not on the worker thread, then post to the worker handler
231 sWorker.post(r);
232 }
233 }
234
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700235 public void setPackageState(PackageInstallInfo installInfo) {
236 enqueueModelUpdateTask(new PackageInstallStateChangedTask(installInfo));
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500237 }
238
Sunny Goyal756adbc2015-04-16 15:20:51 -0700239 /**
240 * Updates the icons and label of all pending icons for the provided package name.
241 */
242 public void updateSessionDisplayInfo(final String packageName) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700243 HashSet<String> packages = new HashSet<>();
244 packages.add(packageName);
245 enqueueModelUpdateTask(new CacheDataUpdatedTask(
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800246 CacheDataUpdatedTask.OP_SESSION_UPDATE, Process.myUserHandle(), packages));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800247 }
248
249 /**
250 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800251 */
Sunny Goyal2bcbe132016-11-16 09:23:42 -0800252 public void addAndBindAddedWorkspaceItems(List<ItemInfo> workspaceApps) {
253 addAndBindAddedWorkspaceItems(Provider.of(workspaceApps));
254 }
255
256 /**
257 * Adds the provided items to the workspace.
258 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700259 public void addAndBindAddedWorkspaceItems(
Sunny Goyal2bcbe132016-11-16 09:23:42 -0800260 Provider<List<ItemInfo>> appsProvider) {
261 enqueueModelUpdateTask(new AddWorkspaceItemsTask(appsProvider));
Winson Chung64359a52013-07-08 17:17:08 -0700262 }
263
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800264 public ModelWriter getWriter(boolean hasVerticalHotseat) {
265 return new ModelWriter(mApp.getContext(), sBgDataModel, hasVerticalHotseat);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 }
267
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700268 static void checkItemInfoLocked(
269 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
Sunny Goyale9956a72016-09-01 17:24:47 -0700270 ItemInfo modelItem = sBgDataModel.itemsIdMap.get(itemId);
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700271 if (modelItem != null && item != modelItem) {
272 // check all the data is consistent
273 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
274 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
275 ShortcutInfo shortcut = (ShortcutInfo) item;
276 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
277 modelShortcut.intent.filterEquals(shortcut.intent) &&
278 modelShortcut.id == shortcut.id &&
279 modelShortcut.itemType == shortcut.itemType &&
280 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700281 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700282 modelShortcut.cellX == shortcut.cellX &&
283 modelShortcut.cellY == shortcut.cellY &&
284 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700285 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700286 // For all intents and purposes, this is the same object
287 return;
288 }
289 }
290
291 // the modelItem needs to match up perfectly with item if our model is
292 // to be consistent with the database-- for now, just require
293 // modelItem == item or the equality check above
294 String msg = "item: " + ((item != null) ? item.toString() : "null") +
295 "modelItem: " +
296 ((modelItem != null) ? modelItem.toString() : "null") +
297 "Error: ItemInfo passed to checkItemInfo doesn't match original";
298 RuntimeException e = new RuntimeException(msg);
299 if (stackTrace != null) {
300 e.setStackTrace(stackTrace);
301 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800302 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700303 }
304 }
305
Michael Jurka816474f2012-06-25 14:49:02 -0700306 static void checkItemInfo(final ItemInfo item) {
307 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
308 final long itemId = item.id;
309 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700310 public void run() {
Sunny Goyale9956a72016-09-01 17:24:47 -0700311 synchronized (sBgDataModel) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700312 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700313 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700314 }
315 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700316 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700317 }
318
Joe Onorato9c1289c2009-08-17 11:03:03 -0400319 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700320 * Update the order of the workspace screens in the database. The array list contains
321 * a list of screen ids in the order that they should appear.
322 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700323 public static void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -0700324 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700325 final ContentResolver cr = context.getContentResolver();
326 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
327
328 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700329 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700330 while (iter.hasNext()) {
331 long id = iter.next();
332 if (id < 0) {
333 iter.remove();
334 }
335 }
336
337 Runnable r = new Runnable() {
338 @Override
339 public void run() {
Yura085c8532014-02-11 15:15:29 +0000340 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700341 // Clear the table
Yura085c8532014-02-11 15:15:29 +0000342 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -0700343 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700344 for (int i = 0; i < count; i++) {
345 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700346 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700347 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
348 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +0000349 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -0700350 }
Yura085c8532014-02-11 15:15:29 +0000351
352 try {
353 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
354 } catch (Exception ex) {
355 throw new RuntimeException(ex);
356 }
Winson Chung9e6a0a22013-08-27 11:58:12 -0700357
Sunny Goyale9956a72016-09-01 17:24:47 -0700358 synchronized (sBgDataModel) {
359 sBgDataModel.workspaceScreens.clear();
360 sBgDataModel.workspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -0700361 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700362 }
363 };
364 runOnWorkerThread(r);
365 }
366
367 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400368 * Set this as the current Launcher activity object for the loader.
369 */
370 public void initialize(Callbacks callbacks) {
371 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -0700372 Preconditions.assertUIThread();
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -0700373 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400374 }
375 }
376
Kenny Guyed131872014-04-30 03:02:21 +0100377 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800378 public void onPackageChanged(String packageName, UserHandle user) {
Kenny Guyed131872014-04-30 03:02:21 +0100379 int op = PackageUpdatedTask.OP_UPDATE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700380 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100381 }
382
383 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800384 public void onPackageRemoved(String packageName, UserHandle user) {
Sunny Goyalc2936bc2016-09-01 15:50:36 -0700385 onPackagesRemoved(user, packageName);
386 }
387
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800388 public void onPackagesRemoved(UserHandle user, String... packages) {
Kenny Guyed131872014-04-30 03:02:21 +0100389 int op = PackageUpdatedTask.OP_REMOVE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700390 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packages));
Kenny Guyed131872014-04-30 03:02:21 +0100391 }
392
393 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800394 public void onPackageAdded(String packageName, UserHandle user) {
Kenny Guyed131872014-04-30 03:02:21 +0100395 int op = PackageUpdatedTask.OP_ADD;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700396 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100397 }
398
399 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800400 public void onPackagesAvailable(String[] packageNames, UserHandle user,
Kenny Guyed131872014-04-30 03:02:21 +0100401 boolean replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700402 enqueueModelUpdateTask(
403 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100404 }
405
406 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800407 public void onPackagesUnavailable(String[] packageNames, UserHandle user,
Kenny Guyed131872014-04-30 03:02:21 +0100408 boolean replacing) {
409 if (!replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700410 enqueueModelUpdateTask(new PackageUpdatedTask(
411 PackageUpdatedTask.OP_UNAVAILABLE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100412 }
Kenny Guyed131872014-04-30 03:02:21 +0100413 }
414
Kenny Guy44cba692016-01-21 19:50:02 +0000415 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800416 public void onPackagesSuspended(String[] packageNames, UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700417 enqueueModelUpdateTask(new PackageUpdatedTask(
418 PackageUpdatedTask.OP_SUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000419 }
420
421 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800422 public void onPackagesUnsuspended(String[] packageNames, UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700423 enqueueModelUpdateTask(new PackageUpdatedTask(
424 PackageUpdatedTask.OP_UNSUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000425 }
426
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700427 @Override
428 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800429 UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700430 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, true));
Sunny Goyal50941fb2016-08-04 12:03:52 -0700431 }
432
433 public void updatePinnedShortcuts(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800434 UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700435 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, false));
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700436 }
437
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700438 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400439 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
440 * ACTION_PACKAGE_CHANGED.
441 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100442 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400443 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -0400444 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700445
Joe Onorato36115782010-06-17 13:28:48 -0400446 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +0100447 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -0700448 // If we have changed locale we need to clear out the labels in all apps/workspace.
449 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700450 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
451 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -0700452 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -0700453 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700454 } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
455 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
456 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800457 UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER);
Sunny Goyalda891c12016-03-18 18:29:24 -0700458 if (user != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700459 if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
460 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700461 enqueueModelUpdateTask(new PackageUpdatedTask(
462 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE, user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700463 }
464
465 // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so
466 // we need to run the state change task again.
467 if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
468 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700469 enqueueModelUpdateTask(new UserLockStateChangedTask(user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700470 }
Sunny Goyalda891c12016-03-18 18:29:24 -0700471 }
Tony Wickham827cef22016-03-17 15:39:39 -0700472 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
473 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700474 }
475 }
476
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800477 /**
478 * Reloads the workspace items from the DB and re-binds the workspace. This should generally
479 * not be called as DB updates are automatically followed by UI update
480 */
481 public void forceReload() {
482 synchronized (mLock) {
483 // Stop any existing loaders first, so they don't set mModelLoaded to true later
484 stopLoaderLocked();
485 mModelLoaded = false;
486 }
Winson Chungf0c6ae02012-03-21 16:10:31 -0700487
Reena Lee93f824a2011-09-23 17:20:28 -0700488 // Do this here because if the launcher activity is running it will be restarted.
489 // If it's not running startLoaderFromBackground will merely tell it that it needs
490 // to reload.
491 startLoaderFromBackground();
492 }
493
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700494 /**
495 * When the launcher is in the background, it's possible for it to miss paired
496 * configuration changes. So whenever we trigger the loader from the background
497 * tell the launcher that it needs to re-run the loader when it comes back instead
498 * of doing it now.
499 */
500 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800501 Callbacks callbacks = getCallback();
502 if (callbacks != null) {
503 // Only actually run the loader if they're not paused.
504 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -0700505 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700506 }
507 }
Joe Onorato36115782010-06-17 13:28:48 -0400508 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400509
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700510 /**
511 * If there is already a loader task running, tell it to stop.
512 */
513 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -0700514 LoaderTask oldTask = mLoaderTask;
515 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -0700516 oldTask.stopLocked();
517 }
Reena Lee93f824a2011-09-23 17:20:28 -0700518 }
519
Adam Cohen1a85c582014-09-30 09:48:49 -0700520 public boolean isCurrentCallbacks(Callbacks callbacks) {
521 return (mCallbacks != null && mCallbacks.get() == callbacks);
522 }
523
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700524 /**
525 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
526 * @return true if the page could be bound synchronously.
527 */
528 public boolean startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700529 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
530 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -0400531 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -0400532 // Don't bother to start the thread if we know it's not going to do anything
533 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -0700534 final Callbacks oldCallbacks = mCallbacks.get();
535 // Clear any pending bind-runnables from the synchronized load process.
Sunny Goyalb265ba72017-02-14 15:03:45 -0800536 mUiExecutor.execute(new Runnable() {
537 public void run() {
538 oldCallbacks.clearPendingBinds();
539 }
540 });
Sunny Goyal527c7d32015-08-28 15:19:36 -0700541
Joe Onorato36115782010-06-17 13:28:48 -0400542 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700543 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700544 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800545 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
546 && mModelLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700547 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700548 return true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700549 } else {
550 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
551 sWorker.post(mLoaderTask);
552 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400553 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400554 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700555 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400556 }
557
Joe Onorato36115782010-06-17 13:28:48 -0400558 public void stopLoader() {
559 synchronized (mLock) {
560 if (mLoaderTask != null) {
561 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400562 }
563 }
Joe Onorato36115782010-06-17 13:28:48 -0400564 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400565
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800566 /**
567 * Loads the workspace screen ids in an ordered list.
568 */
Sunny Goyale5bb7052015-07-27 14:36:07 -0700569 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -0700570 final ContentResolver contentResolver = context.getContentResolver();
571 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -0700572
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800573 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -0700574 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
575 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -0700576 }
577
Mario Bertschler817afa32017-03-15 11:56:47 -0700578 public void onInstallSessionCreated(final PackageInstallInfo sessionInfo) {
579 enqueueModelUpdateTask(new ExtendedModelTask() {
580 @Override
581 public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
582 apps.addPromiseApp(app.getContext(), sessionInfo);
583 if (!apps.added.isEmpty()) {
584 final ArrayList<AppInfo> arrayList = new ArrayList<>(apps.added);
585 apps.added.clear();
586 scheduleCallbackTask(new CallbackTask() {
587 @Override
588 public void execute(Callbacks callbacks) {
589 callbacks.bindAppsAdded(null, null, null, arrayList);
590 }
591 });
592 }
593 }
594 });
595 }
596
Joe Onorato36115782010-06-17 13:28:48 -0400597 /**
598 * Runnable for the thread that loads the contents of the launcher:
599 * - workspace icons
600 * - widgets
601 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700602 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -0400603 */
604 private class LoaderTask implements Runnable {
605 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -0700606 private int mPageToBindFirst;
607
Adam Cohen091440a2015-03-18 14:16:05 -0700608 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -0400609 private boolean mStopped;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700610
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700611 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -0400612 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -0700613 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400614 }
615
Joe Onorato36115782010-06-17 13:28:48 -0400616 private void waitForIdle() {
617 // Wait until the either we're stopped or the other threads are done.
618 // This way we don't start loading all apps until the workspace has settled
619 // down.
620 synchronized (LoaderTask.this) {
Sunny Goyalb265ba72017-02-14 15:03:45 -0800621 LooperIdleLock idleLock = new LooperIdleLock(this, Looper.getMainLooper());
622 // Just in case mFlushingWorkerThread changes but we aren't woken up,
623 // wait no longer than 1sec at a time
624 while (!mStopped && idleLock.awaitLocked(1000));
Daniel Sandler843e8602010-06-07 14:59:01 -0400625 }
Joe Onorato36115782010-06-17 13:28:48 -0400626 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400627
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700628 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -0500629 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700630 // Ensure that we have a valid page index to load synchronously
631 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
632 "valid page index");
633 }
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800634 if (!mModelLoaded) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700635 // Ensure that we don't try and bind a specified page when the pages have not been
636 // loaded already (we should load everything asynchronously in that case)
637 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
638 }
639 synchronized (mLock) {
640 if (mIsLoaderTaskRunning) {
641 // Ensure that we are never running the background loading at this point since
642 // we also touch the background collections
643 throw new RuntimeException("Error! Background loading is already running");
644 }
645 }
646
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700647 // Divide the set of loaded items into those that we are binding synchronously, and
648 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800649 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700650 // XXX: For now, continue posting the binding of AllApps as there are other issues that
651 // arise from that.
652 onlyBindAllApps();
Tony Wickham80f57872016-06-29 18:12:15 -0700653
654 bindDeepShortcuts();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700655 }
656
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800657 private void verifyNotStopped() throws CancellationException {
658 synchronized (LoaderTask.this) {
659 if (mStopped) {
660 throw new CancellationException("Loader stopped");
661 }
662 }
663 }
664
Joe Onorato36115782010-06-17 13:28:48 -0400665 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700666 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -0700667 if (mStopped) {
668 return;
669 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700670 mIsLoaderTaskRunning = true;
671 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400672
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800673 try {
Sunny Goyalb265ba72017-02-14 15:03:45 -0800674 long now = 0;
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800675 if (DEBUG_LOADERS) Log.d(TAG, "step 1.1: loading workspace");
676 // Set to false in bindWorkspace()
677 mIsLoadingAndBindingWorkspace = true;
678 loadWorkspace();
Joe Onorato36115782010-06-17 13:28:48 -0400679
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800680 verifyNotStopped();
681 if (DEBUG_LOADERS) Log.d(TAG, "step 1.2: bind workspace workspace");
682 bindWorkspace(mPageToBindFirst);
683
684 // Take a break
Sunny Goyalb265ba72017-02-14 15:03:45 -0800685 if (DEBUG_LOADERS) {
686 Log.d(TAG, "step 1 completed, wait for idle");
687 now = SystemClock.uptimeMillis();
688 }
Joe Onorato36115782010-06-17 13:28:48 -0400689 waitForIdle();
Sunny Goyalb265ba72017-02-14 15:03:45 -0800690 if (DEBUG_LOADERS) Log.d(TAG, "Waited " + (SystemClock.uptimeMillis() - now) + "ms");
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800691 verifyNotStopped();
Daniel Sandler843e8602010-06-07 14:59:01 -0400692
693 // second step
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800694 if (DEBUG_LOADERS) Log.d(TAG, "step 2.1: loading all apps");
695 loadAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700696
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800697 verifyNotStopped();
698 if (DEBUG_LOADERS) Log.d(TAG, "step 2.2: Update icon cache");
699 updateIconCache();
700
701 // Take a break
Sunny Goyalb265ba72017-02-14 15:03:45 -0800702 if (DEBUG_LOADERS) {
703 Log.d(TAG, "step 2 completed, wait for idle");
704 now = SystemClock.uptimeMillis();
705 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700706 waitForIdle();
Sunny Goyalb265ba72017-02-14 15:03:45 -0800707 if (DEBUG_LOADERS) Log.d(TAG, "Waited " + (SystemClock.uptimeMillis() - now) + "ms");
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800708 verifyNotStopped();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700709
710 // third step
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800711 if (DEBUG_LOADERS) Log.d(TAG, "step 3.1: loading deep shortcuts");
712 loadDeepShortcuts();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400713
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800714 verifyNotStopped();
715 if (DEBUG_LOADERS) Log.d(TAG, "step 3.2: bind deep shortcuts");
716 bindDeepShortcuts();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400717
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800718 synchronized (mLock) {
719 // Everything loaded bind the data.
720 mModelLoaded = true;
721 mHasLoaderCompletedOnce = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400722 }
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800723 } catch (CancellationException e) {
724 // Loader stopped, ignore
725 } finally {
726 // Clear out this reference, otherwise we end up holding it until all of the
727 // callback runnables are done.
728 mContext = null;
729
730 synchronized (mLock) {
731 // If we are still the last one to be scheduled, remove ourselves.
732 if (mLoaderTask == this) {
733 mLoaderTask = null;
734 }
735 mIsLoaderTaskRunning = false;
736 }
Joe Onorato36115782010-06-17 13:28:48 -0400737 }
Joe Onorato36115782010-06-17 13:28:48 -0400738 }
739
740 public void stopLocked() {
741 synchronized (LoaderTask.this) {
742 mStopped = true;
743 this.notify();
744 }
745 }
746
747 /**
748 * Gets the callbacks object. If we've been stopped, or if the launcher object
749 * has somehow been garbage collected, return null instead. Pass in the Callbacks
750 * object that was around when the deferred message was scheduled, and if there's
751 * a new Callbacks object around then also return null. This will save us from
752 * calling onto it with data that will be ignored.
753 */
754 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
755 synchronized (mLock) {
756 if (mStopped) {
757 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400758 }
Joe Onorato36115782010-06-17 13:28:48 -0400759
760 if (mCallbacks == null) {
761 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400762 }
Joe Onorato36115782010-06-17 13:28:48 -0400763
764 final Callbacks callbacks = mCallbacks.get();
765 if (callbacks != oldCallbacks) {
766 return null;
767 }
768 if (callbacks == null) {
769 Log.w(TAG, "no mCallbacks");
770 return null;
771 }
772
773 return callbacks;
774 }
775 }
776
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800777 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -0700778 if (LauncherAppState.PROFILE_STARTUP) {
779 Trace.beginSection("Loading Workspace");
780 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400781
Joe Onorato36115782010-06-17 13:28:48 -0400782 final Context context = mContext;
783 final ContentResolver contentResolver = context.getContentResolver();
Sunny Goyal342e4662017-02-02 15:21:08 -0800784 final PackageManagerHelper pmHelper = new PackageManagerHelper(context);
785 final boolean isSafeMode = pmHelper.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700786 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530787 final DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -0700788 final boolean isSdCardReady = Utilities.isBootCompleted();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800789 final MultiHashMap<UserHandle, String> pendingPackages = new MultiHashMap<>();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400790
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700791 boolean clearDb = false;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -0700792 try {
793 ImportDataTask.performImportIfPossible(context);
794 } catch (Exception e) {
795 // Migration failed. Clear workspace.
796 clearDb = true;
797 }
798
799 if (!clearDb && GridSizeMigrationTask.ENABLED &&
Sunny Goyalf076eae2016-01-11 12:25:10 -0800800 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
801 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700802 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -0700803 }
804
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700805 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -0700806 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -0700807 LauncherSettings.Settings.call(contentResolver,
Sunny Goyale05b08f2017-02-23 18:30:22 -0800808 LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -0500809 }
810
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700811 Log.d(TAG, "loadWorkspace: loading default favorites");
812 LauncherSettings.Settings.call(contentResolver,
813 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -0700814
Sunny Goyale9956a72016-09-01 17:24:47 -0700815 synchronized (sBgDataModel) {
816 sBgDataModel.clear();
817
Sunny Goyal756adbc2015-04-16 15:20:51 -0700818 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -0700819 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyale9956a72016-09-01 17:24:47 -0700820 sBgDataModel.workspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -0800821
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700822 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800823 final LoaderCursor c = new LoaderCursor(contentResolver.query(
824 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null), mApp);
Daniel Sandler8802e962010-05-26 16:28:16 -0400825
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800826 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400827
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700828 try {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700829 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
830 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -0400831 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
832 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700833 final int spanXIndex = c.getColumnIndexOrThrow
834 (LauncherSettings.Favorites.SPANX);
835 final int spanYIndex = c.getColumnIndexOrThrow(
836 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800837 final int rankIndex = c.getColumnIndexOrThrow(
838 LauncherSettings.Favorites.RANK);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700839 final int optionsIndex = c.getColumnIndexOrThrow(
840 LauncherSettings.Favorites.OPTIONS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400841
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800842 final LongSparseArray<UserHandle> allUsers = c.allUsers;
Kenny Guyff05f432016-01-22 17:48:29 +0000843 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700844 final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800845 for (UserHandle user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +0000846 long serialNo = mUserManager.getSerialNumberForUser(user);
847 allUsers.put(serialNo, user);
848 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700849
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700850 boolean userUnlocked = mUserManager.isUserUnlocked(user);
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700851
852 // We can only query for shortcuts when the user is unlocked.
853 if (userUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -0700854 List<ShortcutInfoCompat> pinnedShortcuts =
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530855 shortcutManager.queryForPinnedShortcuts(null, user);
856 if (shortcutManager.wasLastCallSuccess()) {
Sunny Goyal49f4f032016-08-01 15:45:49 -0700857 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
858 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
859 shortcut);
860 }
861 } else {
862 // Shortcut manager can fail due to some race condition when the
863 // lock state changes too frequently. For the purpose of the loading
864 // shortcuts, consider the user is still locked.
865 userUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700866 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700867 }
Sunny Goyal49f4f032016-08-01 15:45:49 -0700868 unlockedUsers.put(serialNo, userUnlocked);
Sunny Goyal7f834d22015-04-21 10:10:23 -0700869 }
870
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700871 ShortcutInfo info;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700872 LauncherAppWidgetInfo appWidgetInfo;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700873 Intent intent;
Sunny Goyal81e44912017-01-14 15:05:14 -0800874 String targetPkg;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400875
Jon Miranda529af302017-02-28 14:18:54 -0800876 FolderIconPreviewVerifier verifier =
877 new FolderIconPreviewVerifier(mApp.getInvariantDeviceProfile());
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700878 while (!mStopped && c.moveToNext()) {
879 try {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800880 if (c.user == null) {
881 // User has been deleted, remove the item.
882 c.markDeleted("User has been deleted");
883 continue;
884 }
885
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700886 boolean allowMissingTarget = false;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800887 switch (c.itemType) {
Sunny Goyal81e44912017-01-14 15:05:14 -0800888 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700889 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700890 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Sunny Goyal81e44912017-01-14 15:05:14 -0800891 intent = c.parseIntent();
892 if (intent == null) {
893 c.markDeleted("Invalid or null intent");
894 continue;
895 }
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800896
Sunny Goyal81e44912017-01-14 15:05:14 -0800897 int disabledState = quietMode.get(c.serialNumber) ?
898 ShortcutInfo.FLAG_DISABLED_QUIET_USER : 0;
899 ComponentName cn = intent.getComponent();
900 targetPkg = cn == null ? intent.getPackage() : cn.getPackageName();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700901
Sunny Goyal81e44912017-01-14 15:05:14 -0800902 if (!Process.myUserHandle().equals(c.user)) {
903 if (c.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
904 c.markDeleted("Legacy shortcuts are only allowed for default user");
905 continue;
906 } else if (c.restoreFlag != 0) {
907 // Don't restore items for other profiles.
908 c.markDeleted("Restore from managed profile not supported");
909 continue;
910 }
911 }
912 if (TextUtils.isEmpty(targetPkg) &&
913 c.itemType != LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
914 c.markDeleted("Only legacy shortcuts can have null package");
915 continue;
916 }
Sunny Goyal34942622014-08-29 17:20:55 -0700917
Sunny Goyal81e44912017-01-14 15:05:14 -0800918 // If there is no target package, its an implicit intent
919 // (legacy shortcut) which is always valid
920 boolean validTarget = TextUtils.isEmpty(targetPkg) ||
921 launcherApps.isPackageEnabledForProfile(targetPkg, c.user);
Sunny Goyal94485362014-09-18 16:13:58 -0700922
Sunny Goyal81e44912017-01-14 15:05:14 -0800923 if (cn != null && validTarget) {
924 // If the apk is present and the shortcut points to a specific
925 // component.
926
927 // If the component is already present
928 if (launcherApps.isActivityEnabledForProfile(cn, c.user)) {
929 // no special handling necessary for this item
930 c.markRestored();
931 } else {
932 if (c.hasRestoreFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
933 // We allow auto install apps to have their intent
934 // updated after an install.
Sunny Goyal342e4662017-02-02 15:21:08 -0800935 intent = pmHelper.getAppLaunchIntent(targetPkg, c.user);
Sunny Goyal81e44912017-01-14 15:05:14 -0800936 if (intent != null) {
937 c.restoreFlag = 0;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800938 c.updater().put(
Sunny Goyal81e44912017-01-14 15:05:14 -0800939 LauncherSettings.Favorites.INTENT,
940 intent.toUri(0)).commit();
941 cn = intent.getComponent();
Sunny Goyalb05a00a2016-08-29 10:06:57 -0700942 } else {
Sunny Goyal81e44912017-01-14 15:05:14 -0800943 c.markDeleted("Unable to find a launch target");
Sunny Goyal94485362014-09-18 16:13:58 -0700944 continue;
945 }
Sunny Goyal1a745e82014-10-02 15:58:31 -0700946 } else {
Sunny Goyal81e44912017-01-14 15:05:14 -0800947 // The app is installed but the component is no
948 // longer available.
949 c.markDeleted("Invalid component removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -0700950 continue;
Winson Chungee055712013-07-30 14:46:24 -0700951 }
Winson Chungee055712013-07-30 14:46:24 -0700952 }
Sunny Goyal81e44912017-01-14 15:05:14 -0800953 }
954 // else if cn == null => can't infer much, leave it
955 // else if !validPkg => could be restored icon or missing sd-card
956
957 if (!TextUtils.isEmpty(targetPkg) && !validTarget) {
958 // Points to a valid app (superset of cn != null) but the apk
959 // is not available.
960
961 if (c.restoreFlag != 0) {
962 // Package is not yet available but might be
963 // installed later.
964 FileLog.d(TAG, "package not yet restored: " + targetPkg);
965
966 if (c.hasRestoreFlag(ShortcutInfo.FLAG_RESTORE_STARTED)) {
967 // Restore has started once.
968 } else if (installingPkgs.containsKey(targetPkg)) {
969 // App restore has started. Update the flag
970 c.restoreFlag |= ShortcutInfo.FLAG_RESTORE_STARTED;
971 c.updater().commit();
972 } else {
973 c.markDeleted("Unrestored app removed: " + targetPkg);
974 continue;
975 }
Sunny Goyalad2e91a2017-02-28 15:14:58 -0800976 } else if (pmHelper.isAppOnSdcard(targetPkg, c.user)) {
Sunny Goyal81e44912017-01-14 15:05:14 -0800977 // Package is present but not available.
978 disabledState |= ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
979 // Add the icon on the workspace anyway.
980 allowMissingTarget = true;
981 } else if (!isSdCardReady) {
982 // SdCard is not ready yet. Package might get available,
983 // once it is ready.
984 Log.d(TAG, "Missing pkg, will check later: " + targetPkg);
985 pendingPackages.addToList(c.user, targetPkg);
986 // Add the icon on the workspace anyway.
987 allowMissingTarget = true;
988 } else {
989 // Do not wait for external media load anymore.
990 c.markDeleted("Invalid package removed: " + targetPkg);
991 continue;
992 }
993 }
994
995 if (validTarget) {
996 // The shortcut points to a valid target (either no target
997 // or something which is ready to be used)
998 c.markRestored();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700999 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001000
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001001 boolean useLowResIcon = !c.isOnWorkspaceOrHotseat() &&
Jon Miranda529af302017-02-28 14:18:54 -08001002 !verifier.isItemInPreview(c.getInt(rankIndex));
Sunny Goyal34b65272015-03-11 16:56:52 -07001003
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001004 if (c.restoreFlag != 0) {
Sunny Goyal81e44912017-01-14 15:05:14 -08001005 // Already verified above that user is same as default user
1006 info = c.getRestoredItemInfo(intent);
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001007 } else if (c.itemType ==
Chris Wrenf4d08112014-01-16 18:13:56 -05001008 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001009 info = c.getAppShortcutInfo(
1010 intent, allowMissingTarget, useLowResIcon);
1011 } else if (c.itemType ==
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001012 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001013
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001014 ShortcutKey key = ShortcutKey.fromIntent(intent, c.user);
1015 if (unlockedUsers.get(c.serialNumber)) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001016 ShortcutInfoCompat pinnedShortcut =
1017 shortcutKeyToPinnedShortcuts.get(key);
1018 if (pinnedShortcut == null) {
1019 // The shortcut is no longer valid.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001020 c.markDeleted("Pinned shortcut not found");
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001021 continue;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001022 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001023 info = new ShortcutInfo(pinnedShortcut, context);
Sunny Goyal1b072632017-01-18 11:30:23 -08001024 info.iconBitmap = LauncherIcons
1025 .createShortcutIcon(pinnedShortcut, context);
Charles He3ff90472017-02-15 12:22:56 +00001026 if (pmHelper.isAppSuspended(
Sunny Goyalad2e91a2017-02-28 15:14:58 -08001027 pinnedShortcut.getPackage(), info.user)) {
Charles He3ff90472017-02-15 12:22:56 +00001028 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1029 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001030 intent = info.intent;
1031 } else {
1032 // Create a shortcut info in disabled mode for now.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001033 info = c.loadSimpleShortcut();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001034 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001035 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001036 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001037 info = c.loadSimpleShortcut();
Michael Jurka96879562012-03-22 05:54:33 -07001038
Sunny Goyald09c3702016-04-06 16:18:20 -07001039 // Shortcuts are only available on the primary profile
Sunny Goyalad2e91a2017-02-28 15:14:58 -08001040 if (pmHelper.isAppSuspended(targetPkg, c.user)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001041 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1042 }
1043
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001044 // App shortcuts that used to be automatically added to Launcher
1045 // didn't always have the correct intent flags set, so do that
1046 // here
1047 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001048 intent.getCategories() != null &&
1049 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001050 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001051 intent.addFlags(
1052 Intent.FLAG_ACTIVITY_NEW_TASK |
1053 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1054 }
Michael Jurka96879562012-03-22 05:54:33 -07001055 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001056
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001057 if (info != null) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001058 c.applyCommonProperties(info);
1059
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001060 info.intent = intent;
Sunny Goyal08f72612015-01-05 13:41:43 -08001061 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001062 info.spanX = 1;
1063 info.spanY = 1;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001064 // TODO: Remove this extra. Instead we should be using
1065 // itemInfo#user.
1066 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, c.serialNumber);
Sunny Goyald09c3702016-04-06 16:18:20 -07001067 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001068 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1069 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1070 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001071
Sunny Goyal81e44912017-01-14 15:05:14 -08001072 if (c.restoreFlag != 0 && !TextUtils.isEmpty(targetPkg)) {
1073 Integer progress = installingPkgs.get(targetPkg);
1074 if (progress != null) {
1075 info.setInstallProgress(progress);
1076 } else {
1077 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001078 }
1079 }
1080
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001081 c.checkAndAddItem(info, sBgDataModel);
Winson Chung1323b482013-08-05 12:41:55 -07001082 } else {
1083 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001084 }
1085 break;
1086
1087 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001088 FolderInfo folderInfo = sBgDataModel.findOrMakeFolder(c.id);
1089 c.applyCommonProperties(folderInfo);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001090
Sunny Goyala508e4f2015-05-21 09:33:57 -07001091 // Do not trim the folder label, as is was set by the user.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001092 folderInfo.title = c.getString(c.titleIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001093 folderInfo.spanX = 1;
1094 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001095 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001096
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001097 // no special handling required for restored folders
1098 c.markRestored();
Chris Wrenf4d08112014-01-16 18:13:56 -05001099
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001100 c.checkAndAddItem(folderInfo, sBgDataModel);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001101 break;
1102
1103 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08001104 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001105 // Read all Launcher-specific widget details
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001106 boolean customWidget = c.itemType ==
Adam Cohen59400422014-03-05 18:07:04 -08001107 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
1108
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001109 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001110 String savedProvider = c.getString(appWidgetProviderIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001111
Sunny Goyalff572272014-07-23 13:58:07 -07001112 final ComponentName component =
1113 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04001114
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001115 final boolean isIdValid = !c.hasRestoreFlag(
1116 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID);
1117 final boolean wasProviderReady = !c.hasRestoreFlag(
1118 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY);
Sunny Goyal651077b2014-06-30 14:15:31 -07001119
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001120 if (widgetProvidersMap == null) {
1121 widgetProvidersMap = AppWidgetManagerCompat
1122 .getInstance(mContext).getAllProvidersMap();
1123 }
1124 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
1125 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00001126 ComponentName.unflattenFromString(savedProvider),
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001127 c.user));
Sunny Goyalff572272014-07-23 13:58:07 -07001128
1129 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08001130 if (!isSafeMode && !customWidget &&
1131 wasProviderReady && !isProviderReady) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001132 c.markDeleted(
1133 "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07001134 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001135 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07001136 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07001137 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1138 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08001139
Sunny Goyal53f96722015-07-13 19:54:53 -07001140 // The provider is available. So the widget is either
1141 // available or not available. We do not need to track
1142 // any future restore updates.
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001143 int status = c.restoreFlag &
Sunny Goyal53f96722015-07-13 19:54:53 -07001144 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07001145 if (!wasProviderReady) {
1146 // If provider was not previously ready, update the
1147 // status and UI flag.
1148
1149 // Id would be valid only if the widget restore broadcast was received.
1150 if (isIdValid) {
Sunny Goyal86df1382016-08-10 15:03:22 -07001151 status |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07001152 } else {
1153 status &= ~LauncherAppWidgetInfo
1154 .FLAG_PROVIDER_NOT_READY;
1155 }
1156 }
1157 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07001158 } else {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001159 Log.v(TAG, "Widget restore pending id=" + c.id
Sunny Goyal651077b2014-06-30 14:15:31 -07001160 + " appWidgetId=" + appWidgetId
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001161 + " status =" + c.restoreFlag);
Sunny Goyal651077b2014-06-30 14:15:31 -07001162 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07001163 component);
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001164 appWidgetInfo.restoreStatus = c.restoreFlag;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001165 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07001166
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001167 if (c.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_RESTORE_STARTED)) {
Sunny Goyal94485362014-09-18 16:13:58 -07001168 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001169 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07001170 // App restore has started. Update the flag
1171 appWidgetInfo.restoreStatus |=
1172 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001173 } else if (!isSafeMode) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001174 c.markDeleted("Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07001175 continue;
1176 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07001177
1178 appWidgetInfo.installProgress =
1179 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07001180 }
Sunny Goyal86df1382016-08-10 15:03:22 -07001181 if (appWidgetInfo.hasRestoreFlag(
1182 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG)) {
Sunny Goyal81e44912017-01-14 15:05:14 -08001183 appWidgetInfo.bindOptions = c.parseIntent();
Sunny Goyal86df1382016-08-10 15:03:22 -07001184 }
Sunny Goyalff572272014-07-23 13:58:07 -07001185
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001186 c.applyCommonProperties(appWidgetInfo);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001187 appWidgetInfo.spanX = c.getInt(spanXIndex);
1188 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001189 appWidgetInfo.user = c.user;
Joe Onorato36115782010-06-17 13:28:48 -04001190
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001191 if (!c.isOnWorkspaceOrHotseat()) {
1192 c.markDeleted("Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001193 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001194 continue;
1195 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001196
Adam Cohen59400422014-03-05 18:07:04 -08001197 if (!customWidget) {
1198 String providerName =
1199 appWidgetInfo.providerName.flattenToString();
1200 if (!providerName.equals(savedProvider) ||
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001201 (appWidgetInfo.restoreStatus != c.restoreFlag)) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001202 c.updater()
1203 .put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1204 providerName)
1205 .put(LauncherSettings.Favorites.RESTORED,
1206 appWidgetInfo.restoreStatus)
1207 .commit();
Adam Cohen59400422014-03-05 18:07:04 -08001208 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001209 }
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001210 c.checkAndAddItem(appWidgetInfo, sBgDataModel);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001211 }
Joe Onorato36115782010-06-17 13:28:48 -04001212 break;
1213 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001214 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001215 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001216 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001217 }
1218 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001219 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001220 }
1221
Winson Chungba9c37f2013-08-30 14:11:37 -07001222 // Break early if we've stopped loading
1223 if (mStopped) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001224 sBgDataModel.clear();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001225 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07001226 }
1227
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001228 // Remove dead items
1229 if (c.commitDeleted()) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001230 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07001231 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
1232 .call(contentResolver,
1233 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
1234 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
1235 for (long folderId : deletedFolderIds) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001236 sBgDataModel.workspaceItems.remove(sBgDataModel.folders.get(folderId));
1237 sBgDataModel.folders.remove(folderId);
1238 sBgDataModel.itemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001239 }
1240 }
1241
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001242 // Unpin shortcuts that don't exist on the workspace.
Sunny Goyalf75baa92016-11-22 03:23:51 +05301243 HashSet<ShortcutKey> pendingShortcuts =
1244 InstallShortcutReceiver.getPendingShortcuts(context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001245 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001246 MutableInt numTimesPinned = sBgDataModel.pinnedShortcutCounts.get(key);
Sunny Goyalf75baa92016-11-22 03:23:51 +05301247 if ((numTimesPinned == null || numTimesPinned.value == 0)
1248 && !pendingShortcuts.contains(key)) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001249 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301250 shortcutManager.unpinShortcut(key);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001251 }
1252 }
1253
Jon Miranda655ec422017-02-07 11:40:22 -08001254 FolderIconPreviewVerifier verifier =
1255 new FolderIconPreviewVerifier(mApp.getInvariantDeviceProfile());
1256 // Sort the folder items and make sure all items in the preview are high resolution.
Sunny Goyale9956a72016-09-01 17:24:47 -07001257 for (FolderInfo folder : sBgDataModel.folders) {
Sunny Goyal317698b2015-07-29 11:45:41 -07001258 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
Jon Miranda655ec422017-02-07 11:40:22 -08001259 verifier.setFolderInfo(folder);
1260
1261 int numItemsInPreview = 0;
Sunny Goyal317698b2015-07-29 11:45:41 -07001262 for (ShortcutInfo info : folder.contents) {
Jon Miranda655ec422017-02-07 11:40:22 -08001263 if (info.usingLowResIcon
1264 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION
1265 && verifier.isItemInPreview(info.rank)) {
Sunny Goyal3fe4a142016-12-15 17:40:07 -08001266 mIconCache.getTitleAndIcon(info, false);
Jon Miranda655ec422017-02-07 11:40:22 -08001267 numItemsInPreview++;
Sunny Goyal317698b2015-07-29 11:45:41 -07001268 }
Jon Miranda655ec422017-02-07 11:40:22 -08001269
1270 if (numItemsInPreview >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
Sunny Goyal317698b2015-07-29 11:45:41 -07001271 break;
1272 }
1273 }
1274 }
1275
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001276 c.commitRestoredItems();
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001277 if (!isSdCardReady && !pendingPackages.isEmpty()) {
1278 context.registerReceiver(
1279 new SdCardAvailableReceiver(
1280 LauncherModel.this, mContext, pendingPackages),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001281 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001282 null,
1283 sWorker);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001284 }
1285
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001286 // Remove any empty screens
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001287 ArrayList<Long> unusedScreens = new ArrayList<>(sBgDataModel.workspaceScreens);
Sunny Goyale9956a72016-09-01 17:24:47 -07001288 for (ItemInfo item: sBgDataModel.itemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001289 long screenId = item.screenId;
1290 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1291 unusedScreens.contains(screenId)) {
1292 unusedScreens.remove(screenId);
1293 }
1294 }
1295
1296 // If there are any empty screens remove them, and update.
1297 if (unusedScreens.size() != 0) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001298 sBgDataModel.workspaceScreens.removeAll(unusedScreens);
1299 updateWorkspaceScreenOrder(context, sBgDataModel.workspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001300 }
Joe Onorato36115782010-06-17 13:28:48 -04001301 }
Sunny Goyale26d1002016-06-20 14:52:14 -07001302 if (LauncherAppState.PROFILE_STARTUP) {
1303 Trace.endSection();
1304 }
Adam Cohene25af792013-06-06 23:08:25 -07001305 }
1306
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001307 /** Filters the set of items who are directly or indirectly (via another container) on the
1308 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08001309 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001310 ArrayList<ItemInfo> allWorkspaceItems,
1311 ArrayList<ItemInfo> currentScreenItems,
1312 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001313 // Purge any null ItemInfos
1314 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
1315 while (iter.hasNext()) {
1316 ItemInfo i = iter.next();
1317 if (i == null) {
1318 iter.remove();
1319 }
1320 }
1321
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001322 // Order the set of items by their containers first, this allows use to walk through the
1323 // list sequentially, build up a list of containers that are in the specified screen,
1324 // as well as all items in those containers.
1325 Set<Long> itemsOnScreen = new HashSet<Long>();
1326 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
1327 @Override
1328 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07001329 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001330 }
1331 });
1332 for (ItemInfo info : allWorkspaceItems) {
1333 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001334 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001335 currentScreenItems.add(info);
1336 itemsOnScreen.add(info.id);
1337 } else {
1338 otherScreenItems.add(info);
1339 }
1340 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1341 currentScreenItems.add(info);
1342 itemsOnScreen.add(info.id);
1343 } else {
1344 if (itemsOnScreen.contains(info.container)) {
1345 currentScreenItems.add(info);
1346 itemsOnScreen.add(info.id);
1347 } else {
1348 otherScreenItems.add(info);
1349 }
1350 }
1351 }
1352 }
1353
1354 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08001355 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001356 ArrayList<LauncherAppWidgetInfo> appWidgets,
1357 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
1358 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001359
1360 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001361 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001362 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08001363 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001364 currentScreenWidgets.add(widget);
1365 } else {
1366 otherScreenWidgets.add(widget);
1367 }
1368 }
1369 }
1370
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001371 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
1372 * right) */
1373 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Sunny Goyal87f784c2017-01-11 10:48:34 -08001374 final InvariantDeviceProfile profile = mApp.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07001375 final int screenCols = profile.numColumns;
1376 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001377 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07001378 @Override
1379 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07001380 if (lhs.container == rhs.container) {
1381 // Within containers, order by their spatial position in that container
1382 switch ((int) lhs.container) {
1383 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
1384 long lr = (lhs.screenId * screenCellCount +
1385 lhs.cellY * screenCols + lhs.cellX);
1386 long rr = (rhs.screenId * screenCellCount +
1387 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07001388 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07001389 }
1390 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
1391 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07001392 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07001393 }
1394 default:
Sunny Goyal3d706ad2017-03-06 16:56:39 -08001395 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07001396 throw new RuntimeException("Unexpected container type when " +
1397 "sorting workspace items.");
1398 }
1399 return 0;
1400 }
1401 } else {
1402 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07001403 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07001404 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001405 }
1406 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001407 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001408
Adam Cohendcd297f2013-06-18 13:13:40 -07001409 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
1410 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001411 final Runnable r = new Runnable() {
1412 @Override
1413 public void run() {
1414 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1415 if (callbacks != null) {
1416 callbacks.bindScreens(orderedScreens);
1417 }
1418 }
1419 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001420 mUiExecutor.execute(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07001421 }
1422
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001423 private void bindWorkspaceItems(final Callbacks oldCallbacks,
1424 final ArrayList<ItemInfo> workspaceItems,
1425 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07001426 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001427
1428 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07001429 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001430 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04001431 final int start = i;
1432 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001433 final Runnable r = new Runnable() {
1434 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001435 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001436 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001437 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07001438 callbacks.bindItems(workspaceItems, start, start+chunkSize,
1439 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001440 }
1441 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001442 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001443 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04001444 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001445
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001446 // Bind the widgets, one at a time
1447 N = appWidgets.size();
1448 for (int i = 0; i < N; i++) {
1449 final LauncherAppWidgetInfo widget = appWidgets.get(i);
1450 final Runnable r = new Runnable() {
1451 public void run() {
1452 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1453 if (callbacks != null) {
1454 callbacks.bindAppWidget(widget);
1455 }
1456 }
1457 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001458 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001459 }
1460 }
1461
1462 /**
1463 * Binds all loaded data to actual views on the main thread.
1464 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001465 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001466 final long t = SystemClock.uptimeMillis();
1467 Runnable r;
1468
1469 // Don't use these two variables in any of the callback runnables.
1470 // Otherwise we hold a reference to them.
1471 final Callbacks oldCallbacks = mCallbacks.get();
1472 if (oldCallbacks == null) {
1473 // This launcher has exited and nobody bothered to tell us. Just bail.
1474 Log.w(TAG, "LoaderTask running with no launcher");
1475 return;
1476 }
1477
Winson Chung9b9fb962013-11-15 15:39:34 -08001478 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07001479 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
1480 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
1481 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07001482
Sunny Goyale9956a72016-09-01 17:24:47 -07001483 synchronized (sBgDataModel) {
1484 workspaceItems.addAll(sBgDataModel.workspaceItems);
1485 appWidgets.addAll(sBgDataModel.appWidgets);
1486 orderedScreenIds.addAll(sBgDataModel.workspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001487 }
1488
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001489 final int currentScreen;
1490 {
1491 int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE
1492 ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen();
1493 if (currScreen >= orderedScreenIds.size()) {
1494 // There may be no workspace screens (just hotseat items and an empty page).
1495 currScreen = PagedView.INVALID_RESTORE_PAGE;
1496 }
1497 currentScreen = currScreen;
Winson Chung9b9fb962013-11-15 15:39:34 -08001498 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001499 final boolean validFirstPage = currentScreen >= 0;
1500 final long currentScreenId =
1501 validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID;
Winson Chung9b9fb962013-11-15 15:39:34 -08001502
Winson Chung9b9fb962013-11-15 15:39:34 -08001503 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07001504 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
1505 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
1506 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
1507 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001508
Winson Chung9b9fb962013-11-15 15:39:34 -08001509 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001510 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08001511 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001512 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001513 sortWorkspaceItemsSpatially(currentWorkspaceItems);
1514 sortWorkspaceItemsSpatially(otherWorkspaceItems);
1515
1516 // Tell the workspace that we're about to start binding items
1517 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001518 public void run() {
1519 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1520 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001521 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001522 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04001523 }
1524 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001525 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001526 mUiExecutor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001527
Adam Cohendcd297f2013-06-18 13:13:40 -07001528 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
1529
Sunny Goyalb265ba72017-02-14 15:03:45 -08001530 Executor mainExecutor = mUiExecutor;
Sunny Goyal527c7d32015-08-28 15:19:36 -07001531 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07001532 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001533
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001534 // In case of validFirstPage, only bind the first screen, and defer binding the
1535 // remaining screens after first onDraw (and an optional the fade animation whichever
1536 // happens later).
1537 // This ensures that the first screen is immediately visible (eg. during rotation)
1538 // In case of !validFirstPage, bind all pages one after other.
1539 final Executor deferredExecutor =
Sunny Goyalb265ba72017-02-14 15:03:45 -08001540 validFirstPage ? new ViewOnDrawExecutor(mUiExecutor) : mainExecutor;
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001541
1542 mainExecutor.execute(new Runnable() {
1543 @Override
1544 public void run() {
1545 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1546 if (callbacks != null) {
1547 callbacks.finishFirstPageBind(
1548 validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null);
1549 }
1550 }
1551 });
Sunny Goyal527c7d32015-08-28 15:19:36 -07001552
Sunny Goyal44c06432016-04-02 10:56:02 -07001553 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001554
1555 // Tell the workspace that we're done binding items
1556 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001557 public void run() {
1558 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1559 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001560 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04001561 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001562
Sunny Goyal639e9062015-08-19 19:17:06 -07001563 mIsLoadingAndBindingWorkspace = false;
1564
1565 // Run all the bind complete runnables after workspace is bound.
1566 if (!mBindCompleteRunnables.isEmpty()) {
1567 synchronized (mBindCompleteRunnables) {
1568 for (final Runnable r : mBindCompleteRunnables) {
1569 runOnWorkerThread(r);
1570 }
1571 mBindCompleteRunnables.clear();
1572 }
1573 }
1574
Winson Chung98e030b2012-05-07 16:01:11 -07001575 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04001576 if (DEBUG_LOADERS) {
1577 Log.d(TAG, "bound workspace in "
1578 + (SystemClock.uptimeMillis()-t) + "ms");
1579 }
Winson Chung36a62fe2012-05-06 18:04:42 -07001580
Joe Onorato36115782010-06-17 13:28:48 -04001581 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001582 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001583 deferredExecutor.execute(r);
1584
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001585 if (validFirstPage) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001586 r = new Runnable() {
1587 public void run() {
1588 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1589 if (callbacks != null) {
1590 // We are loading synchronously, which means, some of the pages will be
1591 // bound after first draw. Inform the callbacks that page binding is
1592 // not complete, and schedule the remaining pages.
1593 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
1594 callbacks.onPageBoundSynchronously(currentScreen);
1595 }
1596 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
1597 }
1598 }
1599 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001600 mUiExecutor.execute(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07001601 }
Joe Onorato36115782010-06-17 13:28:48 -04001602 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001603
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001604 private void updateIconCache() {
1605 // Ignore packages which have a promise icon.
1606 HashSet<String> packagesToIgnore = new HashSet<>();
Sunny Goyale9956a72016-09-01 17:24:47 -07001607 synchronized (sBgDataModel) {
1608 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001609 if (info instanceof ShortcutInfo) {
1610 ShortcutInfo si = (ShortcutInfo) info;
1611 if (si.isPromise() && si.getTargetComponent() != null) {
1612 packagesToIgnore.add(si.getTargetComponent().getPackageName());
1613 }
1614 } else if (info instanceof LauncherAppWidgetInfo) {
1615 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
1616 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
1617 packagesToIgnore.add(lawi.providerName.getPackageName());
1618 }
1619 }
1620 }
1621 }
1622 mIconCache.updateDbIcons(packagesToIgnore);
1623 }
1624
Joe Onorato36115782010-06-17 13:28:48 -04001625 private void onlyBindAllApps() {
1626 final Callbacks oldCallbacks = mCallbacks.get();
1627 if (oldCallbacks == null) {
1628 // This launcher has exited and nobody bothered to tell us. Just bail.
1629 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1630 return;
1631 }
1632
1633 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07001634 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001635 final ArrayList<AppInfo> list
1636 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07001637 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001638 public void run() {
1639 final long t = SystemClock.uptimeMillis();
1640 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1641 if (callbacks != null) {
1642 callbacks.bindAllApplications(list);
1643 }
1644 if (DEBUG_LOADERS) {
1645 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08001646 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04001647 }
1648 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07001649 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001650 mUiExecutor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04001651 }
1652
Winson Chung64359a52013-07-08 17:17:08 -07001653 private void loadAllApps() {
1654 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04001655
Joe Onorato36115782010-06-17 13:28:48 -04001656 final Callbacks oldCallbacks = mCallbacks.get();
1657 if (oldCallbacks == null) {
1658 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07001659 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04001660 return;
1661 }
1662
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001663 final List<UserHandle> profiles = mUserManager.getUserProfiles();
Kenny Guyed131872014-04-30 03:02:21 +01001664
Winson Chung64359a52013-07-08 17:17:08 -07001665 // Clear the list of apps
1666 mBgAllAppsList.clear();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001667 for (UserHandle user : profiles) {
Kenny Guyed131872014-04-30 03:02:21 +01001668 // Query for the set of apps
1669 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal3e9be432017-01-05 15:22:41 -08001670 final List<LauncherActivityInfo> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01001671 if (DEBUG_LOADERS) {
1672 Log.d(TAG, "getActivityList took "
1673 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
1674 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
1675 }
1676 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08001677 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01001678 if (apps == null || apps.isEmpty()) {
1679 return;
1680 }
Kenny Guyff05f432016-01-22 17:48:29 +00001681 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01001682 // Create the ApplicationInfos
1683 for (int i = 0; i < apps.size(); i++) {
Sunny Goyal3e9be432017-01-05 15:22:41 -08001684 LauncherActivityInfo app = apps.get(i);
Kenny Guyed131872014-04-30 03:02:21 +01001685 // This builds the icon bitmaps.
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001686 mBgAllAppsList.add(new AppInfo(mContext, app, user, quietMode), app);
Kenny Guyed131872014-04-30 03:02:21 +01001687 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08001688
Sunny Goyal756a28a2015-04-23 17:07:55 -07001689 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
1690 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07001691 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07001692
1693 @Override
1694 public void run() {
1695 heuristic.processUserApps(apps);
1696 }
Sunny Goyal639e9062015-08-19 19:17:06 -07001697 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001698 mUiExecutor.execute(new Runnable() {
Sunny Goyal639e9062015-08-19 19:17:06 -07001699
Sunny Goyalb265ba72017-02-14 15:03:45 -08001700 @Override
1701 public void run() {
1702 // Check isLoadingWorkspace on the UI thread, as it is updated on
1703 // the UI thread.
1704 if (mIsLoadingAndBindingWorkspace) {
1705 synchronized (mBindCompleteRunnables) {
1706 mBindCompleteRunnables.add(r);
1707 }
1708 } else {
1709 runOnWorkerThread(r);
1710 }
1711 }
1712 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08001713 }
Winson Chung64359a52013-07-08 17:17:08 -07001714 }
Mario Bertschler817afa32017-03-15 11:56:47 -07001715
1716 if (FeatureFlags.LAUNCHER3_PROMISE_APPS_IN_ALL_APPS) {
1717 // get all active sessions and add them to the all apps list
1718 PackageInstallerCompat installer = PackageInstallerCompat.getInstance(mContext);
1719 for (PackageInstaller.SessionInfo info : installer.getAllVerifiedSessions()) {
1720 mBgAllAppsList.addPromiseApp(mContext,
1721 PackageInstallInfo.fromInstallingState(info));
1722 }
1723 }
1724
Bjorn Bringert85f418d2013-09-06 12:50:05 +01001725 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001726 final ArrayList<AppInfo> added = mBgAllAppsList.added;
1727 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07001728
Mario Bertschler817afa32017-03-15 11:56:47 -07001729
Winson Chung64359a52013-07-08 17:17:08 -07001730 // Post callback on main thread
Sunny Goyalb265ba72017-02-14 15:03:45 -08001731 mUiExecutor.execute(new Runnable() {
Winson Chung64359a52013-07-08 17:17:08 -07001732 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07001733
Winson Chung64359a52013-07-08 17:17:08 -07001734 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07001735 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07001736 if (callbacks != null) {
1737 callbacks.bindAllApplications(added);
1738 if (DEBUG_LOADERS) {
1739 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001740 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07001741 }
1742 } else {
1743 Log.i(TAG, "not binding apps: no Launcher activity");
1744 }
1745 }
1746 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07001747 // Cleanup any data stored for a deleted user.
1748 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04001749 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07001750 Log.d(TAG, "Icons processed in "
1751 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08001752 }
1753 }
1754
Sunny Goyaldd96a5e2017-02-17 11:22:34 -08001755 private void loadDeepShortcuts() {
1756 if (!mModelLoaded) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07001757 sBgDataModel.deepShortcutMap.clear();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301758 DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(mContext);
1759 mHasShortcutHostPermission = shortcutManager.hasHostPermission();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001760 if (mHasShortcutHostPermission) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001761 for (UserHandle user : mUserManager.getUserProfiles()) {
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001762 if (mUserManager.isUserUnlocked(user)) {
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301763 List<ShortcutInfoCompat> shortcuts =
1764 shortcutManager.queryForAllShortcuts(user);
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07001765 sBgDataModel.updateDeepShortcutMap(null, user, shortcuts);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001766 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001767 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001768 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001769 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001770 }
Joe Onorato36115782010-06-17 13:28:48 -04001771 }
1772
Tony Wickham80f57872016-06-29 18:12:15 -07001773 public void bindDeepShortcuts() {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07001774 final MultiHashMap<ComponentKey, String> shortcutMapCopy =
1775 sBgDataModel.deepShortcutMap.clone();
Tony Wickham80f57872016-06-29 18:12:15 -07001776 Runnable r = new Runnable() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001777 @Override
1778 public void run() {
1779 Callbacks callbacks = getCallback();
1780 if (callbacks != null) {
1781 callbacks.bindDeepShortcutMap(shortcutMapCopy);
1782 }
1783 }
Tony Wickham80f57872016-06-29 18:12:15 -07001784 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001785 mUiExecutor.execute(r);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001786 }
1787
Sunny Goyal75b0f552015-05-20 21:57:06 -07001788 /**
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001789 * Refreshes the cached shortcuts if the shortcut permission has changed.
1790 * Current implementation simply reloads the workspace, but it can be optimized to
1791 * use partial updates similar to {@link UserManagerCompat}
1792 */
1793 public void refreshShortcutsIfRequired() {
Sunny Goyalf5e37442016-11-02 10:31:24 -07001794 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001795 sWorker.removeCallbacks(mShortcutPermissionCheckRunnable);
1796 sWorker.post(mShortcutPermissionCheckRunnable);
1797 }
1798 }
1799
1800 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -07001801 * Called when the icons for packages have been updated in the icon cache.
1802 */
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001803 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandle user) {
Sunny Goyal75b0f552015-05-20 21:57:06 -07001804 // If any package icon has changed (app was updated while launcher was dead),
1805 // update the corresponding shortcuts.
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001806 enqueueModelUpdateTask(new CacheDataUpdatedTask(
1807 CacheDataUpdatedTask.OP_CACHE_UPDATE, user, updatedPackages));
Sunny Goyal75b0f552015-05-20 21:57:06 -07001808 }
1809
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001810 void enqueueModelUpdateTask(BaseModelUpdateTask task) {
Jon Mirandaf28629a2017-02-23 15:12:42 -08001811 if (!mModelLoaded && mLoaderTask == null) {
1812 if (DEBUG_LOADERS) {
1813 Log.d(TAG, "enqueueModelUpdateTask Ignoring task since loader is pending=" + task);
1814 }
1815 return;
1816 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001817 task.init(this);
1818 runOnWorkerThread(task);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001819 }
1820
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001821 /**
1822 * A task to be executed on the current callbacks on the UI thread.
1823 * If there is no current callbacks, the task is ignored.
1824 */
1825 public interface CallbackTask {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001826
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001827 void execute(Callbacks callbacks);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001828 }
1829
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001830 /**
1831 * A runnable which changes/updates the data model of the launcher based on certain events.
1832 */
1833 public static abstract class BaseModelUpdateTask implements Runnable {
Joe Onorato36115782010-06-17 13:28:48 -04001834
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001835 private LauncherModel mModel;
Sunny Goyalb265ba72017-02-14 15:03:45 -08001836 private Executor mUiExecutor;
Joe Onorato36115782010-06-17 13:28:48 -04001837
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001838 /* package private */
1839 void init(LauncherModel model) {
1840 mModel = model;
Sunny Goyalb265ba72017-02-14 15:03:45 -08001841 mUiExecutor = mModel.mUiExecutor;
Joe Onorato36115782010-06-17 13:28:48 -04001842 }
1843
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001844 @Override
Joe Onorato36115782010-06-17 13:28:48 -04001845 public void run() {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001846 if (!mModel.mHasLoaderCompletedOnce) {
Sunny Goyalc905efc2015-05-06 09:54:53 -07001847 // Loader has not yet run.
1848 return;
1849 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001850 execute(mModel.mApp, sBgDataModel, mModel.mBgAllAppsList);
1851 }
Joe Onorato36115782010-06-17 13:28:48 -04001852
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001853 /**
1854 * Execute the actual task. Called on the worker thread.
1855 */
1856 public abstract void execute(
1857 LauncherAppState app, BgDataModel dataModel, AllAppsList apps);
Sunny Goyale0f58d72014-11-10 18:05:31 -08001858
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001859 /**
1860 * Schedules a {@param task} to be executed on the current callbacks.
1861 */
1862 public final void scheduleCallbackTask(final CallbackTask task) {
1863 final Callbacks callbacks = mModel.getCallback();
Sunny Goyalb265ba72017-02-14 15:03:45 -08001864 mUiExecutor.execute(new Runnable() {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001865 public void run() {
1866 Callbacks cb = mModel.getCallback();
1867 if (callbacks == cb && cb != null) {
1868 task.execute(callbacks);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07001869 }
1870 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001871 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001872 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001873
1874 public ModelWriter getModelWriter() {
1875 // Updates from model task, do not deal with icon position in hotseat.
1876 return mModel.getWriter(false /* hasVerticalHotseat */);
1877 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001878 }
1879
Sunny Goyal1b072632017-01-18 11:30:23 -08001880 public void updateAndBindShortcutInfo(final ShortcutInfo si, final ShortcutInfoCompat info) {
1881 updateAndBindShortcutInfo(new Provider<ShortcutInfo>() {
1882 @Override
1883 public ShortcutInfo get() {
1884 si.updateFromDeepShortcutInfo(info, mApp.getContext());
1885 si.iconBitmap = LauncherIcons.createShortcutIcon(info, mApp.getContext());
1886 return si;
1887 }
1888 });
1889 }
1890
Sunny Goyal10923b32016-07-20 15:42:44 -07001891 /**
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001892 * Utility method to update a shortcut on the background thread.
Sunny Goyal10923b32016-07-20 15:42:44 -07001893 */
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001894 public void updateAndBindShortcutInfo(final Provider<ShortcutInfo> shortcutProvider) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001895 enqueueModelUpdateTask(new ExtendedModelTask() {
Sunny Goyal10923b32016-07-20 15:42:44 -07001896 @Override
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001897 public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001898 ShortcutInfo info = shortcutProvider.get();
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001899 ArrayList<ShortcutInfo> update = new ArrayList<>();
Sunny Goyal10923b32016-07-20 15:42:44 -07001900 update.add(info);
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001901 bindUpdatedShortcuts(update, info.user);
Sunny Goyal10923b32016-07-20 15:42:44 -07001902 }
1903 });
1904 }
1905
Sunny Goyald164b7f2016-10-12 20:49:31 -07001906 private void bindWidgetsModel(final Callbacks callbacks) {
1907 final MultiHashMap<PackageItemInfo, WidgetItem> widgets
1908 = mBgWidgetsModel.getWidgetsMap().clone();
Sunny Goyalb265ba72017-02-14 15:03:45 -08001909 mUiExecutor.execute(new Runnable() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001910 @Override
1911 public void run() {
1912 Callbacks cb = getCallback();
1913 if (callbacks == cb && cb != null) {
Sunny Goyald164b7f2016-10-12 20:49:31 -07001914 callbacks.bindAllWidgets(widgets);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001915 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07001916 }
1917 });
1918 }
1919
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001920 public void refreshAndBindWidgetsAndShortcuts(
1921 final Callbacks callbacks, final boolean bindFirst) {
1922 runOnWorkerThread(new Runnable() {
1923 @Override
1924 public void run() {
1925 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
Sunny Goyald164b7f2016-10-12 20:49:31 -07001926 bindWidgetsModel(callbacks);
Sunny Goyal31860582015-09-18 08:38:57 -07001927 }
Sunny Goyald164b7f2016-10-12 20:49:31 -07001928 ArrayList<WidgetItem> allWidgets = mBgWidgetsModel.update(mApp.getContext());
1929 bindWidgetsModel(callbacks);
1930
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001931 // update the Widget entries inside DB on the worker thread.
Sunny Goyal87f784c2017-01-11 10:48:34 -08001932 mApp.getWidgetCache().removeObsoletePreviews(allWidgets);
Sunny Goyal31860582015-09-18 08:38:57 -07001933 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001934 });
Michael Jurkac402cd92013-05-20 15:49:32 +02001935 }
1936
Sunny Goyal651077b2014-06-30 14:15:31 -07001937 static boolean isValidProvider(AppWidgetProviderInfo provider) {
1938 return (provider != null) && (provider.provider != null)
1939 && (provider.provider.getPackageName() != null);
1940 }
1941
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001942 public void dumpState(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
1943 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
1944 writer.println(prefix + "All apps list: size=" + mBgAllAppsList.data.size());
1945 for (AppInfo info : mBgAllAppsList.data) {
1946 writer.println(prefix + " title=\"" + info.title + "\" iconBitmap=" + info.iconBitmap
1947 + " componentName=" + info.componentName.getPackageName());
1948 }
Joe Onorato36115782010-06-17 13:28:48 -04001949 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001950 sBgDataModel.dump(prefix, fd, writer, args);
Joe Onoratobe386092009-11-17 17:32:16 -08001951 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08001952
1953 public Callbacks getCallback() {
1954 return mCallbacks != null ? mCallbacks.get() : null;
1955 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07001956
1957 /**
1958 * @return {@link FolderInfo} if its already loaded.
1959 */
1960 public FolderInfo findFolderById(Long folderId) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001961 synchronized (sBgDataModel) {
1962 return sBgDataModel.folders.get(folderId);
Sunny Goyal18bf8e22015-04-08 18:13:46 -07001963 }
1964 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07001965
1966 /**
1967 * @return the looper for the worker thread which can be used to start background tasks.
1968 */
1969 public static Looper getWorkerLooper() {
1970 return sWorkerThread.getLooper();
1971 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972}