blob: d39c6ca444aabdd262c8ead5170246565b8908c2 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Romain Guy629de3e2010-01-13 12:20:59 -080019import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070020import android.content.BroadcastReceiver;
21import android.content.ComponentName;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070022import android.content.ContentProviderOperation;
23import android.content.ContentResolver;
24import android.content.ContentValues;
25import android.content.Context;
26import android.content.Intent;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070027import android.content.IntentFilter;
Sunny Goyal3e9be432017-01-05 15:22:41 -080028import android.content.pm.LauncherActivityInfo;
Mario Bertschler817afa32017-03-15 11:56:47 -070029import android.content.pm.PackageInstaller;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.net.Uri;
Joe Onorato36115782010-06-17 13:28:48 -040031import android.os.Handler;
32import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070033import android.os.Looper;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040035import android.os.SystemClock;
Sunny Goyale26d1002016-06-20 14:52:14 -070036import android.os.Trace;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080037import android.os.UserHandle;
Winson Chunga90303b2013-11-15 13:05:06 -080038import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070039import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080040import android.util.LongSparseArray;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070041import android.util.MutableInt;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010042
Sunny Goyalffe83f12014-08-14 17:39:34 -070043import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010044import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070045import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070046import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010047import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyal3d706ad2017-03-06 16:56:39 -080048import com.android.launcher3.config.FeatureFlags;
Tony Wickham827cef22016-03-17 15:39:39 -070049import com.android.launcher3.dynamicui.ExtractionUtils;
Sunny Goyal26119432016-02-18 22:09:23 +000050import com.android.launcher3.folder.Folder;
51import com.android.launcher3.folder.FolderIcon;
Jon Miranda655ec422017-02-07 11:40:22 -080052import com.android.launcher3.folder.FolderIconPreviewVerifier;
Sunny Goyal1b072632017-01-18 11:30:23 -080053import com.android.launcher3.graphics.LauncherIcons;
Sunny Goyal1acb9e92016-05-16 12:41:09 -070054import com.android.launcher3.logging.FileLog;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070055import com.android.launcher3.model.AddWorkspaceItemsTask;
Sunny Goyale9956a72016-09-01 17:24:47 -070056import com.android.launcher3.model.BgDataModel;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070057import com.android.launcher3.model.CacheDataUpdatedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080058import com.android.launcher3.model.ExtendedModelTask;
Sunny Goyalf862a262015-12-14 14:27:38 -080059import com.android.launcher3.model.GridSizeMigrationTask;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -080060import com.android.launcher3.model.LoaderCursor;
Sunny Goyal43bf11d2017-02-02 13:52:53 -080061import com.android.launcher3.model.ModelWriter;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070062import com.android.launcher3.model.PackageInstallStateChangedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080063import com.android.launcher3.model.PackageItemInfo;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070064import com.android.launcher3.model.PackageUpdatedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080065import com.android.launcher3.model.SdCardAvailableReceiver;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070066import com.android.launcher3.model.ShortcutsChangedTask;
67import com.android.launcher3.model.UserLockStateChangedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080068import com.android.launcher3.model.WidgetItem;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070069import com.android.launcher3.model.WidgetsModel;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -070070import com.android.launcher3.provider.ImportDataTask;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070071import com.android.launcher3.provider.LauncherDbUtils;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070072import com.android.launcher3.shortcuts.DeepShortcutManager;
73import com.android.launcher3.shortcuts.ShortcutInfoCompat;
74import com.android.launcher3.shortcuts.ShortcutKey;
Robin Lee26ace122015-03-16 19:41:43 +000075import com.android.launcher3.util.ComponentKey;
Sunny Goyalb265ba72017-02-14 15:03:45 -080076import com.android.launcher3.util.LooperIdleLock;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070077import com.android.launcher3.util.ManagedProfileHeuristic;
Tony Wickhamd82a39d2016-07-01 15:44:13 -070078import com.android.launcher3.util.MultiHashMap;
Sunny Goyald09c3702016-04-06 16:18:20 -070079import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070080import com.android.launcher3.util.Preconditions;
Sunny Goyal2bcbe132016-11-16 09:23:42 -080081import com.android.launcher3.util.Provider;
Adam Cohen091440a2015-03-18 14:16:05 -070082import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070083import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080084
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -080085import java.io.FileDescriptor;
86import java.io.PrintWriter;
Michael Jurkac2f801e2011-07-12 14:19:46 -070087import java.lang.ref.WeakReference;
Michael Jurkac2f801e2011-07-12 14:19:46 -070088import java.util.ArrayList;
89import java.util.Collections;
90import java.util.Comparator;
91import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070092import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070093import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070094import java.util.List;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070095import java.util.Map;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070096import java.util.Set;
Sunny Goyaldd96a5e2017-02-17 11:22:34 -080097import java.util.concurrent.CancellationException;
Sunny Goyal527c7d32015-08-28 15:19:36 -070098import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -070099
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800100/**
101 * Maintains in-memory state of the Launcher. It is expected that there should be only one
102 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700103 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800104 */
Kenny Guyed131872014-04-30 03:02:21 +0100105public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +0100106 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800107 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -0400108 private static final boolean DEBUG_RECEIVER = false;
Chris Wrenb358f812014-04-16 13:37:00 -0400109
Joe Onorato9c1289c2009-08-17 11:03:03 -0400110 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700111
Joe Onorato36115782010-06-17 13:28:48 -0400112 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500113 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800114
Sunny Goyalb265ba72017-02-14 15:03:45 -0800115 private final MainThreadExecutor mUiExecutor = new MainThreadExecutor();
Adam Cohen091440a2015-03-18 14:16:05 -0700116 @Thunk final LauncherAppState mApp;
117 @Thunk final Object mLock = new Object();
Adam Cohen091440a2015-03-18 14:16:05 -0700118 @Thunk LoaderTask mLoaderTask;
119 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700120 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121
Adam Cohen091440a2015-03-18 14:16:05 -0700122 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700123 static {
124 sWorkerThread.start();
125 }
Adam Cohen091440a2015-03-18 14:16:05 -0700126 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700127
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800128 // Indicates whether the current model data is valid or not.
129 // We start off with everything not loaded. After that, we assume that
Joe Onoratocc67f472010-06-08 10:54:30 -0700130 // our monitoring of the package manager provides all updates and we never
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800131 // need to do a requery. This is only ever touched from the loader thread.
132 private boolean mModelLoaded;
Hyunyoung Song6aa37292017-02-06 10:46:24 -0800133 public boolean isModelLoaded() {
134 synchronized (mLock) {
135 return mModelLoaded && mLoaderTask == null;
136 }
137 }
Joe Onoratocc67f472010-06-08 10:54:30 -0700138
Sunny Goyal756a28a2015-04-23 17:07:55 -0700139 /**
140 * Set of runnables to be called on the background thread after the workspace binding
141 * is complete.
142 */
143 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
144
Adam Cohen091440a2015-03-18 14:16:05 -0700145 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700147 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800148 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700149 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800150 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800151
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700152 private boolean mHasShortcutHostPermission;
153 // Runnable to check if the shortcuts permission has changed.
154 private final Runnable mShortcutPermissionCheckRunnable = new Runnable() {
155 @Override
156 public void run() {
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800157 if (mModelLoaded) {
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530158 boolean hasShortcutHostPermission =
159 DeepShortcutManager.getInstance(mApp.getContext()).hasHostPermission();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700160 if (hasShortcutHostPermission != mHasShortcutHostPermission) {
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800161 forceReload();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700162 }
163 }
164 }
165 };
166
Sunny Goyale9956a72016-09-01 17:24:47 -0700167 /**
168 * All the static data should be accessed on the background thread, A lock should be acquired
169 * on this object when accessing any data from this model.
170 */
171 static final BgDataModel sBgDataModel = new BgDataModel();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700172
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700173 // </ only access in worker thread >
174
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700175 private final IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700177 private final LauncherAppsCompat mLauncherApps;
178 private final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100179
Joe Onorato9c1289c2009-08-17 11:03:03 -0400180 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700181 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400182 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700183 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400184 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700185 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
186 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700187 public void bindScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700188 public void finishFirstPageBind(ViewOnDrawExecutor executor);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800189 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400190 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200191 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700192 public void bindAppsAdded(ArrayList<Long> newScreens,
193 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700194 ArrayList<ItemInfo> addAnimated,
195 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200196 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700197 public void bindPromiseAppProgressUpdated(PromiseAppInfo app);
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 {
Mario Bertschlerc06af332017-03-28 12:23:22 -0700932 if (c.hasRestoreFlag(ShortcutInfo.FLAG_AUTOINSTALL_ICON)) {
Sunny Goyal81e44912017-01-14 15:05:14 -0800933 // 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 Goyald09c3702016-04-06 16:18:20 -07001064 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001065 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1066 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1067 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001068
Sunny Goyal81e44912017-01-14 15:05:14 -08001069 if (c.restoreFlag != 0 && !TextUtils.isEmpty(targetPkg)) {
1070 Integer progress = installingPkgs.get(targetPkg);
1071 if (progress != null) {
1072 info.setInstallProgress(progress);
1073 } else {
1074 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001075 }
1076 }
1077
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001078 c.checkAndAddItem(info, sBgDataModel);
Winson Chung1323b482013-08-05 12:41:55 -07001079 } else {
1080 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001081 }
1082 break;
1083
1084 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001085 FolderInfo folderInfo = sBgDataModel.findOrMakeFolder(c.id);
1086 c.applyCommonProperties(folderInfo);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001087
Sunny Goyala508e4f2015-05-21 09:33:57 -07001088 // Do not trim the folder label, as is was set by the user.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001089 folderInfo.title = c.getString(c.titleIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001090 folderInfo.spanX = 1;
1091 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001092 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001093
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001094 // no special handling required for restored folders
1095 c.markRestored();
Chris Wrenf4d08112014-01-16 18:13:56 -05001096
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001097 c.checkAndAddItem(folderInfo, sBgDataModel);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001098 break;
1099
1100 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08001101 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001102 // Read all Launcher-specific widget details
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001103 boolean customWidget = c.itemType ==
Adam Cohen59400422014-03-05 18:07:04 -08001104 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
1105
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001106 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001107 String savedProvider = c.getString(appWidgetProviderIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001108
Sunny Goyalff572272014-07-23 13:58:07 -07001109 final ComponentName component =
1110 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04001111
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001112 final boolean isIdValid = !c.hasRestoreFlag(
1113 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID);
1114 final boolean wasProviderReady = !c.hasRestoreFlag(
1115 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY);
Sunny Goyal651077b2014-06-30 14:15:31 -07001116
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001117 if (widgetProvidersMap == null) {
1118 widgetProvidersMap = AppWidgetManagerCompat
1119 .getInstance(mContext).getAllProvidersMap();
1120 }
1121 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
1122 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00001123 ComponentName.unflattenFromString(savedProvider),
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001124 c.user));
Sunny Goyalff572272014-07-23 13:58:07 -07001125
1126 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08001127 if (!isSafeMode && !customWidget &&
1128 wasProviderReady && !isProviderReady) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001129 c.markDeleted(
1130 "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07001131 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001132 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07001133 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07001134 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1135 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08001136
Sunny Goyal53f96722015-07-13 19:54:53 -07001137 // The provider is available. So the widget is either
1138 // available or not available. We do not need to track
1139 // any future restore updates.
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001140 int status = c.restoreFlag &
Sunny Goyal53f96722015-07-13 19:54:53 -07001141 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07001142 if (!wasProviderReady) {
1143 // If provider was not previously ready, update the
1144 // status and UI flag.
1145
1146 // Id would be valid only if the widget restore broadcast was received.
1147 if (isIdValid) {
Sunny Goyal86df1382016-08-10 15:03:22 -07001148 status |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07001149 } else {
1150 status &= ~LauncherAppWidgetInfo
1151 .FLAG_PROVIDER_NOT_READY;
1152 }
1153 }
1154 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07001155 } else {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001156 Log.v(TAG, "Widget restore pending id=" + c.id
Sunny Goyal651077b2014-06-30 14:15:31 -07001157 + " appWidgetId=" + appWidgetId
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001158 + " status =" + c.restoreFlag);
Sunny Goyal651077b2014-06-30 14:15:31 -07001159 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07001160 component);
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001161 appWidgetInfo.restoreStatus = c.restoreFlag;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001162 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07001163
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001164 if (c.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_RESTORE_STARTED)) {
Sunny Goyal94485362014-09-18 16:13:58 -07001165 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001166 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07001167 // App restore has started. Update the flag
1168 appWidgetInfo.restoreStatus |=
1169 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001170 } else if (!isSafeMode) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001171 c.markDeleted("Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07001172 continue;
1173 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07001174
1175 appWidgetInfo.installProgress =
1176 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07001177 }
Sunny Goyal86df1382016-08-10 15:03:22 -07001178 if (appWidgetInfo.hasRestoreFlag(
1179 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG)) {
Sunny Goyal81e44912017-01-14 15:05:14 -08001180 appWidgetInfo.bindOptions = c.parseIntent();
Sunny Goyal86df1382016-08-10 15:03:22 -07001181 }
Sunny Goyalff572272014-07-23 13:58:07 -07001182
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001183 c.applyCommonProperties(appWidgetInfo);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001184 appWidgetInfo.spanX = c.getInt(spanXIndex);
1185 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001186 appWidgetInfo.user = c.user;
Joe Onorato36115782010-06-17 13:28:48 -04001187
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001188 if (!c.isOnWorkspaceOrHotseat()) {
1189 c.markDeleted("Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001190 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001191 continue;
1192 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001193
Adam Cohen59400422014-03-05 18:07:04 -08001194 if (!customWidget) {
1195 String providerName =
1196 appWidgetInfo.providerName.flattenToString();
1197 if (!providerName.equals(savedProvider) ||
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001198 (appWidgetInfo.restoreStatus != c.restoreFlag)) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001199 c.updater()
1200 .put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1201 providerName)
1202 .put(LauncherSettings.Favorites.RESTORED,
1203 appWidgetInfo.restoreStatus)
1204 .commit();
Adam Cohen59400422014-03-05 18:07:04 -08001205 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001206 }
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001207 c.checkAndAddItem(appWidgetInfo, sBgDataModel);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001208 }
Joe Onorato36115782010-06-17 13:28:48 -04001209 break;
1210 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001211 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001212 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001213 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001214 }
1215 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001216 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001217 }
1218
Winson Chungba9c37f2013-08-30 14:11:37 -07001219 // Break early if we've stopped loading
1220 if (mStopped) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001221 sBgDataModel.clear();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001222 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07001223 }
1224
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001225 // Remove dead items
1226 if (c.commitDeleted()) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001227 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07001228 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
1229 .call(contentResolver,
1230 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
1231 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
1232 for (long folderId : deletedFolderIds) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001233 sBgDataModel.workspaceItems.remove(sBgDataModel.folders.get(folderId));
1234 sBgDataModel.folders.remove(folderId);
1235 sBgDataModel.itemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001236 }
1237 }
1238
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001239 // Unpin shortcuts that don't exist on the workspace.
Sunny Goyalf75baa92016-11-22 03:23:51 +05301240 HashSet<ShortcutKey> pendingShortcuts =
1241 InstallShortcutReceiver.getPendingShortcuts(context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001242 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001243 MutableInt numTimesPinned = sBgDataModel.pinnedShortcutCounts.get(key);
Sunny Goyalf75baa92016-11-22 03:23:51 +05301244 if ((numTimesPinned == null || numTimesPinned.value == 0)
1245 && !pendingShortcuts.contains(key)) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001246 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301247 shortcutManager.unpinShortcut(key);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001248 }
1249 }
1250
Jon Miranda655ec422017-02-07 11:40:22 -08001251 FolderIconPreviewVerifier verifier =
1252 new FolderIconPreviewVerifier(mApp.getInvariantDeviceProfile());
1253 // Sort the folder items and make sure all items in the preview are high resolution.
Sunny Goyale9956a72016-09-01 17:24:47 -07001254 for (FolderInfo folder : sBgDataModel.folders) {
Sunny Goyal317698b2015-07-29 11:45:41 -07001255 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
Jon Miranda655ec422017-02-07 11:40:22 -08001256 verifier.setFolderInfo(folder);
1257
1258 int numItemsInPreview = 0;
Sunny Goyal317698b2015-07-29 11:45:41 -07001259 for (ShortcutInfo info : folder.contents) {
Jon Miranda655ec422017-02-07 11:40:22 -08001260 if (info.usingLowResIcon
1261 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION
1262 && verifier.isItemInPreview(info.rank)) {
Sunny Goyal3fe4a142016-12-15 17:40:07 -08001263 mIconCache.getTitleAndIcon(info, false);
Jon Miranda655ec422017-02-07 11:40:22 -08001264 numItemsInPreview++;
Sunny Goyal317698b2015-07-29 11:45:41 -07001265 }
Jon Miranda655ec422017-02-07 11:40:22 -08001266
1267 if (numItemsInPreview >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
Sunny Goyal317698b2015-07-29 11:45:41 -07001268 break;
1269 }
1270 }
1271 }
1272
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001273 c.commitRestoredItems();
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001274 if (!isSdCardReady && !pendingPackages.isEmpty()) {
1275 context.registerReceiver(
1276 new SdCardAvailableReceiver(
1277 LauncherModel.this, mContext, pendingPackages),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001278 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001279 null,
1280 sWorker);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001281 }
1282
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001283 // Remove any empty screens
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001284 ArrayList<Long> unusedScreens = new ArrayList<>(sBgDataModel.workspaceScreens);
Sunny Goyale9956a72016-09-01 17:24:47 -07001285 for (ItemInfo item: sBgDataModel.itemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001286 long screenId = item.screenId;
1287 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1288 unusedScreens.contains(screenId)) {
1289 unusedScreens.remove(screenId);
1290 }
1291 }
1292
1293 // If there are any empty screens remove them, and update.
1294 if (unusedScreens.size() != 0) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001295 sBgDataModel.workspaceScreens.removeAll(unusedScreens);
1296 updateWorkspaceScreenOrder(context, sBgDataModel.workspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001297 }
Joe Onorato36115782010-06-17 13:28:48 -04001298 }
Sunny Goyale26d1002016-06-20 14:52:14 -07001299 if (LauncherAppState.PROFILE_STARTUP) {
1300 Trace.endSection();
1301 }
Adam Cohene25af792013-06-06 23:08:25 -07001302 }
1303
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001304 /** Filters the set of items who are directly or indirectly (via another container) on the
1305 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08001306 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001307 ArrayList<ItemInfo> allWorkspaceItems,
1308 ArrayList<ItemInfo> currentScreenItems,
1309 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001310 // Purge any null ItemInfos
1311 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
1312 while (iter.hasNext()) {
1313 ItemInfo i = iter.next();
1314 if (i == null) {
1315 iter.remove();
1316 }
1317 }
1318
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001319 // Order the set of items by their containers first, this allows use to walk through the
1320 // list sequentially, build up a list of containers that are in the specified screen,
1321 // as well as all items in those containers.
1322 Set<Long> itemsOnScreen = new HashSet<Long>();
1323 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
1324 @Override
1325 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07001326 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001327 }
1328 });
1329 for (ItemInfo info : allWorkspaceItems) {
1330 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001331 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001332 currentScreenItems.add(info);
1333 itemsOnScreen.add(info.id);
1334 } else {
1335 otherScreenItems.add(info);
1336 }
1337 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1338 currentScreenItems.add(info);
1339 itemsOnScreen.add(info.id);
1340 } else {
1341 if (itemsOnScreen.contains(info.container)) {
1342 currentScreenItems.add(info);
1343 itemsOnScreen.add(info.id);
1344 } else {
1345 otherScreenItems.add(info);
1346 }
1347 }
1348 }
1349 }
1350
1351 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08001352 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001353 ArrayList<LauncherAppWidgetInfo> appWidgets,
1354 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
1355 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001356
1357 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001358 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001359 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08001360 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001361 currentScreenWidgets.add(widget);
1362 } else {
1363 otherScreenWidgets.add(widget);
1364 }
1365 }
1366 }
1367
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001368 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
1369 * right) */
1370 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Sunny Goyal87f784c2017-01-11 10:48:34 -08001371 final InvariantDeviceProfile profile = mApp.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07001372 final int screenCols = profile.numColumns;
1373 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001374 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07001375 @Override
1376 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07001377 if (lhs.container == rhs.container) {
1378 // Within containers, order by their spatial position in that container
1379 switch ((int) lhs.container) {
1380 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
1381 long lr = (lhs.screenId * screenCellCount +
1382 lhs.cellY * screenCols + lhs.cellX);
1383 long rr = (rhs.screenId * screenCellCount +
1384 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07001385 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07001386 }
1387 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
1388 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07001389 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07001390 }
1391 default:
Sunny Goyal3d706ad2017-03-06 16:56:39 -08001392 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07001393 throw new RuntimeException("Unexpected container type when " +
1394 "sorting workspace items.");
1395 }
1396 return 0;
1397 }
1398 } else {
1399 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07001400 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07001401 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001402 }
1403 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001404 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001405
Adam Cohendcd297f2013-06-18 13:13:40 -07001406 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
1407 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001408 final Runnable r = new Runnable() {
1409 @Override
1410 public void run() {
1411 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1412 if (callbacks != null) {
1413 callbacks.bindScreens(orderedScreens);
1414 }
1415 }
1416 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001417 mUiExecutor.execute(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07001418 }
1419
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001420 private void bindWorkspaceItems(final Callbacks oldCallbacks,
1421 final ArrayList<ItemInfo> workspaceItems,
1422 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07001423 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001424
1425 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07001426 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001427 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04001428 final int start = i;
1429 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001430 final Runnable r = new Runnable() {
1431 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001432 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001433 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001434 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07001435 callbacks.bindItems(workspaceItems, start, start+chunkSize,
1436 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001437 }
1438 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001439 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001440 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04001441 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001442
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001443 // Bind the widgets, one at a time
1444 N = appWidgets.size();
1445 for (int i = 0; i < N; i++) {
1446 final LauncherAppWidgetInfo widget = appWidgets.get(i);
1447 final Runnable r = new Runnable() {
1448 public void run() {
1449 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1450 if (callbacks != null) {
1451 callbacks.bindAppWidget(widget);
1452 }
1453 }
1454 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001455 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001456 }
1457 }
1458
1459 /**
1460 * Binds all loaded data to actual views on the main thread.
1461 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001462 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001463 final long t = SystemClock.uptimeMillis();
1464 Runnable r;
1465
1466 // Don't use these two variables in any of the callback runnables.
1467 // Otherwise we hold a reference to them.
1468 final Callbacks oldCallbacks = mCallbacks.get();
1469 if (oldCallbacks == null) {
1470 // This launcher has exited and nobody bothered to tell us. Just bail.
1471 Log.w(TAG, "LoaderTask running with no launcher");
1472 return;
1473 }
1474
Winson Chung9b9fb962013-11-15 15:39:34 -08001475 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07001476 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
1477 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
1478 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07001479
Sunny Goyale9956a72016-09-01 17:24:47 -07001480 synchronized (sBgDataModel) {
1481 workspaceItems.addAll(sBgDataModel.workspaceItems);
1482 appWidgets.addAll(sBgDataModel.appWidgets);
1483 orderedScreenIds.addAll(sBgDataModel.workspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001484 }
1485
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001486 final int currentScreen;
1487 {
1488 int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE
1489 ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen();
1490 if (currScreen >= orderedScreenIds.size()) {
1491 // There may be no workspace screens (just hotseat items and an empty page).
1492 currScreen = PagedView.INVALID_RESTORE_PAGE;
1493 }
1494 currentScreen = currScreen;
Winson Chung9b9fb962013-11-15 15:39:34 -08001495 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001496 final boolean validFirstPage = currentScreen >= 0;
1497 final long currentScreenId =
1498 validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID;
Winson Chung9b9fb962013-11-15 15:39:34 -08001499
Winson Chung9b9fb962013-11-15 15:39:34 -08001500 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07001501 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
1502 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
1503 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
1504 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001505
Winson Chung9b9fb962013-11-15 15:39:34 -08001506 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001507 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08001508 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001509 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001510 sortWorkspaceItemsSpatially(currentWorkspaceItems);
1511 sortWorkspaceItemsSpatially(otherWorkspaceItems);
1512
1513 // Tell the workspace that we're about to start binding items
1514 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001515 public void run() {
1516 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1517 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001518 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001519 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04001520 }
1521 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001522 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001523 mUiExecutor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001524
Adam Cohendcd297f2013-06-18 13:13:40 -07001525 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
1526
Sunny Goyalb265ba72017-02-14 15:03:45 -08001527 Executor mainExecutor = mUiExecutor;
Sunny Goyal527c7d32015-08-28 15:19:36 -07001528 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07001529 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001530
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001531 // In case of validFirstPage, only bind the first screen, and defer binding the
1532 // remaining screens after first onDraw (and an optional the fade animation whichever
1533 // happens later).
1534 // This ensures that the first screen is immediately visible (eg. during rotation)
1535 // In case of !validFirstPage, bind all pages one after other.
1536 final Executor deferredExecutor =
Sunny Goyalb265ba72017-02-14 15:03:45 -08001537 validFirstPage ? new ViewOnDrawExecutor(mUiExecutor) : mainExecutor;
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001538
1539 mainExecutor.execute(new Runnable() {
1540 @Override
1541 public void run() {
1542 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1543 if (callbacks != null) {
1544 callbacks.finishFirstPageBind(
1545 validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null);
1546 }
1547 }
1548 });
Sunny Goyal527c7d32015-08-28 15:19:36 -07001549
Sunny Goyal44c06432016-04-02 10:56:02 -07001550 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001551
1552 // Tell the workspace that we're done binding items
1553 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001554 public void run() {
1555 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1556 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001557 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04001558 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001559
Sunny Goyal639e9062015-08-19 19:17:06 -07001560 mIsLoadingAndBindingWorkspace = false;
1561
1562 // Run all the bind complete runnables after workspace is bound.
1563 if (!mBindCompleteRunnables.isEmpty()) {
1564 synchronized (mBindCompleteRunnables) {
1565 for (final Runnable r : mBindCompleteRunnables) {
1566 runOnWorkerThread(r);
1567 }
1568 mBindCompleteRunnables.clear();
1569 }
1570 }
1571
Winson Chung98e030b2012-05-07 16:01:11 -07001572 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04001573 if (DEBUG_LOADERS) {
1574 Log.d(TAG, "bound workspace in "
1575 + (SystemClock.uptimeMillis()-t) + "ms");
1576 }
Winson Chung36a62fe2012-05-06 18:04:42 -07001577
Joe Onorato36115782010-06-17 13:28:48 -04001578 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001579 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001580 deferredExecutor.execute(r);
1581
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001582 if (validFirstPage) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001583 r = new Runnable() {
1584 public void run() {
1585 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1586 if (callbacks != null) {
1587 // We are loading synchronously, which means, some of the pages will be
1588 // bound after first draw. Inform the callbacks that page binding is
1589 // not complete, and schedule the remaining pages.
1590 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
1591 callbacks.onPageBoundSynchronously(currentScreen);
1592 }
1593 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
1594 }
1595 }
1596 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001597 mUiExecutor.execute(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07001598 }
Joe Onorato36115782010-06-17 13:28:48 -04001599 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001600
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001601 private void updateIconCache() {
1602 // Ignore packages which have a promise icon.
1603 HashSet<String> packagesToIgnore = new HashSet<>();
Sunny Goyale9956a72016-09-01 17:24:47 -07001604 synchronized (sBgDataModel) {
1605 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001606 if (info instanceof ShortcutInfo) {
1607 ShortcutInfo si = (ShortcutInfo) info;
1608 if (si.isPromise() && si.getTargetComponent() != null) {
1609 packagesToIgnore.add(si.getTargetComponent().getPackageName());
1610 }
1611 } else if (info instanceof LauncherAppWidgetInfo) {
1612 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
1613 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
1614 packagesToIgnore.add(lawi.providerName.getPackageName());
1615 }
1616 }
1617 }
1618 }
1619 mIconCache.updateDbIcons(packagesToIgnore);
1620 }
1621
Joe Onorato36115782010-06-17 13:28:48 -04001622 private void onlyBindAllApps() {
1623 final Callbacks oldCallbacks = mCallbacks.get();
1624 if (oldCallbacks == null) {
1625 // This launcher has exited and nobody bothered to tell us. Just bail.
1626 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1627 return;
1628 }
1629
1630 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07001631 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001632 final ArrayList<AppInfo> list
1633 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07001634 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001635 public void run() {
1636 final long t = SystemClock.uptimeMillis();
1637 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1638 if (callbacks != null) {
1639 callbacks.bindAllApplications(list);
1640 }
1641 if (DEBUG_LOADERS) {
1642 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08001643 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04001644 }
1645 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07001646 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001647 mUiExecutor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04001648 }
1649
Winson Chung64359a52013-07-08 17:17:08 -07001650 private void loadAllApps() {
1651 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04001652
Joe Onorato36115782010-06-17 13:28:48 -04001653 final Callbacks oldCallbacks = mCallbacks.get();
1654 if (oldCallbacks == null) {
1655 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07001656 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04001657 return;
1658 }
1659
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001660 final List<UserHandle> profiles = mUserManager.getUserProfiles();
Kenny Guyed131872014-04-30 03:02:21 +01001661
Winson Chung64359a52013-07-08 17:17:08 -07001662 // Clear the list of apps
1663 mBgAllAppsList.clear();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001664 for (UserHandle user : profiles) {
Kenny Guyed131872014-04-30 03:02:21 +01001665 // Query for the set of apps
1666 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal3e9be432017-01-05 15:22:41 -08001667 final List<LauncherActivityInfo> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01001668 if (DEBUG_LOADERS) {
1669 Log.d(TAG, "getActivityList took "
1670 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
1671 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
1672 }
1673 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08001674 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01001675 if (apps == null || apps.isEmpty()) {
1676 return;
1677 }
Kenny Guyff05f432016-01-22 17:48:29 +00001678 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01001679 // Create the ApplicationInfos
1680 for (int i = 0; i < apps.size(); i++) {
Sunny Goyal3e9be432017-01-05 15:22:41 -08001681 LauncherActivityInfo app = apps.get(i);
Kenny Guyed131872014-04-30 03:02:21 +01001682 // This builds the icon bitmaps.
Sunny Goyal24bb66a2017-03-21 15:12:01 -07001683 mBgAllAppsList.add(new AppInfo(app, user, quietMode), app);
Kenny Guyed131872014-04-30 03:02:21 +01001684 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08001685
Sunny Goyal756a28a2015-04-23 17:07:55 -07001686 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
1687 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07001688 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07001689
1690 @Override
1691 public void run() {
1692 heuristic.processUserApps(apps);
1693 }
Sunny Goyal639e9062015-08-19 19:17:06 -07001694 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001695 mUiExecutor.execute(new Runnable() {
Sunny Goyal639e9062015-08-19 19:17:06 -07001696
Sunny Goyalb265ba72017-02-14 15:03:45 -08001697 @Override
1698 public void run() {
1699 // Check isLoadingWorkspace on the UI thread, as it is updated on
1700 // the UI thread.
1701 if (mIsLoadingAndBindingWorkspace) {
1702 synchronized (mBindCompleteRunnables) {
1703 mBindCompleteRunnables.add(r);
1704 }
1705 } else {
1706 runOnWorkerThread(r);
1707 }
1708 }
1709 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08001710 }
Winson Chung64359a52013-07-08 17:17:08 -07001711 }
Mario Bertschler817afa32017-03-15 11:56:47 -07001712
1713 if (FeatureFlags.LAUNCHER3_PROMISE_APPS_IN_ALL_APPS) {
1714 // get all active sessions and add them to the all apps list
1715 PackageInstallerCompat installer = PackageInstallerCompat.getInstance(mContext);
1716 for (PackageInstaller.SessionInfo info : installer.getAllVerifiedSessions()) {
1717 mBgAllAppsList.addPromiseApp(mContext,
1718 PackageInstallInfo.fromInstallingState(info));
1719 }
1720 }
1721
Bjorn Bringert85f418d2013-09-06 12:50:05 +01001722 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001723 final ArrayList<AppInfo> added = mBgAllAppsList.added;
1724 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07001725
Mario Bertschler817afa32017-03-15 11:56:47 -07001726
Winson Chung64359a52013-07-08 17:17:08 -07001727 // Post callback on main thread
Sunny Goyalb265ba72017-02-14 15:03:45 -08001728 mUiExecutor.execute(new Runnable() {
Winson Chung64359a52013-07-08 17:17:08 -07001729 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07001730
Winson Chung64359a52013-07-08 17:17:08 -07001731 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07001732 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07001733 if (callbacks != null) {
1734 callbacks.bindAllApplications(added);
1735 if (DEBUG_LOADERS) {
1736 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001737 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07001738 }
1739 } else {
1740 Log.i(TAG, "not binding apps: no Launcher activity");
1741 }
1742 }
1743 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07001744 // Cleanup any data stored for a deleted user.
1745 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04001746 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07001747 Log.d(TAG, "Icons processed in "
1748 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08001749 }
1750 }
1751
Sunny Goyaldd96a5e2017-02-17 11:22:34 -08001752 private void loadDeepShortcuts() {
1753 if (!mModelLoaded) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07001754 sBgDataModel.deepShortcutMap.clear();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301755 DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(mContext);
1756 mHasShortcutHostPermission = shortcutManager.hasHostPermission();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001757 if (mHasShortcutHostPermission) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001758 for (UserHandle user : mUserManager.getUserProfiles()) {
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001759 if (mUserManager.isUserUnlocked(user)) {
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301760 List<ShortcutInfoCompat> shortcuts =
1761 shortcutManager.queryForAllShortcuts(user);
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07001762 sBgDataModel.updateDeepShortcutMap(null, user, shortcuts);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001763 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001764 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001765 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001766 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001767 }
Joe Onorato36115782010-06-17 13:28:48 -04001768 }
1769
Tony Wickham80f57872016-06-29 18:12:15 -07001770 public void bindDeepShortcuts() {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07001771 final MultiHashMap<ComponentKey, String> shortcutMapCopy =
1772 sBgDataModel.deepShortcutMap.clone();
Tony Wickham80f57872016-06-29 18:12:15 -07001773 Runnable r = new Runnable() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001774 @Override
1775 public void run() {
1776 Callbacks callbacks = getCallback();
1777 if (callbacks != null) {
1778 callbacks.bindDeepShortcutMap(shortcutMapCopy);
1779 }
1780 }
Tony Wickham80f57872016-06-29 18:12:15 -07001781 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001782 mUiExecutor.execute(r);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001783 }
1784
Sunny Goyal75b0f552015-05-20 21:57:06 -07001785 /**
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001786 * Refreshes the cached shortcuts if the shortcut permission has changed.
1787 * Current implementation simply reloads the workspace, but it can be optimized to
1788 * use partial updates similar to {@link UserManagerCompat}
1789 */
1790 public void refreshShortcutsIfRequired() {
Sunny Goyalf5e37442016-11-02 10:31:24 -07001791 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001792 sWorker.removeCallbacks(mShortcutPermissionCheckRunnable);
1793 sWorker.post(mShortcutPermissionCheckRunnable);
1794 }
1795 }
1796
1797 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -07001798 * Called when the icons for packages have been updated in the icon cache.
1799 */
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001800 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandle user) {
Sunny Goyal75b0f552015-05-20 21:57:06 -07001801 // If any package icon has changed (app was updated while launcher was dead),
1802 // update the corresponding shortcuts.
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001803 enqueueModelUpdateTask(new CacheDataUpdatedTask(
1804 CacheDataUpdatedTask.OP_CACHE_UPDATE, user, updatedPackages));
Sunny Goyal75b0f552015-05-20 21:57:06 -07001805 }
1806
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001807 void enqueueModelUpdateTask(BaseModelUpdateTask task) {
Jon Mirandaf28629a2017-02-23 15:12:42 -08001808 if (!mModelLoaded && mLoaderTask == null) {
1809 if (DEBUG_LOADERS) {
1810 Log.d(TAG, "enqueueModelUpdateTask Ignoring task since loader is pending=" + task);
1811 }
1812 return;
1813 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001814 task.init(this);
1815 runOnWorkerThread(task);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001816 }
1817
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001818 /**
1819 * A task to be executed on the current callbacks on the UI thread.
1820 * If there is no current callbacks, the task is ignored.
1821 */
1822 public interface CallbackTask {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001823
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001824 void execute(Callbacks callbacks);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001825 }
1826
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001827 /**
1828 * A runnable which changes/updates the data model of the launcher based on certain events.
1829 */
1830 public static abstract class BaseModelUpdateTask implements Runnable {
Joe Onorato36115782010-06-17 13:28:48 -04001831
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001832 private LauncherModel mModel;
Sunny Goyalb265ba72017-02-14 15:03:45 -08001833 private Executor mUiExecutor;
Joe Onorato36115782010-06-17 13:28:48 -04001834
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001835 /* package private */
1836 void init(LauncherModel model) {
1837 mModel = model;
Sunny Goyalb265ba72017-02-14 15:03:45 -08001838 mUiExecutor = mModel.mUiExecutor;
Joe Onorato36115782010-06-17 13:28:48 -04001839 }
1840
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001841 @Override
Joe Onorato36115782010-06-17 13:28:48 -04001842 public void run() {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001843 if (!mModel.mHasLoaderCompletedOnce) {
Sunny Goyalc905efc2015-05-06 09:54:53 -07001844 // Loader has not yet run.
1845 return;
1846 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001847 execute(mModel.mApp, sBgDataModel, mModel.mBgAllAppsList);
1848 }
Joe Onorato36115782010-06-17 13:28:48 -04001849
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001850 /**
1851 * Execute the actual task. Called on the worker thread.
1852 */
1853 public abstract void execute(
1854 LauncherAppState app, BgDataModel dataModel, AllAppsList apps);
Sunny Goyale0f58d72014-11-10 18:05:31 -08001855
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001856 /**
1857 * Schedules a {@param task} to be executed on the current callbacks.
1858 */
1859 public final void scheduleCallbackTask(final CallbackTask task) {
1860 final Callbacks callbacks = mModel.getCallback();
Sunny Goyalb265ba72017-02-14 15:03:45 -08001861 mUiExecutor.execute(new Runnable() {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001862 public void run() {
1863 Callbacks cb = mModel.getCallback();
1864 if (callbacks == cb && cb != null) {
1865 task.execute(callbacks);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07001866 }
1867 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001868 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001869 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001870
1871 public ModelWriter getModelWriter() {
1872 // Updates from model task, do not deal with icon position in hotseat.
1873 return mModel.getWriter(false /* hasVerticalHotseat */);
1874 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001875 }
1876
Sunny Goyal1b072632017-01-18 11:30:23 -08001877 public void updateAndBindShortcutInfo(final ShortcutInfo si, final ShortcutInfoCompat info) {
1878 updateAndBindShortcutInfo(new Provider<ShortcutInfo>() {
1879 @Override
1880 public ShortcutInfo get() {
1881 si.updateFromDeepShortcutInfo(info, mApp.getContext());
1882 si.iconBitmap = LauncherIcons.createShortcutIcon(info, mApp.getContext());
1883 return si;
1884 }
1885 });
1886 }
1887
Sunny Goyal10923b32016-07-20 15:42:44 -07001888 /**
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001889 * Utility method to update a shortcut on the background thread.
Sunny Goyal10923b32016-07-20 15:42:44 -07001890 */
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001891 public void updateAndBindShortcutInfo(final Provider<ShortcutInfo> shortcutProvider) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001892 enqueueModelUpdateTask(new ExtendedModelTask() {
Sunny Goyal10923b32016-07-20 15:42:44 -07001893 @Override
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001894 public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001895 ShortcutInfo info = shortcutProvider.get();
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001896 ArrayList<ShortcutInfo> update = new ArrayList<>();
Sunny Goyal10923b32016-07-20 15:42:44 -07001897 update.add(info);
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001898 bindUpdatedShortcuts(update, info.user);
Sunny Goyal10923b32016-07-20 15:42:44 -07001899 }
1900 });
1901 }
1902
Sunny Goyald164b7f2016-10-12 20:49:31 -07001903 private void bindWidgetsModel(final Callbacks callbacks) {
1904 final MultiHashMap<PackageItemInfo, WidgetItem> widgets
1905 = mBgWidgetsModel.getWidgetsMap().clone();
Sunny Goyalb265ba72017-02-14 15:03:45 -08001906 mUiExecutor.execute(new Runnable() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001907 @Override
1908 public void run() {
1909 Callbacks cb = getCallback();
1910 if (callbacks == cb && cb != null) {
Sunny Goyald164b7f2016-10-12 20:49:31 -07001911 callbacks.bindAllWidgets(widgets);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001912 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07001913 }
1914 });
1915 }
1916
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001917 public void refreshAndBindWidgetsAndShortcuts(
1918 final Callbacks callbacks, final boolean bindFirst) {
1919 runOnWorkerThread(new Runnable() {
1920 @Override
1921 public void run() {
1922 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
Sunny Goyald164b7f2016-10-12 20:49:31 -07001923 bindWidgetsModel(callbacks);
Sunny Goyal31860582015-09-18 08:38:57 -07001924 }
Sunny Goyald164b7f2016-10-12 20:49:31 -07001925 ArrayList<WidgetItem> allWidgets = mBgWidgetsModel.update(mApp.getContext());
1926 bindWidgetsModel(callbacks);
1927
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001928 // update the Widget entries inside DB on the worker thread.
Sunny Goyal87f784c2017-01-11 10:48:34 -08001929 mApp.getWidgetCache().removeObsoletePreviews(allWidgets);
Sunny Goyal31860582015-09-18 08:38:57 -07001930 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001931 });
Michael Jurkac402cd92013-05-20 15:49:32 +02001932 }
1933
Sunny Goyal651077b2014-06-30 14:15:31 -07001934 static boolean isValidProvider(AppWidgetProviderInfo provider) {
1935 return (provider != null) && (provider.provider != null)
1936 && (provider.provider.getPackageName() != null);
1937 }
1938
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001939 public void dumpState(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
1940 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
1941 writer.println(prefix + "All apps list: size=" + mBgAllAppsList.data.size());
1942 for (AppInfo info : mBgAllAppsList.data) {
1943 writer.println(prefix + " title=\"" + info.title + "\" iconBitmap=" + info.iconBitmap
1944 + " componentName=" + info.componentName.getPackageName());
1945 }
Joe Onorato36115782010-06-17 13:28:48 -04001946 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001947 sBgDataModel.dump(prefix, fd, writer, args);
Joe Onoratobe386092009-11-17 17:32:16 -08001948 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08001949
1950 public Callbacks getCallback() {
1951 return mCallbacks != null ? mCallbacks.get() : null;
1952 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07001953
1954 /**
1955 * @return {@link FolderInfo} if its already loaded.
1956 */
1957 public FolderInfo findFolderById(Long folderId) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001958 synchronized (sBgDataModel) {
1959 return sBgDataModel.folders.get(folderId);
Sunny Goyal18bf8e22015-04-08 18:13:46 -07001960 }
1961 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07001962
1963 /**
1964 * @return the looper for the worker thread which can be used to start background tasks.
1965 */
1966 public static Looper getWorkerLooper() {
1967 return sWorkerThread.getLooper();
1968 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969}