blob: c84eecbc2afcb1315d63411a09f2f4a778f89f46 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Romain Guy629de3e2010-01-13 12:20:59 -080019import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070020import android.content.BroadcastReceiver;
21import android.content.ComponentName;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070022import android.content.ContentProviderOperation;
23import android.content.ContentResolver;
24import android.content.ContentValues;
25import android.content.Context;
26import android.content.Intent;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070027import android.content.IntentFilter;
Sunny Goyal3e9be432017-01-05 15:22:41 -080028import android.content.pm.LauncherActivityInfo;
Mario Bertschler817afa32017-03-15 11:56:47 -070029import android.content.pm.PackageInstaller;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.net.Uri;
Joe Onorato36115782010-06-17 13:28:48 -040031import android.os.Handler;
32import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070033import android.os.Looper;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040035import android.os.SystemClock;
Sunny Goyale26d1002016-06-20 14:52:14 -070036import android.os.Trace;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080037import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070038import android.support.annotation.Nullable;
Winson Chunga90303b2013-11-15 13:05:06 -080039import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070040import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080041import android.util.LongSparseArray;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070042import android.util.MutableInt;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010043
Sunny Goyalffe83f12014-08-14 17:39:34 -070044import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010045import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070046import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070047import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010048import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyal3d706ad2017-03-06 16:56:39 -080049import com.android.launcher3.config.FeatureFlags;
Tony Wickham827cef22016-03-17 15:39:39 -070050import com.android.launcher3.dynamicui.ExtractionUtils;
Sunny Goyal26119432016-02-18 22:09:23 +000051import com.android.launcher3.folder.Folder;
52import com.android.launcher3.folder.FolderIcon;
Jon Miranda655ec422017-02-07 11:40:22 -080053import com.android.launcher3.folder.FolderIconPreviewVerifier;
Sunny Goyal1b072632017-01-18 11:30:23 -080054import com.android.launcher3.graphics.LauncherIcons;
Sunny Goyal1acb9e92016-05-16 12:41:09 -070055import com.android.launcher3.logging.FileLog;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070056import com.android.launcher3.model.AddWorkspaceItemsTask;
Sunny Goyale9956a72016-09-01 17:24:47 -070057import com.android.launcher3.model.BgDataModel;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070058import com.android.launcher3.model.CacheDataUpdatedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080059import com.android.launcher3.model.ExtendedModelTask;
Sunny Goyalf862a262015-12-14 14:27:38 -080060import com.android.launcher3.model.GridSizeMigrationTask;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -080061import com.android.launcher3.model.LoaderCursor;
Sunny Goyal43bf11d2017-02-02 13:52:53 -080062import com.android.launcher3.model.ModelWriter;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070063import com.android.launcher3.model.PackageInstallStateChangedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080064import com.android.launcher3.model.PackageItemInfo;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070065import com.android.launcher3.model.PackageUpdatedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080066import com.android.launcher3.model.SdCardAvailableReceiver;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070067import com.android.launcher3.model.ShortcutsChangedTask;
68import com.android.launcher3.model.UserLockStateChangedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080069import com.android.launcher3.model.WidgetItem;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070070import com.android.launcher3.model.WidgetsModel;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -070071import com.android.launcher3.provider.ImportDataTask;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070072import com.android.launcher3.provider.LauncherDbUtils;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070073import com.android.launcher3.shortcuts.DeepShortcutManager;
74import com.android.launcher3.shortcuts.ShortcutInfoCompat;
75import com.android.launcher3.shortcuts.ShortcutKey;
Robin Lee26ace122015-03-16 19:41:43 +000076import com.android.launcher3.util.ComponentKey;
Sunny Goyalb265ba72017-02-14 15:03:45 -080077import com.android.launcher3.util.LooperIdleLock;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070078import com.android.launcher3.util.ManagedProfileHeuristic;
Tony Wickhamd82a39d2016-07-01 15:44:13 -070079import com.android.launcher3.util.MultiHashMap;
Sunny Goyald09c3702016-04-06 16:18:20 -070080import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham86222d22017-03-29 15:30:43 -070081import com.android.launcher3.util.PackageUserKey;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070082import com.android.launcher3.util.Preconditions;
Sunny Goyal2bcbe132016-11-16 09:23:42 -080083import com.android.launcher3.util.Provider;
Adam Cohen091440a2015-03-18 14:16:05 -070084import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070085import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080086
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -080087import java.io.FileDescriptor;
88import java.io.PrintWriter;
Michael Jurkac2f801e2011-07-12 14:19:46 -070089import java.lang.ref.WeakReference;
Michael Jurkac2f801e2011-07-12 14:19:46 -070090import java.util.ArrayList;
91import java.util.Collections;
92import java.util.Comparator;
93import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070094import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070095import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070096import java.util.List;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070097import java.util.Map;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070098import java.util.Set;
Sunny Goyaldd96a5e2017-02-17 11:22:34 -080099import java.util.concurrent.CancellationException;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700100import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -0700101
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102/**
103 * Maintains in-memory state of the Launcher. It is expected that there should be only one
104 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700105 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106 */
Kenny Guyed131872014-04-30 03:02:21 +0100107public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +0100108 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800109 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -0400110 private static final boolean DEBUG_RECEIVER = false;
Chris Wrenb358f812014-04-16 13:37:00 -0400111
Joe Onorato9c1289c2009-08-17 11:03:03 -0400112 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700113
Joe Onorato36115782010-06-17 13:28:48 -0400114 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500115 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800116
Sunny Goyalb265ba72017-02-14 15:03:45 -0800117 private final MainThreadExecutor mUiExecutor = new MainThreadExecutor();
Adam Cohen091440a2015-03-18 14:16:05 -0700118 @Thunk final LauncherAppState mApp;
119 @Thunk final Object mLock = new Object();
Adam Cohen091440a2015-03-18 14:16:05 -0700120 @Thunk LoaderTask mLoaderTask;
121 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700122 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Adam Cohen091440a2015-03-18 14:16:05 -0700124 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700125 static {
126 sWorkerThread.start();
127 }
Adam Cohen091440a2015-03-18 14:16:05 -0700128 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700129
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800130 // Indicates whether the current model data is valid or not.
131 // We start off with everything not loaded. After that, we assume that
Joe Onoratocc67f472010-06-08 10:54:30 -0700132 // our monitoring of the package manager provides all updates and we never
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800133 // need to do a requery. This is only ever touched from the loader thread.
134 private boolean mModelLoaded;
Hyunyoung Song6aa37292017-02-06 10:46:24 -0800135 public boolean isModelLoaded() {
136 synchronized (mLock) {
137 return mModelLoaded && mLoaderTask == null;
138 }
139 }
Joe Onoratocc67f472010-06-08 10:54:30 -0700140
Sunny Goyal756a28a2015-04-23 17:07:55 -0700141 /**
142 * Set of runnables to be called on the background thread after the workspace binding
143 * is complete.
144 */
145 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
146
Adam Cohen091440a2015-03-18 14:16:05 -0700147 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700149 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800150 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700151 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800152 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800153
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700154 private boolean mHasShortcutHostPermission;
155 // Runnable to check if the shortcuts permission has changed.
156 private final Runnable mShortcutPermissionCheckRunnable = new Runnable() {
157 @Override
158 public void run() {
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800159 if (mModelLoaded) {
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530160 boolean hasShortcutHostPermission =
161 DeepShortcutManager.getInstance(mApp.getContext()).hasHostPermission();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700162 if (hasShortcutHostPermission != mHasShortcutHostPermission) {
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800163 forceReload();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700164 }
165 }
166 }
167 };
168
Sunny Goyale9956a72016-09-01 17:24:47 -0700169 /**
170 * All the static data should be accessed on the background thread, A lock should be acquired
171 * on this object when accessing any data from this model.
172 */
173 static final BgDataModel sBgDataModel = new BgDataModel();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700174
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700175 // </ only access in worker thread >
176
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700177 private final IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700179 private final LauncherAppsCompat mLauncherApps;
180 private final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100181
Joe Onorato9c1289c2009-08-17 11:03:03 -0400182 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700183 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400184 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700185 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400186 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700187 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
188 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700189 public void bindScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700190 public void finishFirstPageBind(ViewOnDrawExecutor executor);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800191 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400192 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200193 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700194 public void bindAppsAdded(ArrayList<Long> newScreens,
195 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700196 ArrayList<ItemInfo> addAnimated,
197 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200198 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700199 public void bindPromiseAppProgressUpdated(PromiseAppInfo app);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700200 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800201 ArrayList<ShortcutInfo> removed, UserHandle user);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700202 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700203 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700204 public void bindWorkspaceComponentsRemoved(
205 HashSet<String> packageNames, HashSet<ComponentName> components,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800206 UserHandle user);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700207 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800208 public void notifyWidgetProvidersChanged();
Sunny Goyald164b7f2016-10-12 20:49:31 -0700209 public void bindAllWidgets(MultiHashMap<PackageItemInfo, WidgetItem> widgets);
Adam Cohen1462de32012-07-24 22:34:36 -0700210 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700211 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700212 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMap);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400213 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530215 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800216 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400217 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100218 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Sunny Goyald164b7f2016-10-12 20:49:31 -0700219 mBgWidgetsModel = new WidgetsModel(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800220 mIconCache = iconCache;
221
Kenny Guyed131872014-04-30 03:02:21 +0100222 mLauncherApps = LauncherAppsCompat.getInstance(context);
223 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800224 }
225
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700226 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
227 * posted on the worker thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700228 private static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700229 if (sWorkerThread.getThreadId() == Process.myTid()) {
230 r.run();
231 } else {
232 // If we are not on the worker thread, then post to the worker handler
233 sWorker.post(r);
234 }
235 }
236
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700237 public void setPackageState(PackageInstallInfo installInfo) {
238 enqueueModelUpdateTask(new PackageInstallStateChangedTask(installInfo));
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500239 }
240
Sunny Goyal756adbc2015-04-16 15:20:51 -0700241 /**
242 * Updates the icons and label of all pending icons for the provided package name.
243 */
244 public void updateSessionDisplayInfo(final String packageName) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700245 HashSet<String> packages = new HashSet<>();
246 packages.add(packageName);
247 enqueueModelUpdateTask(new CacheDataUpdatedTask(
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800248 CacheDataUpdatedTask.OP_SESSION_UPDATE, Process.myUserHandle(), packages));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800249 }
250
251 /**
252 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800253 */
Sunny Goyal2bcbe132016-11-16 09:23:42 -0800254 public void addAndBindAddedWorkspaceItems(List<ItemInfo> workspaceApps) {
255 addAndBindAddedWorkspaceItems(Provider.of(workspaceApps));
256 }
257
258 /**
259 * Adds the provided items to the workspace.
260 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700261 public void addAndBindAddedWorkspaceItems(
Sunny Goyal2bcbe132016-11-16 09:23:42 -0800262 Provider<List<ItemInfo>> appsProvider) {
263 enqueueModelUpdateTask(new AddWorkspaceItemsTask(appsProvider));
Winson Chung64359a52013-07-08 17:17:08 -0700264 }
265
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800266 public ModelWriter getWriter(boolean hasVerticalHotseat) {
267 return new ModelWriter(mApp.getContext(), sBgDataModel, hasVerticalHotseat);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268 }
269
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700270 static void checkItemInfoLocked(
271 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
Sunny Goyale9956a72016-09-01 17:24:47 -0700272 ItemInfo modelItem = sBgDataModel.itemsIdMap.get(itemId);
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700273 if (modelItem != null && item != modelItem) {
274 // check all the data is consistent
275 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
276 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
277 ShortcutInfo shortcut = (ShortcutInfo) item;
278 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
279 modelShortcut.intent.filterEquals(shortcut.intent) &&
280 modelShortcut.id == shortcut.id &&
281 modelShortcut.itemType == shortcut.itemType &&
282 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700283 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700284 modelShortcut.cellX == shortcut.cellX &&
285 modelShortcut.cellY == shortcut.cellY &&
286 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700287 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700288 // For all intents and purposes, this is the same object
289 return;
290 }
291 }
292
293 // the modelItem needs to match up perfectly with item if our model is
294 // to be consistent with the database-- for now, just require
295 // modelItem == item or the equality check above
296 String msg = "item: " + ((item != null) ? item.toString() : "null") +
297 "modelItem: " +
298 ((modelItem != null) ? modelItem.toString() : "null") +
299 "Error: ItemInfo passed to checkItemInfo doesn't match original";
300 RuntimeException e = new RuntimeException(msg);
301 if (stackTrace != null) {
302 e.setStackTrace(stackTrace);
303 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800304 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700305 }
306 }
307
Michael Jurka816474f2012-06-25 14:49:02 -0700308 static void checkItemInfo(final ItemInfo item) {
309 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
310 final long itemId = item.id;
311 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700312 public void run() {
Sunny Goyale9956a72016-09-01 17:24:47 -0700313 synchronized (sBgDataModel) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700314 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700315 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700316 }
317 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700318 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700319 }
320
Joe Onorato9c1289c2009-08-17 11:03:03 -0400321 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700322 * Update the order of the workspace screens in the database. The array list contains
323 * a list of screen ids in the order that they should appear.
324 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700325 public static void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -0700326 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700327 final ContentResolver cr = context.getContentResolver();
328 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
329
330 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700331 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700332 while (iter.hasNext()) {
333 long id = iter.next();
334 if (id < 0) {
335 iter.remove();
336 }
337 }
338
339 Runnable r = new Runnable() {
340 @Override
341 public void run() {
Yura085c8532014-02-11 15:15:29 +0000342 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700343 // Clear the table
Yura085c8532014-02-11 15:15:29 +0000344 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -0700345 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700346 for (int i = 0; i < count; i++) {
347 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700348 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700349 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
350 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +0000351 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -0700352 }
Yura085c8532014-02-11 15:15:29 +0000353
354 try {
355 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
356 } catch (Exception ex) {
357 throw new RuntimeException(ex);
358 }
Winson Chung9e6a0a22013-08-27 11:58:12 -0700359
Sunny Goyale9956a72016-09-01 17:24:47 -0700360 synchronized (sBgDataModel) {
361 sBgDataModel.workspaceScreens.clear();
362 sBgDataModel.workspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -0700363 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700364 }
365 };
366 runOnWorkerThread(r);
367 }
368
369 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400370 * Set this as the current Launcher activity object for the loader.
371 */
372 public void initialize(Callbacks callbacks) {
373 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -0700374 Preconditions.assertUIThread();
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -0700375 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400376 }
377 }
378
Kenny Guyed131872014-04-30 03:02:21 +0100379 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800380 public void onPackageChanged(String packageName, UserHandle user) {
Kenny Guyed131872014-04-30 03:02:21 +0100381 int op = PackageUpdatedTask.OP_UPDATE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700382 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100383 }
384
385 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800386 public void onPackageRemoved(String packageName, UserHandle user) {
Sunny Goyalc2936bc2016-09-01 15:50:36 -0700387 onPackagesRemoved(user, packageName);
388 }
389
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800390 public void onPackagesRemoved(UserHandle user, String... packages) {
Kenny Guyed131872014-04-30 03:02:21 +0100391 int op = PackageUpdatedTask.OP_REMOVE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700392 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packages));
Kenny Guyed131872014-04-30 03:02:21 +0100393 }
394
395 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800396 public void onPackageAdded(String packageName, UserHandle user) {
Kenny Guyed131872014-04-30 03:02:21 +0100397 int op = PackageUpdatedTask.OP_ADD;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700398 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100399 }
400
401 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800402 public void onPackagesAvailable(String[] packageNames, UserHandle user,
Kenny Guyed131872014-04-30 03:02:21 +0100403 boolean replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700404 enqueueModelUpdateTask(
405 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100406 }
407
408 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800409 public void onPackagesUnavailable(String[] packageNames, UserHandle user,
Kenny Guyed131872014-04-30 03:02:21 +0100410 boolean replacing) {
411 if (!replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700412 enqueueModelUpdateTask(new PackageUpdatedTask(
413 PackageUpdatedTask.OP_UNAVAILABLE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100414 }
Kenny Guyed131872014-04-30 03:02:21 +0100415 }
416
Kenny Guy44cba692016-01-21 19:50:02 +0000417 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800418 public void onPackagesSuspended(String[] packageNames, UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700419 enqueueModelUpdateTask(new PackageUpdatedTask(
420 PackageUpdatedTask.OP_SUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000421 }
422
423 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800424 public void onPackagesUnsuspended(String[] packageNames, UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700425 enqueueModelUpdateTask(new PackageUpdatedTask(
426 PackageUpdatedTask.OP_UNSUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000427 }
428
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700429 @Override
430 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800431 UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700432 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, true));
Sunny Goyal50941fb2016-08-04 12:03:52 -0700433 }
434
435 public void updatePinnedShortcuts(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800436 UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700437 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, false));
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700438 }
439
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700440 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400441 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
442 * ACTION_PACKAGE_CHANGED.
443 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100444 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400445 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -0400446 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700447
Joe Onorato36115782010-06-17 13:28:48 -0400448 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +0100449 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -0700450 // If we have changed locale we need to clear out the labels in all apps/workspace.
451 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700452 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
453 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -0700454 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -0700455 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700456 } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
457 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
458 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800459 UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER);
Sunny Goyalda891c12016-03-18 18:29:24 -0700460 if (user != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700461 if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
462 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700463 enqueueModelUpdateTask(new PackageUpdatedTask(
464 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE, user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700465 }
466
467 // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so
468 // we need to run the state change task again.
469 if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
470 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700471 enqueueModelUpdateTask(new UserLockStateChangedTask(user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700472 }
Sunny Goyalda891c12016-03-18 18:29:24 -0700473 }
Tony Wickham827cef22016-03-17 15:39:39 -0700474 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
475 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700476 }
477 }
478
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800479 /**
480 * Reloads the workspace items from the DB and re-binds the workspace. This should generally
481 * not be called as DB updates are automatically followed by UI update
482 */
483 public void forceReload() {
484 synchronized (mLock) {
485 // Stop any existing loaders first, so they don't set mModelLoaded to true later
486 stopLoaderLocked();
487 mModelLoaded = false;
488 }
Winson Chungf0c6ae02012-03-21 16:10:31 -0700489
Reena Lee93f824a2011-09-23 17:20:28 -0700490 // Do this here because if the launcher activity is running it will be restarted.
491 // If it's not running startLoaderFromBackground will merely tell it that it needs
492 // to reload.
493 startLoaderFromBackground();
494 }
495
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700496 /**
497 * When the launcher is in the background, it's possible for it to miss paired
498 * configuration changes. So whenever we trigger the loader from the background
499 * tell the launcher that it needs to re-run the loader when it comes back instead
500 * of doing it now.
501 */
502 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800503 Callbacks callbacks = getCallback();
504 if (callbacks != null) {
505 // Only actually run the loader if they're not paused.
506 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -0700507 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700508 }
509 }
Joe Onorato36115782010-06-17 13:28:48 -0400510 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400511
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700512 /**
513 * If there is already a loader task running, tell it to stop.
514 */
515 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -0700516 LoaderTask oldTask = mLoaderTask;
517 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -0700518 oldTask.stopLocked();
519 }
Reena Lee93f824a2011-09-23 17:20:28 -0700520 }
521
Adam Cohen1a85c582014-09-30 09:48:49 -0700522 public boolean isCurrentCallbacks(Callbacks callbacks) {
523 return (mCallbacks != null && mCallbacks.get() == callbacks);
524 }
525
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700526 /**
527 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
528 * @return true if the page could be bound synchronously.
529 */
530 public boolean startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700531 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
532 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -0400533 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -0400534 // Don't bother to start the thread if we know it's not going to do anything
535 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -0700536 final Callbacks oldCallbacks = mCallbacks.get();
537 // Clear any pending bind-runnables from the synchronized load process.
Sunny Goyalb265ba72017-02-14 15:03:45 -0800538 mUiExecutor.execute(new Runnable() {
539 public void run() {
540 oldCallbacks.clearPendingBinds();
541 }
542 });
Sunny Goyal527c7d32015-08-28 15:19:36 -0700543
Joe Onorato36115782010-06-17 13:28:48 -0400544 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700545 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700546 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800547 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
548 && mModelLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700549 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700550 return true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700551 } else {
552 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
553 sWorker.post(mLoaderTask);
554 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400555 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400556 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700557 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400558 }
559
Joe Onorato36115782010-06-17 13:28:48 -0400560 public void stopLoader() {
561 synchronized (mLock) {
562 if (mLoaderTask != null) {
563 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400564 }
565 }
Joe Onorato36115782010-06-17 13:28:48 -0400566 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400567
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800568 /**
569 * Loads the workspace screen ids in an ordered list.
570 */
Sunny Goyale5bb7052015-07-27 14:36:07 -0700571 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -0700572 final ContentResolver contentResolver = context.getContentResolver();
573 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -0700574
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800575 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -0700576 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
577 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -0700578 }
579
Mario Bertschler817afa32017-03-15 11:56:47 -0700580 public void onInstallSessionCreated(final PackageInstallInfo sessionInfo) {
581 enqueueModelUpdateTask(new ExtendedModelTask() {
582 @Override
583 public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
584 apps.addPromiseApp(app.getContext(), sessionInfo);
585 if (!apps.added.isEmpty()) {
586 final ArrayList<AppInfo> arrayList = new ArrayList<>(apps.added);
587 apps.added.clear();
588 scheduleCallbackTask(new CallbackTask() {
589 @Override
590 public void execute(Callbacks callbacks) {
591 callbacks.bindAppsAdded(null, null, null, arrayList);
592 }
593 });
594 }
595 }
596 });
597 }
598
Joe Onorato36115782010-06-17 13:28:48 -0400599 /**
600 * Runnable for the thread that loads the contents of the launcher:
601 * - workspace icons
602 * - widgets
603 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700604 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -0400605 */
606 private class LoaderTask implements Runnable {
607 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -0700608 private int mPageToBindFirst;
609
Adam Cohen091440a2015-03-18 14:16:05 -0700610 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -0400611 private boolean mStopped;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700612
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700613 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -0400614 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -0700615 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400616 }
617
Joe Onorato36115782010-06-17 13:28:48 -0400618 private void waitForIdle() {
619 // Wait until the either we're stopped or the other threads are done.
620 // This way we don't start loading all apps until the workspace has settled
621 // down.
622 synchronized (LoaderTask.this) {
Sunny Goyalb265ba72017-02-14 15:03:45 -0800623 LooperIdleLock idleLock = new LooperIdleLock(this, Looper.getMainLooper());
624 // Just in case mFlushingWorkerThread changes but we aren't woken up,
625 // wait no longer than 1sec at a time
626 while (!mStopped && idleLock.awaitLocked(1000));
Daniel Sandler843e8602010-06-07 14:59:01 -0400627 }
Joe Onorato36115782010-06-17 13:28:48 -0400628 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400629
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700630 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -0500631 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700632 // Ensure that we have a valid page index to load synchronously
633 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
634 "valid page index");
635 }
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800636 if (!mModelLoaded) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700637 // Ensure that we don't try and bind a specified page when the pages have not been
638 // loaded already (we should load everything asynchronously in that case)
639 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
640 }
641 synchronized (mLock) {
642 if (mIsLoaderTaskRunning) {
643 // Ensure that we are never running the background loading at this point since
644 // we also touch the background collections
645 throw new RuntimeException("Error! Background loading is already running");
646 }
647 }
648
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700649 // Divide the set of loaded items into those that we are binding synchronously, and
650 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800651 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700652 // XXX: For now, continue posting the binding of AllApps as there are other issues that
653 // arise from that.
654 onlyBindAllApps();
Tony Wickham80f57872016-06-29 18:12:15 -0700655
656 bindDeepShortcuts();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700657 }
658
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800659 private void verifyNotStopped() throws CancellationException {
660 synchronized (LoaderTask.this) {
661 if (mStopped) {
662 throw new CancellationException("Loader stopped");
663 }
664 }
665 }
666
Joe Onorato36115782010-06-17 13:28:48 -0400667 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700668 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -0700669 if (mStopped) {
670 return;
671 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700672 mIsLoaderTaskRunning = true;
673 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400674
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800675 try {
Sunny Goyalb265ba72017-02-14 15:03:45 -0800676 long now = 0;
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800677 if (DEBUG_LOADERS) Log.d(TAG, "step 1.1: loading workspace");
678 // Set to false in bindWorkspace()
679 mIsLoadingAndBindingWorkspace = true;
680 loadWorkspace();
Joe Onorato36115782010-06-17 13:28:48 -0400681
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800682 verifyNotStopped();
683 if (DEBUG_LOADERS) Log.d(TAG, "step 1.2: bind workspace workspace");
684 bindWorkspace(mPageToBindFirst);
685
686 // Take a break
Sunny Goyalb265ba72017-02-14 15:03:45 -0800687 if (DEBUG_LOADERS) {
688 Log.d(TAG, "step 1 completed, wait for idle");
689 now = SystemClock.uptimeMillis();
690 }
Joe Onorato36115782010-06-17 13:28:48 -0400691 waitForIdle();
Sunny Goyalb265ba72017-02-14 15:03:45 -0800692 if (DEBUG_LOADERS) Log.d(TAG, "Waited " + (SystemClock.uptimeMillis() - now) + "ms");
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800693 verifyNotStopped();
Daniel Sandler843e8602010-06-07 14:59:01 -0400694
695 // second step
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800696 if (DEBUG_LOADERS) Log.d(TAG, "step 2.1: loading all apps");
697 loadAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700698
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800699 verifyNotStopped();
700 if (DEBUG_LOADERS) Log.d(TAG, "step 2.2: Update icon cache");
701 updateIconCache();
702
703 // Take a break
Sunny Goyalb265ba72017-02-14 15:03:45 -0800704 if (DEBUG_LOADERS) {
705 Log.d(TAG, "step 2 completed, wait for idle");
706 now = SystemClock.uptimeMillis();
707 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700708 waitForIdle();
Sunny Goyalb265ba72017-02-14 15:03:45 -0800709 if (DEBUG_LOADERS) Log.d(TAG, "Waited " + (SystemClock.uptimeMillis() - now) + "ms");
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800710 verifyNotStopped();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700711
712 // third step
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800713 if (DEBUG_LOADERS) Log.d(TAG, "step 3.1: loading deep shortcuts");
714 loadDeepShortcuts();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400715
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800716 verifyNotStopped();
717 if (DEBUG_LOADERS) Log.d(TAG, "step 3.2: bind deep shortcuts");
718 bindDeepShortcuts();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400719
Tony Wickhama7e1c1c2017-04-13 16:24:38 -0700720 // Take a break
721 if (DEBUG_LOADERS) Log.d(TAG, "step 3 completed, wait for idle");
722 waitForIdle();
723 verifyNotStopped();
724
725 // fourth step
726 if (DEBUG_LOADERS) Log.d(TAG, "step 4.1: loading widgets");
727 refreshAndBindWidgetsAndShortcuts(getCallback(), false /* bindFirst */,
728 null /* packageUser */);
729
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800730 synchronized (mLock) {
731 // Everything loaded bind the data.
732 mModelLoaded = true;
733 mHasLoaderCompletedOnce = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400734 }
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800735 } catch (CancellationException e) {
736 // Loader stopped, ignore
737 } finally {
738 // Clear out this reference, otherwise we end up holding it until all of the
739 // callback runnables are done.
740 mContext = null;
741
742 synchronized (mLock) {
743 // If we are still the last one to be scheduled, remove ourselves.
744 if (mLoaderTask == this) {
745 mLoaderTask = null;
746 }
747 mIsLoaderTaskRunning = false;
748 }
Joe Onorato36115782010-06-17 13:28:48 -0400749 }
Joe Onorato36115782010-06-17 13:28:48 -0400750 }
751
752 public void stopLocked() {
753 synchronized (LoaderTask.this) {
754 mStopped = true;
755 this.notify();
756 }
757 }
758
759 /**
760 * Gets the callbacks object. If we've been stopped, or if the launcher object
761 * has somehow been garbage collected, return null instead. Pass in the Callbacks
762 * object that was around when the deferred message was scheduled, and if there's
763 * a new Callbacks object around then also return null. This will save us from
764 * calling onto it with data that will be ignored.
765 */
766 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
767 synchronized (mLock) {
768 if (mStopped) {
769 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400770 }
Joe Onorato36115782010-06-17 13:28:48 -0400771
772 if (mCallbacks == null) {
773 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400774 }
Joe Onorato36115782010-06-17 13:28:48 -0400775
776 final Callbacks callbacks = mCallbacks.get();
777 if (callbacks != oldCallbacks) {
778 return null;
779 }
780 if (callbacks == null) {
781 Log.w(TAG, "no mCallbacks");
782 return null;
783 }
784
785 return callbacks;
786 }
787 }
788
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800789 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -0700790 if (LauncherAppState.PROFILE_STARTUP) {
791 Trace.beginSection("Loading Workspace");
792 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400793
Joe Onorato36115782010-06-17 13:28:48 -0400794 final Context context = mContext;
795 final ContentResolver contentResolver = context.getContentResolver();
Sunny Goyal342e4662017-02-02 15:21:08 -0800796 final PackageManagerHelper pmHelper = new PackageManagerHelper(context);
797 final boolean isSafeMode = pmHelper.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700798 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530799 final DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -0700800 final boolean isSdCardReady = Utilities.isBootCompleted();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800801 final MultiHashMap<UserHandle, String> pendingPackages = new MultiHashMap<>();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400802
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700803 boolean clearDb = false;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -0700804 try {
805 ImportDataTask.performImportIfPossible(context);
806 } catch (Exception e) {
807 // Migration failed. Clear workspace.
808 clearDb = true;
809 }
810
811 if (!clearDb && GridSizeMigrationTask.ENABLED &&
Sunny Goyalf076eae2016-01-11 12:25:10 -0800812 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
813 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700814 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -0700815 }
816
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700817 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -0700818 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -0700819 LauncherSettings.Settings.call(contentResolver,
Sunny Goyale05b08f2017-02-23 18:30:22 -0800820 LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -0500821 }
822
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700823 Log.d(TAG, "loadWorkspace: loading default favorites");
824 LauncherSettings.Settings.call(contentResolver,
825 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -0700826
Sunny Goyale9956a72016-09-01 17:24:47 -0700827 synchronized (sBgDataModel) {
828 sBgDataModel.clear();
829
Sunny Goyal756adbc2015-04-16 15:20:51 -0700830 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -0700831 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyale9956a72016-09-01 17:24:47 -0700832 sBgDataModel.workspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -0800833
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700834 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800835 final LoaderCursor c = new LoaderCursor(contentResolver.query(
836 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null), mApp);
Daniel Sandler8802e962010-05-26 16:28:16 -0400837
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800838 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400839
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700840 try {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700841 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
842 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -0400843 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
844 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700845 final int spanXIndex = c.getColumnIndexOrThrow
846 (LauncherSettings.Favorites.SPANX);
847 final int spanYIndex = c.getColumnIndexOrThrow(
848 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800849 final int rankIndex = c.getColumnIndexOrThrow(
850 LauncherSettings.Favorites.RANK);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700851 final int optionsIndex = c.getColumnIndexOrThrow(
852 LauncherSettings.Favorites.OPTIONS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400853
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800854 final LongSparseArray<UserHandle> allUsers = c.allUsers;
Kenny Guyff05f432016-01-22 17:48:29 +0000855 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700856 final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800857 for (UserHandle user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +0000858 long serialNo = mUserManager.getSerialNumberForUser(user);
859 allUsers.put(serialNo, user);
860 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700861
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700862 boolean userUnlocked = mUserManager.isUserUnlocked(user);
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700863
864 // We can only query for shortcuts when the user is unlocked.
865 if (userUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -0700866 List<ShortcutInfoCompat> pinnedShortcuts =
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530867 shortcutManager.queryForPinnedShortcuts(null, user);
868 if (shortcutManager.wasLastCallSuccess()) {
Sunny Goyal49f4f032016-08-01 15:45:49 -0700869 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
870 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
871 shortcut);
872 }
873 } else {
874 // Shortcut manager can fail due to some race condition when the
875 // lock state changes too frequently. For the purpose of the loading
876 // shortcuts, consider the user is still locked.
877 userUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700878 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700879 }
Sunny Goyal49f4f032016-08-01 15:45:49 -0700880 unlockedUsers.put(serialNo, userUnlocked);
Sunny Goyal7f834d22015-04-21 10:10:23 -0700881 }
882
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700883 ShortcutInfo info;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700884 LauncherAppWidgetInfo appWidgetInfo;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700885 Intent intent;
Sunny Goyal81e44912017-01-14 15:05:14 -0800886 String targetPkg;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400887
Jon Miranda529af302017-02-28 14:18:54 -0800888 FolderIconPreviewVerifier verifier =
889 new FolderIconPreviewVerifier(mApp.getInvariantDeviceProfile());
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700890 while (!mStopped && c.moveToNext()) {
891 try {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800892 if (c.user == null) {
893 // User has been deleted, remove the item.
894 c.markDeleted("User has been deleted");
895 continue;
896 }
897
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700898 boolean allowMissingTarget = false;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800899 switch (c.itemType) {
Sunny Goyal81e44912017-01-14 15:05:14 -0800900 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700901 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700902 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Sunny Goyal81e44912017-01-14 15:05:14 -0800903 intent = c.parseIntent();
904 if (intent == null) {
905 c.markDeleted("Invalid or null intent");
906 continue;
907 }
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800908
Sunny Goyal81e44912017-01-14 15:05:14 -0800909 int disabledState = quietMode.get(c.serialNumber) ?
910 ShortcutInfo.FLAG_DISABLED_QUIET_USER : 0;
911 ComponentName cn = intent.getComponent();
912 targetPkg = cn == null ? intent.getPackage() : cn.getPackageName();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700913
Sunny Goyal81e44912017-01-14 15:05:14 -0800914 if (!Process.myUserHandle().equals(c.user)) {
915 if (c.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
916 c.markDeleted("Legacy shortcuts are only allowed for default user");
917 continue;
918 } else if (c.restoreFlag != 0) {
919 // Don't restore items for other profiles.
920 c.markDeleted("Restore from managed profile not supported");
921 continue;
922 }
923 }
924 if (TextUtils.isEmpty(targetPkg) &&
925 c.itemType != LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
926 c.markDeleted("Only legacy shortcuts can have null package");
927 continue;
928 }
Sunny Goyal34942622014-08-29 17:20:55 -0700929
Sunny Goyal81e44912017-01-14 15:05:14 -0800930 // If there is no target package, its an implicit intent
931 // (legacy shortcut) which is always valid
932 boolean validTarget = TextUtils.isEmpty(targetPkg) ||
933 launcherApps.isPackageEnabledForProfile(targetPkg, c.user);
Sunny Goyal94485362014-09-18 16:13:58 -0700934
Sunny Goyal81e44912017-01-14 15:05:14 -0800935 if (cn != null && validTarget) {
936 // If the apk is present and the shortcut points to a specific
937 // component.
938
939 // If the component is already present
940 if (launcherApps.isActivityEnabledForProfile(cn, c.user)) {
941 // no special handling necessary for this item
942 c.markRestored();
943 } else {
Mario Bertschlerc06af332017-03-28 12:23:22 -0700944 if (c.hasRestoreFlag(ShortcutInfo.FLAG_AUTOINSTALL_ICON)) {
Sunny Goyal81e44912017-01-14 15:05:14 -0800945 // We allow auto install apps to have their intent
946 // updated after an install.
Sunny Goyal342e4662017-02-02 15:21:08 -0800947 intent = pmHelper.getAppLaunchIntent(targetPkg, c.user);
Sunny Goyal81e44912017-01-14 15:05:14 -0800948 if (intent != null) {
949 c.restoreFlag = 0;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800950 c.updater().put(
Sunny Goyal81e44912017-01-14 15:05:14 -0800951 LauncherSettings.Favorites.INTENT,
952 intent.toUri(0)).commit();
953 cn = intent.getComponent();
Sunny Goyalb05a00a2016-08-29 10:06:57 -0700954 } else {
Sunny Goyal81e44912017-01-14 15:05:14 -0800955 c.markDeleted("Unable to find a launch target");
Sunny Goyal94485362014-09-18 16:13:58 -0700956 continue;
957 }
Sunny Goyal1a745e82014-10-02 15:58:31 -0700958 } else {
Sunny Goyal81e44912017-01-14 15:05:14 -0800959 // The app is installed but the component is no
960 // longer available.
961 c.markDeleted("Invalid component removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -0700962 continue;
Winson Chungee055712013-07-30 14:46:24 -0700963 }
Winson Chungee055712013-07-30 14:46:24 -0700964 }
Sunny Goyal81e44912017-01-14 15:05:14 -0800965 }
966 // else if cn == null => can't infer much, leave it
967 // else if !validPkg => could be restored icon or missing sd-card
968
969 if (!TextUtils.isEmpty(targetPkg) && !validTarget) {
970 // Points to a valid app (superset of cn != null) but the apk
971 // is not available.
972
973 if (c.restoreFlag != 0) {
974 // Package is not yet available but might be
975 // installed later.
976 FileLog.d(TAG, "package not yet restored: " + targetPkg);
977
978 if (c.hasRestoreFlag(ShortcutInfo.FLAG_RESTORE_STARTED)) {
979 // Restore has started once.
980 } else if (installingPkgs.containsKey(targetPkg)) {
981 // App restore has started. Update the flag
982 c.restoreFlag |= ShortcutInfo.FLAG_RESTORE_STARTED;
983 c.updater().commit();
984 } else {
985 c.markDeleted("Unrestored app removed: " + targetPkg);
986 continue;
987 }
Sunny Goyalad2e91a2017-02-28 15:14:58 -0800988 } else if (pmHelper.isAppOnSdcard(targetPkg, c.user)) {
Sunny Goyal81e44912017-01-14 15:05:14 -0800989 // Package is present but not available.
990 disabledState |= ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
991 // Add the icon on the workspace anyway.
992 allowMissingTarget = true;
993 } else if (!isSdCardReady) {
994 // SdCard is not ready yet. Package might get available,
995 // once it is ready.
996 Log.d(TAG, "Missing pkg, will check later: " + targetPkg);
997 pendingPackages.addToList(c.user, targetPkg);
998 // Add the icon on the workspace anyway.
999 allowMissingTarget = true;
1000 } else {
1001 // Do not wait for external media load anymore.
1002 c.markDeleted("Invalid package removed: " + targetPkg);
1003 continue;
1004 }
1005 }
1006
1007 if (validTarget) {
1008 // The shortcut points to a valid target (either no target
1009 // or something which is ready to be used)
1010 c.markRestored();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001011 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001012
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001013 boolean useLowResIcon = !c.isOnWorkspaceOrHotseat() &&
Jon Miranda529af302017-02-28 14:18:54 -08001014 !verifier.isItemInPreview(c.getInt(rankIndex));
Sunny Goyal34b65272015-03-11 16:56:52 -07001015
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001016 if (c.restoreFlag != 0) {
Sunny Goyal81e44912017-01-14 15:05:14 -08001017 // Already verified above that user is same as default user
1018 info = c.getRestoredItemInfo(intent);
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001019 } else if (c.itemType ==
Chris Wrenf4d08112014-01-16 18:13:56 -05001020 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001021 info = c.getAppShortcutInfo(
1022 intent, allowMissingTarget, useLowResIcon);
1023 } else if (c.itemType ==
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001024 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001025
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001026 ShortcutKey key = ShortcutKey.fromIntent(intent, c.user);
1027 if (unlockedUsers.get(c.serialNumber)) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001028 ShortcutInfoCompat pinnedShortcut =
1029 shortcutKeyToPinnedShortcuts.get(key);
1030 if (pinnedShortcut == null) {
1031 // The shortcut is no longer valid.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001032 c.markDeleted("Pinned shortcut not found");
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001033 continue;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001034 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001035 info = new ShortcutInfo(pinnedShortcut, context);
Sunny Goyal1b072632017-01-18 11:30:23 -08001036 info.iconBitmap = LauncherIcons
1037 .createShortcutIcon(pinnedShortcut, context);
Charles He3ff90472017-02-15 12:22:56 +00001038 if (pmHelper.isAppSuspended(
Sunny Goyalad2e91a2017-02-28 15:14:58 -08001039 pinnedShortcut.getPackage(), info.user)) {
Charles He3ff90472017-02-15 12:22:56 +00001040 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1041 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001042 intent = info.intent;
1043 } else {
1044 // Create a shortcut info in disabled mode for now.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001045 info = c.loadSimpleShortcut();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001046 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001047 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001048 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001049 info = c.loadSimpleShortcut();
Michael Jurka96879562012-03-22 05:54:33 -07001050
Sunny Goyald09c3702016-04-06 16:18:20 -07001051 // Shortcuts are only available on the primary profile
Sunny Goyalad2e91a2017-02-28 15:14:58 -08001052 if (pmHelper.isAppSuspended(targetPkg, c.user)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001053 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1054 }
1055
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001056 // App shortcuts that used to be automatically added to Launcher
1057 // didn't always have the correct intent flags set, so do that
1058 // here
1059 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001060 intent.getCategories() != null &&
1061 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001062 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001063 intent.addFlags(
1064 Intent.FLAG_ACTIVITY_NEW_TASK |
1065 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1066 }
Michael Jurka96879562012-03-22 05:54:33 -07001067 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001068
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001069 if (info != null) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001070 c.applyCommonProperties(info);
1071
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001072 info.intent = intent;
Sunny Goyal08f72612015-01-05 13:41:43 -08001073 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001074 info.spanX = 1;
1075 info.spanY = 1;
Sunny Goyald09c3702016-04-06 16:18:20 -07001076 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001077 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1078 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1079 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001080
Sunny Goyal81e44912017-01-14 15:05:14 -08001081 if (c.restoreFlag != 0 && !TextUtils.isEmpty(targetPkg)) {
1082 Integer progress = installingPkgs.get(targetPkg);
1083 if (progress != null) {
1084 info.setInstallProgress(progress);
1085 } else {
1086 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001087 }
1088 }
1089
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001090 c.checkAndAddItem(info, sBgDataModel);
Winson Chung1323b482013-08-05 12:41:55 -07001091 } else {
1092 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001093 }
1094 break;
1095
1096 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001097 FolderInfo folderInfo = sBgDataModel.findOrMakeFolder(c.id);
1098 c.applyCommonProperties(folderInfo);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001099
Sunny Goyala508e4f2015-05-21 09:33:57 -07001100 // Do not trim the folder label, as is was set by the user.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001101 folderInfo.title = c.getString(c.titleIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001102 folderInfo.spanX = 1;
1103 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001104 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001105
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001106 // no special handling required for restored folders
1107 c.markRestored();
Chris Wrenf4d08112014-01-16 18:13:56 -05001108
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001109 c.checkAndAddItem(folderInfo, sBgDataModel);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001110 break;
1111
1112 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08001113 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001114 // Read all Launcher-specific widget details
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001115 boolean customWidget = c.itemType ==
Adam Cohen59400422014-03-05 18:07:04 -08001116 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
1117
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001118 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001119 String savedProvider = c.getString(appWidgetProviderIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001120
Sunny Goyalff572272014-07-23 13:58:07 -07001121 final ComponentName component =
1122 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04001123
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001124 final boolean isIdValid = !c.hasRestoreFlag(
1125 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID);
1126 final boolean wasProviderReady = !c.hasRestoreFlag(
1127 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY);
Sunny Goyal651077b2014-06-30 14:15:31 -07001128
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001129 if (widgetProvidersMap == null) {
1130 widgetProvidersMap = AppWidgetManagerCompat
1131 .getInstance(mContext).getAllProvidersMap();
1132 }
1133 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
1134 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00001135 ComponentName.unflattenFromString(savedProvider),
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001136 c.user));
Sunny Goyalff572272014-07-23 13:58:07 -07001137
1138 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08001139 if (!isSafeMode && !customWidget &&
1140 wasProviderReady && !isProviderReady) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001141 c.markDeleted(
1142 "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07001143 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001144 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07001145 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07001146 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1147 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08001148
Sunny Goyal53f96722015-07-13 19:54:53 -07001149 // The provider is available. So the widget is either
1150 // available or not available. We do not need to track
1151 // any future restore updates.
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001152 int status = c.restoreFlag &
Sunny Goyal53f96722015-07-13 19:54:53 -07001153 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07001154 if (!wasProviderReady) {
1155 // If provider was not previously ready, update the
1156 // status and UI flag.
1157
1158 // Id would be valid only if the widget restore broadcast was received.
1159 if (isIdValid) {
Sunny Goyal86df1382016-08-10 15:03:22 -07001160 status |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07001161 } else {
1162 status &= ~LauncherAppWidgetInfo
1163 .FLAG_PROVIDER_NOT_READY;
1164 }
1165 }
1166 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07001167 } else {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001168 Log.v(TAG, "Widget restore pending id=" + c.id
Sunny Goyal651077b2014-06-30 14:15:31 -07001169 + " appWidgetId=" + appWidgetId
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001170 + " status =" + c.restoreFlag);
Sunny Goyal651077b2014-06-30 14:15:31 -07001171 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07001172 component);
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001173 appWidgetInfo.restoreStatus = c.restoreFlag;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001174 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07001175
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001176 if (c.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_RESTORE_STARTED)) {
Sunny Goyal94485362014-09-18 16:13:58 -07001177 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001178 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07001179 // App restore has started. Update the flag
1180 appWidgetInfo.restoreStatus |=
1181 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001182 } else if (!isSafeMode) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001183 c.markDeleted("Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07001184 continue;
1185 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07001186
1187 appWidgetInfo.installProgress =
1188 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07001189 }
Sunny Goyal86df1382016-08-10 15:03:22 -07001190 if (appWidgetInfo.hasRestoreFlag(
1191 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG)) {
Sunny Goyal81e44912017-01-14 15:05:14 -08001192 appWidgetInfo.bindOptions = c.parseIntent();
Sunny Goyal86df1382016-08-10 15:03:22 -07001193 }
Sunny Goyalff572272014-07-23 13:58:07 -07001194
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001195 c.applyCommonProperties(appWidgetInfo);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001196 appWidgetInfo.spanX = c.getInt(spanXIndex);
1197 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001198 appWidgetInfo.user = c.user;
Joe Onorato36115782010-06-17 13:28:48 -04001199
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001200 if (!c.isOnWorkspaceOrHotseat()) {
1201 c.markDeleted("Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001202 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001203 continue;
1204 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001205
Adam Cohen59400422014-03-05 18:07:04 -08001206 if (!customWidget) {
1207 String providerName =
1208 appWidgetInfo.providerName.flattenToString();
1209 if (!providerName.equals(savedProvider) ||
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001210 (appWidgetInfo.restoreStatus != c.restoreFlag)) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001211 c.updater()
1212 .put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1213 providerName)
1214 .put(LauncherSettings.Favorites.RESTORED,
1215 appWidgetInfo.restoreStatus)
1216 .commit();
Adam Cohen59400422014-03-05 18:07:04 -08001217 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001218 }
Jon Miranda2b823f42017-05-02 18:36:18 -07001219
1220 if (appWidgetInfo.restoreStatus !=
1221 LauncherAppWidgetInfo.RESTORE_COMPLETED) {
1222 String pkg = appWidgetInfo.providerName.getPackageName();
1223 appWidgetInfo.pendingItemInfo = new PackageItemInfo(pkg);
1224 appWidgetInfo.pendingItemInfo.user = appWidgetInfo.user;
1225 mIconCache.getTitleAndIconForApp(
1226 appWidgetInfo.pendingItemInfo, false);
1227 }
1228
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001229 c.checkAndAddItem(appWidgetInfo, sBgDataModel);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001230 }
Joe Onorato36115782010-06-17 13:28:48 -04001231 break;
1232 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001233 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001234 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001235 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001236 }
1237 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001238 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001239 }
1240
Winson Chungba9c37f2013-08-30 14:11:37 -07001241 // Break early if we've stopped loading
1242 if (mStopped) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001243 sBgDataModel.clear();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001244 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07001245 }
1246
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001247 // Remove dead items
1248 if (c.commitDeleted()) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001249 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07001250 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
1251 .call(contentResolver,
1252 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
1253 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
1254 for (long folderId : deletedFolderIds) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001255 sBgDataModel.workspaceItems.remove(sBgDataModel.folders.get(folderId));
1256 sBgDataModel.folders.remove(folderId);
1257 sBgDataModel.itemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001258 }
Sunny Goyal55fddc82017-04-06 15:02:52 -07001259
1260 // Remove any ghost widgets
1261 LauncherSettings.Settings.call(contentResolver,
1262 LauncherSettings.Settings.METHOD_REMOVE_GHOST_WIDGETS);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001263 }
1264
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001265 // Unpin shortcuts that don't exist on the workspace.
Sunny Goyalf75baa92016-11-22 03:23:51 +05301266 HashSet<ShortcutKey> pendingShortcuts =
1267 InstallShortcutReceiver.getPendingShortcuts(context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001268 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001269 MutableInt numTimesPinned = sBgDataModel.pinnedShortcutCounts.get(key);
Sunny Goyalf75baa92016-11-22 03:23:51 +05301270 if ((numTimesPinned == null || numTimesPinned.value == 0)
1271 && !pendingShortcuts.contains(key)) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001272 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301273 shortcutManager.unpinShortcut(key);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001274 }
1275 }
1276
Jon Miranda655ec422017-02-07 11:40:22 -08001277 FolderIconPreviewVerifier verifier =
1278 new FolderIconPreviewVerifier(mApp.getInvariantDeviceProfile());
1279 // Sort the folder items and make sure all items in the preview are high resolution.
Sunny Goyale9956a72016-09-01 17:24:47 -07001280 for (FolderInfo folder : sBgDataModel.folders) {
Sunny Goyal317698b2015-07-29 11:45:41 -07001281 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
Jon Miranda655ec422017-02-07 11:40:22 -08001282 verifier.setFolderInfo(folder);
1283
1284 int numItemsInPreview = 0;
Sunny Goyal317698b2015-07-29 11:45:41 -07001285 for (ShortcutInfo info : folder.contents) {
Jon Miranda655ec422017-02-07 11:40:22 -08001286 if (info.usingLowResIcon
1287 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION
1288 && verifier.isItemInPreview(info.rank)) {
Sunny Goyal3fe4a142016-12-15 17:40:07 -08001289 mIconCache.getTitleAndIcon(info, false);
Jon Miranda655ec422017-02-07 11:40:22 -08001290 numItemsInPreview++;
Sunny Goyal317698b2015-07-29 11:45:41 -07001291 }
Jon Miranda655ec422017-02-07 11:40:22 -08001292
1293 if (numItemsInPreview >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
Sunny Goyal317698b2015-07-29 11:45:41 -07001294 break;
1295 }
1296 }
1297 }
1298
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001299 c.commitRestoredItems();
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001300 if (!isSdCardReady && !pendingPackages.isEmpty()) {
1301 context.registerReceiver(
1302 new SdCardAvailableReceiver(
1303 LauncherModel.this, mContext, pendingPackages),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001304 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001305 null,
1306 sWorker);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001307 }
1308
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001309 // Remove any empty screens
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001310 ArrayList<Long> unusedScreens = new ArrayList<>(sBgDataModel.workspaceScreens);
Sunny Goyale9956a72016-09-01 17:24:47 -07001311 for (ItemInfo item: sBgDataModel.itemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001312 long screenId = item.screenId;
1313 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1314 unusedScreens.contains(screenId)) {
1315 unusedScreens.remove(screenId);
1316 }
1317 }
1318
1319 // If there are any empty screens remove them, and update.
1320 if (unusedScreens.size() != 0) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001321 sBgDataModel.workspaceScreens.removeAll(unusedScreens);
1322 updateWorkspaceScreenOrder(context, sBgDataModel.workspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001323 }
Joe Onorato36115782010-06-17 13:28:48 -04001324 }
Sunny Goyale26d1002016-06-20 14:52:14 -07001325 if (LauncherAppState.PROFILE_STARTUP) {
1326 Trace.endSection();
1327 }
Adam Cohene25af792013-06-06 23:08:25 -07001328 }
1329
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001330 /** Filters the set of items who are directly or indirectly (via another container) on the
1331 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08001332 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001333 ArrayList<ItemInfo> allWorkspaceItems,
1334 ArrayList<ItemInfo> currentScreenItems,
1335 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001336 // Purge any null ItemInfos
1337 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
1338 while (iter.hasNext()) {
1339 ItemInfo i = iter.next();
1340 if (i == null) {
1341 iter.remove();
1342 }
1343 }
1344
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001345 // Order the set of items by their containers first, this allows use to walk through the
1346 // list sequentially, build up a list of containers that are in the specified screen,
1347 // as well as all items in those containers.
1348 Set<Long> itemsOnScreen = new HashSet<Long>();
1349 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
1350 @Override
1351 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07001352 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001353 }
1354 });
1355 for (ItemInfo info : allWorkspaceItems) {
1356 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001357 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001358 currentScreenItems.add(info);
1359 itemsOnScreen.add(info.id);
1360 } else {
1361 otherScreenItems.add(info);
1362 }
1363 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1364 currentScreenItems.add(info);
1365 itemsOnScreen.add(info.id);
1366 } else {
1367 if (itemsOnScreen.contains(info.container)) {
1368 currentScreenItems.add(info);
1369 itemsOnScreen.add(info.id);
1370 } else {
1371 otherScreenItems.add(info);
1372 }
1373 }
1374 }
1375 }
1376
1377 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08001378 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001379 ArrayList<LauncherAppWidgetInfo> appWidgets,
1380 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
1381 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001382
1383 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001384 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001385 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08001386 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001387 currentScreenWidgets.add(widget);
1388 } else {
1389 otherScreenWidgets.add(widget);
1390 }
1391 }
1392 }
1393
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001394 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
1395 * right) */
1396 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Sunny Goyal87f784c2017-01-11 10:48:34 -08001397 final InvariantDeviceProfile profile = mApp.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07001398 final int screenCols = profile.numColumns;
1399 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001400 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07001401 @Override
1402 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07001403 if (lhs.container == rhs.container) {
1404 // Within containers, order by their spatial position in that container
1405 switch ((int) lhs.container) {
1406 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
1407 long lr = (lhs.screenId * screenCellCount +
1408 lhs.cellY * screenCols + lhs.cellX);
1409 long rr = (rhs.screenId * screenCellCount +
1410 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07001411 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07001412 }
1413 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
1414 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07001415 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07001416 }
1417 default:
Sunny Goyal3d706ad2017-03-06 16:56:39 -08001418 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07001419 throw new RuntimeException("Unexpected container type when " +
1420 "sorting workspace items.");
1421 }
1422 return 0;
1423 }
1424 } else {
1425 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07001426 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07001427 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001428 }
1429 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001430 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001431
Adam Cohendcd297f2013-06-18 13:13:40 -07001432 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
1433 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001434 final Runnable r = new Runnable() {
1435 @Override
1436 public void run() {
1437 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1438 if (callbacks != null) {
1439 callbacks.bindScreens(orderedScreens);
1440 }
1441 }
1442 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001443 mUiExecutor.execute(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07001444 }
1445
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001446 private void bindWorkspaceItems(final Callbacks oldCallbacks,
1447 final ArrayList<ItemInfo> workspaceItems,
1448 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07001449 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001450
1451 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07001452 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001453 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04001454 final int start = i;
1455 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001456 final Runnable r = new Runnable() {
1457 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001458 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001459 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001460 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07001461 callbacks.bindItems(workspaceItems, start, start+chunkSize,
1462 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001463 }
1464 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001465 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001466 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04001467 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001468
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001469 // Bind the widgets, one at a time
1470 N = appWidgets.size();
1471 for (int i = 0; i < N; i++) {
1472 final LauncherAppWidgetInfo widget = appWidgets.get(i);
1473 final Runnable r = new Runnable() {
1474 public void run() {
1475 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1476 if (callbacks != null) {
1477 callbacks.bindAppWidget(widget);
1478 }
1479 }
1480 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001481 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001482 }
1483 }
1484
1485 /**
1486 * Binds all loaded data to actual views on the main thread.
1487 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001488 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001489 final long t = SystemClock.uptimeMillis();
1490 Runnable r;
1491
1492 // Don't use these two variables in any of the callback runnables.
1493 // Otherwise we hold a reference to them.
1494 final Callbacks oldCallbacks = mCallbacks.get();
1495 if (oldCallbacks == null) {
1496 // This launcher has exited and nobody bothered to tell us. Just bail.
1497 Log.w(TAG, "LoaderTask running with no launcher");
1498 return;
1499 }
1500
Winson Chung9b9fb962013-11-15 15:39:34 -08001501 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07001502 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
1503 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
1504 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07001505
Sunny Goyale9956a72016-09-01 17:24:47 -07001506 synchronized (sBgDataModel) {
1507 workspaceItems.addAll(sBgDataModel.workspaceItems);
1508 appWidgets.addAll(sBgDataModel.appWidgets);
1509 orderedScreenIds.addAll(sBgDataModel.workspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001510 }
1511
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001512 final int currentScreen;
1513 {
1514 int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE
1515 ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen();
1516 if (currScreen >= orderedScreenIds.size()) {
1517 // There may be no workspace screens (just hotseat items and an empty page).
1518 currScreen = PagedView.INVALID_RESTORE_PAGE;
1519 }
1520 currentScreen = currScreen;
Winson Chung9b9fb962013-11-15 15:39:34 -08001521 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001522 final boolean validFirstPage = currentScreen >= 0;
1523 final long currentScreenId =
1524 validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID;
Winson Chung9b9fb962013-11-15 15:39:34 -08001525
Winson Chung9b9fb962013-11-15 15:39:34 -08001526 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07001527 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
1528 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
1529 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
1530 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001531
Winson Chung9b9fb962013-11-15 15:39:34 -08001532 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001533 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08001534 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001535 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001536 sortWorkspaceItemsSpatially(currentWorkspaceItems);
1537 sortWorkspaceItemsSpatially(otherWorkspaceItems);
1538
1539 // Tell the workspace that we're about to start binding items
1540 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001541 public void run() {
1542 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1543 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001544 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001545 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04001546 }
1547 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001548 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001549 mUiExecutor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001550
Adam Cohendcd297f2013-06-18 13:13:40 -07001551 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
1552
Sunny Goyalb265ba72017-02-14 15:03:45 -08001553 Executor mainExecutor = mUiExecutor;
Sunny Goyal527c7d32015-08-28 15:19:36 -07001554 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07001555 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001556
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001557 // In case of validFirstPage, only bind the first screen, and defer binding the
1558 // remaining screens after first onDraw (and an optional the fade animation whichever
1559 // happens later).
1560 // This ensures that the first screen is immediately visible (eg. during rotation)
1561 // In case of !validFirstPage, bind all pages one after other.
1562 final Executor deferredExecutor =
Sunny Goyalb265ba72017-02-14 15:03:45 -08001563 validFirstPage ? new ViewOnDrawExecutor(mUiExecutor) : mainExecutor;
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001564
1565 mainExecutor.execute(new Runnable() {
1566 @Override
1567 public void run() {
1568 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1569 if (callbacks != null) {
1570 callbacks.finishFirstPageBind(
1571 validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null);
1572 }
1573 }
1574 });
Sunny Goyal527c7d32015-08-28 15:19:36 -07001575
Sunny Goyal44c06432016-04-02 10:56:02 -07001576 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001577
1578 // Tell the workspace that we're done binding items
1579 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001580 public void run() {
1581 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1582 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001583 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04001584 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001585
Sunny Goyal639e9062015-08-19 19:17:06 -07001586 mIsLoadingAndBindingWorkspace = false;
1587
1588 // Run all the bind complete runnables after workspace is bound.
1589 if (!mBindCompleteRunnables.isEmpty()) {
1590 synchronized (mBindCompleteRunnables) {
1591 for (final Runnable r : mBindCompleteRunnables) {
1592 runOnWorkerThread(r);
1593 }
1594 mBindCompleteRunnables.clear();
1595 }
1596 }
1597
Winson Chung98e030b2012-05-07 16:01:11 -07001598 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04001599 if (DEBUG_LOADERS) {
1600 Log.d(TAG, "bound workspace in "
1601 + (SystemClock.uptimeMillis()-t) + "ms");
1602 }
Winson Chung36a62fe2012-05-06 18:04:42 -07001603
Joe Onorato36115782010-06-17 13:28:48 -04001604 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001605 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001606 deferredExecutor.execute(r);
1607
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001608 if (validFirstPage) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001609 r = new Runnable() {
1610 public void run() {
1611 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1612 if (callbacks != null) {
1613 // We are loading synchronously, which means, some of the pages will be
1614 // bound after first draw. Inform the callbacks that page binding is
1615 // not complete, and schedule the remaining pages.
1616 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
1617 callbacks.onPageBoundSynchronously(currentScreen);
1618 }
1619 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
1620 }
1621 }
1622 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001623 mUiExecutor.execute(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07001624 }
Joe Onorato36115782010-06-17 13:28:48 -04001625 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001626
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001627 private void updateIconCache() {
1628 // Ignore packages which have a promise icon.
1629 HashSet<String> packagesToIgnore = new HashSet<>();
Sunny Goyale9956a72016-09-01 17:24:47 -07001630 synchronized (sBgDataModel) {
1631 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001632 if (info instanceof ShortcutInfo) {
1633 ShortcutInfo si = (ShortcutInfo) info;
1634 if (si.isPromise() && si.getTargetComponent() != null) {
1635 packagesToIgnore.add(si.getTargetComponent().getPackageName());
1636 }
1637 } else if (info instanceof LauncherAppWidgetInfo) {
1638 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
1639 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
1640 packagesToIgnore.add(lawi.providerName.getPackageName());
1641 }
1642 }
1643 }
1644 }
1645 mIconCache.updateDbIcons(packagesToIgnore);
1646 }
1647
Joe Onorato36115782010-06-17 13:28:48 -04001648 private void onlyBindAllApps() {
1649 final Callbacks oldCallbacks = mCallbacks.get();
1650 if (oldCallbacks == null) {
1651 // This launcher has exited and nobody bothered to tell us. Just bail.
1652 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1653 return;
1654 }
1655
1656 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07001657 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001658 final ArrayList<AppInfo> list
1659 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07001660 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001661 public void run() {
1662 final long t = SystemClock.uptimeMillis();
1663 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1664 if (callbacks != null) {
1665 callbacks.bindAllApplications(list);
1666 }
1667 if (DEBUG_LOADERS) {
1668 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08001669 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04001670 }
1671 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07001672 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001673 mUiExecutor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04001674 }
1675
Winson Chung64359a52013-07-08 17:17:08 -07001676 private void loadAllApps() {
1677 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04001678
Joe Onorato36115782010-06-17 13:28:48 -04001679 final Callbacks oldCallbacks = mCallbacks.get();
1680 if (oldCallbacks == null) {
1681 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07001682 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04001683 return;
1684 }
1685
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001686 final List<UserHandle> profiles = mUserManager.getUserProfiles();
Kenny Guyed131872014-04-30 03:02:21 +01001687
Winson Chung64359a52013-07-08 17:17:08 -07001688 // Clear the list of apps
1689 mBgAllAppsList.clear();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001690 for (UserHandle user : profiles) {
Kenny Guyed131872014-04-30 03:02:21 +01001691 // Query for the set of apps
1692 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal3e9be432017-01-05 15:22:41 -08001693 final List<LauncherActivityInfo> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01001694 if (DEBUG_LOADERS) {
1695 Log.d(TAG, "getActivityList took "
1696 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
1697 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
1698 }
1699 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08001700 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01001701 if (apps == null || apps.isEmpty()) {
1702 return;
1703 }
Kenny Guyff05f432016-01-22 17:48:29 +00001704 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01001705 // Create the ApplicationInfos
1706 for (int i = 0; i < apps.size(); i++) {
Sunny Goyal3e9be432017-01-05 15:22:41 -08001707 LauncherActivityInfo app = apps.get(i);
Kenny Guyed131872014-04-30 03:02:21 +01001708 // This builds the icon bitmaps.
Sunny Goyal24bb66a2017-03-21 15:12:01 -07001709 mBgAllAppsList.add(new AppInfo(app, user, quietMode), app);
Kenny Guyed131872014-04-30 03:02:21 +01001710 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08001711
Sunny Goyal756a28a2015-04-23 17:07:55 -07001712 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
1713 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07001714 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07001715
1716 @Override
1717 public void run() {
1718 heuristic.processUserApps(apps);
1719 }
Sunny Goyal639e9062015-08-19 19:17:06 -07001720 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001721 mUiExecutor.execute(new Runnable() {
Sunny Goyal639e9062015-08-19 19:17:06 -07001722
Sunny Goyalb265ba72017-02-14 15:03:45 -08001723 @Override
1724 public void run() {
1725 // Check isLoadingWorkspace on the UI thread, as it is updated on
1726 // the UI thread.
1727 if (mIsLoadingAndBindingWorkspace) {
1728 synchronized (mBindCompleteRunnables) {
1729 mBindCompleteRunnables.add(r);
1730 }
1731 } else {
1732 runOnWorkerThread(r);
1733 }
1734 }
1735 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08001736 }
Winson Chung64359a52013-07-08 17:17:08 -07001737 }
Mario Bertschler817afa32017-03-15 11:56:47 -07001738
1739 if (FeatureFlags.LAUNCHER3_PROMISE_APPS_IN_ALL_APPS) {
1740 // get all active sessions and add them to the all apps list
1741 PackageInstallerCompat installer = PackageInstallerCompat.getInstance(mContext);
1742 for (PackageInstaller.SessionInfo info : installer.getAllVerifiedSessions()) {
1743 mBgAllAppsList.addPromiseApp(mContext,
1744 PackageInstallInfo.fromInstallingState(info));
1745 }
1746 }
1747
Bjorn Bringert85f418d2013-09-06 12:50:05 +01001748 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001749 final ArrayList<AppInfo> added = mBgAllAppsList.added;
1750 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07001751
Mario Bertschler817afa32017-03-15 11:56:47 -07001752
Winson Chung64359a52013-07-08 17:17:08 -07001753 // Post callback on main thread
Sunny Goyalb265ba72017-02-14 15:03:45 -08001754 mUiExecutor.execute(new Runnable() {
Winson Chung64359a52013-07-08 17:17:08 -07001755 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07001756
Winson Chung64359a52013-07-08 17:17:08 -07001757 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07001758 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07001759 if (callbacks != null) {
1760 callbacks.bindAllApplications(added);
1761 if (DEBUG_LOADERS) {
1762 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001763 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07001764 }
1765 } else {
1766 Log.i(TAG, "not binding apps: no Launcher activity");
1767 }
1768 }
1769 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07001770 // Cleanup any data stored for a deleted user.
1771 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04001772 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07001773 Log.d(TAG, "Icons processed in "
1774 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08001775 }
1776 }
1777
Sunny Goyaldd96a5e2017-02-17 11:22:34 -08001778 private void loadDeepShortcuts() {
1779 if (!mModelLoaded) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07001780 sBgDataModel.deepShortcutMap.clear();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301781 DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(mContext);
1782 mHasShortcutHostPermission = shortcutManager.hasHostPermission();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001783 if (mHasShortcutHostPermission) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001784 for (UserHandle user : mUserManager.getUserProfiles()) {
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001785 if (mUserManager.isUserUnlocked(user)) {
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301786 List<ShortcutInfoCompat> shortcuts =
1787 shortcutManager.queryForAllShortcuts(user);
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07001788 sBgDataModel.updateDeepShortcutMap(null, user, shortcuts);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001789 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001790 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001791 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001792 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001793 }
Joe Onorato36115782010-06-17 13:28:48 -04001794 }
1795
Tony Wickham80f57872016-06-29 18:12:15 -07001796 public void bindDeepShortcuts() {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07001797 final MultiHashMap<ComponentKey, String> shortcutMapCopy =
1798 sBgDataModel.deepShortcutMap.clone();
Tony Wickham80f57872016-06-29 18:12:15 -07001799 Runnable r = new Runnable() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001800 @Override
1801 public void run() {
1802 Callbacks callbacks = getCallback();
1803 if (callbacks != null) {
1804 callbacks.bindDeepShortcutMap(shortcutMapCopy);
1805 }
1806 }
Tony Wickham80f57872016-06-29 18:12:15 -07001807 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001808 mUiExecutor.execute(r);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001809 }
1810
Sunny Goyal75b0f552015-05-20 21:57:06 -07001811 /**
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001812 * Refreshes the cached shortcuts if the shortcut permission has changed.
1813 * Current implementation simply reloads the workspace, but it can be optimized to
1814 * use partial updates similar to {@link UserManagerCompat}
1815 */
1816 public void refreshShortcutsIfRequired() {
Sunny Goyalf5e37442016-11-02 10:31:24 -07001817 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001818 sWorker.removeCallbacks(mShortcutPermissionCheckRunnable);
1819 sWorker.post(mShortcutPermissionCheckRunnable);
1820 }
1821 }
1822
1823 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -07001824 * Called when the icons for packages have been updated in the icon cache.
1825 */
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001826 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandle user) {
Sunny Goyal75b0f552015-05-20 21:57:06 -07001827 // If any package icon has changed (app was updated while launcher was dead),
1828 // update the corresponding shortcuts.
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001829 enqueueModelUpdateTask(new CacheDataUpdatedTask(
1830 CacheDataUpdatedTask.OP_CACHE_UPDATE, user, updatedPackages));
Sunny Goyal75b0f552015-05-20 21:57:06 -07001831 }
1832
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001833 void enqueueModelUpdateTask(BaseModelUpdateTask task) {
Jon Mirandaf28629a2017-02-23 15:12:42 -08001834 if (!mModelLoaded && mLoaderTask == null) {
1835 if (DEBUG_LOADERS) {
1836 Log.d(TAG, "enqueueModelUpdateTask Ignoring task since loader is pending=" + task);
1837 }
1838 return;
1839 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001840 task.init(this);
1841 runOnWorkerThread(task);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001842 }
1843
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001844 /**
1845 * A task to be executed on the current callbacks on the UI thread.
1846 * If there is no current callbacks, the task is ignored.
1847 */
1848 public interface CallbackTask {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001849
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001850 void execute(Callbacks callbacks);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001851 }
1852
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001853 /**
1854 * A runnable which changes/updates the data model of the launcher based on certain events.
1855 */
1856 public static abstract class BaseModelUpdateTask implements Runnable {
Joe Onorato36115782010-06-17 13:28:48 -04001857
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001858 private LauncherModel mModel;
Sunny Goyalb265ba72017-02-14 15:03:45 -08001859 private Executor mUiExecutor;
Joe Onorato36115782010-06-17 13:28:48 -04001860
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001861 /* package private */
1862 void init(LauncherModel model) {
1863 mModel = model;
Sunny Goyalb265ba72017-02-14 15:03:45 -08001864 mUiExecutor = mModel.mUiExecutor;
Joe Onorato36115782010-06-17 13:28:48 -04001865 }
1866
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001867 @Override
Joe Onorato36115782010-06-17 13:28:48 -04001868 public void run() {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001869 if (!mModel.mHasLoaderCompletedOnce) {
Sunny Goyalc905efc2015-05-06 09:54:53 -07001870 // Loader has not yet run.
1871 return;
1872 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001873 execute(mModel.mApp, sBgDataModel, mModel.mBgAllAppsList);
1874 }
Joe Onorato36115782010-06-17 13:28:48 -04001875
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001876 /**
1877 * Execute the actual task. Called on the worker thread.
1878 */
1879 public abstract void execute(
1880 LauncherAppState app, BgDataModel dataModel, AllAppsList apps);
Sunny Goyale0f58d72014-11-10 18:05:31 -08001881
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001882 /**
1883 * Schedules a {@param task} to be executed on the current callbacks.
1884 */
1885 public final void scheduleCallbackTask(final CallbackTask task) {
1886 final Callbacks callbacks = mModel.getCallback();
Sunny Goyalb265ba72017-02-14 15:03:45 -08001887 mUiExecutor.execute(new Runnable() {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001888 public void run() {
1889 Callbacks cb = mModel.getCallback();
1890 if (callbacks == cb && cb != null) {
1891 task.execute(callbacks);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07001892 }
1893 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001894 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001895 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001896
1897 public ModelWriter getModelWriter() {
1898 // Updates from model task, do not deal with icon position in hotseat.
1899 return mModel.getWriter(false /* hasVerticalHotseat */);
1900 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001901 }
1902
Sunny Goyal1b072632017-01-18 11:30:23 -08001903 public void updateAndBindShortcutInfo(final ShortcutInfo si, final ShortcutInfoCompat info) {
1904 updateAndBindShortcutInfo(new Provider<ShortcutInfo>() {
1905 @Override
1906 public ShortcutInfo get() {
1907 si.updateFromDeepShortcutInfo(info, mApp.getContext());
1908 si.iconBitmap = LauncherIcons.createShortcutIcon(info, mApp.getContext());
1909 return si;
1910 }
1911 });
1912 }
1913
Sunny Goyal10923b32016-07-20 15:42:44 -07001914 /**
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001915 * Utility method to update a shortcut on the background thread.
Sunny Goyal10923b32016-07-20 15:42:44 -07001916 */
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001917 public void updateAndBindShortcutInfo(final Provider<ShortcutInfo> shortcutProvider) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001918 enqueueModelUpdateTask(new ExtendedModelTask() {
Sunny Goyal10923b32016-07-20 15:42:44 -07001919 @Override
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001920 public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001921 ShortcutInfo info = shortcutProvider.get();
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001922 ArrayList<ShortcutInfo> update = new ArrayList<>();
Sunny Goyal10923b32016-07-20 15:42:44 -07001923 update.add(info);
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001924 bindUpdatedShortcuts(update, info.user);
Sunny Goyal10923b32016-07-20 15:42:44 -07001925 }
1926 });
1927 }
1928
Sunny Goyald164b7f2016-10-12 20:49:31 -07001929 private void bindWidgetsModel(final Callbacks callbacks) {
1930 final MultiHashMap<PackageItemInfo, WidgetItem> widgets
1931 = mBgWidgetsModel.getWidgetsMap().clone();
Sunny Goyalb265ba72017-02-14 15:03:45 -08001932 mUiExecutor.execute(new Runnable() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001933 @Override
1934 public void run() {
1935 Callbacks cb = getCallback();
1936 if (callbacks == cb && cb != null) {
Sunny Goyald164b7f2016-10-12 20:49:31 -07001937 callbacks.bindAllWidgets(widgets);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001938 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07001939 }
1940 });
1941 }
1942
Tony Wickham86222d22017-03-29 15:30:43 -07001943 public void refreshAndBindWidgetsAndShortcuts(final Callbacks callbacks,
1944 final boolean bindFirst, @Nullable final PackageUserKey packageUser) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001945 runOnWorkerThread(new Runnable() {
1946 @Override
1947 public void run() {
1948 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
Sunny Goyald164b7f2016-10-12 20:49:31 -07001949 bindWidgetsModel(callbacks);
Sunny Goyal31860582015-09-18 08:38:57 -07001950 }
Tony Wickham86222d22017-03-29 15:30:43 -07001951 ArrayList<WidgetItem> widgets = mBgWidgetsModel.update(
1952 mApp.getContext(), packageUser);
Sunny Goyald164b7f2016-10-12 20:49:31 -07001953 bindWidgetsModel(callbacks);
1954
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001955 // update the Widget entries inside DB on the worker thread.
Tony Wickham86222d22017-03-29 15:30:43 -07001956 mApp.getWidgetCache().removeObsoletePreviews(widgets, packageUser);
Sunny Goyal31860582015-09-18 08:38:57 -07001957 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001958 });
Michael Jurkac402cd92013-05-20 15:49:32 +02001959 }
1960
Sunny Goyal651077b2014-06-30 14:15:31 -07001961 static boolean isValidProvider(AppWidgetProviderInfo provider) {
1962 return (provider != null) && (provider.provider != null)
1963 && (provider.provider.getPackageName() != null);
1964 }
1965
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001966 public void dumpState(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
1967 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
1968 writer.println(prefix + "All apps list: size=" + mBgAllAppsList.data.size());
1969 for (AppInfo info : mBgAllAppsList.data) {
1970 writer.println(prefix + " title=\"" + info.title + "\" iconBitmap=" + info.iconBitmap
1971 + " componentName=" + info.componentName.getPackageName());
1972 }
Joe Onorato36115782010-06-17 13:28:48 -04001973 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001974 sBgDataModel.dump(prefix, fd, writer, args);
Joe Onoratobe386092009-11-17 17:32:16 -08001975 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08001976
1977 public Callbacks getCallback() {
1978 return mCallbacks != null ? mCallbacks.get() : null;
1979 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07001980
1981 /**
1982 * @return {@link FolderInfo} if its already loaded.
1983 */
1984 public FolderInfo findFolderById(Long folderId) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001985 synchronized (sBgDataModel) {
1986 return sBgDataModel.folders.get(folderId);
Sunny Goyal18bf8e22015-04-08 18:13:46 -07001987 }
1988 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07001989
1990 /**
1991 * @return the looper for the worker thread which can be used to start background tasks.
1992 */
1993 public static Looper getWorkerLooper() {
1994 return sWorkerThread.getLooper();
1995 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996}