blob: 5bde8393d12ddfa19711c26b1980f454db455903 [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.net.Uri;
Joe Onorato36115782010-06-17 13:28:48 -040030import android.os.Handler;
31import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070032import android.os.Looper;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040034import android.os.SystemClock;
Sunny Goyale26d1002016-06-20 14:52:14 -070035import android.os.Trace;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080036import android.os.UserHandle;
Tony Wickham86222d22017-03-29 15:30:43 -070037import android.support.annotation.Nullable;
Winson Chunga90303b2013-11-15 13:05:06 -080038import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070039import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080040import android.util.LongSparseArray;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070041import android.util.MutableInt;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010042
Sunny Goyalffe83f12014-08-14 17:39:34 -070043import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010044import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070045import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070046import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010047import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyal6c56c682015-07-16 14:09:05 -070048import com.android.launcher3.config.ProviderConfig;
Tony Wickham827cef22016-03-17 15:39:39 -070049import com.android.launcher3.dynamicui.ExtractionUtils;
Sunny Goyal26119432016-02-18 22:09:23 +000050import com.android.launcher3.folder.Folder;
51import com.android.launcher3.folder.FolderIcon;
Sunny Goyal1b072632017-01-18 11:30:23 -080052import com.android.launcher3.graphics.LauncherIcons;
Sunny Goyal1acb9e92016-05-16 12:41:09 -070053import com.android.launcher3.logging.FileLog;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070054import com.android.launcher3.model.AddWorkspaceItemsTask;
Sunny Goyale9956a72016-09-01 17:24:47 -070055import com.android.launcher3.model.BgDataModel;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070056import com.android.launcher3.model.CacheDataUpdatedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080057import com.android.launcher3.model.ExtendedModelTask;
Sunny Goyalf862a262015-12-14 14:27:38 -080058import com.android.launcher3.model.GridSizeMigrationTask;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -080059import com.android.launcher3.model.LoaderCursor;
Sunny Goyal43bf11d2017-02-02 13:52:53 -080060import com.android.launcher3.model.ModelWriter;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070061import com.android.launcher3.model.PackageInstallStateChangedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080062import com.android.launcher3.model.PackageItemInfo;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070063import com.android.launcher3.model.PackageUpdatedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080064import com.android.launcher3.model.SdCardAvailableReceiver;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070065import com.android.launcher3.model.ShortcutsChangedTask;
66import com.android.launcher3.model.UserLockStateChangedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080067import com.android.launcher3.model.WidgetItem;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070068import com.android.launcher3.model.WidgetsModel;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -070069import com.android.launcher3.provider.ImportDataTask;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070070import com.android.launcher3.provider.LauncherDbUtils;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070071import com.android.launcher3.shortcuts.DeepShortcutManager;
72import com.android.launcher3.shortcuts.ShortcutInfoCompat;
73import com.android.launcher3.shortcuts.ShortcutKey;
Robin Lee26ace122015-03-16 19:41:43 +000074import com.android.launcher3.util.ComponentKey;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070075import com.android.launcher3.util.ManagedProfileHeuristic;
Tony Wickhamd82a39d2016-07-01 15:44:13 -070076import com.android.launcher3.util.MultiHashMap;
Sunny Goyald09c3702016-04-06 16:18:20 -070077import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham86222d22017-03-29 15:30:43 -070078import com.android.launcher3.util.PackageUserKey;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070079import com.android.launcher3.util.Preconditions;
Sunny Goyal2bcbe132016-11-16 09:23:42 -080080import com.android.launcher3.util.Provider;
Adam Cohen091440a2015-03-18 14:16:05 -070081import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070082import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080083
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -080084import java.io.FileDescriptor;
85import java.io.PrintWriter;
Michael Jurkac2f801e2011-07-12 14:19:46 -070086import java.lang.ref.WeakReference;
Michael Jurkac2f801e2011-07-12 14:19:46 -070087import java.util.ArrayList;
88import java.util.Collections;
89import java.util.Comparator;
90import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070091import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070092import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070093import java.util.List;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070094import java.util.Map;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070095import java.util.Set;
Sunny Goyaldd96a5e2017-02-17 11:22:34 -080096import java.util.concurrent.CancellationException;
Sunny Goyal527c7d32015-08-28 15:19:36 -070097import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -070098
The Android Open Source Project31dd5032009-03-03 19:32:27 -080099/**
100 * Maintains in-memory state of the Launcher. It is expected that there should be only one
101 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700102 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800103 */
Kenny Guyed131872014-04-30 03:02:21 +0100104public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +0100105 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800106 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -0400107 private static final boolean DEBUG_RECEIVER = false;
Chris Wrenb358f812014-04-16 13:37:00 -0400108
Joe Onorato9c1289c2009-08-17 11:03:03 -0400109 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700110
Joe Onorato36115782010-06-17 13:28:48 -0400111 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500112 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800113
Adam Cohen091440a2015-03-18 14:16:05 -0700114 @Thunk final LauncherAppState mApp;
115 @Thunk final Object mLock = new Object();
116 @Thunk DeferredHandler mHandler = new DeferredHandler();
117 @Thunk LoaderTask mLoaderTask;
118 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700119 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120
Adam Cohen091440a2015-03-18 14:16:05 -0700121 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700122 static {
123 sWorkerThread.start();
124 }
Adam Cohen091440a2015-03-18 14:16:05 -0700125 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700126
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800127 // Indicates whether the current model data is valid or not.
128 // We start off with everything not loaded. After that, we assume that
Joe Onoratocc67f472010-06-08 10:54:30 -0700129 // our monitoring of the package manager provides all updates and we never
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800130 // need to do a requery. This is only ever touched from the loader thread.
131 private boolean mModelLoaded;
Hyunyoung Songe295aca2017-02-06 10:46:24 -0800132 public boolean isModelLoaded() {
133 synchronized (mLock) {
134 return mModelLoaded && mLoaderTask == null;
135 }
136 }
Joe Onoratocc67f472010-06-08 10:54:30 -0700137
Sunny Goyal756a28a2015-04-23 17:07:55 -0700138 /**
139 * Set of runnables to be called on the background thread after the workspace binding
140 * is complete.
141 */
142 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
143
Adam Cohen091440a2015-03-18 14:16:05 -0700144 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700146 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800147 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700148 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800149 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800150
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700151 private boolean mHasShortcutHostPermission;
152 // Runnable to check if the shortcuts permission has changed.
153 private final Runnable mShortcutPermissionCheckRunnable = new Runnable() {
154 @Override
155 public void run() {
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800156 if (mModelLoaded) {
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530157 boolean hasShortcutHostPermission =
158 DeepShortcutManager.getInstance(mApp.getContext()).hasHostPermission();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700159 if (hasShortcutHostPermission != mHasShortcutHostPermission) {
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800160 forceReload();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700161 }
162 }
163 }
164 };
165
Sunny Goyale9956a72016-09-01 17:24:47 -0700166 /**
167 * All the static data should be accessed on the background thread, A lock should be acquired
168 * on this object when accessing any data from this model.
169 */
170 static final BgDataModel sBgDataModel = new BgDataModel();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700171
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700172 // </ only access in worker thread >
173
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700174 private final IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700176 private final LauncherAppsCompat mLauncherApps;
177 private final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100178
Joe Onorato9c1289c2009-08-17 11:03:03 -0400179 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700180 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400181 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700182 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400183 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700184 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
185 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700186 public void bindScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700187 public void finishFirstPageBind(ViewOnDrawExecutor executor);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800188 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400189 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200190 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700191 public void bindAppsAdded(ArrayList<Long> newScreens,
192 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700193 ArrayList<ItemInfo> addAnimated,
194 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200195 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700196 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800197 ArrayList<ShortcutInfo> removed, UserHandle user);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700198 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700199 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700200 public void bindWorkspaceComponentsRemoved(
201 HashSet<String> packageNames, HashSet<ComponentName> components,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800202 UserHandle user);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700203 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800204 public void notifyWidgetProvidersChanged();
Sunny Goyald164b7f2016-10-12 20:49:31 -0700205 public void bindAllWidgets(MultiHashMap<PackageItemInfo, WidgetItem> widgets);
Adam Cohen1462de32012-07-24 22:34:36 -0700206 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700207 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700208 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMap);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400209 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530211 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800212 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400213 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100214 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Sunny Goyald164b7f2016-10-12 20:49:31 -0700215 mBgWidgetsModel = new WidgetsModel(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800216 mIconCache = iconCache;
217
Kenny Guyed131872014-04-30 03:02:21 +0100218 mLauncherApps = LauncherAppsCompat.getInstance(context);
219 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800220 }
221
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700222 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
223 * posted on the main thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700224 private void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700225 if (sWorkerThread.getThreadId() == Process.myTid()) {
226 // If we are on the worker thread, post onto the main handler
227 mHandler.post(r);
228 } else {
229 r.run();
230 }
231 }
232
233 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
234 * posted on the worker thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700235 private static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700236 if (sWorkerThread.getThreadId() == Process.myTid()) {
237 r.run();
238 } else {
239 // If we are not on the worker thread, then post to the worker handler
240 sWorker.post(r);
241 }
242 }
243
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700244 public void setPackageState(PackageInstallInfo installInfo) {
245 enqueueModelUpdateTask(new PackageInstallStateChangedTask(installInfo));
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500246 }
247
Sunny Goyal756adbc2015-04-16 15:20:51 -0700248 /**
249 * Updates the icons and label of all pending icons for the provided package name.
250 */
251 public void updateSessionDisplayInfo(final String packageName) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700252 HashSet<String> packages = new HashSet<>();
253 packages.add(packageName);
254 enqueueModelUpdateTask(new CacheDataUpdatedTask(
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800255 CacheDataUpdatedTask.OP_SESSION_UPDATE, Process.myUserHandle(), packages));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800256 }
257
258 /**
259 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800260 */
Sunny Goyal2bcbe132016-11-16 09:23:42 -0800261 public void addAndBindAddedWorkspaceItems(List<ItemInfo> workspaceApps) {
262 addAndBindAddedWorkspaceItems(Provider.of(workspaceApps));
263 }
264
265 /**
266 * Adds the provided items to the workspace.
267 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700268 public void addAndBindAddedWorkspaceItems(
Sunny Goyal2bcbe132016-11-16 09:23:42 -0800269 Provider<List<ItemInfo>> appsProvider) {
270 enqueueModelUpdateTask(new AddWorkspaceItemsTask(appsProvider));
Winson Chung64359a52013-07-08 17:17:08 -0700271 }
272
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800273 public ModelWriter getWriter(boolean hasVerticalHotseat) {
274 return new ModelWriter(mApp.getContext(), sBgDataModel, hasVerticalHotseat);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275 }
276
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700277 static void checkItemInfoLocked(
278 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
Sunny Goyale9956a72016-09-01 17:24:47 -0700279 ItemInfo modelItem = sBgDataModel.itemsIdMap.get(itemId);
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700280 if (modelItem != null && item != modelItem) {
281 // check all the data is consistent
282 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
283 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
284 ShortcutInfo shortcut = (ShortcutInfo) item;
285 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
286 modelShortcut.intent.filterEquals(shortcut.intent) &&
287 modelShortcut.id == shortcut.id &&
288 modelShortcut.itemType == shortcut.itemType &&
289 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700290 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700291 modelShortcut.cellX == shortcut.cellX &&
292 modelShortcut.cellY == shortcut.cellY &&
293 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700294 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700295 // For all intents and purposes, this is the same object
296 return;
297 }
298 }
299
300 // the modelItem needs to match up perfectly with item if our model is
301 // to be consistent with the database-- for now, just require
302 // modelItem == item or the equality check above
303 String msg = "item: " + ((item != null) ? item.toString() : "null") +
304 "modelItem: " +
305 ((modelItem != null) ? modelItem.toString() : "null") +
306 "Error: ItemInfo passed to checkItemInfo doesn't match original";
307 RuntimeException e = new RuntimeException(msg);
308 if (stackTrace != null) {
309 e.setStackTrace(stackTrace);
310 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800311 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700312 }
313 }
314
Michael Jurka816474f2012-06-25 14:49:02 -0700315 static void checkItemInfo(final ItemInfo item) {
316 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
317 final long itemId = item.id;
318 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700319 public void run() {
Sunny Goyale9956a72016-09-01 17:24:47 -0700320 synchronized (sBgDataModel) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700321 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700322 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700323 }
324 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700325 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700326 }
327
Joe Onorato9c1289c2009-08-17 11:03:03 -0400328 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700329 * Update the order of the workspace screens in the database. The array list contains
330 * a list of screen ids in the order that they should appear.
331 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700332 public static void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -0700333 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700334 final ContentResolver cr = context.getContentResolver();
335 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
336
337 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700338 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700339 while (iter.hasNext()) {
340 long id = iter.next();
341 if (id < 0) {
342 iter.remove();
343 }
344 }
345
346 Runnable r = new Runnable() {
347 @Override
348 public void run() {
Yura085c8532014-02-11 15:15:29 +0000349 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700350 // Clear the table
Yura085c8532014-02-11 15:15:29 +0000351 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -0700352 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700353 for (int i = 0; i < count; i++) {
354 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700355 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700356 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
357 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +0000358 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -0700359 }
Yura085c8532014-02-11 15:15:29 +0000360
361 try {
362 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
363 } catch (Exception ex) {
364 throw new RuntimeException(ex);
365 }
Winson Chung9e6a0a22013-08-27 11:58:12 -0700366
Sunny Goyale9956a72016-09-01 17:24:47 -0700367 synchronized (sBgDataModel) {
368 sBgDataModel.workspaceScreens.clear();
369 sBgDataModel.workspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -0700370 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700371 }
372 };
373 runOnWorkerThread(r);
374 }
375
376 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400377 * Set this as the current Launcher activity object for the loader.
378 */
379 public void initialize(Callbacks callbacks) {
380 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -0700381 Preconditions.assertUIThread();
382 // Remove any queued UI runnables
383 mHandler.cancelAll();
384 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400385 }
386 }
387
Kenny Guyed131872014-04-30 03:02:21 +0100388 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800389 public void onPackageChanged(String packageName, UserHandle user) {
Kenny Guyed131872014-04-30 03:02:21 +0100390 int op = PackageUpdatedTask.OP_UPDATE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700391 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100392 }
393
394 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800395 public void onPackageRemoved(String packageName, UserHandle user) {
Sunny Goyalc2936bc2016-09-01 15:50:36 -0700396 onPackagesRemoved(user, packageName);
397 }
398
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800399 public void onPackagesRemoved(UserHandle user, String... packages) {
Kenny Guyed131872014-04-30 03:02:21 +0100400 int op = PackageUpdatedTask.OP_REMOVE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700401 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packages));
Kenny Guyed131872014-04-30 03:02:21 +0100402 }
403
404 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800405 public void onPackageAdded(String packageName, UserHandle user) {
Kenny Guyed131872014-04-30 03:02:21 +0100406 int op = PackageUpdatedTask.OP_ADD;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700407 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100408 }
409
410 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800411 public void onPackagesAvailable(String[] packageNames, UserHandle user,
Kenny Guyed131872014-04-30 03:02:21 +0100412 boolean replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700413 enqueueModelUpdateTask(
414 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100415 }
416
417 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800418 public void onPackagesUnavailable(String[] packageNames, UserHandle user,
Kenny Guyed131872014-04-30 03:02:21 +0100419 boolean replacing) {
420 if (!replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700421 enqueueModelUpdateTask(new PackageUpdatedTask(
422 PackageUpdatedTask.OP_UNAVAILABLE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100423 }
Kenny Guyed131872014-04-30 03:02:21 +0100424 }
425
Kenny Guy44cba692016-01-21 19:50:02 +0000426 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800427 public void onPackagesSuspended(String[] packageNames, UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700428 enqueueModelUpdateTask(new PackageUpdatedTask(
429 PackageUpdatedTask.OP_SUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000430 }
431
432 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800433 public void onPackagesUnsuspended(String[] packageNames, UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700434 enqueueModelUpdateTask(new PackageUpdatedTask(
435 PackageUpdatedTask.OP_UNSUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000436 }
437
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700438 @Override
439 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800440 UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700441 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, true));
Sunny Goyal50941fb2016-08-04 12:03:52 -0700442 }
443
444 public void updatePinnedShortcuts(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800445 UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700446 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, false));
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700447 }
448
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700449 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400450 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
451 * ACTION_PACKAGE_CHANGED.
452 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100453 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400454 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -0400455 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700456
Joe Onorato36115782010-06-17 13:28:48 -0400457 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +0100458 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -0700459 // If we have changed locale we need to clear out the labels in all apps/workspace.
460 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700461 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
462 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -0700463 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -0700464 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700465 } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
466 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
467 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800468 UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER);
Sunny Goyalda891c12016-03-18 18:29:24 -0700469 if (user != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700470 if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
471 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700472 enqueueModelUpdateTask(new PackageUpdatedTask(
473 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE, user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700474 }
475
476 // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so
477 // we need to run the state change task again.
478 if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
479 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700480 enqueueModelUpdateTask(new UserLockStateChangedTask(user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700481 }
Sunny Goyalda891c12016-03-18 18:29:24 -0700482 }
Tony Wickham827cef22016-03-17 15:39:39 -0700483 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
484 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700485 }
486 }
487
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800488 /**
489 * Reloads the workspace items from the DB and re-binds the workspace. This should generally
490 * not be called as DB updates are automatically followed by UI update
491 */
492 public void forceReload() {
493 synchronized (mLock) {
494 // Stop any existing loaders first, so they don't set mModelLoaded to true later
495 stopLoaderLocked();
496 mModelLoaded = false;
497 }
Winson Chungf0c6ae02012-03-21 16:10:31 -0700498
Reena Lee93f824a2011-09-23 17:20:28 -0700499 // Do this here because if the launcher activity is running it will be restarted.
500 // If it's not running startLoaderFromBackground will merely tell it that it needs
501 // to reload.
502 startLoaderFromBackground();
503 }
504
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700505 /**
506 * When the launcher is in the background, it's possible for it to miss paired
507 * configuration changes. So whenever we trigger the loader from the background
508 * tell the launcher that it needs to re-run the loader when it comes back instead
509 * of doing it now.
510 */
511 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800512 Callbacks callbacks = getCallback();
513 if (callbacks != null) {
514 // Only actually run the loader if they're not paused.
515 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -0700516 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700517 }
518 }
Joe Onorato36115782010-06-17 13:28:48 -0400519 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400520
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700521 /**
522 * If there is already a loader task running, tell it to stop.
523 */
524 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -0700525 LoaderTask oldTask = mLoaderTask;
526 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -0700527 oldTask.stopLocked();
528 }
Reena Lee93f824a2011-09-23 17:20:28 -0700529 }
530
Adam Cohen1a85c582014-09-30 09:48:49 -0700531 public boolean isCurrentCallbacks(Callbacks callbacks) {
532 return (mCallbacks != null && mCallbacks.get() == callbacks);
533 }
534
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700535 /**
536 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
537 * @return true if the page could be bound synchronously.
538 */
539 public boolean startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700540 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
541 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -0400542 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -0400543 // Don't bother to start the thread if we know it's not going to do anything
544 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -0700545 final Callbacks oldCallbacks = mCallbacks.get();
546 // Clear any pending bind-runnables from the synchronized load process.
547 runOnMainThread(new Runnable() {
548 public void run() {
549 oldCallbacks.clearPendingBinds();
550 }
551 });
552
Joe Onorato36115782010-06-17 13:28:48 -0400553 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700554 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700555 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800556 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
557 && mModelLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700558 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700559 return true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700560 } else {
561 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
562 sWorker.post(mLoaderTask);
563 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400564 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400565 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700566 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400567 }
568
Joe Onorato36115782010-06-17 13:28:48 -0400569 public void stopLoader() {
570 synchronized (mLock) {
571 if (mLoaderTask != null) {
572 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400573 }
574 }
Joe Onorato36115782010-06-17 13:28:48 -0400575 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400576
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800577 /**
578 * Loads the workspace screen ids in an ordered list.
579 */
Sunny Goyale5bb7052015-07-27 14:36:07 -0700580 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -0700581 final ContentResolver contentResolver = context.getContentResolver();
582 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -0700583
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800584 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -0700585 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
586 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -0700587 }
588
Joe Onorato36115782010-06-17 13:28:48 -0400589 /**
590 * Runnable for the thread that loads the contents of the launcher:
591 * - workspace icons
592 * - widgets
593 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700594 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -0400595 */
596 private class LoaderTask implements Runnable {
597 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -0700598 private int mPageToBindFirst;
599
Adam Cohen091440a2015-03-18 14:16:05 -0700600 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -0400601 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -0700602 @Thunk boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700603
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700604 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -0400605 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -0700606 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400607 }
608
Joe Onorato36115782010-06-17 13:28:48 -0400609 private void waitForIdle() {
610 // Wait until the either we're stopped or the other threads are done.
611 // This way we don't start loading all apps until the workspace has settled
612 // down.
613 synchronized (LoaderTask.this) {
614 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700615
Joe Onorato36115782010-06-17 13:28:48 -0400616 mHandler.postIdle(new Runnable() {
617 public void run() {
618 synchronized (LoaderTask.this) {
619 mLoadAndBindStepFinished = true;
620 if (DEBUG_LOADERS) {
621 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400622 }
Joe Onorato36115782010-06-17 13:28:48 -0400623 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400624 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400625 }
Joe Onorato36115782010-06-17 13:28:48 -0400626 });
627
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800628 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -0400629 try {
Michael Jurkac7700af2013-05-14 20:17:58 +0200630 // Just in case mFlushingWorkerThread changes but we aren't woken up,
631 // wait no longer than 1sec at a time
632 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -0400633 } catch (InterruptedException ex) {
634 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400635 }
636 }
Joe Onorato36115782010-06-17 13:28:48 -0400637 if (DEBUG_LOADERS) {
638 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700639 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400640 + "ms for previous step to finish binding");
641 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400642 }
Joe Onorato36115782010-06-17 13:28:48 -0400643 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400644
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700645 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -0500646 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700647 // Ensure that we have a valid page index to load synchronously
648 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
649 "valid page index");
650 }
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800651 if (!mModelLoaded) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700652 // Ensure that we don't try and bind a specified page when the pages have not been
653 // loaded already (we should load everything asynchronously in that case)
654 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
655 }
656 synchronized (mLock) {
657 if (mIsLoaderTaskRunning) {
658 // Ensure that we are never running the background loading at this point since
659 // we also touch the background collections
660 throw new RuntimeException("Error! Background loading is already running");
661 }
662 }
663
664 // XXX: Throw an exception if we are already loading (since we touch the worker thread
665 // data structures, we can't allow any other thread to touch that data, but because
666 // this call is synchronous, we can get away with not locking).
667
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400668 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -0700669 // operations from the previous activity. We need to ensure that all queued operations
670 // are executed before any synchronous binding work is done.
671 mHandler.flush();
672
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700673 // Divide the set of loaded items into those that we are binding synchronously, and
674 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800675 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700676 // XXX: For now, continue posting the binding of AllApps as there are other issues that
677 // arise from that.
678 onlyBindAllApps();
Tony Wickham80f57872016-06-29 18:12:15 -0700679
680 bindDeepShortcuts();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700681 }
682
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800683 private void verifyNotStopped() throws CancellationException {
684 synchronized (LoaderTask.this) {
685 if (mStopped) {
686 throw new CancellationException("Loader stopped");
687 }
688 }
689 }
690
Joe Onorato36115782010-06-17 13:28:48 -0400691 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700692 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -0700693 if (mStopped) {
694 return;
695 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700696 mIsLoaderTaskRunning = true;
697 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400698
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800699 try {
700 if (DEBUG_LOADERS) Log.d(TAG, "step 1.1: loading workspace");
701 // Set to false in bindWorkspace()
702 mIsLoadingAndBindingWorkspace = true;
703 loadWorkspace();
Joe Onorato36115782010-06-17 13:28:48 -0400704
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800705 verifyNotStopped();
706 if (DEBUG_LOADERS) Log.d(TAG, "step 1.2: bind workspace workspace");
707 bindWorkspace(mPageToBindFirst);
708
709 // Take a break
710 if (DEBUG_LOADERS) Log.d(TAG, "step 1 completed, wait for idle");
Joe Onorato36115782010-06-17 13:28:48 -0400711 waitForIdle();
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800712 verifyNotStopped();
Daniel Sandler843e8602010-06-07 14:59:01 -0400713
714 // second step
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800715 if (DEBUG_LOADERS) Log.d(TAG, "step 2.1: loading all apps");
716 loadAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700717
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800718 verifyNotStopped();
719 if (DEBUG_LOADERS) Log.d(TAG, "step 2.2: Update icon cache");
720 updateIconCache();
721
722 // Take a break
723 if (DEBUG_LOADERS) Log.d(TAG, "step 2 completed, wait for idle");
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700724 waitForIdle();
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800725 verifyNotStopped();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700726
727 // third step
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800728 if (DEBUG_LOADERS) Log.d(TAG, "step 3.1: loading deep shortcuts");
729 loadDeepShortcuts();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400730
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800731 verifyNotStopped();
732 if (DEBUG_LOADERS) Log.d(TAG, "step 3.2: bind deep shortcuts");
733 bindDeepShortcuts();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400734
Tony Wickhama7e1c1c2017-04-13 16:24:38 -0700735 // Take a break
736 if (DEBUG_LOADERS) Log.d(TAG, "step 3 completed, wait for idle");
737 waitForIdle();
738 verifyNotStopped();
739
740 // fourth step
741 if (DEBUG_LOADERS) Log.d(TAG, "step 4.1: loading widgets");
742 refreshAndBindWidgetsAndShortcuts(getCallback(), false /* bindFirst */,
743 null /* packageUser */);
744
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800745 synchronized (mLock) {
746 // Everything loaded bind the data.
747 mModelLoaded = true;
748 mHasLoaderCompletedOnce = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400749 }
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800750 } catch (CancellationException e) {
751 // Loader stopped, ignore
752 } finally {
753 // Clear out this reference, otherwise we end up holding it until all of the
754 // callback runnables are done.
755 mContext = null;
756
757 synchronized (mLock) {
758 // If we are still the last one to be scheduled, remove ourselves.
759 if (mLoaderTask == this) {
760 mLoaderTask = null;
761 }
762 mIsLoaderTaskRunning = false;
763 }
Joe Onorato36115782010-06-17 13:28:48 -0400764 }
Joe Onorato36115782010-06-17 13:28:48 -0400765 }
766
767 public void stopLocked() {
768 synchronized (LoaderTask.this) {
769 mStopped = true;
770 this.notify();
771 }
772 }
773
774 /**
775 * Gets the callbacks object. If we've been stopped, or if the launcher object
776 * has somehow been garbage collected, return null instead. Pass in the Callbacks
777 * object that was around when the deferred message was scheduled, and if there's
778 * a new Callbacks object around then also return null. This will save us from
779 * calling onto it with data that will be ignored.
780 */
781 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
782 synchronized (mLock) {
783 if (mStopped) {
784 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400785 }
Joe Onorato36115782010-06-17 13:28:48 -0400786
787 if (mCallbacks == null) {
788 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400789 }
Joe Onorato36115782010-06-17 13:28:48 -0400790
791 final Callbacks callbacks = mCallbacks.get();
792 if (callbacks != oldCallbacks) {
793 return null;
794 }
795 if (callbacks == null) {
796 Log.w(TAG, "no mCallbacks");
797 return null;
798 }
799
800 return callbacks;
801 }
802 }
803
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800804 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -0700805 if (LauncherAppState.PROFILE_STARTUP) {
806 Trace.beginSection("Loading Workspace");
807 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400808
Joe Onorato36115782010-06-17 13:28:48 -0400809 final Context context = mContext;
810 final ContentResolver contentResolver = context.getContentResolver();
Sunny Goyal342e4662017-02-02 15:21:08 -0800811 final PackageManagerHelper pmHelper = new PackageManagerHelper(context);
812 final boolean isSafeMode = pmHelper.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700813 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530814 final DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -0700815 final boolean isSdCardReady = Utilities.isBootCompleted();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800816 final MultiHashMap<UserHandle, String> pendingPackages = new MultiHashMap<>();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400817
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700818 boolean clearDb = false;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -0700819 try {
820 ImportDataTask.performImportIfPossible(context);
821 } catch (Exception e) {
822 // Migration failed. Clear workspace.
823 clearDb = true;
824 }
825
826 if (!clearDb && GridSizeMigrationTask.ENABLED &&
Sunny Goyalf076eae2016-01-11 12:25:10 -0800827 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
828 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700829 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -0700830 }
831
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700832 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -0700833 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -0700834 LauncherSettings.Settings.call(contentResolver,
Sunny Goyale05b08f2017-02-23 18:30:22 -0800835 LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -0500836 }
837
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700838 Log.d(TAG, "loadWorkspace: loading default favorites");
839 LauncherSettings.Settings.call(contentResolver,
840 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -0700841
Sunny Goyale9956a72016-09-01 17:24:47 -0700842 synchronized (sBgDataModel) {
843 sBgDataModel.clear();
844
Sunny Goyal756adbc2015-04-16 15:20:51 -0700845 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -0700846 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyale9956a72016-09-01 17:24:47 -0700847 sBgDataModel.workspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -0800848
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700849 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800850 final LoaderCursor c = new LoaderCursor(contentResolver.query(
851 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null), mApp);
Daniel Sandler8802e962010-05-26 16:28:16 -0400852
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800853 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400854
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700855 try {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700856 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
857 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -0400858 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
859 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700860 final int spanXIndex = c.getColumnIndexOrThrow
861 (LauncherSettings.Favorites.SPANX);
862 final int spanYIndex = c.getColumnIndexOrThrow(
863 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800864 final int rankIndex = c.getColumnIndexOrThrow(
865 LauncherSettings.Favorites.RANK);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700866 final int optionsIndex = c.getColumnIndexOrThrow(
867 LauncherSettings.Favorites.OPTIONS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400868
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800869 final LongSparseArray<UserHandle> allUsers = c.allUsers;
Kenny Guyff05f432016-01-22 17:48:29 +0000870 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700871 final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800872 for (UserHandle user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +0000873 long serialNo = mUserManager.getSerialNumberForUser(user);
874 allUsers.put(serialNo, user);
875 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700876
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700877 boolean userUnlocked = mUserManager.isUserUnlocked(user);
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700878
879 // We can only query for shortcuts when the user is unlocked.
880 if (userUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -0700881 List<ShortcutInfoCompat> pinnedShortcuts =
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530882 shortcutManager.queryForPinnedShortcuts(null, user);
883 if (shortcutManager.wasLastCallSuccess()) {
Sunny Goyal49f4f032016-08-01 15:45:49 -0700884 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
885 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
886 shortcut);
887 }
888 } else {
889 // Shortcut manager can fail due to some race condition when the
890 // lock state changes too frequently. For the purpose of the loading
891 // shortcuts, consider the user is still locked.
892 userUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700893 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700894 }
Sunny Goyal49f4f032016-08-01 15:45:49 -0700895 unlockedUsers.put(serialNo, userUnlocked);
Sunny Goyal7f834d22015-04-21 10:10:23 -0700896 }
897
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700898 ShortcutInfo info;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700899 LauncherAppWidgetInfo appWidgetInfo;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700900 Intent intent;
Sunny Goyal81e44912017-01-14 15:05:14 -0800901 String targetPkg;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400902
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700903 while (!mStopped && c.moveToNext()) {
904 try {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800905 if (c.user == null) {
906 // User has been deleted, remove the item.
907 c.markDeleted("User has been deleted");
908 continue;
909 }
910
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700911 boolean allowMissingTarget = false;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800912 switch (c.itemType) {
Sunny Goyal81e44912017-01-14 15:05:14 -0800913 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700914 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700915 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Sunny Goyal81e44912017-01-14 15:05:14 -0800916 intent = c.parseIntent();
917 if (intent == null) {
918 c.markDeleted("Invalid or null intent");
919 continue;
920 }
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800921
Sunny Goyal81e44912017-01-14 15:05:14 -0800922 int disabledState = quietMode.get(c.serialNumber) ?
923 ShortcutInfo.FLAG_DISABLED_QUIET_USER : 0;
924 ComponentName cn = intent.getComponent();
925 targetPkg = cn == null ? intent.getPackage() : cn.getPackageName();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700926
Sunny Goyal81e44912017-01-14 15:05:14 -0800927 if (!Process.myUserHandle().equals(c.user)) {
928 if (c.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
929 c.markDeleted("Legacy shortcuts are only allowed for default user");
930 continue;
931 } else if (c.restoreFlag != 0) {
932 // Don't restore items for other profiles.
933 c.markDeleted("Restore from managed profile not supported");
934 continue;
935 }
936 }
937 if (TextUtils.isEmpty(targetPkg) &&
938 c.itemType != LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
939 c.markDeleted("Only legacy shortcuts can have null package");
940 continue;
941 }
Sunny Goyal34942622014-08-29 17:20:55 -0700942
Sunny Goyal81e44912017-01-14 15:05:14 -0800943 // If there is no target package, its an implicit intent
944 // (legacy shortcut) which is always valid
945 boolean validTarget = TextUtils.isEmpty(targetPkg) ||
946 launcherApps.isPackageEnabledForProfile(targetPkg, c.user);
Sunny Goyal94485362014-09-18 16:13:58 -0700947
Sunny Goyal81e44912017-01-14 15:05:14 -0800948 if (cn != null && validTarget) {
949 // If the apk is present and the shortcut points to a specific
950 // component.
951
952 // If the component is already present
953 if (launcherApps.isActivityEnabledForProfile(cn, c.user)) {
954 // no special handling necessary for this item
955 c.markRestored();
956 } else {
957 if (c.hasRestoreFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
958 // We allow auto install apps to have their intent
959 // updated after an install.
Sunny Goyal342e4662017-02-02 15:21:08 -0800960 intent = pmHelper.getAppLaunchIntent(targetPkg, c.user);
Sunny Goyal81e44912017-01-14 15:05:14 -0800961 if (intent != null) {
962 c.restoreFlag = 0;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800963 c.updater().put(
Sunny Goyal81e44912017-01-14 15:05:14 -0800964 LauncherSettings.Favorites.INTENT,
965 intent.toUri(0)).commit();
966 cn = intent.getComponent();
Sunny Goyalb05a00a2016-08-29 10:06:57 -0700967 } else {
Sunny Goyal81e44912017-01-14 15:05:14 -0800968 c.markDeleted("Unable to find a launch target");
Sunny Goyal94485362014-09-18 16:13:58 -0700969 continue;
970 }
Sunny Goyal1a745e82014-10-02 15:58:31 -0700971 } else {
Sunny Goyal81e44912017-01-14 15:05:14 -0800972 // The app is installed but the component is no
973 // longer available.
974 c.markDeleted("Invalid component removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -0700975 continue;
Winson Chungee055712013-07-30 14:46:24 -0700976 }
Winson Chungee055712013-07-30 14:46:24 -0700977 }
Sunny Goyal81e44912017-01-14 15:05:14 -0800978 }
979 // else if cn == null => can't infer much, leave it
980 // else if !validPkg => could be restored icon or missing sd-card
981
982 if (!TextUtils.isEmpty(targetPkg) && !validTarget) {
983 // Points to a valid app (superset of cn != null) but the apk
984 // is not available.
985
986 if (c.restoreFlag != 0) {
987 // Package is not yet available but might be
988 // installed later.
989 FileLog.d(TAG, "package not yet restored: " + targetPkg);
990
991 if (c.hasRestoreFlag(ShortcutInfo.FLAG_RESTORE_STARTED)) {
992 // Restore has started once.
993 } else if (installingPkgs.containsKey(targetPkg)) {
994 // App restore has started. Update the flag
995 c.restoreFlag |= ShortcutInfo.FLAG_RESTORE_STARTED;
996 c.updater().commit();
997 } else {
998 c.markDeleted("Unrestored app removed: " + targetPkg);
999 continue;
1000 }
Sunny Goyalad2e91a2017-02-28 15:14:58 -08001001 } else if (pmHelper.isAppOnSdcard(targetPkg, c.user)) {
Sunny Goyal81e44912017-01-14 15:05:14 -08001002 // Package is present but not available.
1003 disabledState |= ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1004 // Add the icon on the workspace anyway.
1005 allowMissingTarget = true;
1006 } else if (!isSdCardReady) {
1007 // SdCard is not ready yet. Package might get available,
1008 // once it is ready.
1009 Log.d(TAG, "Missing pkg, will check later: " + targetPkg);
1010 pendingPackages.addToList(c.user, targetPkg);
1011 // Add the icon on the workspace anyway.
1012 allowMissingTarget = true;
1013 } else {
1014 // Do not wait for external media load anymore.
1015 c.markDeleted("Invalid package removed: " + targetPkg);
1016 continue;
1017 }
1018 }
1019
1020 if (validTarget) {
1021 // The shortcut points to a valid target (either no target
1022 // or something which is ready to be used)
1023 c.markRestored();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001024 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001025
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001026 boolean useLowResIcon = !c.isOnWorkspaceOrHotseat() &&
Sunny Goyal34b65272015-03-11 16:56:52 -07001027 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1028
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001029 if (c.restoreFlag != 0) {
Sunny Goyal81e44912017-01-14 15:05:14 -08001030 // Already verified above that user is same as default user
1031 info = c.getRestoredItemInfo(intent);
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001032 } else if (c.itemType ==
Chris Wrenf4d08112014-01-16 18:13:56 -05001033 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001034 info = c.getAppShortcutInfo(
1035 intent, allowMissingTarget, useLowResIcon);
1036 } else if (c.itemType ==
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001037 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001038
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001039 ShortcutKey key = ShortcutKey.fromIntent(intent, c.user);
1040 if (unlockedUsers.get(c.serialNumber)) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001041 ShortcutInfoCompat pinnedShortcut =
1042 shortcutKeyToPinnedShortcuts.get(key);
1043 if (pinnedShortcut == null) {
1044 // The shortcut is no longer valid.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001045 c.markDeleted("Pinned shortcut not found");
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001046 continue;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001047 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001048 info = new ShortcutInfo(pinnedShortcut, context);
Sunny Goyal1b072632017-01-18 11:30:23 -08001049 info.iconBitmap = LauncherIcons
1050 .createShortcutIcon(pinnedShortcut, context);
Charles He3ff90472017-02-15 12:22:56 +00001051 if (pmHelper.isAppSuspended(
Sunny Goyalad2e91a2017-02-28 15:14:58 -08001052 pinnedShortcut.getPackage(), info.user)) {
Charles He3ff90472017-02-15 12:22:56 +00001053 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1054 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001055 intent = info.intent;
1056 } else {
1057 // Create a shortcut info in disabled mode for now.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001058 info = c.loadSimpleShortcut();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001059 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001060 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001061 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001062 info = c.loadSimpleShortcut();
Michael Jurka96879562012-03-22 05:54:33 -07001063
Sunny Goyald09c3702016-04-06 16:18:20 -07001064 // Shortcuts are only available on the primary profile
Sunny Goyalad2e91a2017-02-28 15:14:58 -08001065 if (pmHelper.isAppSuspended(targetPkg, c.user)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001066 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1067 }
1068
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001069 // App shortcuts that used to be automatically added to Launcher
1070 // didn't always have the correct intent flags set, so do that
1071 // here
1072 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001073 intent.getCategories() != null &&
1074 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001075 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001076 intent.addFlags(
1077 Intent.FLAG_ACTIVITY_NEW_TASK |
1078 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1079 }
Michael Jurka96879562012-03-22 05:54:33 -07001080 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001081
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001082 if (info != null) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001083 c.applyCommonProperties(info);
1084
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001085 info.intent = intent;
Sunny Goyal08f72612015-01-05 13:41:43 -08001086 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001087 info.spanX = 1;
1088 info.spanY = 1;
Sunny Goyald09c3702016-04-06 16:18:20 -07001089 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001090 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1091 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1092 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001093
Sunny Goyal81e44912017-01-14 15:05:14 -08001094 if (c.restoreFlag != 0 && !TextUtils.isEmpty(targetPkg)) {
1095 Integer progress = installingPkgs.get(targetPkg);
1096 if (progress != null) {
1097 info.setInstallProgress(progress);
1098 } else {
1099 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001100 }
1101 }
1102
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001103 c.checkAndAddItem(info, sBgDataModel);
Winson Chung1323b482013-08-05 12:41:55 -07001104 } else {
1105 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001106 }
1107 break;
1108
1109 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001110 FolderInfo folderInfo = sBgDataModel.findOrMakeFolder(c.id);
1111 c.applyCommonProperties(folderInfo);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001112
Sunny Goyala508e4f2015-05-21 09:33:57 -07001113 // Do not trim the folder label, as is was set by the user.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001114 folderInfo.title = c.getString(c.titleIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001115 folderInfo.spanX = 1;
1116 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001117 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001118
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001119 // no special handling required for restored folders
1120 c.markRestored();
Chris Wrenf4d08112014-01-16 18:13:56 -05001121
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001122 c.checkAndAddItem(folderInfo, sBgDataModel);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001123 break;
1124
1125 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08001126 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001127 // Read all Launcher-specific widget details
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001128 boolean customWidget = c.itemType ==
Adam Cohen59400422014-03-05 18:07:04 -08001129 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
1130
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001131 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001132 String savedProvider = c.getString(appWidgetProviderIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001133
Sunny Goyalff572272014-07-23 13:58:07 -07001134 final ComponentName component =
1135 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04001136
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001137 final boolean isIdValid = !c.hasRestoreFlag(
1138 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID);
1139 final boolean wasProviderReady = !c.hasRestoreFlag(
1140 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY);
Sunny Goyal651077b2014-06-30 14:15:31 -07001141
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001142 if (widgetProvidersMap == null) {
1143 widgetProvidersMap = AppWidgetManagerCompat
1144 .getInstance(mContext).getAllProvidersMap();
1145 }
1146 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
1147 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00001148 ComponentName.unflattenFromString(savedProvider),
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001149 c.user));
Sunny Goyalff572272014-07-23 13:58:07 -07001150
1151 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08001152 if (!isSafeMode && !customWidget &&
1153 wasProviderReady && !isProviderReady) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001154 c.markDeleted(
1155 "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07001156 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001157 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07001158 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07001159 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1160 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08001161
Sunny Goyal53f96722015-07-13 19:54:53 -07001162 // The provider is available. So the widget is either
1163 // available or not available. We do not need to track
1164 // any future restore updates.
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001165 int status = c.restoreFlag &
Sunny Goyal53f96722015-07-13 19:54:53 -07001166 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07001167 if (!wasProviderReady) {
1168 // If provider was not previously ready, update the
1169 // status and UI flag.
1170
1171 // Id would be valid only if the widget restore broadcast was received.
1172 if (isIdValid) {
Sunny Goyal86df1382016-08-10 15:03:22 -07001173 status |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07001174 } else {
1175 status &= ~LauncherAppWidgetInfo
1176 .FLAG_PROVIDER_NOT_READY;
1177 }
1178 }
1179 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07001180 } else {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001181 Log.v(TAG, "Widget restore pending id=" + c.id
Sunny Goyal651077b2014-06-30 14:15:31 -07001182 + " appWidgetId=" + appWidgetId
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001183 + " status =" + c.restoreFlag);
Sunny Goyal651077b2014-06-30 14:15:31 -07001184 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07001185 component);
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001186 appWidgetInfo.restoreStatus = c.restoreFlag;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001187 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07001188
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001189 if (c.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_RESTORE_STARTED)) {
Sunny Goyal94485362014-09-18 16:13:58 -07001190 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001191 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07001192 // App restore has started. Update the flag
1193 appWidgetInfo.restoreStatus |=
1194 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001195 } else if (!isSafeMode) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001196 c.markDeleted("Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07001197 continue;
1198 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07001199
1200 appWidgetInfo.installProgress =
1201 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07001202 }
Sunny Goyal86df1382016-08-10 15:03:22 -07001203 if (appWidgetInfo.hasRestoreFlag(
1204 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG)) {
Sunny Goyal81e44912017-01-14 15:05:14 -08001205 appWidgetInfo.bindOptions = c.parseIntent();
Sunny Goyal86df1382016-08-10 15:03:22 -07001206 }
Sunny Goyalff572272014-07-23 13:58:07 -07001207
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001208 c.applyCommonProperties(appWidgetInfo);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001209 appWidgetInfo.spanX = c.getInt(spanXIndex);
1210 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001211 appWidgetInfo.user = c.user;
Joe Onorato36115782010-06-17 13:28:48 -04001212
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001213 if (!c.isOnWorkspaceOrHotseat()) {
1214 c.markDeleted("Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001215 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001216 continue;
1217 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001218
Adam Cohen59400422014-03-05 18:07:04 -08001219 if (!customWidget) {
1220 String providerName =
1221 appWidgetInfo.providerName.flattenToString();
1222 if (!providerName.equals(savedProvider) ||
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001223 (appWidgetInfo.restoreStatus != c.restoreFlag)) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001224 c.updater()
1225 .put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1226 providerName)
1227 .put(LauncherSettings.Favorites.RESTORED,
1228 appWidgetInfo.restoreStatus)
1229 .commit();
Adam Cohen59400422014-03-05 18:07:04 -08001230 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001231 }
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001232 c.checkAndAddItem(appWidgetInfo, sBgDataModel);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001233 }
Joe Onorato36115782010-06-17 13:28:48 -04001234 break;
1235 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001236 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001237 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001238 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001239 }
1240 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001241 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001242 }
1243
Winson Chungba9c37f2013-08-30 14:11:37 -07001244 // Break early if we've stopped loading
1245 if (mStopped) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001246 sBgDataModel.clear();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001247 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07001248 }
1249
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001250 // Remove dead items
1251 if (c.commitDeleted()) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001252 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07001253 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
1254 .call(contentResolver,
1255 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
1256 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
1257 for (long folderId : deletedFolderIds) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001258 sBgDataModel.workspaceItems.remove(sBgDataModel.folders.get(folderId));
1259 sBgDataModel.folders.remove(folderId);
1260 sBgDataModel.itemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001261 }
Sunny Goyal55fddc82017-04-06 15:02:52 -07001262
1263 // Remove any ghost widgets
1264 LauncherSettings.Settings.call(contentResolver,
1265 LauncherSettings.Settings.METHOD_REMOVE_GHOST_WIDGETS);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001266 }
1267
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001268 // Unpin shortcuts that don't exist on the workspace.
Sunny Goyalf75baa92016-11-22 03:23:51 +05301269 HashSet<ShortcutKey> pendingShortcuts =
1270 InstallShortcutReceiver.getPendingShortcuts(context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001271 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001272 MutableInt numTimesPinned = sBgDataModel.pinnedShortcutCounts.get(key);
Sunny Goyalf75baa92016-11-22 03:23:51 +05301273 if ((numTimesPinned == null || numTimesPinned.value == 0)
1274 && !pendingShortcuts.contains(key)) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001275 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301276 shortcutManager.unpinShortcut(key);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001277 }
1278 }
1279
Sunny Goyal317698b2015-07-29 11:45:41 -07001280 // Sort all the folder items and make sure the first 3 items are high resolution.
Sunny Goyale9956a72016-09-01 17:24:47 -07001281 for (FolderInfo folder : sBgDataModel.folders) {
Sunny Goyal317698b2015-07-29 11:45:41 -07001282 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
1283 int pos = 0;
1284 for (ShortcutInfo info : folder.contents) {
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001285 if (info.usingLowResIcon &&
1286 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal3fe4a142016-12-15 17:40:07 -08001287 mIconCache.getTitleAndIcon(info, false);
Sunny Goyal317698b2015-07-29 11:45:41 -07001288 }
1289 pos ++;
1290 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
1291 break;
1292 }
1293 }
1294 }
1295
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001296 c.commitRestoredItems();
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001297 if (!isSdCardReady && !pendingPackages.isEmpty()) {
1298 context.registerReceiver(
1299 new SdCardAvailableReceiver(
1300 LauncherModel.this, mContext, pendingPackages),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001301 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001302 null,
1303 sWorker);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001304 }
1305
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001306 // Remove any empty screens
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001307 ArrayList<Long> unusedScreens = new ArrayList<>(sBgDataModel.workspaceScreens);
Sunny Goyale9956a72016-09-01 17:24:47 -07001308 for (ItemInfo item: sBgDataModel.itemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001309 long screenId = item.screenId;
1310 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1311 unusedScreens.contains(screenId)) {
1312 unusedScreens.remove(screenId);
1313 }
1314 }
1315
1316 // If there are any empty screens remove them, and update.
1317 if (unusedScreens.size() != 0) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001318 sBgDataModel.workspaceScreens.removeAll(unusedScreens);
1319 updateWorkspaceScreenOrder(context, sBgDataModel.workspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001320 }
Joe Onorato36115782010-06-17 13:28:48 -04001321 }
Sunny Goyale26d1002016-06-20 14:52:14 -07001322 if (LauncherAppState.PROFILE_STARTUP) {
1323 Trace.endSection();
1324 }
Adam Cohene25af792013-06-06 23:08:25 -07001325 }
1326
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001327 /** Filters the set of items who are directly or indirectly (via another container) on the
1328 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08001329 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001330 ArrayList<ItemInfo> allWorkspaceItems,
1331 ArrayList<ItemInfo> currentScreenItems,
1332 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001333 // Purge any null ItemInfos
1334 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
1335 while (iter.hasNext()) {
1336 ItemInfo i = iter.next();
1337 if (i == null) {
1338 iter.remove();
1339 }
1340 }
1341
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001342 // Order the set of items by their containers first, this allows use to walk through the
1343 // list sequentially, build up a list of containers that are in the specified screen,
1344 // as well as all items in those containers.
1345 Set<Long> itemsOnScreen = new HashSet<Long>();
1346 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
1347 @Override
1348 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07001349 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001350 }
1351 });
1352 for (ItemInfo info : allWorkspaceItems) {
1353 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001354 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001355 currentScreenItems.add(info);
1356 itemsOnScreen.add(info.id);
1357 } else {
1358 otherScreenItems.add(info);
1359 }
1360 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1361 currentScreenItems.add(info);
1362 itemsOnScreen.add(info.id);
1363 } else {
1364 if (itemsOnScreen.contains(info.container)) {
1365 currentScreenItems.add(info);
1366 itemsOnScreen.add(info.id);
1367 } else {
1368 otherScreenItems.add(info);
1369 }
1370 }
1371 }
1372 }
1373
1374 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08001375 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001376 ArrayList<LauncherAppWidgetInfo> appWidgets,
1377 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
1378 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001379
1380 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001381 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001382 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08001383 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001384 currentScreenWidgets.add(widget);
1385 } else {
1386 otherScreenWidgets.add(widget);
1387 }
1388 }
1389 }
1390
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001391 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
1392 * right) */
1393 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Sunny Goyal87f784c2017-01-11 10:48:34 -08001394 final InvariantDeviceProfile profile = mApp.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07001395 final int screenCols = profile.numColumns;
1396 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001397 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07001398 @Override
1399 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07001400 if (lhs.container == rhs.container) {
1401 // Within containers, order by their spatial position in that container
1402 switch ((int) lhs.container) {
1403 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
1404 long lr = (lhs.screenId * screenCellCount +
1405 lhs.cellY * screenCols + lhs.cellX);
1406 long rr = (rhs.screenId * screenCellCount +
1407 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07001408 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07001409 }
1410 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
1411 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07001412 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07001413 }
1414 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07001415 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07001416 throw new RuntimeException("Unexpected container type when " +
1417 "sorting workspace items.");
1418 }
1419 return 0;
1420 }
1421 } else {
1422 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07001423 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07001424 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001425 }
1426 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001427 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001428
Adam Cohendcd297f2013-06-18 13:13:40 -07001429 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
1430 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001431 final Runnable r = new Runnable() {
1432 @Override
1433 public void run() {
1434 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1435 if (callbacks != null) {
1436 callbacks.bindScreens(orderedScreens);
1437 }
1438 }
1439 };
Sunny Goyald33860f2015-04-23 16:02:20 -07001440 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07001441 }
1442
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001443 private void bindWorkspaceItems(final Callbacks oldCallbacks,
1444 final ArrayList<ItemInfo> workspaceItems,
1445 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07001446 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001447
1448 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07001449 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001450 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04001451 final int start = i;
1452 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001453 final Runnable r = new Runnable() {
1454 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001455 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001456 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001457 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07001458 callbacks.bindItems(workspaceItems, start, start+chunkSize,
1459 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001460 }
1461 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001462 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001463 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04001464 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001465
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001466 // Bind the widgets, one at a time
1467 N = appWidgets.size();
1468 for (int i = 0; i < N; i++) {
1469 final LauncherAppWidgetInfo widget = appWidgets.get(i);
1470 final Runnable r = new Runnable() {
1471 public void run() {
1472 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1473 if (callbacks != null) {
1474 callbacks.bindAppWidget(widget);
1475 }
1476 }
1477 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001478 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001479 }
1480 }
1481
1482 /**
1483 * Binds all loaded data to actual views on the main thread.
1484 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001485 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001486 final long t = SystemClock.uptimeMillis();
1487 Runnable r;
1488
1489 // Don't use these two variables in any of the callback runnables.
1490 // Otherwise we hold a reference to them.
1491 final Callbacks oldCallbacks = mCallbacks.get();
1492 if (oldCallbacks == null) {
1493 // This launcher has exited and nobody bothered to tell us. Just bail.
1494 Log.w(TAG, "LoaderTask running with no launcher");
1495 return;
1496 }
1497
Winson Chung9b9fb962013-11-15 15:39:34 -08001498 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07001499 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
1500 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
1501 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07001502
Sunny Goyale9956a72016-09-01 17:24:47 -07001503 synchronized (sBgDataModel) {
1504 workspaceItems.addAll(sBgDataModel.workspaceItems);
1505 appWidgets.addAll(sBgDataModel.appWidgets);
1506 orderedScreenIds.addAll(sBgDataModel.workspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001507 }
1508
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001509 final int currentScreen;
1510 {
1511 int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE
1512 ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen();
1513 if (currScreen >= orderedScreenIds.size()) {
1514 // There may be no workspace screens (just hotseat items and an empty page).
1515 currScreen = PagedView.INVALID_RESTORE_PAGE;
1516 }
1517 currentScreen = currScreen;
Winson Chung9b9fb962013-11-15 15:39:34 -08001518 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001519 final boolean validFirstPage = currentScreen >= 0;
1520 final long currentScreenId =
1521 validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID;
Winson Chung9b9fb962013-11-15 15:39:34 -08001522
Winson Chung9b9fb962013-11-15 15:39:34 -08001523 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07001524 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
1525 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
1526 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
1527 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001528
Winson Chung9b9fb962013-11-15 15:39:34 -08001529 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001530 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08001531 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001532 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001533 sortWorkspaceItemsSpatially(currentWorkspaceItems);
1534 sortWorkspaceItemsSpatially(otherWorkspaceItems);
1535
1536 // Tell the workspace that we're about to start binding items
1537 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001538 public void run() {
1539 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1540 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001541 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001542 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04001543 }
1544 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001545 };
Sunny Goyald33860f2015-04-23 16:02:20 -07001546 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001547
Adam Cohendcd297f2013-06-18 13:13:40 -07001548 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
1549
Sunny Goyal527c7d32015-08-28 15:19:36 -07001550 Executor mainExecutor = new DeferredMainThreadExecutor();
1551 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07001552 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001553
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001554 // In case of validFirstPage, only bind the first screen, and defer binding the
1555 // remaining screens after first onDraw (and an optional the fade animation whichever
1556 // happens later).
1557 // This ensures that the first screen is immediately visible (eg. during rotation)
1558 // In case of !validFirstPage, bind all pages one after other.
1559 final Executor deferredExecutor =
1560 validFirstPage ? new ViewOnDrawExecutor(mHandler) : mainExecutor;
1561
1562 mainExecutor.execute(new Runnable() {
1563 @Override
1564 public void run() {
1565 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1566 if (callbacks != null) {
1567 callbacks.finishFirstPageBind(
1568 validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null);
1569 }
1570 }
1571 });
Sunny Goyal527c7d32015-08-28 15:19:36 -07001572
Sunny Goyal44c06432016-04-02 10:56:02 -07001573 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001574
1575 // Tell the workspace that we're done binding items
1576 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001577 public void run() {
1578 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1579 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001580 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04001581 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001582
Sunny Goyal639e9062015-08-19 19:17:06 -07001583 mIsLoadingAndBindingWorkspace = false;
1584
1585 // Run all the bind complete runnables after workspace is bound.
1586 if (!mBindCompleteRunnables.isEmpty()) {
1587 synchronized (mBindCompleteRunnables) {
1588 for (final Runnable r : mBindCompleteRunnables) {
1589 runOnWorkerThread(r);
1590 }
1591 mBindCompleteRunnables.clear();
1592 }
1593 }
1594
Winson Chung98e030b2012-05-07 16:01:11 -07001595 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04001596 if (DEBUG_LOADERS) {
1597 Log.d(TAG, "bound workspace in "
1598 + (SystemClock.uptimeMillis()-t) + "ms");
1599 }
Winson Chung36a62fe2012-05-06 18:04:42 -07001600
Joe Onorato36115782010-06-17 13:28:48 -04001601 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001602 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001603 deferredExecutor.execute(r);
1604
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001605 if (validFirstPage) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001606 r = new Runnable() {
1607 public void run() {
1608 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1609 if (callbacks != null) {
1610 // We are loading synchronously, which means, some of the pages will be
1611 // bound after first draw. Inform the callbacks that page binding is
1612 // not complete, and schedule the remaining pages.
1613 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
1614 callbacks.onPageBoundSynchronously(currentScreen);
1615 }
1616 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
1617 }
1618 }
1619 };
Sunny Goyald33860f2015-04-23 16:02:20 -07001620 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07001621 }
Joe Onorato36115782010-06-17 13:28:48 -04001622 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001623
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001624 private void updateIconCache() {
1625 // Ignore packages which have a promise icon.
1626 HashSet<String> packagesToIgnore = new HashSet<>();
Sunny Goyale9956a72016-09-01 17:24:47 -07001627 synchronized (sBgDataModel) {
1628 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001629 if (info instanceof ShortcutInfo) {
1630 ShortcutInfo si = (ShortcutInfo) info;
1631 if (si.isPromise() && si.getTargetComponent() != null) {
1632 packagesToIgnore.add(si.getTargetComponent().getPackageName());
1633 }
1634 } else if (info instanceof LauncherAppWidgetInfo) {
1635 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
1636 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
1637 packagesToIgnore.add(lawi.providerName.getPackageName());
1638 }
1639 }
1640 }
1641 }
1642 mIconCache.updateDbIcons(packagesToIgnore);
1643 }
1644
Joe Onorato36115782010-06-17 13:28:48 -04001645 private void onlyBindAllApps() {
1646 final Callbacks oldCallbacks = mCallbacks.get();
1647 if (oldCallbacks == null) {
1648 // This launcher has exited and nobody bothered to tell us. Just bail.
1649 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1650 return;
1651 }
1652
1653 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07001654 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001655 final ArrayList<AppInfo> list
1656 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07001657 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001658 public void run() {
1659 final long t = SystemClock.uptimeMillis();
1660 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1661 if (callbacks != null) {
1662 callbacks.bindAllApplications(list);
1663 }
1664 if (DEBUG_LOADERS) {
1665 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08001666 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04001667 }
1668 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07001669 };
Tony Wickham80f57872016-06-29 18:12:15 -07001670 runOnMainThread(r);
Joe Onorato36115782010-06-17 13:28:48 -04001671 }
1672
Winson Chung64359a52013-07-08 17:17:08 -07001673 private void loadAllApps() {
1674 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04001675
Joe Onorato36115782010-06-17 13:28:48 -04001676 final Callbacks oldCallbacks = mCallbacks.get();
1677 if (oldCallbacks == null) {
1678 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07001679 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04001680 return;
1681 }
1682
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001683 final List<UserHandle> profiles = mUserManager.getUserProfiles();
Kenny Guyed131872014-04-30 03:02:21 +01001684
Winson Chung64359a52013-07-08 17:17:08 -07001685 // Clear the list of apps
1686 mBgAllAppsList.clear();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001687 for (UserHandle user : profiles) {
Kenny Guyed131872014-04-30 03:02:21 +01001688 // Query for the set of apps
1689 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal3e9be432017-01-05 15:22:41 -08001690 final List<LauncherActivityInfo> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01001691 if (DEBUG_LOADERS) {
1692 Log.d(TAG, "getActivityList took "
1693 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
1694 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
1695 }
1696 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08001697 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01001698 if (apps == null || apps.isEmpty()) {
1699 return;
1700 }
Kenny Guyff05f432016-01-22 17:48:29 +00001701 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01001702 // Create the ApplicationInfos
1703 for (int i = 0; i < apps.size(); i++) {
Sunny Goyal3e9be432017-01-05 15:22:41 -08001704 LauncherActivityInfo app = apps.get(i);
Kenny Guyed131872014-04-30 03:02:21 +01001705 // This builds the icon bitmaps.
Sunny Goyal24bb66a2017-03-21 15:12:01 -07001706 mBgAllAppsList.add(new AppInfo(app, user, quietMode), app);
Kenny Guyed131872014-04-30 03:02:21 +01001707 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08001708
Sunny Goyal756a28a2015-04-23 17:07:55 -07001709 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
1710 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07001711 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07001712
1713 @Override
1714 public void run() {
1715 heuristic.processUserApps(apps);
1716 }
Sunny Goyal639e9062015-08-19 19:17:06 -07001717 };
1718 runOnMainThread(new Runnable() {
1719
1720 @Override
1721 public void run() {
1722 // Check isLoadingWorkspace on the UI thread, as it is updated on
1723 // the UI thread.
1724 if (mIsLoadingAndBindingWorkspace) {
1725 synchronized (mBindCompleteRunnables) {
1726 mBindCompleteRunnables.add(r);
1727 }
1728 } else {
1729 runOnWorkerThread(r);
1730 }
1731 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07001732 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08001733 }
Winson Chung64359a52013-07-08 17:17:08 -07001734 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01001735 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001736 final ArrayList<AppInfo> added = mBgAllAppsList.added;
1737 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07001738
1739 // Post callback on main thread
1740 mHandler.post(new Runnable() {
1741 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07001742
Winson Chung64359a52013-07-08 17:17:08 -07001743 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07001744 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07001745 if (callbacks != null) {
1746 callbacks.bindAllApplications(added);
1747 if (DEBUG_LOADERS) {
1748 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001749 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07001750 }
1751 } else {
1752 Log.i(TAG, "not binding apps: no Launcher activity");
1753 }
1754 }
1755 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07001756 // Cleanup any data stored for a deleted user.
1757 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04001758 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07001759 Log.d(TAG, "Icons processed in "
1760 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08001761 }
1762 }
1763
Sunny Goyaldd96a5e2017-02-17 11:22:34 -08001764 private void loadDeepShortcuts() {
1765 if (!mModelLoaded) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07001766 sBgDataModel.deepShortcutMap.clear();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301767 DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(mContext);
1768 mHasShortcutHostPermission = shortcutManager.hasHostPermission();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001769 if (mHasShortcutHostPermission) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001770 for (UserHandle user : mUserManager.getUserProfiles()) {
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001771 if (mUserManager.isUserUnlocked(user)) {
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301772 List<ShortcutInfoCompat> shortcuts =
1773 shortcutManager.queryForAllShortcuts(user);
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07001774 sBgDataModel.updateDeepShortcutMap(null, user, shortcuts);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001775 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001776 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001777 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001778 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001779 }
Joe Onorato36115782010-06-17 13:28:48 -04001780 }
1781
Tony Wickham80f57872016-06-29 18:12:15 -07001782 public void bindDeepShortcuts() {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07001783 final MultiHashMap<ComponentKey, String> shortcutMapCopy =
1784 sBgDataModel.deepShortcutMap.clone();
Tony Wickham80f57872016-06-29 18:12:15 -07001785 Runnable r = new Runnable() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001786 @Override
1787 public void run() {
1788 Callbacks callbacks = getCallback();
1789 if (callbacks != null) {
1790 callbacks.bindDeepShortcutMap(shortcutMapCopy);
1791 }
1792 }
Tony Wickham80f57872016-06-29 18:12:15 -07001793 };
1794 runOnMainThread(r);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001795 }
1796
Sunny Goyal75b0f552015-05-20 21:57:06 -07001797 /**
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001798 * Refreshes the cached shortcuts if the shortcut permission has changed.
1799 * Current implementation simply reloads the workspace, but it can be optimized to
1800 * use partial updates similar to {@link UserManagerCompat}
1801 */
1802 public void refreshShortcutsIfRequired() {
Sunny Goyalf5e37442016-11-02 10:31:24 -07001803 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001804 sWorker.removeCallbacks(mShortcutPermissionCheckRunnable);
1805 sWorker.post(mShortcutPermissionCheckRunnable);
1806 }
1807 }
1808
1809 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -07001810 * Called when the icons for packages have been updated in the icon cache.
1811 */
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001812 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandle user) {
Sunny Goyal75b0f552015-05-20 21:57:06 -07001813 // If any package icon has changed (app was updated while launcher was dead),
1814 // update the corresponding shortcuts.
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001815 enqueueModelUpdateTask(new CacheDataUpdatedTask(
1816 CacheDataUpdatedTask.OP_CACHE_UPDATE, user, updatedPackages));
Sunny Goyal75b0f552015-05-20 21:57:06 -07001817 }
1818
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001819 void enqueueModelUpdateTask(BaseModelUpdateTask task) {
Jon Mirandaf28629a2017-02-23 15:12:42 -08001820 if (!mModelLoaded && mLoaderTask == null) {
1821 if (DEBUG_LOADERS) {
1822 Log.d(TAG, "enqueueModelUpdateTask Ignoring task since loader is pending=" + task);
1823 }
1824 return;
1825 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001826 task.init(this);
1827 runOnWorkerThread(task);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001828 }
1829
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001830 /**
1831 * A task to be executed on the current callbacks on the UI thread.
1832 * If there is no current callbacks, the task is ignored.
1833 */
1834 public interface CallbackTask {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001835
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001836 void execute(Callbacks callbacks);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001837 }
1838
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001839 /**
1840 * A runnable which changes/updates the data model of the launcher based on certain events.
1841 */
1842 public static abstract class BaseModelUpdateTask implements Runnable {
Joe Onorato36115782010-06-17 13:28:48 -04001843
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001844 private LauncherModel mModel;
1845 private DeferredHandler mUiHandler;
Joe Onorato36115782010-06-17 13:28:48 -04001846
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001847 /* package private */
1848 void init(LauncherModel model) {
1849 mModel = model;
1850 mUiHandler = mModel.mHandler;
Joe Onorato36115782010-06-17 13:28:48 -04001851 }
1852
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001853 @Override
Joe Onorato36115782010-06-17 13:28:48 -04001854 public void run() {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001855 if (!mModel.mHasLoaderCompletedOnce) {
Sunny Goyalc905efc2015-05-06 09:54:53 -07001856 // Loader has not yet run.
1857 return;
1858 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001859 execute(mModel.mApp, sBgDataModel, mModel.mBgAllAppsList);
1860 }
Joe Onorato36115782010-06-17 13:28:48 -04001861
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001862 /**
1863 * Execute the actual task. Called on the worker thread.
1864 */
1865 public abstract void execute(
1866 LauncherAppState app, BgDataModel dataModel, AllAppsList apps);
Sunny Goyale0f58d72014-11-10 18:05:31 -08001867
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001868 /**
1869 * Schedules a {@param task} to be executed on the current callbacks.
1870 */
1871 public final void scheduleCallbackTask(final CallbackTask task) {
1872 final Callbacks callbacks = mModel.getCallback();
1873 mUiHandler.post(new Runnable() {
1874 public void run() {
1875 Callbacks cb = mModel.getCallback();
1876 if (callbacks == cb && cb != null) {
1877 task.execute(callbacks);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07001878 }
1879 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001880 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001881 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001882
1883 public ModelWriter getModelWriter() {
1884 // Updates from model task, do not deal with icon position in hotseat.
1885 return mModel.getWriter(false /* hasVerticalHotseat */);
1886 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001887 }
1888
Sunny Goyal1b072632017-01-18 11:30:23 -08001889 public void updateAndBindShortcutInfo(final ShortcutInfo si, final ShortcutInfoCompat info) {
1890 updateAndBindShortcutInfo(new Provider<ShortcutInfo>() {
1891 @Override
1892 public ShortcutInfo get() {
1893 si.updateFromDeepShortcutInfo(info, mApp.getContext());
1894 si.iconBitmap = LauncherIcons.createShortcutIcon(info, mApp.getContext());
1895 return si;
1896 }
1897 });
1898 }
1899
Sunny Goyal10923b32016-07-20 15:42:44 -07001900 /**
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001901 * Utility method to update a shortcut on the background thread.
Sunny Goyal10923b32016-07-20 15:42:44 -07001902 */
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001903 public void updateAndBindShortcutInfo(final Provider<ShortcutInfo> shortcutProvider) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001904 enqueueModelUpdateTask(new ExtendedModelTask() {
Sunny Goyal10923b32016-07-20 15:42:44 -07001905 @Override
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001906 public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001907 ShortcutInfo info = shortcutProvider.get();
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001908 ArrayList<ShortcutInfo> update = new ArrayList<>();
Sunny Goyal10923b32016-07-20 15:42:44 -07001909 update.add(info);
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001910 bindUpdatedShortcuts(update, info.user);
Sunny Goyal10923b32016-07-20 15:42:44 -07001911 }
1912 });
1913 }
1914
Sunny Goyald164b7f2016-10-12 20:49:31 -07001915 private void bindWidgetsModel(final Callbacks callbacks) {
1916 final MultiHashMap<PackageItemInfo, WidgetItem> widgets
1917 = mBgWidgetsModel.getWidgetsMap().clone();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001918 mHandler.post(new Runnable() {
1919 @Override
1920 public void run() {
1921 Callbacks cb = getCallback();
1922 if (callbacks == cb && cb != null) {
Sunny Goyald164b7f2016-10-12 20:49:31 -07001923 callbacks.bindAllWidgets(widgets);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001924 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07001925 }
1926 });
1927 }
1928
Tony Wickham86222d22017-03-29 15:30:43 -07001929 public void refreshAndBindWidgetsAndShortcuts(final Callbacks callbacks,
1930 final boolean bindFirst, @Nullable final PackageUserKey packageUser) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001931 runOnWorkerThread(new Runnable() {
1932 @Override
1933 public void run() {
1934 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
Sunny Goyald164b7f2016-10-12 20:49:31 -07001935 bindWidgetsModel(callbacks);
Sunny Goyal31860582015-09-18 08:38:57 -07001936 }
Tony Wickham86222d22017-03-29 15:30:43 -07001937 ArrayList<WidgetItem> widgets = mBgWidgetsModel.update(
1938 mApp.getContext(), packageUser);
Sunny Goyald164b7f2016-10-12 20:49:31 -07001939 bindWidgetsModel(callbacks);
1940
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001941 // update the Widget entries inside DB on the worker thread.
Tony Wickham86222d22017-03-29 15:30:43 -07001942 mApp.getWidgetCache().removeObsoletePreviews(widgets, packageUser);
Sunny Goyal31860582015-09-18 08:38:57 -07001943 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001944 });
Michael Jurkac402cd92013-05-20 15:49:32 +02001945 }
1946
Sunny Goyal651077b2014-06-30 14:15:31 -07001947 static boolean isValidProvider(AppWidgetProviderInfo provider) {
1948 return (provider != null) && (provider.provider != null)
1949 && (provider.provider.getPackageName() != null);
1950 }
1951
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001952 public void dumpState(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
1953 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
1954 writer.println(prefix + "All apps list: size=" + mBgAllAppsList.data.size());
1955 for (AppInfo info : mBgAllAppsList.data) {
1956 writer.println(prefix + " title=\"" + info.title + "\" iconBitmap=" + info.iconBitmap
1957 + " componentName=" + info.componentName.getPackageName());
1958 }
Joe Onorato36115782010-06-17 13:28:48 -04001959 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001960 sBgDataModel.dump(prefix, fd, writer, args);
Joe Onoratobe386092009-11-17 17:32:16 -08001961 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08001962
1963 public Callbacks getCallback() {
1964 return mCallbacks != null ? mCallbacks.get() : null;
1965 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07001966
1967 /**
1968 * @return {@link FolderInfo} if its already loaded.
1969 */
1970 public FolderInfo findFolderById(Long folderId) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001971 synchronized (sBgDataModel) {
1972 return sBgDataModel.folders.get(folderId);
Sunny Goyal18bf8e22015-04-08 18:13:46 -07001973 }
1974 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07001975
Sunny Goyal527c7d32015-08-28 15:19:36 -07001976 @Thunk class DeferredMainThreadExecutor implements Executor {
1977
1978 @Override
1979 public void execute(Runnable command) {
1980 runOnMainThread(command);
1981 }
1982 }
1983
Sunny Goyal756adbc2015-04-16 15:20:51 -07001984 /**
1985 * @return the looper for the worker thread which can be used to start background tasks.
1986 */
1987 public static Looper getWorkerLooper() {
1988 return sWorkerThread.getLooper();
1989 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990}