blob: 5a41eeb1fad6e944c9ae43cfca57423c3f25bc65 [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;
Winson Chunga90303b2013-11-15 13:05:06 -080037import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070038import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080039import android.util.LongSparseArray;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070040import android.util.MutableInt;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010041
Sunny Goyalffe83f12014-08-14 17:39:34 -070042import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010043import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070044import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070045import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010046import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyal6c56c682015-07-16 14:09:05 -070047import com.android.launcher3.config.ProviderConfig;
Tony Wickham827cef22016-03-17 15:39:39 -070048import com.android.launcher3.dynamicui.ExtractionUtils;
Sunny Goyal26119432016-02-18 22:09:23 +000049import com.android.launcher3.folder.Folder;
50import com.android.launcher3.folder.FolderIcon;
Sunny Goyal1b072632017-01-18 11:30:23 -080051import com.android.launcher3.graphics.LauncherIcons;
Sunny Goyal1acb9e92016-05-16 12:41:09 -070052import com.android.launcher3.logging.FileLog;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070053import com.android.launcher3.model.AddWorkspaceItemsTask;
Sunny Goyale9956a72016-09-01 17:24:47 -070054import com.android.launcher3.model.BgDataModel;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070055import com.android.launcher3.model.CacheDataUpdatedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080056import com.android.launcher3.model.ExtendedModelTask;
Sunny Goyalf862a262015-12-14 14:27:38 -080057import com.android.launcher3.model.GridSizeMigrationTask;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -080058import com.android.launcher3.model.LoaderCursor;
Sunny Goyal43bf11d2017-02-02 13:52:53 -080059import com.android.launcher3.model.ModelWriter;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070060import com.android.launcher3.model.PackageInstallStateChangedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080061import com.android.launcher3.model.PackageItemInfo;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070062import com.android.launcher3.model.PackageUpdatedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080063import com.android.launcher3.model.SdCardAvailableReceiver;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070064import com.android.launcher3.model.ShortcutsChangedTask;
65import com.android.launcher3.model.UserLockStateChangedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080066import com.android.launcher3.model.WidgetItem;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070067import com.android.launcher3.model.WidgetsModel;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -070068import com.android.launcher3.provider.ImportDataTask;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070069import com.android.launcher3.provider.LauncherDbUtils;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070070import com.android.launcher3.shortcuts.DeepShortcutManager;
71import com.android.launcher3.shortcuts.ShortcutInfoCompat;
72import com.android.launcher3.shortcuts.ShortcutKey;
Robin Lee26ace122015-03-16 19:41:43 +000073import com.android.launcher3.util.ComponentKey;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070074import com.android.launcher3.util.ManagedProfileHeuristic;
Tony Wickhamd82a39d2016-07-01 15:44:13 -070075import com.android.launcher3.util.MultiHashMap;
Sunny Goyald09c3702016-04-06 16:18:20 -070076import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070077import com.android.launcher3.util.Preconditions;
Sunny Goyal2bcbe132016-11-16 09:23:42 -080078import com.android.launcher3.util.Provider;
Adam Cohen091440a2015-03-18 14:16:05 -070079import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070080import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080081
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -080082import java.io.FileDescriptor;
83import java.io.PrintWriter;
Michael Jurkac2f801e2011-07-12 14:19:46 -070084import java.lang.ref.WeakReference;
Michael Jurkac2f801e2011-07-12 14:19:46 -070085import java.util.ArrayList;
86import java.util.Collections;
87import java.util.Comparator;
88import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070089import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070090import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070091import java.util.List;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070092import java.util.Map;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070093import java.util.Set;
Sunny Goyaldd96a5e2017-02-17 11:22:34 -080094import java.util.concurrent.CancellationException;
Sunny Goyal527c7d32015-08-28 15:19:36 -070095import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -070096
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097/**
98 * Maintains in-memory state of the Launcher. It is expected that there should be only one
99 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700100 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101 */
Kenny Guyed131872014-04-30 03:02:21 +0100102public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +0100103 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800104 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -0400105 private static final boolean DEBUG_RECEIVER = false;
Chris Wrenb358f812014-04-16 13:37:00 -0400106
Joe Onorato9c1289c2009-08-17 11:03:03 -0400107 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700108
Joe Onorato36115782010-06-17 13:28:48 -0400109 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500110 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800111
Adam Cohen091440a2015-03-18 14:16:05 -0700112 @Thunk final LauncherAppState mApp;
113 @Thunk final Object mLock = new Object();
114 @Thunk DeferredHandler mHandler = new DeferredHandler();
115 @Thunk LoaderTask mLoaderTask;
116 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700117 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118
Adam Cohen091440a2015-03-18 14:16:05 -0700119 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700120 static {
121 sWorkerThread.start();
122 }
Adam Cohen091440a2015-03-18 14:16:05 -0700123 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700124
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800125 // Indicates whether the current model data is valid or not.
126 // We start off with everything not loaded. After that, we assume that
Joe Onoratocc67f472010-06-08 10:54:30 -0700127 // our monitoring of the package manager provides all updates and we never
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800128 // need to do a requery. This is only ever touched from the loader thread.
129 private boolean mModelLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700130
Sunny Goyal756a28a2015-04-23 17:07:55 -0700131 /**
132 * Set of runnables to be called on the background thread after the workspace binding
133 * is complete.
134 */
135 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
136
Adam Cohen091440a2015-03-18 14:16:05 -0700137 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700139 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800140 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700141 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800142 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800143
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700144 private boolean mHasShortcutHostPermission;
145 // Runnable to check if the shortcuts permission has changed.
146 private final Runnable mShortcutPermissionCheckRunnable = new Runnable() {
147 @Override
148 public void run() {
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800149 if (mModelLoaded) {
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530150 boolean hasShortcutHostPermission =
151 DeepShortcutManager.getInstance(mApp.getContext()).hasHostPermission();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700152 if (hasShortcutHostPermission != mHasShortcutHostPermission) {
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800153 forceReload();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700154 }
155 }
156 }
157 };
158
Sunny Goyale9956a72016-09-01 17:24:47 -0700159 /**
160 * All the static data should be accessed on the background thread, A lock should be acquired
161 * on this object when accessing any data from this model.
162 */
163 static final BgDataModel sBgDataModel = new BgDataModel();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700164
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700165 // </ only access in worker thread >
166
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700167 private final IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700169 private final LauncherAppsCompat mLauncherApps;
170 private final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100171
Joe Onorato9c1289c2009-08-17 11:03:03 -0400172 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700173 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400174 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700175 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400176 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700177 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
178 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700179 public void bindScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700180 public void finishFirstPageBind(ViewOnDrawExecutor executor);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800181 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400182 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200183 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700184 public void bindAppsAdded(ArrayList<Long> newScreens,
185 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700186 ArrayList<ItemInfo> addAnimated,
187 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200188 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700189 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800190 ArrayList<ShortcutInfo> removed, UserHandle user);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700191 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700192 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700193 public void bindWorkspaceComponentsRemoved(
194 HashSet<String> packageNames, HashSet<ComponentName> components,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800195 UserHandle user);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700196 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800197 public void notifyWidgetProvidersChanged();
Sunny Goyald164b7f2016-10-12 20:49:31 -0700198 public void bindAllWidgets(MultiHashMap<PackageItemInfo, WidgetItem> widgets);
Adam Cohen1462de32012-07-24 22:34:36 -0700199 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700200 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700201 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMap);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400202 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530204 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800205 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400206 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100207 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Sunny Goyald164b7f2016-10-12 20:49:31 -0700208 mBgWidgetsModel = new WidgetsModel(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800209 mIconCache = iconCache;
210
Kenny Guyed131872014-04-30 03:02:21 +0100211 mLauncherApps = LauncherAppsCompat.getInstance(context);
212 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800213 }
214
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700215 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
216 * posted on the main thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700217 private void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700218 if (sWorkerThread.getThreadId() == Process.myTid()) {
219 // If we are on the worker thread, post onto the main handler
220 mHandler.post(r);
221 } else {
222 r.run();
223 }
224 }
225
226 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
227 * posted on the worker thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700228 private static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700229 if (sWorkerThread.getThreadId() == Process.myTid()) {
230 r.run();
231 } else {
232 // If we are not on the worker thread, then post to the worker handler
233 sWorker.post(r);
234 }
235 }
236
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700237 public void setPackageState(PackageInstallInfo installInfo) {
238 enqueueModelUpdateTask(new PackageInstallStateChangedTask(installInfo));
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500239 }
240
Sunny Goyal756adbc2015-04-16 15:20:51 -0700241 /**
242 * Updates the icons and label of all pending icons for the provided package name.
243 */
244 public void updateSessionDisplayInfo(final String packageName) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700245 HashSet<String> packages = new HashSet<>();
246 packages.add(packageName);
247 enqueueModelUpdateTask(new CacheDataUpdatedTask(
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800248 CacheDataUpdatedTask.OP_SESSION_UPDATE, Process.myUserHandle(), packages));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800249 }
250
251 /**
252 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800253 */
Sunny Goyal2bcbe132016-11-16 09:23:42 -0800254 public void addAndBindAddedWorkspaceItems(List<ItemInfo> workspaceApps) {
255 addAndBindAddedWorkspaceItems(Provider.of(workspaceApps));
256 }
257
258 /**
259 * Adds the provided items to the workspace.
260 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700261 public void addAndBindAddedWorkspaceItems(
Sunny Goyal2bcbe132016-11-16 09:23:42 -0800262 Provider<List<ItemInfo>> appsProvider) {
263 enqueueModelUpdateTask(new AddWorkspaceItemsTask(appsProvider));
Winson Chung64359a52013-07-08 17:17:08 -0700264 }
265
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800266 public ModelWriter getWriter(boolean hasVerticalHotseat) {
267 return new ModelWriter(mApp.getContext(), sBgDataModel, hasVerticalHotseat);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268 }
269
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700270 static void checkItemInfoLocked(
271 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
Sunny Goyale9956a72016-09-01 17:24:47 -0700272 ItemInfo modelItem = sBgDataModel.itemsIdMap.get(itemId);
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700273 if (modelItem != null && item != modelItem) {
274 // check all the data is consistent
275 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
276 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
277 ShortcutInfo shortcut = (ShortcutInfo) item;
278 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
279 modelShortcut.intent.filterEquals(shortcut.intent) &&
280 modelShortcut.id == shortcut.id &&
281 modelShortcut.itemType == shortcut.itemType &&
282 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700283 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700284 modelShortcut.cellX == shortcut.cellX &&
285 modelShortcut.cellY == shortcut.cellY &&
286 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700287 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700288 // For all intents and purposes, this is the same object
289 return;
290 }
291 }
292
293 // the modelItem needs to match up perfectly with item if our model is
294 // to be consistent with the database-- for now, just require
295 // modelItem == item or the equality check above
296 String msg = "item: " + ((item != null) ? item.toString() : "null") +
297 "modelItem: " +
298 ((modelItem != null) ? modelItem.toString() : "null") +
299 "Error: ItemInfo passed to checkItemInfo doesn't match original";
300 RuntimeException e = new RuntimeException(msg);
301 if (stackTrace != null) {
302 e.setStackTrace(stackTrace);
303 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800304 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700305 }
306 }
307
Michael Jurka816474f2012-06-25 14:49:02 -0700308 static void checkItemInfo(final ItemInfo item) {
309 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
310 final long itemId = item.id;
311 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700312 public void run() {
Sunny Goyale9956a72016-09-01 17:24:47 -0700313 synchronized (sBgDataModel) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700314 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700315 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700316 }
317 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700318 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700319 }
320
Joe Onorato9c1289c2009-08-17 11:03:03 -0400321 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700322 * Update the order of the workspace screens in the database. The array list contains
323 * a list of screen ids in the order that they should appear.
324 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700325 public static void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -0700326 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700327 final ContentResolver cr = context.getContentResolver();
328 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
329
330 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700331 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700332 while (iter.hasNext()) {
333 long id = iter.next();
334 if (id < 0) {
335 iter.remove();
336 }
337 }
338
339 Runnable r = new Runnable() {
340 @Override
341 public void run() {
Yura085c8532014-02-11 15:15:29 +0000342 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700343 // Clear the table
Yura085c8532014-02-11 15:15:29 +0000344 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -0700345 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700346 for (int i = 0; i < count; i++) {
347 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700348 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700349 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
350 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +0000351 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -0700352 }
Yura085c8532014-02-11 15:15:29 +0000353
354 try {
355 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
356 } catch (Exception ex) {
357 throw new RuntimeException(ex);
358 }
Winson Chung9e6a0a22013-08-27 11:58:12 -0700359
Sunny Goyale9956a72016-09-01 17:24:47 -0700360 synchronized (sBgDataModel) {
361 sBgDataModel.workspaceScreens.clear();
362 sBgDataModel.workspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -0700363 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700364 }
365 };
366 runOnWorkerThread(r);
367 }
368
369 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400370 * Set this as the current Launcher activity object for the loader.
371 */
372 public void initialize(Callbacks callbacks) {
373 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -0700374 Preconditions.assertUIThread();
375 // Remove any queued UI runnables
376 mHandler.cancelAll();
377 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400378 }
379 }
380
Kenny Guyed131872014-04-30 03:02:21 +0100381 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800382 public void onPackageChanged(String packageName, UserHandle user) {
Kenny Guyed131872014-04-30 03:02:21 +0100383 int op = PackageUpdatedTask.OP_UPDATE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700384 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100385 }
386
387 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800388 public void onPackageRemoved(String packageName, UserHandle user) {
Sunny Goyalc2936bc2016-09-01 15:50:36 -0700389 onPackagesRemoved(user, packageName);
390 }
391
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800392 public void onPackagesRemoved(UserHandle user, String... packages) {
Kenny Guyed131872014-04-30 03:02:21 +0100393 int op = PackageUpdatedTask.OP_REMOVE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700394 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packages));
Kenny Guyed131872014-04-30 03:02:21 +0100395 }
396
397 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800398 public void onPackageAdded(String packageName, UserHandle user) {
Kenny Guyed131872014-04-30 03:02:21 +0100399 int op = PackageUpdatedTask.OP_ADD;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700400 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100401 }
402
403 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800404 public void onPackagesAvailable(String[] packageNames, UserHandle user,
Kenny Guyed131872014-04-30 03:02:21 +0100405 boolean replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700406 enqueueModelUpdateTask(
407 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100408 }
409
410 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800411 public void onPackagesUnavailable(String[] packageNames, UserHandle user,
Kenny Guyed131872014-04-30 03:02:21 +0100412 boolean replacing) {
413 if (!replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700414 enqueueModelUpdateTask(new PackageUpdatedTask(
415 PackageUpdatedTask.OP_UNAVAILABLE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100416 }
Kenny Guyed131872014-04-30 03:02:21 +0100417 }
418
Kenny Guy44cba692016-01-21 19:50:02 +0000419 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800420 public void onPackagesSuspended(String[] packageNames, UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700421 enqueueModelUpdateTask(new PackageUpdatedTask(
422 PackageUpdatedTask.OP_SUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000423 }
424
425 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800426 public void onPackagesUnsuspended(String[] packageNames, UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700427 enqueueModelUpdateTask(new PackageUpdatedTask(
428 PackageUpdatedTask.OP_UNSUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000429 }
430
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700431 @Override
432 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800433 UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700434 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, true));
Sunny Goyal50941fb2016-08-04 12:03:52 -0700435 }
436
437 public void updatePinnedShortcuts(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800438 UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700439 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, false));
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700440 }
441
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700442 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400443 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
444 * ACTION_PACKAGE_CHANGED.
445 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100446 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400447 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -0400448 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700449
Joe Onorato36115782010-06-17 13:28:48 -0400450 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +0100451 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -0700452 // If we have changed locale we need to clear out the labels in all apps/workspace.
453 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700454 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
455 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -0700456 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -0700457 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700458 } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
459 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
460 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800461 UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER);
Sunny Goyalda891c12016-03-18 18:29:24 -0700462 if (user != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700463 if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
464 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700465 enqueueModelUpdateTask(new PackageUpdatedTask(
466 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE, user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700467 }
468
469 // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so
470 // we need to run the state change task again.
471 if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
472 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700473 enqueueModelUpdateTask(new UserLockStateChangedTask(user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700474 }
Sunny Goyalda891c12016-03-18 18:29:24 -0700475 }
Tony Wickham827cef22016-03-17 15:39:39 -0700476 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
477 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700478 }
479 }
480
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800481 /**
482 * Reloads the workspace items from the DB and re-binds the workspace. This should generally
483 * not be called as DB updates are automatically followed by UI update
484 */
485 public void forceReload() {
486 synchronized (mLock) {
487 // Stop any existing loaders first, so they don't set mModelLoaded to true later
488 stopLoaderLocked();
489 mModelLoaded = false;
490 }
Winson Chungf0c6ae02012-03-21 16:10:31 -0700491
Reena Lee93f824a2011-09-23 17:20:28 -0700492 // Do this here because if the launcher activity is running it will be restarted.
493 // If it's not running startLoaderFromBackground will merely tell it that it needs
494 // to reload.
495 startLoaderFromBackground();
496 }
497
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700498 /**
499 * When the launcher is in the background, it's possible for it to miss paired
500 * configuration changes. So whenever we trigger the loader from the background
501 * tell the launcher that it needs to re-run the loader when it comes back instead
502 * of doing it now.
503 */
504 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800505 Callbacks callbacks = getCallback();
506 if (callbacks != null) {
507 // Only actually run the loader if they're not paused.
508 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -0700509 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700510 }
511 }
Joe Onorato36115782010-06-17 13:28:48 -0400512 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400513
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700514 /**
515 * If there is already a loader task running, tell it to stop.
516 */
517 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -0700518 LoaderTask oldTask = mLoaderTask;
519 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -0700520 oldTask.stopLocked();
521 }
Reena Lee93f824a2011-09-23 17:20:28 -0700522 }
523
Adam Cohen1a85c582014-09-30 09:48:49 -0700524 public boolean isCurrentCallbacks(Callbacks callbacks) {
525 return (mCallbacks != null && mCallbacks.get() == callbacks);
526 }
527
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700528 /**
529 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
530 * @return true if the page could be bound synchronously.
531 */
532 public boolean startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700533 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
534 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -0400535 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -0400536 // Don't bother to start the thread if we know it's not going to do anything
537 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -0700538 final Callbacks oldCallbacks = mCallbacks.get();
539 // Clear any pending bind-runnables from the synchronized load process.
540 runOnMainThread(new Runnable() {
541 public void run() {
542 oldCallbacks.clearPendingBinds();
543 }
544 });
545
Joe Onorato36115782010-06-17 13:28:48 -0400546 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700547 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700548 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800549 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
550 && mModelLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700551 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700552 return true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700553 } else {
554 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
555 sWorker.post(mLoaderTask);
556 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400557 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400558 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700559 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400560 }
561
Joe Onorato36115782010-06-17 13:28:48 -0400562 public void stopLoader() {
563 synchronized (mLock) {
564 if (mLoaderTask != null) {
565 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400566 }
567 }
Joe Onorato36115782010-06-17 13:28:48 -0400568 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400569
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800570 /**
571 * Loads the workspace screen ids in an ordered list.
572 */
Sunny Goyale5bb7052015-07-27 14:36:07 -0700573 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -0700574 final ContentResolver contentResolver = context.getContentResolver();
575 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -0700576
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800577 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -0700578 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
579 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -0700580 }
581
Joe Onorato36115782010-06-17 13:28:48 -0400582 /**
583 * Runnable for the thread that loads the contents of the launcher:
584 * - workspace icons
585 * - widgets
586 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700587 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -0400588 */
589 private class LoaderTask implements Runnable {
590 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -0700591 private int mPageToBindFirst;
592
Adam Cohen091440a2015-03-18 14:16:05 -0700593 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -0400594 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -0700595 @Thunk boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700596
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700597 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -0400598 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -0700599 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400600 }
601
Joe Onorato36115782010-06-17 13:28:48 -0400602 private void waitForIdle() {
603 // Wait until the either we're stopped or the other threads are done.
604 // This way we don't start loading all apps until the workspace has settled
605 // down.
606 synchronized (LoaderTask.this) {
607 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700608
Joe Onorato36115782010-06-17 13:28:48 -0400609 mHandler.postIdle(new Runnable() {
610 public void run() {
611 synchronized (LoaderTask.this) {
612 mLoadAndBindStepFinished = true;
613 if (DEBUG_LOADERS) {
614 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400615 }
Joe Onorato36115782010-06-17 13:28:48 -0400616 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400617 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400618 }
Joe Onorato36115782010-06-17 13:28:48 -0400619 });
620
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800621 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -0400622 try {
Michael Jurkac7700af2013-05-14 20:17:58 +0200623 // Just in case mFlushingWorkerThread changes but we aren't woken up,
624 // wait no longer than 1sec at a time
625 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -0400626 } catch (InterruptedException ex) {
627 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400628 }
629 }
Joe Onorato36115782010-06-17 13:28:48 -0400630 if (DEBUG_LOADERS) {
631 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700632 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400633 + "ms for previous step to finish binding");
634 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400635 }
Joe Onorato36115782010-06-17 13:28:48 -0400636 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400637
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700638 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -0500639 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700640 // Ensure that we have a valid page index to load synchronously
641 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
642 "valid page index");
643 }
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800644 if (!mModelLoaded) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700645 // Ensure that we don't try and bind a specified page when the pages have not been
646 // loaded already (we should load everything asynchronously in that case)
647 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
648 }
649 synchronized (mLock) {
650 if (mIsLoaderTaskRunning) {
651 // Ensure that we are never running the background loading at this point since
652 // we also touch the background collections
653 throw new RuntimeException("Error! Background loading is already running");
654 }
655 }
656
657 // XXX: Throw an exception if we are already loading (since we touch the worker thread
658 // data structures, we can't allow any other thread to touch that data, but because
659 // this call is synchronous, we can get away with not locking).
660
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400661 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -0700662 // operations from the previous activity. We need to ensure that all queued operations
663 // are executed before any synchronous binding work is done.
664 mHandler.flush();
665
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700666 // Divide the set of loaded items into those that we are binding synchronously, and
667 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800668 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700669 // XXX: For now, continue posting the binding of AllApps as there are other issues that
670 // arise from that.
671 onlyBindAllApps();
Tony Wickham80f57872016-06-29 18:12:15 -0700672
673 bindDeepShortcuts();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700674 }
675
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800676 private void verifyNotStopped() throws CancellationException {
677 synchronized (LoaderTask.this) {
678 if (mStopped) {
679 throw new CancellationException("Loader stopped");
680 }
681 }
682 }
683
Joe Onorato36115782010-06-17 13:28:48 -0400684 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700685 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -0700686 if (mStopped) {
687 return;
688 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700689 mIsLoaderTaskRunning = true;
690 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400691
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800692 try {
693 if (DEBUG_LOADERS) Log.d(TAG, "step 1.1: loading workspace");
694 // Set to false in bindWorkspace()
695 mIsLoadingAndBindingWorkspace = true;
696 loadWorkspace();
Joe Onorato36115782010-06-17 13:28:48 -0400697
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800698 verifyNotStopped();
699 if (DEBUG_LOADERS) Log.d(TAG, "step 1.2: bind workspace workspace");
700 bindWorkspace(mPageToBindFirst);
701
702 // Take a break
703 if (DEBUG_LOADERS) Log.d(TAG, "step 1 completed, wait for idle");
Joe Onorato36115782010-06-17 13:28:48 -0400704 waitForIdle();
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800705 verifyNotStopped();
Daniel Sandler843e8602010-06-07 14:59:01 -0400706
707 // second step
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800708 if (DEBUG_LOADERS) Log.d(TAG, "step 2.1: loading all apps");
709 loadAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700710
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800711 verifyNotStopped();
712 if (DEBUG_LOADERS) Log.d(TAG, "step 2.2: Update icon cache");
713 updateIconCache();
714
715 // Take a break
716 if (DEBUG_LOADERS) Log.d(TAG, "step 2 completed, wait for idle");
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700717 waitForIdle();
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800718 verifyNotStopped();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700719
720 // third step
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800721 if (DEBUG_LOADERS) Log.d(TAG, "step 3.1: loading deep shortcuts");
722 loadDeepShortcuts();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400723
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800724 verifyNotStopped();
725 if (DEBUG_LOADERS) Log.d(TAG, "step 3.2: bind deep shortcuts");
726 bindDeepShortcuts();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400727
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800728 synchronized (mLock) {
729 // Everything loaded bind the data.
730 mModelLoaded = true;
731 mHasLoaderCompletedOnce = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400732 }
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800733 } catch (CancellationException e) {
734 // Loader stopped, ignore
735 } finally {
736 // Clear out this reference, otherwise we end up holding it until all of the
737 // callback runnables are done.
738 mContext = null;
739
740 synchronized (mLock) {
741 // If we are still the last one to be scheduled, remove ourselves.
742 if (mLoaderTask == this) {
743 mLoaderTask = null;
744 }
745 mIsLoaderTaskRunning = false;
746 }
Joe Onorato36115782010-06-17 13:28:48 -0400747 }
Joe Onorato36115782010-06-17 13:28:48 -0400748 }
749
750 public void stopLocked() {
751 synchronized (LoaderTask.this) {
752 mStopped = true;
753 this.notify();
754 }
755 }
756
757 /**
758 * Gets the callbacks object. If we've been stopped, or if the launcher object
759 * has somehow been garbage collected, return null instead. Pass in the Callbacks
760 * object that was around when the deferred message was scheduled, and if there's
761 * a new Callbacks object around then also return null. This will save us from
762 * calling onto it with data that will be ignored.
763 */
764 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
765 synchronized (mLock) {
766 if (mStopped) {
767 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400768 }
Joe Onorato36115782010-06-17 13:28:48 -0400769
770 if (mCallbacks == null) {
771 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400772 }
Joe Onorato36115782010-06-17 13:28:48 -0400773
774 final Callbacks callbacks = mCallbacks.get();
775 if (callbacks != oldCallbacks) {
776 return null;
777 }
778 if (callbacks == null) {
779 Log.w(TAG, "no mCallbacks");
780 return null;
781 }
782
783 return callbacks;
784 }
785 }
786
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800787 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -0700788 if (LauncherAppState.PROFILE_STARTUP) {
789 Trace.beginSection("Loading Workspace");
790 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400791
Joe Onorato36115782010-06-17 13:28:48 -0400792 final Context context = mContext;
793 final ContentResolver contentResolver = context.getContentResolver();
Sunny Goyal342e4662017-02-02 15:21:08 -0800794 final PackageManagerHelper pmHelper = new PackageManagerHelper(context);
795 final boolean isSafeMode = pmHelper.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700796 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530797 final DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -0700798 final boolean isSdCardReady = Utilities.isBootCompleted();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800799 final MultiHashMap<UserHandle, String> pendingPackages = new MultiHashMap<>();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400800
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700801 boolean clearDb = false;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -0700802 try {
803 ImportDataTask.performImportIfPossible(context);
804 } catch (Exception e) {
805 // Migration failed. Clear workspace.
806 clearDb = true;
807 }
808
809 if (!clearDb && GridSizeMigrationTask.ENABLED &&
Sunny Goyalf076eae2016-01-11 12:25:10 -0800810 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
811 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700812 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -0700813 }
814
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700815 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -0700816 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -0700817 LauncherSettings.Settings.call(contentResolver,
Sunny Goyale05b08f2017-02-23 18:30:22 -0800818 LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -0500819 }
820
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700821 Log.d(TAG, "loadWorkspace: loading default favorites");
822 LauncherSettings.Settings.call(contentResolver,
823 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -0700824
Sunny Goyale9956a72016-09-01 17:24:47 -0700825 synchronized (sBgDataModel) {
826 sBgDataModel.clear();
827
Sunny Goyal756adbc2015-04-16 15:20:51 -0700828 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -0700829 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyale9956a72016-09-01 17:24:47 -0700830 sBgDataModel.workspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -0800831
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700832 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800833 final LoaderCursor c = new LoaderCursor(contentResolver.query(
834 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null), mApp);
Daniel Sandler8802e962010-05-26 16:28:16 -0400835
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800836 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400837
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700838 try {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700839 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
840 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -0400841 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
842 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700843 final int spanXIndex = c.getColumnIndexOrThrow
844 (LauncherSettings.Favorites.SPANX);
845 final int spanYIndex = c.getColumnIndexOrThrow(
846 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800847 final int rankIndex = c.getColumnIndexOrThrow(
848 LauncherSettings.Favorites.RANK);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700849 final int optionsIndex = c.getColumnIndexOrThrow(
850 LauncherSettings.Favorites.OPTIONS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400851
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800852 final LongSparseArray<UserHandle> allUsers = c.allUsers;
Kenny Guyff05f432016-01-22 17:48:29 +0000853 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700854 final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800855 for (UserHandle user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +0000856 long serialNo = mUserManager.getSerialNumberForUser(user);
857 allUsers.put(serialNo, user);
858 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700859
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700860 boolean userUnlocked = mUserManager.isUserUnlocked(user);
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700861
862 // We can only query for shortcuts when the user is unlocked.
863 if (userUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -0700864 List<ShortcutInfoCompat> pinnedShortcuts =
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530865 shortcutManager.queryForPinnedShortcuts(null, user);
866 if (shortcutManager.wasLastCallSuccess()) {
Sunny Goyal49f4f032016-08-01 15:45:49 -0700867 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
868 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
869 shortcut);
870 }
871 } else {
872 // Shortcut manager can fail due to some race condition when the
873 // lock state changes too frequently. For the purpose of the loading
874 // shortcuts, consider the user is still locked.
875 userUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700876 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700877 }
Sunny Goyal49f4f032016-08-01 15:45:49 -0700878 unlockedUsers.put(serialNo, userUnlocked);
Sunny Goyal7f834d22015-04-21 10:10:23 -0700879 }
880
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700881 ShortcutInfo info;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700882 LauncherAppWidgetInfo appWidgetInfo;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700883 Intent intent;
Sunny Goyal81e44912017-01-14 15:05:14 -0800884 String targetPkg;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400885
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700886 while (!mStopped && c.moveToNext()) {
887 try {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800888 if (c.user == null) {
889 // User has been deleted, remove the item.
890 c.markDeleted("User has been deleted");
891 continue;
892 }
893
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700894 boolean allowMissingTarget = false;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800895 switch (c.itemType) {
Sunny Goyal81e44912017-01-14 15:05:14 -0800896 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700897 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700898 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Sunny Goyal81e44912017-01-14 15:05:14 -0800899 intent = c.parseIntent();
900 if (intent == null) {
901 c.markDeleted("Invalid or null intent");
902 continue;
903 }
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800904
Sunny Goyal81e44912017-01-14 15:05:14 -0800905 int disabledState = quietMode.get(c.serialNumber) ?
906 ShortcutInfo.FLAG_DISABLED_QUIET_USER : 0;
907 ComponentName cn = intent.getComponent();
908 targetPkg = cn == null ? intent.getPackage() : cn.getPackageName();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700909
Sunny Goyal81e44912017-01-14 15:05:14 -0800910 if (!Process.myUserHandle().equals(c.user)) {
911 if (c.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
912 c.markDeleted("Legacy shortcuts are only allowed for default user");
913 continue;
914 } else if (c.restoreFlag != 0) {
915 // Don't restore items for other profiles.
916 c.markDeleted("Restore from managed profile not supported");
917 continue;
918 }
919 }
920 if (TextUtils.isEmpty(targetPkg) &&
921 c.itemType != LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
922 c.markDeleted("Only legacy shortcuts can have null package");
923 continue;
924 }
Sunny Goyal34942622014-08-29 17:20:55 -0700925
Sunny Goyal81e44912017-01-14 15:05:14 -0800926 // If there is no target package, its an implicit intent
927 // (legacy shortcut) which is always valid
928 boolean validTarget = TextUtils.isEmpty(targetPkg) ||
929 launcherApps.isPackageEnabledForProfile(targetPkg, c.user);
Sunny Goyal94485362014-09-18 16:13:58 -0700930
Sunny Goyal81e44912017-01-14 15:05:14 -0800931 if (cn != null && validTarget) {
932 // If the apk is present and the shortcut points to a specific
933 // component.
934
935 // If the component is already present
936 if (launcherApps.isActivityEnabledForProfile(cn, c.user)) {
937 // no special handling necessary for this item
938 c.markRestored();
939 } else {
940 if (c.hasRestoreFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
941 // We allow auto install apps to have their intent
942 // updated after an install.
Sunny Goyal342e4662017-02-02 15:21:08 -0800943 intent = pmHelper.getAppLaunchIntent(targetPkg, c.user);
Sunny Goyal81e44912017-01-14 15:05:14 -0800944 if (intent != null) {
945 c.restoreFlag = 0;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800946 c.updater().put(
Sunny Goyal81e44912017-01-14 15:05:14 -0800947 LauncherSettings.Favorites.INTENT,
948 intent.toUri(0)).commit();
949 cn = intent.getComponent();
Sunny Goyalb05a00a2016-08-29 10:06:57 -0700950 } else {
Sunny Goyal81e44912017-01-14 15:05:14 -0800951 c.markDeleted("Unable to find a launch target");
Sunny Goyal94485362014-09-18 16:13:58 -0700952 continue;
953 }
Sunny Goyal1a745e82014-10-02 15:58:31 -0700954 } else {
Sunny Goyal81e44912017-01-14 15:05:14 -0800955 // The app is installed but the component is no
956 // longer available.
957 c.markDeleted("Invalid component removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -0700958 continue;
Winson Chungee055712013-07-30 14:46:24 -0700959 }
Winson Chungee055712013-07-30 14:46:24 -0700960 }
Sunny Goyal81e44912017-01-14 15:05:14 -0800961 }
962 // else if cn == null => can't infer much, leave it
963 // else if !validPkg => could be restored icon or missing sd-card
964
965 if (!TextUtils.isEmpty(targetPkg) && !validTarget) {
966 // Points to a valid app (superset of cn != null) but the apk
967 // is not available.
968
969 if (c.restoreFlag != 0) {
970 // Package is not yet available but might be
971 // installed later.
972 FileLog.d(TAG, "package not yet restored: " + targetPkg);
973
974 if (c.hasRestoreFlag(ShortcutInfo.FLAG_RESTORE_STARTED)) {
975 // Restore has started once.
976 } else if (installingPkgs.containsKey(targetPkg)) {
977 // App restore has started. Update the flag
978 c.restoreFlag |= ShortcutInfo.FLAG_RESTORE_STARTED;
979 c.updater().commit();
980 } else {
981 c.markDeleted("Unrestored app removed: " + targetPkg);
982 continue;
983 }
Sunny Goyalad2e91a2017-02-28 15:14:58 -0800984 } else if (pmHelper.isAppOnSdcard(targetPkg, c.user)) {
Sunny Goyal81e44912017-01-14 15:05:14 -0800985 // Package is present but not available.
986 disabledState |= ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
987 // Add the icon on the workspace anyway.
988 allowMissingTarget = true;
989 } else if (!isSdCardReady) {
990 // SdCard is not ready yet. Package might get available,
991 // once it is ready.
992 Log.d(TAG, "Missing pkg, will check later: " + targetPkg);
993 pendingPackages.addToList(c.user, targetPkg);
994 // Add the icon on the workspace anyway.
995 allowMissingTarget = true;
996 } else {
997 // Do not wait for external media load anymore.
998 c.markDeleted("Invalid package removed: " + targetPkg);
999 continue;
1000 }
1001 }
1002
1003 if (validTarget) {
1004 // The shortcut points to a valid target (either no target
1005 // or something which is ready to be used)
1006 c.markRestored();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001007 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001008
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001009 boolean useLowResIcon = !c.isOnWorkspaceOrHotseat() &&
Sunny Goyal34b65272015-03-11 16:56:52 -07001010 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1011
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001012 if (c.restoreFlag != 0) {
Sunny Goyal81e44912017-01-14 15:05:14 -08001013 // Already verified above that user is same as default user
1014 info = c.getRestoredItemInfo(intent);
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001015 } else if (c.itemType ==
Chris Wrenf4d08112014-01-16 18:13:56 -05001016 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001017 info = c.getAppShortcutInfo(
1018 intent, allowMissingTarget, useLowResIcon);
1019 } else if (c.itemType ==
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001020 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001021
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001022 ShortcutKey key = ShortcutKey.fromIntent(intent, c.user);
1023 if (unlockedUsers.get(c.serialNumber)) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001024 ShortcutInfoCompat pinnedShortcut =
1025 shortcutKeyToPinnedShortcuts.get(key);
1026 if (pinnedShortcut == null) {
1027 // The shortcut is no longer valid.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001028 c.markDeleted("Pinned shortcut not found");
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001029 continue;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001030 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001031 info = new ShortcutInfo(pinnedShortcut, context);
Sunny Goyal1b072632017-01-18 11:30:23 -08001032 info.iconBitmap = LauncherIcons
1033 .createShortcutIcon(pinnedShortcut, context);
Charles He3ff90472017-02-15 12:22:56 +00001034 if (pmHelper.isAppSuspended(
Sunny Goyalad2e91a2017-02-28 15:14:58 -08001035 pinnedShortcut.getPackage(), info.user)) {
Charles He3ff90472017-02-15 12:22:56 +00001036 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1037 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001038 intent = info.intent;
1039 } else {
1040 // Create a shortcut info in disabled mode for now.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001041 info = c.loadSimpleShortcut();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001042 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001043 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001044 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001045 info = c.loadSimpleShortcut();
Michael Jurka96879562012-03-22 05:54:33 -07001046
Sunny Goyald09c3702016-04-06 16:18:20 -07001047 // Shortcuts are only available on the primary profile
Sunny Goyalad2e91a2017-02-28 15:14:58 -08001048 if (pmHelper.isAppSuspended(targetPkg, c.user)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001049 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1050 }
1051
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001052 // App shortcuts that used to be automatically added to Launcher
1053 // didn't always have the correct intent flags set, so do that
1054 // here
1055 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001056 intent.getCategories() != null &&
1057 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001058 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001059 intent.addFlags(
1060 Intent.FLAG_ACTIVITY_NEW_TASK |
1061 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1062 }
Michael Jurka96879562012-03-22 05:54:33 -07001063 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001064
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001065 if (info != null) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001066 c.applyCommonProperties(info);
1067
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001068 info.intent = intent;
Sunny Goyal08f72612015-01-05 13:41:43 -08001069 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001070 info.spanX = 1;
1071 info.spanY = 1;
Sunny Goyald09c3702016-04-06 16:18:20 -07001072 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001073 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1074 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1075 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001076
Sunny Goyal81e44912017-01-14 15:05:14 -08001077 if (c.restoreFlag != 0 && !TextUtils.isEmpty(targetPkg)) {
1078 Integer progress = installingPkgs.get(targetPkg);
1079 if (progress != null) {
1080 info.setInstallProgress(progress);
1081 } else {
1082 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001083 }
1084 }
1085
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001086 c.checkAndAddItem(info, sBgDataModel);
Winson Chung1323b482013-08-05 12:41:55 -07001087 } else {
1088 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001089 }
1090 break;
1091
1092 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001093 FolderInfo folderInfo = sBgDataModel.findOrMakeFolder(c.id);
1094 c.applyCommonProperties(folderInfo);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001095
Sunny Goyala508e4f2015-05-21 09:33:57 -07001096 // Do not trim the folder label, as is was set by the user.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001097 folderInfo.title = c.getString(c.titleIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001098 folderInfo.spanX = 1;
1099 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001100 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001101
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001102 // no special handling required for restored folders
1103 c.markRestored();
Chris Wrenf4d08112014-01-16 18:13:56 -05001104
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001105 c.checkAndAddItem(folderInfo, sBgDataModel);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001106 break;
1107
1108 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08001109 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001110 // Read all Launcher-specific widget details
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001111 boolean customWidget = c.itemType ==
Adam Cohen59400422014-03-05 18:07:04 -08001112 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
1113
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001114 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001115 String savedProvider = c.getString(appWidgetProviderIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001116
Sunny Goyalff572272014-07-23 13:58:07 -07001117 final ComponentName component =
1118 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04001119
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001120 final boolean isIdValid = !c.hasRestoreFlag(
1121 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID);
1122 final boolean wasProviderReady = !c.hasRestoreFlag(
1123 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY);
Sunny Goyal651077b2014-06-30 14:15:31 -07001124
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001125 if (widgetProvidersMap == null) {
1126 widgetProvidersMap = AppWidgetManagerCompat
1127 .getInstance(mContext).getAllProvidersMap();
1128 }
1129 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
1130 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00001131 ComponentName.unflattenFromString(savedProvider),
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001132 c.user));
Sunny Goyalff572272014-07-23 13:58:07 -07001133
1134 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08001135 if (!isSafeMode && !customWidget &&
1136 wasProviderReady && !isProviderReady) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001137 c.markDeleted(
1138 "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07001139 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001140 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07001141 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07001142 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1143 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08001144
Sunny Goyal53f96722015-07-13 19:54:53 -07001145 // The provider is available. So the widget is either
1146 // available or not available. We do not need to track
1147 // any future restore updates.
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001148 int status = c.restoreFlag &
Sunny Goyal53f96722015-07-13 19:54:53 -07001149 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07001150 if (!wasProviderReady) {
1151 // If provider was not previously ready, update the
1152 // status and UI flag.
1153
1154 // Id would be valid only if the widget restore broadcast was received.
1155 if (isIdValid) {
Sunny Goyal86df1382016-08-10 15:03:22 -07001156 status |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07001157 } else {
1158 status &= ~LauncherAppWidgetInfo
1159 .FLAG_PROVIDER_NOT_READY;
1160 }
1161 }
1162 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07001163 } else {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001164 Log.v(TAG, "Widget restore pending id=" + c.id
Sunny Goyal651077b2014-06-30 14:15:31 -07001165 + " appWidgetId=" + appWidgetId
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001166 + " status =" + c.restoreFlag);
Sunny Goyal651077b2014-06-30 14:15:31 -07001167 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07001168 component);
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001169 appWidgetInfo.restoreStatus = c.restoreFlag;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001170 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07001171
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001172 if (c.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_RESTORE_STARTED)) {
Sunny Goyal94485362014-09-18 16:13:58 -07001173 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001174 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07001175 // App restore has started. Update the flag
1176 appWidgetInfo.restoreStatus |=
1177 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001178 } else if (!isSafeMode) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001179 c.markDeleted("Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07001180 continue;
1181 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07001182
1183 appWidgetInfo.installProgress =
1184 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07001185 }
Sunny Goyal86df1382016-08-10 15:03:22 -07001186 if (appWidgetInfo.hasRestoreFlag(
1187 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG)) {
Sunny Goyal81e44912017-01-14 15:05:14 -08001188 appWidgetInfo.bindOptions = c.parseIntent();
Sunny Goyal86df1382016-08-10 15:03:22 -07001189 }
Sunny Goyalff572272014-07-23 13:58:07 -07001190
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001191 c.applyCommonProperties(appWidgetInfo);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001192 appWidgetInfo.spanX = c.getInt(spanXIndex);
1193 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001194 appWidgetInfo.user = c.user;
Joe Onorato36115782010-06-17 13:28:48 -04001195
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001196 if (!c.isOnWorkspaceOrHotseat()) {
1197 c.markDeleted("Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001198 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001199 continue;
1200 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001201
Adam Cohen59400422014-03-05 18:07:04 -08001202 if (!customWidget) {
1203 String providerName =
1204 appWidgetInfo.providerName.flattenToString();
1205 if (!providerName.equals(savedProvider) ||
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001206 (appWidgetInfo.restoreStatus != c.restoreFlag)) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001207 c.updater()
1208 .put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1209 providerName)
1210 .put(LauncherSettings.Favorites.RESTORED,
1211 appWidgetInfo.restoreStatus)
1212 .commit();
Adam Cohen59400422014-03-05 18:07:04 -08001213 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001214 }
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001215 c.checkAndAddItem(appWidgetInfo, sBgDataModel);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001216 }
Joe Onorato36115782010-06-17 13:28:48 -04001217 break;
1218 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001219 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001220 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001221 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001222 }
1223 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001224 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001225 }
1226
Winson Chungba9c37f2013-08-30 14:11:37 -07001227 // Break early if we've stopped loading
1228 if (mStopped) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001229 sBgDataModel.clear();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001230 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07001231 }
1232
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001233 // Remove dead items
1234 if (c.commitDeleted()) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001235 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07001236 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
1237 .call(contentResolver,
1238 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
1239 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
1240 for (long folderId : deletedFolderIds) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001241 sBgDataModel.workspaceItems.remove(sBgDataModel.folders.get(folderId));
1242 sBgDataModel.folders.remove(folderId);
1243 sBgDataModel.itemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001244 }
1245 }
1246
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001247 // Unpin shortcuts that don't exist on the workspace.
Sunny Goyalf75baa92016-11-22 03:23:51 +05301248 HashSet<ShortcutKey> pendingShortcuts =
1249 InstallShortcutReceiver.getPendingShortcuts(context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001250 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001251 MutableInt numTimesPinned = sBgDataModel.pinnedShortcutCounts.get(key);
Sunny Goyalf75baa92016-11-22 03:23:51 +05301252 if ((numTimesPinned == null || numTimesPinned.value == 0)
1253 && !pendingShortcuts.contains(key)) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001254 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301255 shortcutManager.unpinShortcut(key);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001256 }
1257 }
1258
Sunny Goyal317698b2015-07-29 11:45:41 -07001259 // Sort all the folder items and make sure the first 3 items are high resolution.
Sunny Goyale9956a72016-09-01 17:24:47 -07001260 for (FolderInfo folder : sBgDataModel.folders) {
Sunny Goyal317698b2015-07-29 11:45:41 -07001261 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
1262 int pos = 0;
1263 for (ShortcutInfo info : folder.contents) {
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001264 if (info.usingLowResIcon &&
1265 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal3fe4a142016-12-15 17:40:07 -08001266 mIconCache.getTitleAndIcon(info, false);
Sunny Goyal317698b2015-07-29 11:45:41 -07001267 }
1268 pos ++;
1269 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
1270 break;
1271 }
1272 }
1273 }
1274
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001275 c.commitRestoredItems();
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001276 if (!isSdCardReady && !pendingPackages.isEmpty()) {
1277 context.registerReceiver(
1278 new SdCardAvailableReceiver(
1279 LauncherModel.this, mContext, pendingPackages),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001280 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001281 null,
1282 sWorker);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001283 }
1284
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001285 // Remove any empty screens
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001286 ArrayList<Long> unusedScreens = new ArrayList<>(sBgDataModel.workspaceScreens);
Sunny Goyale9956a72016-09-01 17:24:47 -07001287 for (ItemInfo item: sBgDataModel.itemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001288 long screenId = item.screenId;
1289 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1290 unusedScreens.contains(screenId)) {
1291 unusedScreens.remove(screenId);
1292 }
1293 }
1294
1295 // If there are any empty screens remove them, and update.
1296 if (unusedScreens.size() != 0) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001297 sBgDataModel.workspaceScreens.removeAll(unusedScreens);
1298 updateWorkspaceScreenOrder(context, sBgDataModel.workspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001299 }
Joe Onorato36115782010-06-17 13:28:48 -04001300 }
Sunny Goyale26d1002016-06-20 14:52:14 -07001301 if (LauncherAppState.PROFILE_STARTUP) {
1302 Trace.endSection();
1303 }
Adam Cohene25af792013-06-06 23:08:25 -07001304 }
1305
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001306 /** Filters the set of items who are directly or indirectly (via another container) on the
1307 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08001308 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001309 ArrayList<ItemInfo> allWorkspaceItems,
1310 ArrayList<ItemInfo> currentScreenItems,
1311 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001312 // Purge any null ItemInfos
1313 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
1314 while (iter.hasNext()) {
1315 ItemInfo i = iter.next();
1316 if (i == null) {
1317 iter.remove();
1318 }
1319 }
1320
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001321 // Order the set of items by their containers first, this allows use to walk through the
1322 // list sequentially, build up a list of containers that are in the specified screen,
1323 // as well as all items in those containers.
1324 Set<Long> itemsOnScreen = new HashSet<Long>();
1325 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
1326 @Override
1327 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07001328 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001329 }
1330 });
1331 for (ItemInfo info : allWorkspaceItems) {
1332 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001333 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001334 currentScreenItems.add(info);
1335 itemsOnScreen.add(info.id);
1336 } else {
1337 otherScreenItems.add(info);
1338 }
1339 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1340 currentScreenItems.add(info);
1341 itemsOnScreen.add(info.id);
1342 } else {
1343 if (itemsOnScreen.contains(info.container)) {
1344 currentScreenItems.add(info);
1345 itemsOnScreen.add(info.id);
1346 } else {
1347 otherScreenItems.add(info);
1348 }
1349 }
1350 }
1351 }
1352
1353 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08001354 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001355 ArrayList<LauncherAppWidgetInfo> appWidgets,
1356 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
1357 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001358
1359 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001360 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001361 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08001362 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001363 currentScreenWidgets.add(widget);
1364 } else {
1365 otherScreenWidgets.add(widget);
1366 }
1367 }
1368 }
1369
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001370 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
1371 * right) */
1372 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Sunny Goyal87f784c2017-01-11 10:48:34 -08001373 final InvariantDeviceProfile profile = mApp.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07001374 final int screenCols = profile.numColumns;
1375 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001376 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07001377 @Override
1378 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07001379 if (lhs.container == rhs.container) {
1380 // Within containers, order by their spatial position in that container
1381 switch ((int) lhs.container) {
1382 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
1383 long lr = (lhs.screenId * screenCellCount +
1384 lhs.cellY * screenCols + lhs.cellX);
1385 long rr = (rhs.screenId * screenCellCount +
1386 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07001387 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07001388 }
1389 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
1390 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07001391 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07001392 }
1393 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07001394 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07001395 throw new RuntimeException("Unexpected container type when " +
1396 "sorting workspace items.");
1397 }
1398 return 0;
1399 }
1400 } else {
1401 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07001402 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07001403 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001404 }
1405 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001406 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001407
Adam Cohendcd297f2013-06-18 13:13:40 -07001408 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
1409 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001410 final Runnable r = new Runnable() {
1411 @Override
1412 public void run() {
1413 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1414 if (callbacks != null) {
1415 callbacks.bindScreens(orderedScreens);
1416 }
1417 }
1418 };
Sunny Goyald33860f2015-04-23 16:02:20 -07001419 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07001420 }
1421
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001422 private void bindWorkspaceItems(final Callbacks oldCallbacks,
1423 final ArrayList<ItemInfo> workspaceItems,
1424 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07001425 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001426
1427 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07001428 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001429 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04001430 final int start = i;
1431 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001432 final Runnable r = new Runnable() {
1433 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001434 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001435 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001436 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07001437 callbacks.bindItems(workspaceItems, start, start+chunkSize,
1438 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001439 }
1440 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001441 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001442 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04001443 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001444
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001445 // Bind the widgets, one at a time
1446 N = appWidgets.size();
1447 for (int i = 0; i < N; i++) {
1448 final LauncherAppWidgetInfo widget = appWidgets.get(i);
1449 final Runnable r = new Runnable() {
1450 public void run() {
1451 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1452 if (callbacks != null) {
1453 callbacks.bindAppWidget(widget);
1454 }
1455 }
1456 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001457 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001458 }
1459 }
1460
1461 /**
1462 * Binds all loaded data to actual views on the main thread.
1463 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001464 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001465 final long t = SystemClock.uptimeMillis();
1466 Runnable r;
1467
1468 // Don't use these two variables in any of the callback runnables.
1469 // Otherwise we hold a reference to them.
1470 final Callbacks oldCallbacks = mCallbacks.get();
1471 if (oldCallbacks == null) {
1472 // This launcher has exited and nobody bothered to tell us. Just bail.
1473 Log.w(TAG, "LoaderTask running with no launcher");
1474 return;
1475 }
1476
Winson Chung9b9fb962013-11-15 15:39:34 -08001477 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07001478 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
1479 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
1480 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07001481
Sunny Goyale9956a72016-09-01 17:24:47 -07001482 synchronized (sBgDataModel) {
1483 workspaceItems.addAll(sBgDataModel.workspaceItems);
1484 appWidgets.addAll(sBgDataModel.appWidgets);
1485 orderedScreenIds.addAll(sBgDataModel.workspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001486 }
1487
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001488 final int currentScreen;
1489 {
1490 int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE
1491 ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen();
1492 if (currScreen >= orderedScreenIds.size()) {
1493 // There may be no workspace screens (just hotseat items and an empty page).
1494 currScreen = PagedView.INVALID_RESTORE_PAGE;
1495 }
1496 currentScreen = currScreen;
Winson Chung9b9fb962013-11-15 15:39:34 -08001497 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001498 final boolean validFirstPage = currentScreen >= 0;
1499 final long currentScreenId =
1500 validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID;
Winson Chung9b9fb962013-11-15 15:39:34 -08001501
Winson Chung9b9fb962013-11-15 15:39:34 -08001502 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07001503 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
1504 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
1505 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
1506 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001507
Winson Chung9b9fb962013-11-15 15:39:34 -08001508 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001509 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08001510 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001511 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001512 sortWorkspaceItemsSpatially(currentWorkspaceItems);
1513 sortWorkspaceItemsSpatially(otherWorkspaceItems);
1514
1515 // Tell the workspace that we're about to start binding items
1516 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001517 public void run() {
1518 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1519 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001520 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001521 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04001522 }
1523 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001524 };
Sunny Goyald33860f2015-04-23 16:02:20 -07001525 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001526
Adam Cohendcd297f2013-06-18 13:13:40 -07001527 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
1528
Sunny Goyal527c7d32015-08-28 15:19:36 -07001529 Executor mainExecutor = new DeferredMainThreadExecutor();
1530 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07001531 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001532
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001533 // In case of validFirstPage, only bind the first screen, and defer binding the
1534 // remaining screens after first onDraw (and an optional the fade animation whichever
1535 // happens later).
1536 // This ensures that the first screen is immediately visible (eg. during rotation)
1537 // In case of !validFirstPage, bind all pages one after other.
1538 final Executor deferredExecutor =
1539 validFirstPage ? new ViewOnDrawExecutor(mHandler) : mainExecutor;
1540
1541 mainExecutor.execute(new Runnable() {
1542 @Override
1543 public void run() {
1544 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1545 if (callbacks != null) {
1546 callbacks.finishFirstPageBind(
1547 validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null);
1548 }
1549 }
1550 });
Sunny Goyal527c7d32015-08-28 15:19:36 -07001551
Sunny Goyal44c06432016-04-02 10:56:02 -07001552 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001553
1554 // Tell the workspace that we're done binding items
1555 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001556 public void run() {
1557 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1558 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001559 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04001560 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001561
Sunny Goyal639e9062015-08-19 19:17:06 -07001562 mIsLoadingAndBindingWorkspace = false;
1563
1564 // Run all the bind complete runnables after workspace is bound.
1565 if (!mBindCompleteRunnables.isEmpty()) {
1566 synchronized (mBindCompleteRunnables) {
1567 for (final Runnable r : mBindCompleteRunnables) {
1568 runOnWorkerThread(r);
1569 }
1570 mBindCompleteRunnables.clear();
1571 }
1572 }
1573
Winson Chung98e030b2012-05-07 16:01:11 -07001574 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04001575 if (DEBUG_LOADERS) {
1576 Log.d(TAG, "bound workspace in "
1577 + (SystemClock.uptimeMillis()-t) + "ms");
1578 }
Winson Chung36a62fe2012-05-06 18:04:42 -07001579
Joe Onorato36115782010-06-17 13:28:48 -04001580 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001581 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001582 deferredExecutor.execute(r);
1583
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001584 if (validFirstPage) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001585 r = new Runnable() {
1586 public void run() {
1587 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1588 if (callbacks != null) {
1589 // We are loading synchronously, which means, some of the pages will be
1590 // bound after first draw. Inform the callbacks that page binding is
1591 // not complete, and schedule the remaining pages.
1592 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
1593 callbacks.onPageBoundSynchronously(currentScreen);
1594 }
1595 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
1596 }
1597 }
1598 };
Sunny Goyald33860f2015-04-23 16:02:20 -07001599 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07001600 }
Joe Onorato36115782010-06-17 13:28:48 -04001601 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001602
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001603 private void updateIconCache() {
1604 // Ignore packages which have a promise icon.
1605 HashSet<String> packagesToIgnore = new HashSet<>();
Sunny Goyale9956a72016-09-01 17:24:47 -07001606 synchronized (sBgDataModel) {
1607 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001608 if (info instanceof ShortcutInfo) {
1609 ShortcutInfo si = (ShortcutInfo) info;
1610 if (si.isPromise() && si.getTargetComponent() != null) {
1611 packagesToIgnore.add(si.getTargetComponent().getPackageName());
1612 }
1613 } else if (info instanceof LauncherAppWidgetInfo) {
1614 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
1615 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
1616 packagesToIgnore.add(lawi.providerName.getPackageName());
1617 }
1618 }
1619 }
1620 }
1621 mIconCache.updateDbIcons(packagesToIgnore);
1622 }
1623
Joe Onorato36115782010-06-17 13:28:48 -04001624 private void onlyBindAllApps() {
1625 final Callbacks oldCallbacks = mCallbacks.get();
1626 if (oldCallbacks == null) {
1627 // This launcher has exited and nobody bothered to tell us. Just bail.
1628 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1629 return;
1630 }
1631
1632 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07001633 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001634 final ArrayList<AppInfo> list
1635 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07001636 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001637 public void run() {
1638 final long t = SystemClock.uptimeMillis();
1639 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1640 if (callbacks != null) {
1641 callbacks.bindAllApplications(list);
1642 }
1643 if (DEBUG_LOADERS) {
1644 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08001645 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04001646 }
1647 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07001648 };
Tony Wickham80f57872016-06-29 18:12:15 -07001649 runOnMainThread(r);
Joe Onorato36115782010-06-17 13:28:48 -04001650 }
1651
Winson Chung64359a52013-07-08 17:17:08 -07001652 private void loadAllApps() {
1653 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04001654
Joe Onorato36115782010-06-17 13:28:48 -04001655 final Callbacks oldCallbacks = mCallbacks.get();
1656 if (oldCallbacks == null) {
1657 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07001658 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04001659 return;
1660 }
1661
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001662 final List<UserHandle> profiles = mUserManager.getUserProfiles();
Kenny Guyed131872014-04-30 03:02:21 +01001663
Winson Chung64359a52013-07-08 17:17:08 -07001664 // Clear the list of apps
1665 mBgAllAppsList.clear();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001666 for (UserHandle user : profiles) {
Kenny Guyed131872014-04-30 03:02:21 +01001667 // Query for the set of apps
1668 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal3e9be432017-01-05 15:22:41 -08001669 final List<LauncherActivityInfo> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01001670 if (DEBUG_LOADERS) {
1671 Log.d(TAG, "getActivityList took "
1672 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
1673 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
1674 }
1675 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08001676 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01001677 if (apps == null || apps.isEmpty()) {
1678 return;
1679 }
Kenny Guyff05f432016-01-22 17:48:29 +00001680 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01001681 // Create the ApplicationInfos
1682 for (int i = 0; i < apps.size(); i++) {
Sunny Goyal3e9be432017-01-05 15:22:41 -08001683 LauncherActivityInfo app = apps.get(i);
Kenny Guyed131872014-04-30 03:02:21 +01001684 // This builds the icon bitmaps.
Sunny Goyal24bb66a2017-03-21 15:12:01 -07001685 mBgAllAppsList.add(new AppInfo(app, user, quietMode), app);
Kenny Guyed131872014-04-30 03:02:21 +01001686 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08001687
Sunny Goyal756a28a2015-04-23 17:07:55 -07001688 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
1689 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07001690 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07001691
1692 @Override
1693 public void run() {
1694 heuristic.processUserApps(apps);
1695 }
Sunny Goyal639e9062015-08-19 19:17:06 -07001696 };
1697 runOnMainThread(new Runnable() {
1698
1699 @Override
1700 public void run() {
1701 // Check isLoadingWorkspace on the UI thread, as it is updated on
1702 // the UI thread.
1703 if (mIsLoadingAndBindingWorkspace) {
1704 synchronized (mBindCompleteRunnables) {
1705 mBindCompleteRunnables.add(r);
1706 }
1707 } else {
1708 runOnWorkerThread(r);
1709 }
1710 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07001711 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08001712 }
Winson Chung64359a52013-07-08 17:17:08 -07001713 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01001714 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001715 final ArrayList<AppInfo> added = mBgAllAppsList.added;
1716 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07001717
1718 // Post callback on main thread
1719 mHandler.post(new Runnable() {
1720 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07001721
Winson Chung64359a52013-07-08 17:17:08 -07001722 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07001723 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07001724 if (callbacks != null) {
1725 callbacks.bindAllApplications(added);
1726 if (DEBUG_LOADERS) {
1727 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001728 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07001729 }
1730 } else {
1731 Log.i(TAG, "not binding apps: no Launcher activity");
1732 }
1733 }
1734 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07001735 // Cleanup any data stored for a deleted user.
1736 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04001737 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07001738 Log.d(TAG, "Icons processed in "
1739 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08001740 }
1741 }
1742
Sunny Goyaldd96a5e2017-02-17 11:22:34 -08001743 private void loadDeepShortcuts() {
1744 if (!mModelLoaded) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07001745 sBgDataModel.deepShortcutMap.clear();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301746 DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(mContext);
1747 mHasShortcutHostPermission = shortcutManager.hasHostPermission();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001748 if (mHasShortcutHostPermission) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001749 for (UserHandle user : mUserManager.getUserProfiles()) {
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001750 if (mUserManager.isUserUnlocked(user)) {
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301751 List<ShortcutInfoCompat> shortcuts =
1752 shortcutManager.queryForAllShortcuts(user);
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07001753 sBgDataModel.updateDeepShortcutMap(null, user, shortcuts);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001754 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001755 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001756 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001757 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001758 }
Joe Onorato36115782010-06-17 13:28:48 -04001759 }
1760
Tony Wickham80f57872016-06-29 18:12:15 -07001761 public void bindDeepShortcuts() {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07001762 final MultiHashMap<ComponentKey, String> shortcutMapCopy =
1763 sBgDataModel.deepShortcutMap.clone();
Tony Wickham80f57872016-06-29 18:12:15 -07001764 Runnable r = new Runnable() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001765 @Override
1766 public void run() {
1767 Callbacks callbacks = getCallback();
1768 if (callbacks != null) {
1769 callbacks.bindDeepShortcutMap(shortcutMapCopy);
1770 }
1771 }
Tony Wickham80f57872016-06-29 18:12:15 -07001772 };
1773 runOnMainThread(r);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001774 }
1775
Sunny Goyal75b0f552015-05-20 21:57:06 -07001776 /**
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001777 * Refreshes the cached shortcuts if the shortcut permission has changed.
1778 * Current implementation simply reloads the workspace, but it can be optimized to
1779 * use partial updates similar to {@link UserManagerCompat}
1780 */
1781 public void refreshShortcutsIfRequired() {
Sunny Goyalf5e37442016-11-02 10:31:24 -07001782 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001783 sWorker.removeCallbacks(mShortcutPermissionCheckRunnable);
1784 sWorker.post(mShortcutPermissionCheckRunnable);
1785 }
1786 }
1787
1788 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -07001789 * Called when the icons for packages have been updated in the icon cache.
1790 */
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001791 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandle user) {
Sunny Goyal75b0f552015-05-20 21:57:06 -07001792 // If any package icon has changed (app was updated while launcher was dead),
1793 // update the corresponding shortcuts.
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001794 enqueueModelUpdateTask(new CacheDataUpdatedTask(
1795 CacheDataUpdatedTask.OP_CACHE_UPDATE, user, updatedPackages));
Sunny Goyal75b0f552015-05-20 21:57:06 -07001796 }
1797
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001798 void enqueueModelUpdateTask(BaseModelUpdateTask task) {
Jon Mirandaf28629a2017-02-23 15:12:42 -08001799 if (!mModelLoaded && mLoaderTask == null) {
1800 if (DEBUG_LOADERS) {
1801 Log.d(TAG, "enqueueModelUpdateTask Ignoring task since loader is pending=" + task);
1802 }
1803 return;
1804 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001805 task.init(this);
1806 runOnWorkerThread(task);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001807 }
1808
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001809 /**
1810 * A task to be executed on the current callbacks on the UI thread.
1811 * If there is no current callbacks, the task is ignored.
1812 */
1813 public interface CallbackTask {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001814
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001815 void execute(Callbacks callbacks);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001816 }
1817
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001818 /**
1819 * A runnable which changes/updates the data model of the launcher based on certain events.
1820 */
1821 public static abstract class BaseModelUpdateTask implements Runnable {
Joe Onorato36115782010-06-17 13:28:48 -04001822
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001823 private LauncherModel mModel;
1824 private DeferredHandler mUiHandler;
Joe Onorato36115782010-06-17 13:28:48 -04001825
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001826 /* package private */
1827 void init(LauncherModel model) {
1828 mModel = model;
1829 mUiHandler = mModel.mHandler;
Joe Onorato36115782010-06-17 13:28:48 -04001830 }
1831
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001832 @Override
Joe Onorato36115782010-06-17 13:28:48 -04001833 public void run() {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001834 if (!mModel.mHasLoaderCompletedOnce) {
Sunny Goyalc905efc2015-05-06 09:54:53 -07001835 // Loader has not yet run.
1836 return;
1837 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001838 execute(mModel.mApp, sBgDataModel, mModel.mBgAllAppsList);
1839 }
Joe Onorato36115782010-06-17 13:28:48 -04001840
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001841 /**
1842 * Execute the actual task. Called on the worker thread.
1843 */
1844 public abstract void execute(
1845 LauncherAppState app, BgDataModel dataModel, AllAppsList apps);
Sunny Goyale0f58d72014-11-10 18:05:31 -08001846
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001847 /**
1848 * Schedules a {@param task} to be executed on the current callbacks.
1849 */
1850 public final void scheduleCallbackTask(final CallbackTask task) {
1851 final Callbacks callbacks = mModel.getCallback();
1852 mUiHandler.post(new Runnable() {
1853 public void run() {
1854 Callbacks cb = mModel.getCallback();
1855 if (callbacks == cb && cb != null) {
1856 task.execute(callbacks);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07001857 }
1858 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001859 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001860 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001861
1862 public ModelWriter getModelWriter() {
1863 // Updates from model task, do not deal with icon position in hotseat.
1864 return mModel.getWriter(false /* hasVerticalHotseat */);
1865 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001866 }
1867
Sunny Goyal1b072632017-01-18 11:30:23 -08001868 public void updateAndBindShortcutInfo(final ShortcutInfo si, final ShortcutInfoCompat info) {
1869 updateAndBindShortcutInfo(new Provider<ShortcutInfo>() {
1870 @Override
1871 public ShortcutInfo get() {
1872 si.updateFromDeepShortcutInfo(info, mApp.getContext());
1873 si.iconBitmap = LauncherIcons.createShortcutIcon(info, mApp.getContext());
1874 return si;
1875 }
1876 });
1877 }
1878
Sunny Goyal10923b32016-07-20 15:42:44 -07001879 /**
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001880 * Utility method to update a shortcut on the background thread.
Sunny Goyal10923b32016-07-20 15:42:44 -07001881 */
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001882 public void updateAndBindShortcutInfo(final Provider<ShortcutInfo> shortcutProvider) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001883 enqueueModelUpdateTask(new ExtendedModelTask() {
Sunny Goyal10923b32016-07-20 15:42:44 -07001884 @Override
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001885 public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001886 ShortcutInfo info = shortcutProvider.get();
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001887 ArrayList<ShortcutInfo> update = new ArrayList<>();
Sunny Goyal10923b32016-07-20 15:42:44 -07001888 update.add(info);
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001889 bindUpdatedShortcuts(update, info.user);
Sunny Goyal10923b32016-07-20 15:42:44 -07001890 }
1891 });
1892 }
1893
Sunny Goyald164b7f2016-10-12 20:49:31 -07001894 private void bindWidgetsModel(final Callbacks callbacks) {
1895 final MultiHashMap<PackageItemInfo, WidgetItem> widgets
1896 = mBgWidgetsModel.getWidgetsMap().clone();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001897 mHandler.post(new Runnable() {
1898 @Override
1899 public void run() {
1900 Callbacks cb = getCallback();
1901 if (callbacks == cb && cb != null) {
Sunny Goyald164b7f2016-10-12 20:49:31 -07001902 callbacks.bindAllWidgets(widgets);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001903 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07001904 }
1905 });
1906 }
1907
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001908 public void refreshAndBindWidgetsAndShortcuts(
1909 final Callbacks callbacks, final boolean bindFirst) {
1910 runOnWorkerThread(new Runnable() {
1911 @Override
1912 public void run() {
1913 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
Sunny Goyald164b7f2016-10-12 20:49:31 -07001914 bindWidgetsModel(callbacks);
Sunny Goyal31860582015-09-18 08:38:57 -07001915 }
Sunny Goyald164b7f2016-10-12 20:49:31 -07001916 ArrayList<WidgetItem> allWidgets = mBgWidgetsModel.update(mApp.getContext());
1917 bindWidgetsModel(callbacks);
1918
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001919 // update the Widget entries inside DB on the worker thread.
Sunny Goyal87f784c2017-01-11 10:48:34 -08001920 mApp.getWidgetCache().removeObsoletePreviews(allWidgets);
Sunny Goyal31860582015-09-18 08:38:57 -07001921 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001922 });
Michael Jurkac402cd92013-05-20 15:49:32 +02001923 }
1924
Sunny Goyal651077b2014-06-30 14:15:31 -07001925 static boolean isValidProvider(AppWidgetProviderInfo provider) {
1926 return (provider != null) && (provider.provider != null)
1927 && (provider.provider.getPackageName() != null);
1928 }
1929
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001930 public void dumpState(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
1931 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
1932 writer.println(prefix + "All apps list: size=" + mBgAllAppsList.data.size());
1933 for (AppInfo info : mBgAllAppsList.data) {
1934 writer.println(prefix + " title=\"" + info.title + "\" iconBitmap=" + info.iconBitmap
1935 + " componentName=" + info.componentName.getPackageName());
1936 }
Joe Onorato36115782010-06-17 13:28:48 -04001937 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001938 sBgDataModel.dump(prefix, fd, writer, args);
Joe Onoratobe386092009-11-17 17:32:16 -08001939 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08001940
1941 public Callbacks getCallback() {
1942 return mCallbacks != null ? mCallbacks.get() : null;
1943 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07001944
1945 /**
1946 * @return {@link FolderInfo} if its already loaded.
1947 */
1948 public FolderInfo findFolderById(Long folderId) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001949 synchronized (sBgDataModel) {
1950 return sBgDataModel.folders.get(folderId);
Sunny Goyal18bf8e22015-04-08 18:13:46 -07001951 }
1952 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07001953
Sunny Goyal527c7d32015-08-28 15:19:36 -07001954 @Thunk class DeferredMainThreadExecutor implements Executor {
1955
1956 @Override
1957 public void execute(Runnable command) {
1958 runOnMainThread(command);
1959 }
1960 }
1961
Sunny Goyal756adbc2015-04-16 15:20:51 -07001962 /**
1963 * @return the looper for the worker thread which can be used to start background tasks.
1964 */
1965 public static Looper getWorkerLooper() {
1966 return sWorkerThread.getLooper();
1967 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001968}