blob: 384f20237dd783a216dc235bba86ce32fa0bb6de [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;
Jon Miranda655ec422017-02-07 11:40:22 -080051import com.android.launcher3.folder.FolderIconPreviewVerifier;
Sunny Goyal1b072632017-01-18 11:30:23 -080052import com.android.launcher3.graphics.LauncherIcons;
Sunny Goyal1acb9e92016-05-16 12:41:09 -070053import com.android.launcher3.logging.FileLog;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070054import com.android.launcher3.model.AddWorkspaceItemsTask;
Sunny Goyale9956a72016-09-01 17:24:47 -070055import com.android.launcher3.model.BgDataModel;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070056import com.android.launcher3.model.CacheDataUpdatedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080057import com.android.launcher3.model.ExtendedModelTask;
Sunny Goyalf862a262015-12-14 14:27:38 -080058import com.android.launcher3.model.GridSizeMigrationTask;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -080059import com.android.launcher3.model.LoaderCursor;
Sunny Goyal43bf11d2017-02-02 13:52:53 -080060import com.android.launcher3.model.ModelWriter;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070061import com.android.launcher3.model.PackageInstallStateChangedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080062import com.android.launcher3.model.PackageItemInfo;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070063import com.android.launcher3.model.PackageUpdatedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080064import com.android.launcher3.model.SdCardAvailableReceiver;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070065import com.android.launcher3.model.ShortcutsChangedTask;
66import com.android.launcher3.model.UserLockStateChangedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080067import com.android.launcher3.model.WidgetItem;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070068import com.android.launcher3.model.WidgetsModel;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -070069import com.android.launcher3.provider.ImportDataTask;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070070import com.android.launcher3.provider.LauncherDbUtils;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070071import com.android.launcher3.shortcuts.DeepShortcutManager;
72import com.android.launcher3.shortcuts.ShortcutInfoCompat;
73import com.android.launcher3.shortcuts.ShortcutKey;
Robin Lee26ace122015-03-16 19:41:43 +000074import com.android.launcher3.util.ComponentKey;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070075import com.android.launcher3.util.ManagedProfileHeuristic;
Tony Wickhamd82a39d2016-07-01 15:44:13 -070076import com.android.launcher3.util.MultiHashMap;
Sunny Goyald09c3702016-04-06 16:18:20 -070077import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070078import com.android.launcher3.util.Preconditions;
Sunny Goyal2bcbe132016-11-16 09:23:42 -080079import com.android.launcher3.util.Provider;
Adam Cohen091440a2015-03-18 14:16:05 -070080import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070081import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080082
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -080083import java.io.FileDescriptor;
84import java.io.PrintWriter;
Michael Jurkac2f801e2011-07-12 14:19:46 -070085import java.lang.ref.WeakReference;
Michael Jurkac2f801e2011-07-12 14:19:46 -070086import java.util.ArrayList;
87import java.util.Collections;
88import java.util.Comparator;
89import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070090import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070091import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070092import java.util.List;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070093import java.util.Map;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070094import java.util.Set;
Sunny Goyaldd96a5e2017-02-17 11:22:34 -080095import java.util.concurrent.CancellationException;
Sunny Goyal527c7d32015-08-28 15:19:36 -070096import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -070097
The Android Open Source Project31dd5032009-03-03 19:32:27 -080098/**
99 * Maintains in-memory state of the Launcher. It is expected that there should be only one
100 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700101 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102 */
Kenny Guyed131872014-04-30 03:02:21 +0100103public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +0100104 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800105 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -0400106 private static final boolean DEBUG_RECEIVER = false;
Chris Wrenb358f812014-04-16 13:37:00 -0400107
Joe Onorato9c1289c2009-08-17 11:03:03 -0400108 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700109
Joe Onorato36115782010-06-17 13:28:48 -0400110 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500111 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800112
Adam Cohen091440a2015-03-18 14:16:05 -0700113 @Thunk final LauncherAppState mApp;
114 @Thunk final Object mLock = new Object();
115 @Thunk DeferredHandler mHandler = new DeferredHandler();
116 @Thunk LoaderTask mLoaderTask;
117 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700118 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119
Adam Cohen091440a2015-03-18 14:16:05 -0700120 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700121 static {
122 sWorkerThread.start();
123 }
Adam Cohen091440a2015-03-18 14:16:05 -0700124 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700125
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800126 // Indicates whether the current model data is valid or not.
127 // We start off with everything not loaded. After that, we assume that
Joe Onoratocc67f472010-06-08 10:54:30 -0700128 // our monitoring of the package manager provides all updates and we never
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800129 // need to do a requery. This is only ever touched from the loader thread.
130 private boolean mModelLoaded;
Hyunyoung Song6aa37292017-02-06 10:46:24 -0800131 public boolean isModelLoaded() {
132 synchronized (mLock) {
133 return mModelLoaded && mLoaderTask == null;
134 }
135 }
Joe Onoratocc67f472010-06-08 10:54:30 -0700136
Sunny Goyal756a28a2015-04-23 17:07:55 -0700137 /**
138 * Set of runnables to be called on the background thread after the workspace binding
139 * is complete.
140 */
141 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
142
Adam Cohen091440a2015-03-18 14:16:05 -0700143 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700145 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800146 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700147 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800148 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800149
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700150 private boolean mHasShortcutHostPermission;
151 // Runnable to check if the shortcuts permission has changed.
152 private final Runnable mShortcutPermissionCheckRunnable = new Runnable() {
153 @Override
154 public void run() {
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800155 if (mModelLoaded) {
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530156 boolean hasShortcutHostPermission =
157 DeepShortcutManager.getInstance(mApp.getContext()).hasHostPermission();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700158 if (hasShortcutHostPermission != mHasShortcutHostPermission) {
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800159 forceReload();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700160 }
161 }
162 }
163 };
164
Sunny Goyale9956a72016-09-01 17:24:47 -0700165 /**
166 * All the static data should be accessed on the background thread, A lock should be acquired
167 * on this object when accessing any data from this model.
168 */
169 static final BgDataModel sBgDataModel = new BgDataModel();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700170
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700171 // </ only access in worker thread >
172
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700173 private final IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700175 private final LauncherAppsCompat mLauncherApps;
176 private final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100177
Joe Onorato9c1289c2009-08-17 11:03:03 -0400178 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700179 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400180 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700181 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400182 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700183 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
184 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700185 public void bindScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700186 public void finishFirstPageBind(ViewOnDrawExecutor executor);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800187 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400188 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200189 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700190 public void bindAppsAdded(ArrayList<Long> newScreens,
191 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700192 ArrayList<ItemInfo> addAnimated,
193 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200194 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700195 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800196 ArrayList<ShortcutInfo> removed, UserHandle user);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700197 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700198 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700199 public void bindWorkspaceComponentsRemoved(
200 HashSet<String> packageNames, HashSet<ComponentName> components,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800201 UserHandle user);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700202 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800203 public void notifyWidgetProvidersChanged();
Sunny Goyald164b7f2016-10-12 20:49:31 -0700204 public void bindAllWidgets(MultiHashMap<PackageItemInfo, WidgetItem> widgets);
Adam Cohen1462de32012-07-24 22:34:36 -0700205 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700206 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700207 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMap);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400208 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530210 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800211 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400212 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100213 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Sunny Goyald164b7f2016-10-12 20:49:31 -0700214 mBgWidgetsModel = new WidgetsModel(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800215 mIconCache = iconCache;
216
Kenny Guyed131872014-04-30 03:02:21 +0100217 mLauncherApps = LauncherAppsCompat.getInstance(context);
218 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800219 }
220
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700221 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
222 * posted on the main thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700223 private void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700224 if (sWorkerThread.getThreadId() == Process.myTid()) {
225 // If we are on the worker thread, post onto the main handler
226 mHandler.post(r);
227 } else {
228 r.run();
229 }
230 }
231
232 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
233 * posted on the worker thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700234 private static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700235 if (sWorkerThread.getThreadId() == Process.myTid()) {
236 r.run();
237 } else {
238 // If we are not on the worker thread, then post to the worker handler
239 sWorker.post(r);
240 }
241 }
242
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700243 public void setPackageState(PackageInstallInfo installInfo) {
244 enqueueModelUpdateTask(new PackageInstallStateChangedTask(installInfo));
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500245 }
246
Sunny Goyal756adbc2015-04-16 15:20:51 -0700247 /**
248 * Updates the icons and label of all pending icons for the provided package name.
249 */
250 public void updateSessionDisplayInfo(final String packageName) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700251 HashSet<String> packages = new HashSet<>();
252 packages.add(packageName);
253 enqueueModelUpdateTask(new CacheDataUpdatedTask(
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800254 CacheDataUpdatedTask.OP_SESSION_UPDATE, Process.myUserHandle(), packages));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800255 }
256
257 /**
258 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800259 */
Sunny Goyal2bcbe132016-11-16 09:23:42 -0800260 public void addAndBindAddedWorkspaceItems(List<ItemInfo> workspaceApps) {
261 addAndBindAddedWorkspaceItems(Provider.of(workspaceApps));
262 }
263
264 /**
265 * Adds the provided items to the workspace.
266 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700267 public void addAndBindAddedWorkspaceItems(
Sunny Goyal2bcbe132016-11-16 09:23:42 -0800268 Provider<List<ItemInfo>> appsProvider) {
269 enqueueModelUpdateTask(new AddWorkspaceItemsTask(appsProvider));
Winson Chung64359a52013-07-08 17:17:08 -0700270 }
271
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800272 public ModelWriter getWriter(boolean hasVerticalHotseat) {
273 return new ModelWriter(mApp.getContext(), sBgDataModel, hasVerticalHotseat);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274 }
275
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700276 static void checkItemInfoLocked(
277 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
Sunny Goyale9956a72016-09-01 17:24:47 -0700278 ItemInfo modelItem = sBgDataModel.itemsIdMap.get(itemId);
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700279 if (modelItem != null && item != modelItem) {
280 // check all the data is consistent
281 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
282 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
283 ShortcutInfo shortcut = (ShortcutInfo) item;
284 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
285 modelShortcut.intent.filterEquals(shortcut.intent) &&
286 modelShortcut.id == shortcut.id &&
287 modelShortcut.itemType == shortcut.itemType &&
288 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700289 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700290 modelShortcut.cellX == shortcut.cellX &&
291 modelShortcut.cellY == shortcut.cellY &&
292 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700293 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700294 // For all intents and purposes, this is the same object
295 return;
296 }
297 }
298
299 // the modelItem needs to match up perfectly with item if our model is
300 // to be consistent with the database-- for now, just require
301 // modelItem == item or the equality check above
302 String msg = "item: " + ((item != null) ? item.toString() : "null") +
303 "modelItem: " +
304 ((modelItem != null) ? modelItem.toString() : "null") +
305 "Error: ItemInfo passed to checkItemInfo doesn't match original";
306 RuntimeException e = new RuntimeException(msg);
307 if (stackTrace != null) {
308 e.setStackTrace(stackTrace);
309 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800310 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700311 }
312 }
313
Michael Jurka816474f2012-06-25 14:49:02 -0700314 static void checkItemInfo(final ItemInfo item) {
315 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
316 final long itemId = item.id;
317 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700318 public void run() {
Sunny Goyale9956a72016-09-01 17:24:47 -0700319 synchronized (sBgDataModel) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700320 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700321 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700322 }
323 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700324 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700325 }
326
Joe Onorato9c1289c2009-08-17 11:03:03 -0400327 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700328 * Update the order of the workspace screens in the database. The array list contains
329 * a list of screen ids in the order that they should appear.
330 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700331 public static void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -0700332 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700333 final ContentResolver cr = context.getContentResolver();
334 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
335
336 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700337 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700338 while (iter.hasNext()) {
339 long id = iter.next();
340 if (id < 0) {
341 iter.remove();
342 }
343 }
344
345 Runnable r = new Runnable() {
346 @Override
347 public void run() {
Yura085c8532014-02-11 15:15:29 +0000348 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700349 // Clear the table
Yura085c8532014-02-11 15:15:29 +0000350 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -0700351 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700352 for (int i = 0; i < count; i++) {
353 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700354 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700355 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
356 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +0000357 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -0700358 }
Yura085c8532014-02-11 15:15:29 +0000359
360 try {
361 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
362 } catch (Exception ex) {
363 throw new RuntimeException(ex);
364 }
Winson Chung9e6a0a22013-08-27 11:58:12 -0700365
Sunny Goyale9956a72016-09-01 17:24:47 -0700366 synchronized (sBgDataModel) {
367 sBgDataModel.workspaceScreens.clear();
368 sBgDataModel.workspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -0700369 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700370 }
371 };
372 runOnWorkerThread(r);
373 }
374
375 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400376 * Set this as the current Launcher activity object for the loader.
377 */
378 public void initialize(Callbacks callbacks) {
379 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -0700380 Preconditions.assertUIThread();
381 // Remove any queued UI runnables
382 mHandler.cancelAll();
383 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400384 }
385 }
386
Kenny Guyed131872014-04-30 03:02:21 +0100387 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800388 public void onPackageChanged(String packageName, UserHandle user) {
Kenny Guyed131872014-04-30 03:02:21 +0100389 int op = PackageUpdatedTask.OP_UPDATE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700390 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100391 }
392
393 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800394 public void onPackageRemoved(String packageName, UserHandle user) {
Sunny Goyalc2936bc2016-09-01 15:50:36 -0700395 onPackagesRemoved(user, packageName);
396 }
397
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800398 public void onPackagesRemoved(UserHandle user, String... packages) {
Kenny Guyed131872014-04-30 03:02:21 +0100399 int op = PackageUpdatedTask.OP_REMOVE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700400 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packages));
Kenny Guyed131872014-04-30 03:02:21 +0100401 }
402
403 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800404 public void onPackageAdded(String packageName, UserHandle user) {
Kenny Guyed131872014-04-30 03:02:21 +0100405 int op = PackageUpdatedTask.OP_ADD;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700406 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100407 }
408
409 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800410 public void onPackagesAvailable(String[] packageNames, UserHandle user,
Kenny Guyed131872014-04-30 03:02:21 +0100411 boolean replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700412 enqueueModelUpdateTask(
413 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100414 }
415
416 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800417 public void onPackagesUnavailable(String[] packageNames, UserHandle user,
Kenny Guyed131872014-04-30 03:02:21 +0100418 boolean replacing) {
419 if (!replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700420 enqueueModelUpdateTask(new PackageUpdatedTask(
421 PackageUpdatedTask.OP_UNAVAILABLE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100422 }
Kenny Guyed131872014-04-30 03:02:21 +0100423 }
424
Kenny Guy44cba692016-01-21 19:50:02 +0000425 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800426 public void onPackagesSuspended(String[] packageNames, UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700427 enqueueModelUpdateTask(new PackageUpdatedTask(
428 PackageUpdatedTask.OP_SUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000429 }
430
431 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800432 public void onPackagesUnsuspended(String[] packageNames, UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700433 enqueueModelUpdateTask(new PackageUpdatedTask(
434 PackageUpdatedTask.OP_UNSUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000435 }
436
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700437 @Override
438 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800439 UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700440 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, true));
Sunny Goyal50941fb2016-08-04 12:03:52 -0700441 }
442
443 public void updatePinnedShortcuts(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800444 UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700445 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, false));
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700446 }
447
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700448 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400449 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
450 * ACTION_PACKAGE_CHANGED.
451 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100452 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400453 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -0400454 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700455
Joe Onorato36115782010-06-17 13:28:48 -0400456 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +0100457 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -0700458 // If we have changed locale we need to clear out the labels in all apps/workspace.
459 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700460 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
461 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -0700462 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -0700463 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700464 } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
465 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
466 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800467 UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER);
Sunny Goyalda891c12016-03-18 18:29:24 -0700468 if (user != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700469 if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
470 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700471 enqueueModelUpdateTask(new PackageUpdatedTask(
472 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE, user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700473 }
474
475 // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so
476 // we need to run the state change task again.
477 if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
478 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700479 enqueueModelUpdateTask(new UserLockStateChangedTask(user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700480 }
Sunny Goyalda891c12016-03-18 18:29:24 -0700481 }
Tony Wickham827cef22016-03-17 15:39:39 -0700482 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
483 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700484 }
485 }
486
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800487 /**
488 * Reloads the workspace items from the DB and re-binds the workspace. This should generally
489 * not be called as DB updates are automatically followed by UI update
490 */
491 public void forceReload() {
492 synchronized (mLock) {
493 // Stop any existing loaders first, so they don't set mModelLoaded to true later
494 stopLoaderLocked();
495 mModelLoaded = false;
496 }
Winson Chungf0c6ae02012-03-21 16:10:31 -0700497
Reena Lee93f824a2011-09-23 17:20:28 -0700498 // Do this here because if the launcher activity is running it will be restarted.
499 // If it's not running startLoaderFromBackground will merely tell it that it needs
500 // to reload.
501 startLoaderFromBackground();
502 }
503
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700504 /**
505 * When the launcher is in the background, it's possible for it to miss paired
506 * configuration changes. So whenever we trigger the loader from the background
507 * tell the launcher that it needs to re-run the loader when it comes back instead
508 * of doing it now.
509 */
510 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800511 Callbacks callbacks = getCallback();
512 if (callbacks != null) {
513 // Only actually run the loader if they're not paused.
514 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -0700515 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700516 }
517 }
Joe Onorato36115782010-06-17 13:28:48 -0400518 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400519
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700520 /**
521 * If there is already a loader task running, tell it to stop.
522 */
523 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -0700524 LoaderTask oldTask = mLoaderTask;
525 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -0700526 oldTask.stopLocked();
527 }
Reena Lee93f824a2011-09-23 17:20:28 -0700528 }
529
Adam Cohen1a85c582014-09-30 09:48:49 -0700530 public boolean isCurrentCallbacks(Callbacks callbacks) {
531 return (mCallbacks != null && mCallbacks.get() == callbacks);
532 }
533
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700534 /**
535 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
536 * @return true if the page could be bound synchronously.
537 */
538 public boolean startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700539 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
540 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -0400541 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -0400542 // Don't bother to start the thread if we know it's not going to do anything
543 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -0700544 final Callbacks oldCallbacks = mCallbacks.get();
545 // Clear any pending bind-runnables from the synchronized load process.
546 runOnMainThread(new Runnable() {
547 public void run() {
548 oldCallbacks.clearPendingBinds();
549 }
550 });
551
Joe Onorato36115782010-06-17 13:28:48 -0400552 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700553 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700554 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800555 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
556 && mModelLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700557 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700558 return true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700559 } else {
560 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
561 sWorker.post(mLoaderTask);
562 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400563 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400564 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700565 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400566 }
567
Joe Onorato36115782010-06-17 13:28:48 -0400568 public void stopLoader() {
569 synchronized (mLock) {
570 if (mLoaderTask != null) {
571 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400572 }
573 }
Joe Onorato36115782010-06-17 13:28:48 -0400574 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400575
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800576 /**
577 * Loads the workspace screen ids in an ordered list.
578 */
Sunny Goyale5bb7052015-07-27 14:36:07 -0700579 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -0700580 final ContentResolver contentResolver = context.getContentResolver();
581 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -0700582
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800583 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -0700584 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
585 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -0700586 }
587
Joe Onorato36115782010-06-17 13:28:48 -0400588 /**
589 * Runnable for the thread that loads the contents of the launcher:
590 * - workspace icons
591 * - widgets
592 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700593 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -0400594 */
595 private class LoaderTask implements Runnable {
596 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -0700597 private int mPageToBindFirst;
598
Adam Cohen091440a2015-03-18 14:16:05 -0700599 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -0400600 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -0700601 @Thunk boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700602
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700603 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -0400604 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -0700605 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400606 }
607
Joe Onorato36115782010-06-17 13:28:48 -0400608 private void waitForIdle() {
609 // Wait until the either we're stopped or the other threads are done.
610 // This way we don't start loading all apps until the workspace has settled
611 // down.
612 synchronized (LoaderTask.this) {
613 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700614
Joe Onorato36115782010-06-17 13:28:48 -0400615 mHandler.postIdle(new Runnable() {
616 public void run() {
617 synchronized (LoaderTask.this) {
618 mLoadAndBindStepFinished = true;
619 if (DEBUG_LOADERS) {
620 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400621 }
Joe Onorato36115782010-06-17 13:28:48 -0400622 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400623 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400624 }
Joe Onorato36115782010-06-17 13:28:48 -0400625 });
626
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800627 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -0400628 try {
Michael Jurkac7700af2013-05-14 20:17:58 +0200629 // Just in case mFlushingWorkerThread changes but we aren't woken up,
630 // wait no longer than 1sec at a time
631 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -0400632 } catch (InterruptedException ex) {
633 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400634 }
635 }
Joe Onorato36115782010-06-17 13:28:48 -0400636 if (DEBUG_LOADERS) {
637 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700638 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400639 + "ms for previous step to finish binding");
640 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400641 }
Joe Onorato36115782010-06-17 13:28:48 -0400642 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400643
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700644 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -0500645 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700646 // Ensure that we have a valid page index to load synchronously
647 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
648 "valid page index");
649 }
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800650 if (!mModelLoaded) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700651 // Ensure that we don't try and bind a specified page when the pages have not been
652 // loaded already (we should load everything asynchronously in that case)
653 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
654 }
655 synchronized (mLock) {
656 if (mIsLoaderTaskRunning) {
657 // Ensure that we are never running the background loading at this point since
658 // we also touch the background collections
659 throw new RuntimeException("Error! Background loading is already running");
660 }
661 }
662
663 // XXX: Throw an exception if we are already loading (since we touch the worker thread
664 // data structures, we can't allow any other thread to touch that data, but because
665 // this call is synchronous, we can get away with not locking).
666
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400667 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -0700668 // operations from the previous activity. We need to ensure that all queued operations
669 // are executed before any synchronous binding work is done.
670 mHandler.flush();
671
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700672 // Divide the set of loaded items into those that we are binding synchronously, and
673 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800674 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700675 // XXX: For now, continue posting the binding of AllApps as there are other issues that
676 // arise from that.
677 onlyBindAllApps();
Tony Wickham80f57872016-06-29 18:12:15 -0700678
679 bindDeepShortcuts();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700680 }
681
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800682 private void verifyNotStopped() throws CancellationException {
683 synchronized (LoaderTask.this) {
684 if (mStopped) {
685 throw new CancellationException("Loader stopped");
686 }
687 }
688 }
689
Joe Onorato36115782010-06-17 13:28:48 -0400690 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700691 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -0700692 if (mStopped) {
693 return;
694 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700695 mIsLoaderTaskRunning = true;
696 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400697
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800698 try {
699 if (DEBUG_LOADERS) Log.d(TAG, "step 1.1: loading workspace");
700 // Set to false in bindWorkspace()
701 mIsLoadingAndBindingWorkspace = true;
702 loadWorkspace();
Joe Onorato36115782010-06-17 13:28:48 -0400703
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800704 verifyNotStopped();
705 if (DEBUG_LOADERS) Log.d(TAG, "step 1.2: bind workspace workspace");
706 bindWorkspace(mPageToBindFirst);
707
708 // Take a break
709 if (DEBUG_LOADERS) Log.d(TAG, "step 1 completed, wait for idle");
Joe Onorato36115782010-06-17 13:28:48 -0400710 waitForIdle();
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800711 verifyNotStopped();
Daniel Sandler843e8602010-06-07 14:59:01 -0400712
713 // second step
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800714 if (DEBUG_LOADERS) Log.d(TAG, "step 2.1: loading all apps");
715 loadAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700716
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800717 verifyNotStopped();
718 if (DEBUG_LOADERS) Log.d(TAG, "step 2.2: Update icon cache");
719 updateIconCache();
720
721 // Take a break
722 if (DEBUG_LOADERS) Log.d(TAG, "step 2 completed, wait for idle");
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700723 waitForIdle();
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800724 verifyNotStopped();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700725
726 // third step
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800727 if (DEBUG_LOADERS) Log.d(TAG, "step 3.1: loading deep shortcuts");
728 loadDeepShortcuts();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400729
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800730 verifyNotStopped();
731 if (DEBUG_LOADERS) Log.d(TAG, "step 3.2: bind deep shortcuts");
732 bindDeepShortcuts();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400733
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800734 synchronized (mLock) {
735 // Everything loaded bind the data.
736 mModelLoaded = true;
737 mHasLoaderCompletedOnce = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400738 }
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800739 } catch (CancellationException e) {
740 // Loader stopped, ignore
741 } finally {
742 // Clear out this reference, otherwise we end up holding it until all of the
743 // callback runnables are done.
744 mContext = null;
745
746 synchronized (mLock) {
747 // If we are still the last one to be scheduled, remove ourselves.
748 if (mLoaderTask == this) {
749 mLoaderTask = null;
750 }
751 mIsLoaderTaskRunning = false;
752 }
Joe Onorato36115782010-06-17 13:28:48 -0400753 }
Joe Onorato36115782010-06-17 13:28:48 -0400754 }
755
756 public void stopLocked() {
757 synchronized (LoaderTask.this) {
758 mStopped = true;
759 this.notify();
760 }
761 }
762
763 /**
764 * Gets the callbacks object. If we've been stopped, or if the launcher object
765 * has somehow been garbage collected, return null instead. Pass in the Callbacks
766 * object that was around when the deferred message was scheduled, and if there's
767 * a new Callbacks object around then also return null. This will save us from
768 * calling onto it with data that will be ignored.
769 */
770 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
771 synchronized (mLock) {
772 if (mStopped) {
773 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400774 }
Joe Onorato36115782010-06-17 13:28:48 -0400775
776 if (mCallbacks == null) {
777 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400778 }
Joe Onorato36115782010-06-17 13:28:48 -0400779
780 final Callbacks callbacks = mCallbacks.get();
781 if (callbacks != oldCallbacks) {
782 return null;
783 }
784 if (callbacks == null) {
785 Log.w(TAG, "no mCallbacks");
786 return null;
787 }
788
789 return callbacks;
790 }
791 }
792
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800793 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -0700794 if (LauncherAppState.PROFILE_STARTUP) {
795 Trace.beginSection("Loading Workspace");
796 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400797
Joe Onorato36115782010-06-17 13:28:48 -0400798 final Context context = mContext;
799 final ContentResolver contentResolver = context.getContentResolver();
Sunny Goyal342e4662017-02-02 15:21:08 -0800800 final PackageManagerHelper pmHelper = new PackageManagerHelper(context);
801 final boolean isSafeMode = pmHelper.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700802 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530803 final DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -0700804 final boolean isSdCardReady = Utilities.isBootCompleted();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800805 final MultiHashMap<UserHandle, String> pendingPackages = new MultiHashMap<>();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400806
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700807 boolean clearDb = false;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -0700808 try {
809 ImportDataTask.performImportIfPossible(context);
810 } catch (Exception e) {
811 // Migration failed. Clear workspace.
812 clearDb = true;
813 }
814
815 if (!clearDb && GridSizeMigrationTask.ENABLED &&
Sunny Goyalf076eae2016-01-11 12:25:10 -0800816 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
817 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700818 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -0700819 }
820
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700821 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -0700822 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -0700823 LauncherSettings.Settings.call(contentResolver,
Sunny Goyale05b08f2017-02-23 18:30:22 -0800824 LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -0500825 }
826
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700827 Log.d(TAG, "loadWorkspace: loading default favorites");
828 LauncherSettings.Settings.call(contentResolver,
829 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -0700830
Sunny Goyale9956a72016-09-01 17:24:47 -0700831 synchronized (sBgDataModel) {
832 sBgDataModel.clear();
833
Sunny Goyal756adbc2015-04-16 15:20:51 -0700834 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -0700835 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyale9956a72016-09-01 17:24:47 -0700836 sBgDataModel.workspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -0800837
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700838 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800839 final LoaderCursor c = new LoaderCursor(contentResolver.query(
840 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null), mApp);
Daniel Sandler8802e962010-05-26 16:28:16 -0400841
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800842 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400843
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700844 try {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700845 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
846 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -0400847 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
848 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700849 final int spanXIndex = c.getColumnIndexOrThrow
850 (LauncherSettings.Favorites.SPANX);
851 final int spanYIndex = c.getColumnIndexOrThrow(
852 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800853 final int rankIndex = c.getColumnIndexOrThrow(
854 LauncherSettings.Favorites.RANK);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700855 final int optionsIndex = c.getColumnIndexOrThrow(
856 LauncherSettings.Favorites.OPTIONS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400857
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800858 final LongSparseArray<UserHandle> allUsers = c.allUsers;
Kenny Guyff05f432016-01-22 17:48:29 +0000859 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700860 final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800861 for (UserHandle user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +0000862 long serialNo = mUserManager.getSerialNumberForUser(user);
863 allUsers.put(serialNo, user);
864 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700865
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700866 boolean userUnlocked = mUserManager.isUserUnlocked(user);
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700867
868 // We can only query for shortcuts when the user is unlocked.
869 if (userUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -0700870 List<ShortcutInfoCompat> pinnedShortcuts =
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530871 shortcutManager.queryForPinnedShortcuts(null, user);
872 if (shortcutManager.wasLastCallSuccess()) {
Sunny Goyal49f4f032016-08-01 15:45:49 -0700873 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
874 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
875 shortcut);
876 }
877 } else {
878 // Shortcut manager can fail due to some race condition when the
879 // lock state changes too frequently. For the purpose of the loading
880 // shortcuts, consider the user is still locked.
881 userUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700882 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700883 }
Sunny Goyal49f4f032016-08-01 15:45:49 -0700884 unlockedUsers.put(serialNo, userUnlocked);
Sunny Goyal7f834d22015-04-21 10:10:23 -0700885 }
886
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700887 ShortcutInfo info;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700888 LauncherAppWidgetInfo appWidgetInfo;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700889 Intent intent;
Sunny Goyal81e44912017-01-14 15:05:14 -0800890 String targetPkg;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400891
Jon Miranda529af302017-02-28 14:18:54 -0800892 FolderIconPreviewVerifier verifier =
893 new FolderIconPreviewVerifier(mApp.getInvariantDeviceProfile());
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700894 while (!mStopped && c.moveToNext()) {
895 try {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800896 if (c.user == null) {
897 // User has been deleted, remove the item.
898 c.markDeleted("User has been deleted");
899 continue;
900 }
901
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700902 boolean allowMissingTarget = false;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800903 switch (c.itemType) {
Sunny Goyal81e44912017-01-14 15:05:14 -0800904 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700905 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700906 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Sunny Goyal81e44912017-01-14 15:05:14 -0800907 intent = c.parseIntent();
908 if (intent == null) {
909 c.markDeleted("Invalid or null intent");
910 continue;
911 }
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800912
Sunny Goyal81e44912017-01-14 15:05:14 -0800913 int disabledState = quietMode.get(c.serialNumber) ?
914 ShortcutInfo.FLAG_DISABLED_QUIET_USER : 0;
915 ComponentName cn = intent.getComponent();
916 targetPkg = cn == null ? intent.getPackage() : cn.getPackageName();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700917
Sunny Goyal81e44912017-01-14 15:05:14 -0800918 if (!Process.myUserHandle().equals(c.user)) {
919 if (c.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
920 c.markDeleted("Legacy shortcuts are only allowed for default user");
921 continue;
922 } else if (c.restoreFlag != 0) {
923 // Don't restore items for other profiles.
924 c.markDeleted("Restore from managed profile not supported");
925 continue;
926 }
927 }
928 if (TextUtils.isEmpty(targetPkg) &&
929 c.itemType != LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
930 c.markDeleted("Only legacy shortcuts can have null package");
931 continue;
932 }
Sunny Goyal34942622014-08-29 17:20:55 -0700933
Sunny Goyal81e44912017-01-14 15:05:14 -0800934 // If there is no target package, its an implicit intent
935 // (legacy shortcut) which is always valid
936 boolean validTarget = TextUtils.isEmpty(targetPkg) ||
937 launcherApps.isPackageEnabledForProfile(targetPkg, c.user);
Sunny Goyal94485362014-09-18 16:13:58 -0700938
Sunny Goyal81e44912017-01-14 15:05:14 -0800939 if (cn != null && validTarget) {
940 // If the apk is present and the shortcut points to a specific
941 // component.
942
943 // If the component is already present
944 if (launcherApps.isActivityEnabledForProfile(cn, c.user)) {
945 // no special handling necessary for this item
946 c.markRestored();
947 } else {
948 if (c.hasRestoreFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
949 // We allow auto install apps to have their intent
950 // updated after an install.
Sunny Goyal342e4662017-02-02 15:21:08 -0800951 intent = pmHelper.getAppLaunchIntent(targetPkg, c.user);
Sunny Goyal81e44912017-01-14 15:05:14 -0800952 if (intent != null) {
953 c.restoreFlag = 0;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800954 c.updater().put(
Sunny Goyal81e44912017-01-14 15:05:14 -0800955 LauncherSettings.Favorites.INTENT,
956 intent.toUri(0)).commit();
957 cn = intent.getComponent();
Sunny Goyalb05a00a2016-08-29 10:06:57 -0700958 } else {
Sunny Goyal81e44912017-01-14 15:05:14 -0800959 c.markDeleted("Unable to find a launch target");
Sunny Goyal94485362014-09-18 16:13:58 -0700960 continue;
961 }
Sunny Goyal1a745e82014-10-02 15:58:31 -0700962 } else {
Sunny Goyal81e44912017-01-14 15:05:14 -0800963 // The app is installed but the component is no
964 // longer available.
965 c.markDeleted("Invalid component removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -0700966 continue;
Winson Chungee055712013-07-30 14:46:24 -0700967 }
Winson Chungee055712013-07-30 14:46:24 -0700968 }
Sunny Goyal81e44912017-01-14 15:05:14 -0800969 }
970 // else if cn == null => can't infer much, leave it
971 // else if !validPkg => could be restored icon or missing sd-card
972
973 if (!TextUtils.isEmpty(targetPkg) && !validTarget) {
974 // Points to a valid app (superset of cn != null) but the apk
975 // is not available.
976
977 if (c.restoreFlag != 0) {
978 // Package is not yet available but might be
979 // installed later.
980 FileLog.d(TAG, "package not yet restored: " + targetPkg);
981
982 if (c.hasRestoreFlag(ShortcutInfo.FLAG_RESTORE_STARTED)) {
983 // Restore has started once.
984 } else if (installingPkgs.containsKey(targetPkg)) {
985 // App restore has started. Update the flag
986 c.restoreFlag |= ShortcutInfo.FLAG_RESTORE_STARTED;
987 c.updater().commit();
988 } else {
989 c.markDeleted("Unrestored app removed: " + targetPkg);
990 continue;
991 }
Sunny Goyalad2e91a2017-02-28 15:14:58 -0800992 } else if (pmHelper.isAppOnSdcard(targetPkg, c.user)) {
Sunny Goyal81e44912017-01-14 15:05:14 -0800993 // Package is present but not available.
994 disabledState |= ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
995 // Add the icon on the workspace anyway.
996 allowMissingTarget = true;
997 } else if (!isSdCardReady) {
998 // SdCard is not ready yet. Package might get available,
999 // once it is ready.
1000 Log.d(TAG, "Missing pkg, will check later: " + targetPkg);
1001 pendingPackages.addToList(c.user, targetPkg);
1002 // Add the icon on the workspace anyway.
1003 allowMissingTarget = true;
1004 } else {
1005 // Do not wait for external media load anymore.
1006 c.markDeleted("Invalid package removed: " + targetPkg);
1007 continue;
1008 }
1009 }
1010
1011 if (validTarget) {
1012 // The shortcut points to a valid target (either no target
1013 // or something which is ready to be used)
1014 c.markRestored();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001015 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001016
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001017 boolean useLowResIcon = !c.isOnWorkspaceOrHotseat() &&
Jon Miranda529af302017-02-28 14:18:54 -08001018 !verifier.isItemInPreview(c.getInt(rankIndex));
Sunny Goyal34b65272015-03-11 16:56:52 -07001019
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001020 if (c.restoreFlag != 0) {
Sunny Goyal81e44912017-01-14 15:05:14 -08001021 // Already verified above that user is same as default user
1022 info = c.getRestoredItemInfo(intent);
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001023 } else if (c.itemType ==
Chris Wrenf4d08112014-01-16 18:13:56 -05001024 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001025 info = c.getAppShortcutInfo(
1026 intent, allowMissingTarget, useLowResIcon);
1027 } else if (c.itemType ==
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001028 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001029
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001030 ShortcutKey key = ShortcutKey.fromIntent(intent, c.user);
1031 if (unlockedUsers.get(c.serialNumber)) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001032 ShortcutInfoCompat pinnedShortcut =
1033 shortcutKeyToPinnedShortcuts.get(key);
1034 if (pinnedShortcut == null) {
1035 // The shortcut is no longer valid.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001036 c.markDeleted("Pinned shortcut not found");
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001037 continue;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001038 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001039 info = new ShortcutInfo(pinnedShortcut, context);
Sunny Goyal1b072632017-01-18 11:30:23 -08001040 info.iconBitmap = LauncherIcons
1041 .createShortcutIcon(pinnedShortcut, context);
Charles He3ff90472017-02-15 12:22:56 +00001042 if (pmHelper.isAppSuspended(
Sunny Goyalad2e91a2017-02-28 15:14:58 -08001043 pinnedShortcut.getPackage(), info.user)) {
Charles He3ff90472017-02-15 12:22:56 +00001044 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1045 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001046 intent = info.intent;
1047 } else {
1048 // Create a shortcut info in disabled mode for now.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001049 info = c.loadSimpleShortcut();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001050 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001051 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001052 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001053 info = c.loadSimpleShortcut();
Michael Jurka96879562012-03-22 05:54:33 -07001054
Sunny Goyald09c3702016-04-06 16:18:20 -07001055 // Shortcuts are only available on the primary profile
Sunny Goyalad2e91a2017-02-28 15:14:58 -08001056 if (pmHelper.isAppSuspended(targetPkg, c.user)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001057 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1058 }
1059
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001060 // App shortcuts that used to be automatically added to Launcher
1061 // didn't always have the correct intent flags set, so do that
1062 // here
1063 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001064 intent.getCategories() != null &&
1065 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001066 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001067 intent.addFlags(
1068 Intent.FLAG_ACTIVITY_NEW_TASK |
1069 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1070 }
Michael Jurka96879562012-03-22 05:54:33 -07001071 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001072
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001073 if (info != null) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001074 c.applyCommonProperties(info);
1075
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001076 info.intent = intent;
Sunny Goyal08f72612015-01-05 13:41:43 -08001077 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001078 info.spanX = 1;
1079 info.spanY = 1;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001080 // TODO: Remove this extra. Instead we should be using
1081 // itemInfo#user.
1082 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, c.serialNumber);
Sunny Goyald09c3702016-04-06 16:18:20 -07001083 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001084 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1085 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1086 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001087
Sunny Goyal81e44912017-01-14 15:05:14 -08001088 if (c.restoreFlag != 0 && !TextUtils.isEmpty(targetPkg)) {
1089 Integer progress = installingPkgs.get(targetPkg);
1090 if (progress != null) {
1091 info.setInstallProgress(progress);
1092 } else {
1093 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001094 }
1095 }
1096
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001097 c.checkAndAddItem(info, sBgDataModel);
Winson Chung1323b482013-08-05 12:41:55 -07001098 } else {
1099 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001100 }
1101 break;
1102
1103 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001104 FolderInfo folderInfo = sBgDataModel.findOrMakeFolder(c.id);
1105 c.applyCommonProperties(folderInfo);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001106
Sunny Goyala508e4f2015-05-21 09:33:57 -07001107 // Do not trim the folder label, as is was set by the user.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001108 folderInfo.title = c.getString(c.titleIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001109 folderInfo.spanX = 1;
1110 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001111 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001112
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001113 // no special handling required for restored folders
1114 c.markRestored();
Chris Wrenf4d08112014-01-16 18:13:56 -05001115
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001116 c.checkAndAddItem(folderInfo, sBgDataModel);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001117 break;
1118
1119 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08001120 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001121 // Read all Launcher-specific widget details
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001122 boolean customWidget = c.itemType ==
Adam Cohen59400422014-03-05 18:07:04 -08001123 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
1124
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001125 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001126 String savedProvider = c.getString(appWidgetProviderIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001127
Sunny Goyalff572272014-07-23 13:58:07 -07001128 final ComponentName component =
1129 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04001130
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001131 final boolean isIdValid = !c.hasRestoreFlag(
1132 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID);
1133 final boolean wasProviderReady = !c.hasRestoreFlag(
1134 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY);
Sunny Goyal651077b2014-06-30 14:15:31 -07001135
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001136 if (widgetProvidersMap == null) {
1137 widgetProvidersMap = AppWidgetManagerCompat
1138 .getInstance(mContext).getAllProvidersMap();
1139 }
1140 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
1141 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00001142 ComponentName.unflattenFromString(savedProvider),
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001143 c.user));
Sunny Goyalff572272014-07-23 13:58:07 -07001144
1145 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08001146 if (!isSafeMode && !customWidget &&
1147 wasProviderReady && !isProviderReady) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001148 c.markDeleted(
1149 "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07001150 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001151 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07001152 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07001153 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1154 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08001155
Sunny Goyal53f96722015-07-13 19:54:53 -07001156 // The provider is available. So the widget is either
1157 // available or not available. We do not need to track
1158 // any future restore updates.
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001159 int status = c.restoreFlag &
Sunny Goyal53f96722015-07-13 19:54:53 -07001160 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07001161 if (!wasProviderReady) {
1162 // If provider was not previously ready, update the
1163 // status and UI flag.
1164
1165 // Id would be valid only if the widget restore broadcast was received.
1166 if (isIdValid) {
Sunny Goyal86df1382016-08-10 15:03:22 -07001167 status |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07001168 } else {
1169 status &= ~LauncherAppWidgetInfo
1170 .FLAG_PROVIDER_NOT_READY;
1171 }
1172 }
1173 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07001174 } else {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001175 Log.v(TAG, "Widget restore pending id=" + c.id
Sunny Goyal651077b2014-06-30 14:15:31 -07001176 + " appWidgetId=" + appWidgetId
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001177 + " status =" + c.restoreFlag);
Sunny Goyal651077b2014-06-30 14:15:31 -07001178 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07001179 component);
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001180 appWidgetInfo.restoreStatus = c.restoreFlag;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001181 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07001182
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001183 if (c.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_RESTORE_STARTED)) {
Sunny Goyal94485362014-09-18 16:13:58 -07001184 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001185 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07001186 // App restore has started. Update the flag
1187 appWidgetInfo.restoreStatus |=
1188 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001189 } else if (!isSafeMode) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001190 c.markDeleted("Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07001191 continue;
1192 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07001193
1194 appWidgetInfo.installProgress =
1195 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07001196 }
Sunny Goyal86df1382016-08-10 15:03:22 -07001197 if (appWidgetInfo.hasRestoreFlag(
1198 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG)) {
Sunny Goyal81e44912017-01-14 15:05:14 -08001199 appWidgetInfo.bindOptions = c.parseIntent();
Sunny Goyal86df1382016-08-10 15:03:22 -07001200 }
Sunny Goyalff572272014-07-23 13:58:07 -07001201
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001202 c.applyCommonProperties(appWidgetInfo);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001203 appWidgetInfo.spanX = c.getInt(spanXIndex);
1204 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001205 appWidgetInfo.user = c.user;
Joe Onorato36115782010-06-17 13:28:48 -04001206
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001207 if (!c.isOnWorkspaceOrHotseat()) {
1208 c.markDeleted("Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001209 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001210 continue;
1211 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001212
Adam Cohen59400422014-03-05 18:07:04 -08001213 if (!customWidget) {
1214 String providerName =
1215 appWidgetInfo.providerName.flattenToString();
1216 if (!providerName.equals(savedProvider) ||
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001217 (appWidgetInfo.restoreStatus != c.restoreFlag)) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001218 c.updater()
1219 .put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1220 providerName)
1221 .put(LauncherSettings.Favorites.RESTORED,
1222 appWidgetInfo.restoreStatus)
1223 .commit();
Adam Cohen59400422014-03-05 18:07:04 -08001224 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001225 }
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001226 c.checkAndAddItem(appWidgetInfo, sBgDataModel);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001227 }
Joe Onorato36115782010-06-17 13:28:48 -04001228 break;
1229 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001230 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001231 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001232 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001233 }
1234 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001235 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001236 }
1237
Winson Chungba9c37f2013-08-30 14:11:37 -07001238 // Break early if we've stopped loading
1239 if (mStopped) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001240 sBgDataModel.clear();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001241 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07001242 }
1243
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001244 // Remove dead items
1245 if (c.commitDeleted()) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001246 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07001247 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
1248 .call(contentResolver,
1249 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
1250 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
1251 for (long folderId : deletedFolderIds) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001252 sBgDataModel.workspaceItems.remove(sBgDataModel.folders.get(folderId));
1253 sBgDataModel.folders.remove(folderId);
1254 sBgDataModel.itemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001255 }
1256 }
1257
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001258 // Unpin shortcuts that don't exist on the workspace.
Sunny Goyalf75baa92016-11-22 03:23:51 +05301259 HashSet<ShortcutKey> pendingShortcuts =
1260 InstallShortcutReceiver.getPendingShortcuts(context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001261 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001262 MutableInt numTimesPinned = sBgDataModel.pinnedShortcutCounts.get(key);
Sunny Goyalf75baa92016-11-22 03:23:51 +05301263 if ((numTimesPinned == null || numTimesPinned.value == 0)
1264 && !pendingShortcuts.contains(key)) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001265 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301266 shortcutManager.unpinShortcut(key);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001267 }
1268 }
1269
Jon Miranda655ec422017-02-07 11:40:22 -08001270 FolderIconPreviewVerifier verifier =
1271 new FolderIconPreviewVerifier(mApp.getInvariantDeviceProfile());
1272 // Sort the folder items and make sure all items in the preview are high resolution.
Sunny Goyale9956a72016-09-01 17:24:47 -07001273 for (FolderInfo folder : sBgDataModel.folders) {
Sunny Goyal317698b2015-07-29 11:45:41 -07001274 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
Jon Miranda655ec422017-02-07 11:40:22 -08001275 verifier.setFolderInfo(folder);
1276
1277 int numItemsInPreview = 0;
Sunny Goyal317698b2015-07-29 11:45:41 -07001278 for (ShortcutInfo info : folder.contents) {
Jon Miranda655ec422017-02-07 11:40:22 -08001279 if (info.usingLowResIcon
1280 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION
1281 && verifier.isItemInPreview(info.rank)) {
Sunny Goyal3fe4a142016-12-15 17:40:07 -08001282 mIconCache.getTitleAndIcon(info, false);
Jon Miranda655ec422017-02-07 11:40:22 -08001283 numItemsInPreview++;
Sunny Goyal317698b2015-07-29 11:45:41 -07001284 }
Jon Miranda655ec422017-02-07 11:40:22 -08001285
1286 if (numItemsInPreview >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
Sunny Goyal317698b2015-07-29 11:45:41 -07001287 break;
1288 }
1289 }
1290 }
1291
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001292 c.commitRestoredItems();
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001293 if (!isSdCardReady && !pendingPackages.isEmpty()) {
1294 context.registerReceiver(
1295 new SdCardAvailableReceiver(
1296 LauncherModel.this, mContext, pendingPackages),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001297 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001298 null,
1299 sWorker);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001300 }
1301
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001302 // Remove any empty screens
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001303 ArrayList<Long> unusedScreens = new ArrayList<>(sBgDataModel.workspaceScreens);
Sunny Goyale9956a72016-09-01 17:24:47 -07001304 for (ItemInfo item: sBgDataModel.itemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001305 long screenId = item.screenId;
1306 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1307 unusedScreens.contains(screenId)) {
1308 unusedScreens.remove(screenId);
1309 }
1310 }
1311
1312 // If there are any empty screens remove them, and update.
1313 if (unusedScreens.size() != 0) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001314 sBgDataModel.workspaceScreens.removeAll(unusedScreens);
1315 updateWorkspaceScreenOrder(context, sBgDataModel.workspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001316 }
Joe Onorato36115782010-06-17 13:28:48 -04001317 }
Sunny Goyale26d1002016-06-20 14:52:14 -07001318 if (LauncherAppState.PROFILE_STARTUP) {
1319 Trace.endSection();
1320 }
Adam Cohene25af792013-06-06 23:08:25 -07001321 }
1322
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001323 /** Filters the set of items who are directly or indirectly (via another container) on the
1324 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08001325 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001326 ArrayList<ItemInfo> allWorkspaceItems,
1327 ArrayList<ItemInfo> currentScreenItems,
1328 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001329 // Purge any null ItemInfos
1330 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
1331 while (iter.hasNext()) {
1332 ItemInfo i = iter.next();
1333 if (i == null) {
1334 iter.remove();
1335 }
1336 }
1337
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001338 // Order the set of items by their containers first, this allows use to walk through the
1339 // list sequentially, build up a list of containers that are in the specified screen,
1340 // as well as all items in those containers.
1341 Set<Long> itemsOnScreen = new HashSet<Long>();
1342 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
1343 @Override
1344 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07001345 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001346 }
1347 });
1348 for (ItemInfo info : allWorkspaceItems) {
1349 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001350 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001351 currentScreenItems.add(info);
1352 itemsOnScreen.add(info.id);
1353 } else {
1354 otherScreenItems.add(info);
1355 }
1356 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1357 currentScreenItems.add(info);
1358 itemsOnScreen.add(info.id);
1359 } else {
1360 if (itemsOnScreen.contains(info.container)) {
1361 currentScreenItems.add(info);
1362 itemsOnScreen.add(info.id);
1363 } else {
1364 otherScreenItems.add(info);
1365 }
1366 }
1367 }
1368 }
1369
1370 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08001371 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001372 ArrayList<LauncherAppWidgetInfo> appWidgets,
1373 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
1374 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001375
1376 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001377 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001378 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08001379 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001380 currentScreenWidgets.add(widget);
1381 } else {
1382 otherScreenWidgets.add(widget);
1383 }
1384 }
1385 }
1386
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001387 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
1388 * right) */
1389 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Sunny Goyal87f784c2017-01-11 10:48:34 -08001390 final InvariantDeviceProfile profile = mApp.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07001391 final int screenCols = profile.numColumns;
1392 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001393 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07001394 @Override
1395 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07001396 if (lhs.container == rhs.container) {
1397 // Within containers, order by their spatial position in that container
1398 switch ((int) lhs.container) {
1399 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
1400 long lr = (lhs.screenId * screenCellCount +
1401 lhs.cellY * screenCols + lhs.cellX);
1402 long rr = (rhs.screenId * screenCellCount +
1403 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07001404 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07001405 }
1406 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
1407 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07001408 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07001409 }
1410 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07001411 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07001412 throw new RuntimeException("Unexpected container type when " +
1413 "sorting workspace items.");
1414 }
1415 return 0;
1416 }
1417 } else {
1418 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07001419 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07001420 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001421 }
1422 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001423 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001424
Adam Cohendcd297f2013-06-18 13:13:40 -07001425 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
1426 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001427 final Runnable r = new Runnable() {
1428 @Override
1429 public void run() {
1430 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1431 if (callbacks != null) {
1432 callbacks.bindScreens(orderedScreens);
1433 }
1434 }
1435 };
Sunny Goyald33860f2015-04-23 16:02:20 -07001436 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07001437 }
1438
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001439 private void bindWorkspaceItems(final Callbacks oldCallbacks,
1440 final ArrayList<ItemInfo> workspaceItems,
1441 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07001442 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001443
1444 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07001445 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001446 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04001447 final int start = i;
1448 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001449 final Runnable r = new Runnable() {
1450 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001451 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001452 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001453 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07001454 callbacks.bindItems(workspaceItems, start, start+chunkSize,
1455 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001456 }
1457 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001458 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001459 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04001460 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001461
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001462 // Bind the widgets, one at a time
1463 N = appWidgets.size();
1464 for (int i = 0; i < N; i++) {
1465 final LauncherAppWidgetInfo widget = appWidgets.get(i);
1466 final Runnable r = new Runnable() {
1467 public void run() {
1468 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1469 if (callbacks != null) {
1470 callbacks.bindAppWidget(widget);
1471 }
1472 }
1473 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001474 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001475 }
1476 }
1477
1478 /**
1479 * Binds all loaded data to actual views on the main thread.
1480 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001481 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001482 final long t = SystemClock.uptimeMillis();
1483 Runnable r;
1484
1485 // Don't use these two variables in any of the callback runnables.
1486 // Otherwise we hold a reference to them.
1487 final Callbacks oldCallbacks = mCallbacks.get();
1488 if (oldCallbacks == null) {
1489 // This launcher has exited and nobody bothered to tell us. Just bail.
1490 Log.w(TAG, "LoaderTask running with no launcher");
1491 return;
1492 }
1493
Winson Chung9b9fb962013-11-15 15:39:34 -08001494 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07001495 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
1496 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
1497 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07001498
Sunny Goyale9956a72016-09-01 17:24:47 -07001499 synchronized (sBgDataModel) {
1500 workspaceItems.addAll(sBgDataModel.workspaceItems);
1501 appWidgets.addAll(sBgDataModel.appWidgets);
1502 orderedScreenIds.addAll(sBgDataModel.workspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001503 }
1504
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001505 final int currentScreen;
1506 {
1507 int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE
1508 ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen();
1509 if (currScreen >= orderedScreenIds.size()) {
1510 // There may be no workspace screens (just hotseat items and an empty page).
1511 currScreen = PagedView.INVALID_RESTORE_PAGE;
1512 }
1513 currentScreen = currScreen;
Winson Chung9b9fb962013-11-15 15:39:34 -08001514 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001515 final boolean validFirstPage = currentScreen >= 0;
1516 final long currentScreenId =
1517 validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID;
Winson Chung9b9fb962013-11-15 15:39:34 -08001518
Winson Chung9b9fb962013-11-15 15:39:34 -08001519 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07001520 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
1521 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
1522 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
1523 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001524
Winson Chung9b9fb962013-11-15 15:39:34 -08001525 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001526 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08001527 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001528 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001529 sortWorkspaceItemsSpatially(currentWorkspaceItems);
1530 sortWorkspaceItemsSpatially(otherWorkspaceItems);
1531
1532 // Tell the workspace that we're about to start binding items
1533 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001534 public void run() {
1535 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1536 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001537 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001538 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04001539 }
1540 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001541 };
Sunny Goyald33860f2015-04-23 16:02:20 -07001542 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001543
Adam Cohendcd297f2013-06-18 13:13:40 -07001544 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
1545
Sunny Goyal527c7d32015-08-28 15:19:36 -07001546 Executor mainExecutor = new DeferredMainThreadExecutor();
1547 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07001548 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001549
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001550 // In case of validFirstPage, only bind the first screen, and defer binding the
1551 // remaining screens after first onDraw (and an optional the fade animation whichever
1552 // happens later).
1553 // This ensures that the first screen is immediately visible (eg. during rotation)
1554 // In case of !validFirstPage, bind all pages one after other.
1555 final Executor deferredExecutor =
1556 validFirstPage ? new ViewOnDrawExecutor(mHandler) : mainExecutor;
1557
1558 mainExecutor.execute(new Runnable() {
1559 @Override
1560 public void run() {
1561 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1562 if (callbacks != null) {
1563 callbacks.finishFirstPageBind(
1564 validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null);
1565 }
1566 }
1567 });
Sunny Goyal527c7d32015-08-28 15:19:36 -07001568
Sunny Goyal44c06432016-04-02 10:56:02 -07001569 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001570
1571 // Tell the workspace that we're done binding items
1572 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001573 public void run() {
1574 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1575 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001576 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04001577 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001578
Sunny Goyal639e9062015-08-19 19:17:06 -07001579 mIsLoadingAndBindingWorkspace = false;
1580
1581 // Run all the bind complete runnables after workspace is bound.
1582 if (!mBindCompleteRunnables.isEmpty()) {
1583 synchronized (mBindCompleteRunnables) {
1584 for (final Runnable r : mBindCompleteRunnables) {
1585 runOnWorkerThread(r);
1586 }
1587 mBindCompleteRunnables.clear();
1588 }
1589 }
1590
Winson Chung98e030b2012-05-07 16:01:11 -07001591 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04001592 if (DEBUG_LOADERS) {
1593 Log.d(TAG, "bound workspace in "
1594 + (SystemClock.uptimeMillis()-t) + "ms");
1595 }
Winson Chung36a62fe2012-05-06 18:04:42 -07001596
Joe Onorato36115782010-06-17 13:28:48 -04001597 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001598 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001599 deferredExecutor.execute(r);
1600
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001601 if (validFirstPage) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001602 r = new Runnable() {
1603 public void run() {
1604 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1605 if (callbacks != null) {
1606 // We are loading synchronously, which means, some of the pages will be
1607 // bound after first draw. Inform the callbacks that page binding is
1608 // not complete, and schedule the remaining pages.
1609 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
1610 callbacks.onPageBoundSynchronously(currentScreen);
1611 }
1612 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
1613 }
1614 }
1615 };
Sunny Goyald33860f2015-04-23 16:02:20 -07001616 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07001617 }
Joe Onorato36115782010-06-17 13:28:48 -04001618 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001619
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001620 private void updateIconCache() {
1621 // Ignore packages which have a promise icon.
1622 HashSet<String> packagesToIgnore = new HashSet<>();
Sunny Goyale9956a72016-09-01 17:24:47 -07001623 synchronized (sBgDataModel) {
1624 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001625 if (info instanceof ShortcutInfo) {
1626 ShortcutInfo si = (ShortcutInfo) info;
1627 if (si.isPromise() && si.getTargetComponent() != null) {
1628 packagesToIgnore.add(si.getTargetComponent().getPackageName());
1629 }
1630 } else if (info instanceof LauncherAppWidgetInfo) {
1631 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
1632 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
1633 packagesToIgnore.add(lawi.providerName.getPackageName());
1634 }
1635 }
1636 }
1637 }
1638 mIconCache.updateDbIcons(packagesToIgnore);
1639 }
1640
Joe Onorato36115782010-06-17 13:28:48 -04001641 private void onlyBindAllApps() {
1642 final Callbacks oldCallbacks = mCallbacks.get();
1643 if (oldCallbacks == null) {
1644 // This launcher has exited and nobody bothered to tell us. Just bail.
1645 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1646 return;
1647 }
1648
1649 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07001650 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001651 final ArrayList<AppInfo> list
1652 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07001653 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001654 public void run() {
1655 final long t = SystemClock.uptimeMillis();
1656 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1657 if (callbacks != null) {
1658 callbacks.bindAllApplications(list);
1659 }
1660 if (DEBUG_LOADERS) {
1661 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08001662 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04001663 }
1664 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07001665 };
Tony Wickham80f57872016-06-29 18:12:15 -07001666 runOnMainThread(r);
Joe Onorato36115782010-06-17 13:28:48 -04001667 }
1668
Winson Chung64359a52013-07-08 17:17:08 -07001669 private void loadAllApps() {
1670 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04001671
Joe Onorato36115782010-06-17 13:28:48 -04001672 final Callbacks oldCallbacks = mCallbacks.get();
1673 if (oldCallbacks == null) {
1674 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07001675 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04001676 return;
1677 }
1678
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001679 final List<UserHandle> profiles = mUserManager.getUserProfiles();
Kenny Guyed131872014-04-30 03:02:21 +01001680
Winson Chung64359a52013-07-08 17:17:08 -07001681 // Clear the list of apps
1682 mBgAllAppsList.clear();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001683 for (UserHandle user : profiles) {
Kenny Guyed131872014-04-30 03:02:21 +01001684 // Query for the set of apps
1685 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal3e9be432017-01-05 15:22:41 -08001686 final List<LauncherActivityInfo> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01001687 if (DEBUG_LOADERS) {
1688 Log.d(TAG, "getActivityList took "
1689 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
1690 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
1691 }
1692 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08001693 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01001694 if (apps == null || apps.isEmpty()) {
1695 return;
1696 }
Kenny Guyff05f432016-01-22 17:48:29 +00001697 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01001698 // Create the ApplicationInfos
1699 for (int i = 0; i < apps.size(); i++) {
Sunny Goyal3e9be432017-01-05 15:22:41 -08001700 LauncherActivityInfo app = apps.get(i);
Kenny Guyed131872014-04-30 03:02:21 +01001701 // This builds the icon bitmaps.
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001702 mBgAllAppsList.add(new AppInfo(mContext, app, user, quietMode), app);
Kenny Guyed131872014-04-30 03:02:21 +01001703 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08001704
Sunny Goyal756a28a2015-04-23 17:07:55 -07001705 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
1706 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07001707 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07001708
1709 @Override
1710 public void run() {
1711 heuristic.processUserApps(apps);
1712 }
Sunny Goyal639e9062015-08-19 19:17:06 -07001713 };
1714 runOnMainThread(new Runnable() {
1715
1716 @Override
1717 public void run() {
1718 // Check isLoadingWorkspace on the UI thread, as it is updated on
1719 // the UI thread.
1720 if (mIsLoadingAndBindingWorkspace) {
1721 synchronized (mBindCompleteRunnables) {
1722 mBindCompleteRunnables.add(r);
1723 }
1724 } else {
1725 runOnWorkerThread(r);
1726 }
1727 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07001728 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08001729 }
Winson Chung64359a52013-07-08 17:17:08 -07001730 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01001731 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001732 final ArrayList<AppInfo> added = mBgAllAppsList.added;
1733 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07001734
1735 // Post callback on main thread
1736 mHandler.post(new Runnable() {
1737 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07001738
Winson Chung64359a52013-07-08 17:17:08 -07001739 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07001740 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07001741 if (callbacks != null) {
1742 callbacks.bindAllApplications(added);
1743 if (DEBUG_LOADERS) {
1744 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001745 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07001746 }
1747 } else {
1748 Log.i(TAG, "not binding apps: no Launcher activity");
1749 }
1750 }
1751 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07001752 // Cleanup any data stored for a deleted user.
1753 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04001754 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07001755 Log.d(TAG, "Icons processed in "
1756 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08001757 }
1758 }
1759
Sunny Goyaldd96a5e2017-02-17 11:22:34 -08001760 private void loadDeepShortcuts() {
1761 if (!mModelLoaded) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07001762 sBgDataModel.deepShortcutMap.clear();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301763 DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(mContext);
1764 mHasShortcutHostPermission = shortcutManager.hasHostPermission();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001765 if (mHasShortcutHostPermission) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001766 for (UserHandle user : mUserManager.getUserProfiles()) {
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001767 if (mUserManager.isUserUnlocked(user)) {
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301768 List<ShortcutInfoCompat> shortcuts =
1769 shortcutManager.queryForAllShortcuts(user);
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07001770 sBgDataModel.updateDeepShortcutMap(null, user, shortcuts);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001771 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001772 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001773 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001774 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001775 }
Joe Onorato36115782010-06-17 13:28:48 -04001776 }
1777
Tony Wickham80f57872016-06-29 18:12:15 -07001778 public void bindDeepShortcuts() {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07001779 final MultiHashMap<ComponentKey, String> shortcutMapCopy =
1780 sBgDataModel.deepShortcutMap.clone();
Tony Wickham80f57872016-06-29 18:12:15 -07001781 Runnable r = new Runnable() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001782 @Override
1783 public void run() {
1784 Callbacks callbacks = getCallback();
1785 if (callbacks != null) {
1786 callbacks.bindDeepShortcutMap(shortcutMapCopy);
1787 }
1788 }
Tony Wickham80f57872016-06-29 18:12:15 -07001789 };
1790 runOnMainThread(r);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001791 }
1792
Sunny Goyal75b0f552015-05-20 21:57:06 -07001793 /**
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001794 * Refreshes the cached shortcuts if the shortcut permission has changed.
1795 * Current implementation simply reloads the workspace, but it can be optimized to
1796 * use partial updates similar to {@link UserManagerCompat}
1797 */
1798 public void refreshShortcutsIfRequired() {
Sunny Goyalf5e37442016-11-02 10:31:24 -07001799 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001800 sWorker.removeCallbacks(mShortcutPermissionCheckRunnable);
1801 sWorker.post(mShortcutPermissionCheckRunnable);
1802 }
1803 }
1804
1805 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -07001806 * Called when the icons for packages have been updated in the icon cache.
1807 */
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001808 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandle user) {
Sunny Goyal75b0f552015-05-20 21:57:06 -07001809 // If any package icon has changed (app was updated while launcher was dead),
1810 // update the corresponding shortcuts.
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001811 enqueueModelUpdateTask(new CacheDataUpdatedTask(
1812 CacheDataUpdatedTask.OP_CACHE_UPDATE, user, updatedPackages));
Sunny Goyal75b0f552015-05-20 21:57:06 -07001813 }
1814
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001815 void enqueueModelUpdateTask(BaseModelUpdateTask task) {
Jon Mirandaf28629a2017-02-23 15:12:42 -08001816 if (!mModelLoaded && mLoaderTask == null) {
1817 if (DEBUG_LOADERS) {
1818 Log.d(TAG, "enqueueModelUpdateTask Ignoring task since loader is pending=" + task);
1819 }
1820 return;
1821 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001822 task.init(this);
1823 runOnWorkerThread(task);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001824 }
1825
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001826 /**
1827 * A task to be executed on the current callbacks on the UI thread.
1828 * If there is no current callbacks, the task is ignored.
1829 */
1830 public interface CallbackTask {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001831
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001832 void execute(Callbacks callbacks);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001833 }
1834
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001835 /**
1836 * A runnable which changes/updates the data model of the launcher based on certain events.
1837 */
1838 public static abstract class BaseModelUpdateTask implements Runnable {
Joe Onorato36115782010-06-17 13:28:48 -04001839
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001840 private LauncherModel mModel;
1841 private DeferredHandler mUiHandler;
Joe Onorato36115782010-06-17 13:28:48 -04001842
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001843 /* package private */
1844 void init(LauncherModel model) {
1845 mModel = model;
1846 mUiHandler = mModel.mHandler;
Joe Onorato36115782010-06-17 13:28:48 -04001847 }
1848
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001849 @Override
Joe Onorato36115782010-06-17 13:28:48 -04001850 public void run() {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001851 if (!mModel.mHasLoaderCompletedOnce) {
Sunny Goyalc905efc2015-05-06 09:54:53 -07001852 // Loader has not yet run.
1853 return;
1854 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001855 execute(mModel.mApp, sBgDataModel, mModel.mBgAllAppsList);
1856 }
Joe Onorato36115782010-06-17 13:28:48 -04001857
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001858 /**
1859 * Execute the actual task. Called on the worker thread.
1860 */
1861 public abstract void execute(
1862 LauncherAppState app, BgDataModel dataModel, AllAppsList apps);
Sunny Goyale0f58d72014-11-10 18:05:31 -08001863
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001864 /**
1865 * Schedules a {@param task} to be executed on the current callbacks.
1866 */
1867 public final void scheduleCallbackTask(final CallbackTask task) {
1868 final Callbacks callbacks = mModel.getCallback();
1869 mUiHandler.post(new Runnable() {
1870 public void run() {
1871 Callbacks cb = mModel.getCallback();
1872 if (callbacks == cb && cb != null) {
1873 task.execute(callbacks);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07001874 }
1875 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001876 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001877 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001878
1879 public ModelWriter getModelWriter() {
1880 // Updates from model task, do not deal with icon position in hotseat.
1881 return mModel.getWriter(false /* hasVerticalHotseat */);
1882 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001883 }
1884
Sunny Goyal1b072632017-01-18 11:30:23 -08001885 public void updateAndBindShortcutInfo(final ShortcutInfo si, final ShortcutInfoCompat info) {
1886 updateAndBindShortcutInfo(new Provider<ShortcutInfo>() {
1887 @Override
1888 public ShortcutInfo get() {
1889 si.updateFromDeepShortcutInfo(info, mApp.getContext());
1890 si.iconBitmap = LauncherIcons.createShortcutIcon(info, mApp.getContext());
1891 return si;
1892 }
1893 });
1894 }
1895
Sunny Goyal10923b32016-07-20 15:42:44 -07001896 /**
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001897 * Utility method to update a shortcut on the background thread.
Sunny Goyal10923b32016-07-20 15:42:44 -07001898 */
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001899 public void updateAndBindShortcutInfo(final Provider<ShortcutInfo> shortcutProvider) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001900 enqueueModelUpdateTask(new ExtendedModelTask() {
Sunny Goyal10923b32016-07-20 15:42:44 -07001901 @Override
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001902 public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001903 ShortcutInfo info = shortcutProvider.get();
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001904 ArrayList<ShortcutInfo> update = new ArrayList<>();
Sunny Goyal10923b32016-07-20 15:42:44 -07001905 update.add(info);
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001906 bindUpdatedShortcuts(update, info.user);
Sunny Goyal10923b32016-07-20 15:42:44 -07001907 }
1908 });
1909 }
1910
Sunny Goyald164b7f2016-10-12 20:49:31 -07001911 private void bindWidgetsModel(final Callbacks callbacks) {
1912 final MultiHashMap<PackageItemInfo, WidgetItem> widgets
1913 = mBgWidgetsModel.getWidgetsMap().clone();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001914 mHandler.post(new Runnable() {
1915 @Override
1916 public void run() {
1917 Callbacks cb = getCallback();
1918 if (callbacks == cb && cb != null) {
Sunny Goyald164b7f2016-10-12 20:49:31 -07001919 callbacks.bindAllWidgets(widgets);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001920 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07001921 }
1922 });
1923 }
1924
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001925 public void refreshAndBindWidgetsAndShortcuts(
1926 final Callbacks callbacks, final boolean bindFirst) {
1927 runOnWorkerThread(new Runnable() {
1928 @Override
1929 public void run() {
1930 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
Sunny Goyald164b7f2016-10-12 20:49:31 -07001931 bindWidgetsModel(callbacks);
Sunny Goyal31860582015-09-18 08:38:57 -07001932 }
Sunny Goyald164b7f2016-10-12 20:49:31 -07001933 ArrayList<WidgetItem> allWidgets = mBgWidgetsModel.update(mApp.getContext());
1934 bindWidgetsModel(callbacks);
1935
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001936 // update the Widget entries inside DB on the worker thread.
Sunny Goyal87f784c2017-01-11 10:48:34 -08001937 mApp.getWidgetCache().removeObsoletePreviews(allWidgets);
Sunny Goyal31860582015-09-18 08:38:57 -07001938 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001939 });
Michael Jurkac402cd92013-05-20 15:49:32 +02001940 }
1941
Sunny Goyal651077b2014-06-30 14:15:31 -07001942 static boolean isValidProvider(AppWidgetProviderInfo provider) {
1943 return (provider != null) && (provider.provider != null)
1944 && (provider.provider.getPackageName() != null);
1945 }
1946
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001947 public void dumpState(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
1948 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
1949 writer.println(prefix + "All apps list: size=" + mBgAllAppsList.data.size());
1950 for (AppInfo info : mBgAllAppsList.data) {
1951 writer.println(prefix + " title=\"" + info.title + "\" iconBitmap=" + info.iconBitmap
1952 + " componentName=" + info.componentName.getPackageName());
1953 }
Joe Onorato36115782010-06-17 13:28:48 -04001954 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001955 sBgDataModel.dump(prefix, fd, writer, args);
Joe Onoratobe386092009-11-17 17:32:16 -08001956 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08001957
1958 public Callbacks getCallback() {
1959 return mCallbacks != null ? mCallbacks.get() : null;
1960 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07001961
1962 /**
1963 * @return {@link FolderInfo} if its already loaded.
1964 */
1965 public FolderInfo findFolderById(Long folderId) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001966 synchronized (sBgDataModel) {
1967 return sBgDataModel.folders.get(folderId);
Sunny Goyal18bf8e22015-04-08 18:13:46 -07001968 }
1969 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07001970
Sunny Goyal527c7d32015-08-28 15:19:36 -07001971 @Thunk class DeferredMainThreadExecutor implements Executor {
1972
1973 @Override
1974 public void execute(Runnable command) {
1975 runOnMainThread(command);
1976 }
1977 }
1978
Sunny Goyal756adbc2015-04-16 15:20:51 -07001979 /**
1980 * @return the looper for the worker thread which can be used to start background tasks.
1981 */
1982 public static Looper getWorkerLooper() {
1983 return sWorkerThread.getLooper();
1984 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985}