blob: 7540dac4620a23f1db46bfca644e8fb138601b3c [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.content.pm.PackageManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.net.Uri;
Joe Onorato36115782010-06-17 13:28:48 -040031import android.os.Handler;
32import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070033import android.os.Looper;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040035import android.os.SystemClock;
Sunny Goyale26d1002016-06-20 14:52:14 -070036import android.os.Trace;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080037import android.os.UserHandle;
Winson Chunga90303b2013-11-15 13:05:06 -080038import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070039import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080040import android.util.LongSparseArray;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070041import android.util.MutableInt;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010042
Sunny Goyalffe83f12014-08-14 17:39:34 -070043import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010044import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070045import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070046import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010047import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyal6c56c682015-07-16 14:09:05 -070048import com.android.launcher3.config.ProviderConfig;
Tony Wickham827cef22016-03-17 15:39:39 -070049import com.android.launcher3.dynamicui.ExtractionUtils;
Sunny Goyal26119432016-02-18 22:09:23 +000050import com.android.launcher3.folder.Folder;
51import com.android.launcher3.folder.FolderIcon;
Sunny Goyal1b072632017-01-18 11:30:23 -080052import com.android.launcher3.graphics.LauncherIcons;
Sunny Goyal1acb9e92016-05-16 12:41:09 -070053import com.android.launcher3.logging.FileLog;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070054import com.android.launcher3.model.AddWorkspaceItemsTask;
Sunny Goyale9956a72016-09-01 17:24:47 -070055import com.android.launcher3.model.BgDataModel;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070056import com.android.launcher3.model.CacheDataUpdatedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080057import com.android.launcher3.model.ExtendedModelTask;
Sunny Goyalf862a262015-12-14 14:27:38 -080058import com.android.launcher3.model.GridSizeMigrationTask;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -080059import com.android.launcher3.model.LoaderCursor;
Sunny Goyal43bf11d2017-02-02 13:52:53 -080060import com.android.launcher3.model.ModelWriter;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070061import com.android.launcher3.model.PackageInstallStateChangedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080062import com.android.launcher3.model.PackageItemInfo;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070063import com.android.launcher3.model.PackageUpdatedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080064import com.android.launcher3.model.SdCardAvailableReceiver;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070065import com.android.launcher3.model.ShortcutsChangedTask;
66import com.android.launcher3.model.UserLockStateChangedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080067import com.android.launcher3.model.WidgetItem;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070068import com.android.launcher3.model.WidgetsModel;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -070069import com.android.launcher3.provider.ImportDataTask;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070070import com.android.launcher3.provider.LauncherDbUtils;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070071import com.android.launcher3.shortcuts.DeepShortcutManager;
72import com.android.launcher3.shortcuts.ShortcutInfoCompat;
73import com.android.launcher3.shortcuts.ShortcutKey;
Robin Lee26ace122015-03-16 19:41:43 +000074import com.android.launcher3.util.ComponentKey;
Sunny Goyal32f3dda2016-11-11 11:45:00 -080075import com.android.launcher3.util.ContentWriter;
Sunny Goyal40452cf2016-09-01 15:17:46 -070076import com.android.launcher3.util.ItemInfoMatcher;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070077import com.android.launcher3.util.ManagedProfileHeuristic;
Tony Wickhamd82a39d2016-07-01 15:44:13 -070078import com.android.launcher3.util.MultiHashMap;
Sunny Goyald09c3702016-04-06 16:18:20 -070079import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070080import com.android.launcher3.util.Preconditions;
Sunny Goyal2bcbe132016-11-16 09:23:42 -080081import com.android.launcher3.util.Provider;
Adam Cohen091440a2015-03-18 14:16:05 -070082import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070083import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080084
Michael Jurkac2f801e2011-07-12 14:19:46 -070085import java.lang.ref.WeakReference;
86import java.net.URISyntaxException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070087import java.util.ArrayList;
88import java.util.Collections;
89import java.util.Comparator;
90import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070091import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070092import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070093import java.util.List;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070094import java.util.Map;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070095import java.util.Set;
Sunny 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
Joe Onoratocc67f472010-06-08 10:54:30 -0700126 // We start off with everything not loaded. After that, we assume that
127 // our monitoring of the package manager provides all updates and we never
128 // need to do a requery. These are only ever touched from the loader thread.
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700129 private boolean mWorkspaceLoaded;
130 private boolean mAllAppsLoaded;
131 private boolean mDeepShortcutsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700132
Sunny Goyal756a28a2015-04-23 17:07:55 -0700133 /**
134 * Set of runnables to be called on the background thread after the workspace binding
135 * is complete.
136 */
137 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
138
Adam Cohen091440a2015-03-18 14:16:05 -0700139 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700141 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800142 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700143 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800144 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800145
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700146 private boolean mHasShortcutHostPermission;
147 // Runnable to check if the shortcuts permission has changed.
148 private final Runnable mShortcutPermissionCheckRunnable = new Runnable() {
149 @Override
150 public void run() {
151 if (mDeepShortcutsLoaded) {
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530152 boolean hasShortcutHostPermission =
153 DeepShortcutManager.getInstance(mApp.getContext()).hasHostPermission();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700154 if (hasShortcutHostPermission != mHasShortcutHostPermission) {
155 mApp.reloadWorkspace();
156 }
157 }
158 }
159 };
160
Sunny Goyale9956a72016-09-01 17:24:47 -0700161 /**
162 * All the static data should be accessed on the background thread, A lock should be acquired
163 * on this object when accessing any data from this model.
164 */
165 static final BgDataModel sBgDataModel = new BgDataModel();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700166
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700167 // </ only access in worker thread >
168
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700169 private final IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700171 private final LauncherAppsCompat mLauncherApps;
172 private final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100173
Joe Onorato9c1289c2009-08-17 11:03:03 -0400174 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700175 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400176 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700177 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400178 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700179 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
180 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700181 public void bindScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700182 public void finishFirstPageBind(ViewOnDrawExecutor executor);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800183 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400184 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200185 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700186 public void bindAppsAdded(ArrayList<Long> newScreens,
187 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700188 ArrayList<ItemInfo> addAnimated,
189 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200190 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700191 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800192 ArrayList<ShortcutInfo> removed, UserHandle user);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700193 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700194 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700195 public void bindWorkspaceComponentsRemoved(
196 HashSet<String> packageNames, HashSet<ComponentName> components,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800197 UserHandle user);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700198 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800199 public void notifyWidgetProvidersChanged();
Sunny Goyald164b7f2016-10-12 20:49:31 -0700200 public void bindAllWidgets(MultiHashMap<PackageItemInfo, WidgetItem> widgets);
Adam Cohen1462de32012-07-24 22:34:36 -0700201 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700202 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700203 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMap);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400204 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530206 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800207 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400208 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100209 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Sunny Goyald164b7f2016-10-12 20:49:31 -0700210 mBgWidgetsModel = new WidgetsModel(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800211 mIconCache = iconCache;
212
Kenny Guyed131872014-04-30 03:02:21 +0100213 mLauncherApps = LauncherAppsCompat.getInstance(context);
214 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800215 }
216
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700217 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
218 * posted on the main thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700219 private void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700220 if (sWorkerThread.getThreadId() == Process.myTid()) {
221 // If we are on the worker thread, post onto the main handler
222 mHandler.post(r);
223 } else {
224 r.run();
225 }
226 }
227
228 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
229 * posted on the worker thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700230 private static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700231 if (sWorkerThread.getThreadId() == Process.myTid()) {
232 r.run();
233 } else {
234 // If we are not on the worker thread, then post to the worker handler
235 sWorker.post(r);
236 }
237 }
238
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700239 public void setPackageState(PackageInstallInfo installInfo) {
240 enqueueModelUpdateTask(new PackageInstallStateChangedTask(installInfo));
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500241 }
242
Sunny Goyal756adbc2015-04-16 15:20:51 -0700243 /**
244 * Updates the icons and label of all pending icons for the provided package name.
245 */
246 public void updateSessionDisplayInfo(final String packageName) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700247 HashSet<String> packages = new HashSet<>();
248 packages.add(packageName);
249 enqueueModelUpdateTask(new CacheDataUpdatedTask(
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800250 CacheDataUpdatedTask.OP_SESSION_UPDATE, Process.myUserHandle(), packages));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800251 }
252
253 /**
254 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800255 */
Sunny Goyal2bcbe132016-11-16 09:23:42 -0800256 public void addAndBindAddedWorkspaceItems(List<ItemInfo> workspaceApps) {
257 addAndBindAddedWorkspaceItems(Provider.of(workspaceApps));
258 }
259
260 /**
261 * Adds the provided items to the workspace.
262 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700263 public void addAndBindAddedWorkspaceItems(
Sunny Goyal2bcbe132016-11-16 09:23:42 -0800264 Provider<List<ItemInfo>> appsProvider) {
265 enqueueModelUpdateTask(new AddWorkspaceItemsTask(appsProvider));
Winson Chung64359a52013-07-08 17:17:08 -0700266 }
267
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800268 public ModelWriter getWriter(boolean hasVerticalHotseat) {
269 return new ModelWriter(mApp.getContext(), sBgDataModel, hasVerticalHotseat);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800270 }
271
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700272 static void checkItemInfoLocked(
273 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
Sunny Goyale9956a72016-09-01 17:24:47 -0700274 ItemInfo modelItem = sBgDataModel.itemsIdMap.get(itemId);
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700275 if (modelItem != null && item != modelItem) {
276 // check all the data is consistent
277 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
278 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
279 ShortcutInfo shortcut = (ShortcutInfo) item;
280 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
281 modelShortcut.intent.filterEquals(shortcut.intent) &&
282 modelShortcut.id == shortcut.id &&
283 modelShortcut.itemType == shortcut.itemType &&
284 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700285 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700286 modelShortcut.cellX == shortcut.cellX &&
287 modelShortcut.cellY == shortcut.cellY &&
288 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700289 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700290 // For all intents and purposes, this is the same object
291 return;
292 }
293 }
294
295 // the modelItem needs to match up perfectly with item if our model is
296 // to be consistent with the database-- for now, just require
297 // modelItem == item or the equality check above
298 String msg = "item: " + ((item != null) ? item.toString() : "null") +
299 "modelItem: " +
300 ((modelItem != null) ? modelItem.toString() : "null") +
301 "Error: ItemInfo passed to checkItemInfo doesn't match original";
302 RuntimeException e = new RuntimeException(msg);
303 if (stackTrace != null) {
304 e.setStackTrace(stackTrace);
305 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800306 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700307 }
308 }
309
Michael Jurka816474f2012-06-25 14:49:02 -0700310 static void checkItemInfo(final ItemInfo item) {
311 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
312 final long itemId = item.id;
313 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700314 public void run() {
Sunny Goyale9956a72016-09-01 17:24:47 -0700315 synchronized (sBgDataModel) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700316 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700317 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700318 }
319 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700320 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700321 }
322
Joe Onorato9c1289c2009-08-17 11:03:03 -0400323 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700324 * Update the order of the workspace screens in the database. The array list contains
325 * a list of screen ids in the order that they should appear.
326 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700327 public static void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -0700328 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700329 final ContentResolver cr = context.getContentResolver();
330 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
331
332 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700333 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700334 while (iter.hasNext()) {
335 long id = iter.next();
336 if (id < 0) {
337 iter.remove();
338 }
339 }
340
341 Runnable r = new Runnable() {
342 @Override
343 public void run() {
Yura085c8532014-02-11 15:15:29 +0000344 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700345 // Clear the table
Yura085c8532014-02-11 15:15:29 +0000346 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -0700347 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700348 for (int i = 0; i < count; i++) {
349 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700350 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700351 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
352 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +0000353 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -0700354 }
Yura085c8532014-02-11 15:15:29 +0000355
356 try {
357 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
358 } catch (Exception ex) {
359 throw new RuntimeException(ex);
360 }
Winson Chung9e6a0a22013-08-27 11:58:12 -0700361
Sunny Goyale9956a72016-09-01 17:24:47 -0700362 synchronized (sBgDataModel) {
363 sBgDataModel.workspaceScreens.clear();
364 sBgDataModel.workspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -0700365 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700366 }
367 };
368 runOnWorkerThread(r);
369 }
370
371 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400372 * Set this as the current Launcher activity object for the loader.
373 */
374 public void initialize(Callbacks callbacks) {
375 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -0700376 Preconditions.assertUIThread();
377 // Remove any queued UI runnables
378 mHandler.cancelAll();
379 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400380 }
381 }
382
Kenny Guyed131872014-04-30 03:02:21 +0100383 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800384 public void onPackageChanged(String packageName, UserHandle user) {
Kenny Guyed131872014-04-30 03:02:21 +0100385 int op = PackageUpdatedTask.OP_UPDATE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700386 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100387 }
388
389 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800390 public void onPackageRemoved(String packageName, UserHandle user) {
Sunny Goyalc2936bc2016-09-01 15:50:36 -0700391 onPackagesRemoved(user, packageName);
392 }
393
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800394 public void onPackagesRemoved(UserHandle user, String... packages) {
Kenny Guyed131872014-04-30 03:02:21 +0100395 int op = PackageUpdatedTask.OP_REMOVE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700396 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packages));
Kenny Guyed131872014-04-30 03:02:21 +0100397 }
398
399 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800400 public void onPackageAdded(String packageName, UserHandle user) {
Kenny Guyed131872014-04-30 03:02:21 +0100401 int op = PackageUpdatedTask.OP_ADD;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700402 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100403 }
404
405 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800406 public void onPackagesAvailable(String[] packageNames, UserHandle user,
Kenny Guyed131872014-04-30 03:02:21 +0100407 boolean replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700408 enqueueModelUpdateTask(
409 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100410 }
411
412 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800413 public void onPackagesUnavailable(String[] packageNames, UserHandle user,
Kenny Guyed131872014-04-30 03:02:21 +0100414 boolean replacing) {
415 if (!replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700416 enqueueModelUpdateTask(new PackageUpdatedTask(
417 PackageUpdatedTask.OP_UNAVAILABLE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100418 }
Kenny Guyed131872014-04-30 03:02:21 +0100419 }
420
Kenny Guy44cba692016-01-21 19:50:02 +0000421 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800422 public void onPackagesSuspended(String[] packageNames, UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700423 enqueueModelUpdateTask(new PackageUpdatedTask(
424 PackageUpdatedTask.OP_SUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000425 }
426
427 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800428 public void onPackagesUnsuspended(String[] packageNames, UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700429 enqueueModelUpdateTask(new PackageUpdatedTask(
430 PackageUpdatedTask.OP_UNSUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000431 }
432
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700433 @Override
434 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800435 UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700436 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, true));
Sunny Goyal50941fb2016-08-04 12:03:52 -0700437 }
438
439 public void updatePinnedShortcuts(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800440 UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700441 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, false));
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700442 }
443
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700444 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400445 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
446 * ACTION_PACKAGE_CHANGED.
447 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100448 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400449 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -0400450 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700451
Joe Onorato36115782010-06-17 13:28:48 -0400452 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +0100453 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -0700454 // If we have changed locale we need to clear out the labels in all apps/workspace.
455 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700456 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
457 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -0700458 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -0700459 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700460 } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
461 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
462 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800463 UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER);
Sunny Goyalda891c12016-03-18 18:29:24 -0700464 if (user != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700465 if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
466 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700467 enqueueModelUpdateTask(new PackageUpdatedTask(
468 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE, user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700469 }
470
471 // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so
472 // we need to run the state change task again.
473 if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
474 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700475 enqueueModelUpdateTask(new UserLockStateChangedTask(user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700476 }
Sunny Goyalda891c12016-03-18 18:29:24 -0700477 }
Tony Wickham827cef22016-03-17 15:39:39 -0700478 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
479 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700480 }
481 }
482
Amith Yamasani6cc806d2014-05-02 13:47:11 -0700483 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700484 resetLoadedState(true, true);
485
Reena Lee93f824a2011-09-23 17:20:28 -0700486 // Do this here because if the launcher activity is running it will be restarted.
487 // If it's not running startLoaderFromBackground will merely tell it that it needs
488 // to reload.
489 startLoaderFromBackground();
490 }
491
Winson Chungf0c6ae02012-03-21 16:10:31 -0700492 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
493 synchronized (mLock) {
494 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
495 // mWorkspaceLoaded to true later
496 stopLoaderLocked();
497 if (resetAllAppsLoaded) mAllAppsLoaded = false;
498 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700499 // Always reset deep shortcuts loaded.
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700500 // TODO: why?
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700501 mDeepShortcutsLoaded = false;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700502 }
503 }
504
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700505 /**
506 * When the launcher is in the background, it's possible for it to miss paired
507 * configuration changes. So whenever we trigger the loader from the background
508 * tell the launcher that it needs to re-run the loader when it comes back instead
509 * of doing it now.
510 */
511 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800512 Callbacks callbacks = getCallback();
513 if (callbacks != null) {
514 // Only actually run the loader if they're not paused.
515 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -0700516 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700517 }
518 }
Joe Onorato36115782010-06-17 13:28:48 -0400519 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400520
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700521 /**
522 * If there is already a loader task running, tell it to stop.
523 */
524 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -0700525 LoaderTask oldTask = mLoaderTask;
526 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -0700527 oldTask.stopLocked();
528 }
Reena Lee93f824a2011-09-23 17:20:28 -0700529 }
530
Adam Cohen1a85c582014-09-30 09:48:49 -0700531 public boolean isCurrentCallbacks(Callbacks callbacks) {
532 return (mCallbacks != null && mCallbacks.get() == callbacks);
533 }
534
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700535 /**
536 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
537 * @return true if the page could be bound synchronously.
538 */
539 public boolean startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700540 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
541 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -0400542 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -0400543 // Don't bother to start the thread if we know it's not going to do anything
544 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -0700545 final Callbacks oldCallbacks = mCallbacks.get();
546 // Clear any pending bind-runnables from the synchronized load process.
547 runOnMainThread(new Runnable() {
548 public void run() {
549 oldCallbacks.clearPendingBinds();
550 }
551 });
552
Joe Onorato36115782010-06-17 13:28:48 -0400553 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700554 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700555 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700556 // TODO: mDeepShortcutsLoaded does not need to be true for synchronous bind.
Tony Wickham80f57872016-06-29 18:12:15 -0700557 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE && mAllAppsLoaded
558 && mWorkspaceLoaded && mDeepShortcutsLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700559 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700560 return true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700561 } else {
562 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
563 sWorker.post(mLoaderTask);
564 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400565 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400566 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700567 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400568 }
569
Joe Onorato36115782010-06-17 13:28:48 -0400570 public void stopLoader() {
571 synchronized (mLock) {
572 if (mLoaderTask != null) {
573 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400574 }
575 }
Joe Onorato36115782010-06-17 13:28:48 -0400576 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400577
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800578 /**
579 * Loads the workspace screen ids in an ordered list.
580 */
Sunny Goyale5bb7052015-07-27 14:36:07 -0700581 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -0700582 final ContentResolver contentResolver = context.getContentResolver();
583 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -0700584
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800585 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -0700586 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
587 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -0700588 }
589
Joe Onorato36115782010-06-17 13:28:48 -0400590 /**
591 * Runnable for the thread that loads the contents of the launcher:
592 * - workspace icons
593 * - widgets
594 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700595 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -0400596 */
597 private class LoaderTask implements Runnable {
598 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -0700599 private int mPageToBindFirst;
600
Adam Cohen091440a2015-03-18 14:16:05 -0700601 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -0400602 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -0700603 @Thunk boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700604
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700605 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -0400606 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -0700607 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400608 }
609
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800610 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700611 mIsLoadingAndBindingWorkspace = true;
612
Joe Onorato36115782010-06-17 13:28:48 -0400613 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -0400614 if (DEBUG_LOADERS) {
615 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400616 }
Michael Jurka288a36b2011-07-12 16:53:48 -0700617
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700618 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800619 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -0700620 synchronized (LoaderTask.this) {
621 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800622 return;
Reena Lee93f824a2011-09-23 17:20:28 -0700623 }
624 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400625 }
626 }
627
Joe Onorato36115782010-06-17 13:28:48 -0400628 // Bind the workspace
Sunny Goyal93f878c2016-03-30 17:31:24 -0700629 bindWorkspace(mPageToBindFirst);
Joe Onorato36115782010-06-17 13:28:48 -0400630 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400631
Joe Onorato36115782010-06-17 13:28:48 -0400632 private void waitForIdle() {
633 // Wait until the either we're stopped or the other threads are done.
634 // This way we don't start loading all apps until the workspace has settled
635 // down.
636 synchronized (LoaderTask.this) {
637 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700638
Joe Onorato36115782010-06-17 13:28:48 -0400639 mHandler.postIdle(new Runnable() {
640 public void run() {
641 synchronized (LoaderTask.this) {
642 mLoadAndBindStepFinished = true;
643 if (DEBUG_LOADERS) {
644 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400645 }
Joe Onorato36115782010-06-17 13:28:48 -0400646 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400647 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400648 }
Joe Onorato36115782010-06-17 13:28:48 -0400649 });
650
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800651 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -0400652 try {
Michael Jurkac7700af2013-05-14 20:17:58 +0200653 // Just in case mFlushingWorkerThread changes but we aren't woken up,
654 // wait no longer than 1sec at a time
655 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -0400656 } catch (InterruptedException ex) {
657 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400658 }
659 }
Joe Onorato36115782010-06-17 13:28:48 -0400660 if (DEBUG_LOADERS) {
661 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700662 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400663 + "ms for previous step to finish binding");
664 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400665 }
Joe Onorato36115782010-06-17 13:28:48 -0400666 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400667
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700668 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -0500669 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700670 // Ensure that we have a valid page index to load synchronously
671 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
672 "valid page index");
673 }
674 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
675 // Ensure that we don't try and bind a specified page when the pages have not been
676 // loaded already (we should load everything asynchronously in that case)
677 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
678 }
679 synchronized (mLock) {
680 if (mIsLoaderTaskRunning) {
681 // Ensure that we are never running the background loading at this point since
682 // we also touch the background collections
683 throw new RuntimeException("Error! Background loading is already running");
684 }
685 }
686
687 // XXX: Throw an exception if we are already loading (since we touch the worker thread
688 // data structures, we can't allow any other thread to touch that data, but because
689 // this call is synchronous, we can get away with not locking).
690
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400691 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -0700692 // operations from the previous activity. We need to ensure that all queued operations
693 // are executed before any synchronous binding work is done.
694 mHandler.flush();
695
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700696 // Divide the set of loaded items into those that we are binding synchronously, and
697 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800698 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700699 // XXX: For now, continue posting the binding of AllApps as there are other issues that
700 // arise from that.
701 onlyBindAllApps();
Tony Wickham80f57872016-06-29 18:12:15 -0700702
703 bindDeepShortcuts();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700704 }
705
Joe Onorato36115782010-06-17 13:28:48 -0400706 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700707 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -0700708 if (mStopped) {
709 return;
710 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700711 mIsLoaderTaskRunning = true;
712 }
Joe Onorato36115782010-06-17 13:28:48 -0400713 // Optimize for end-user experience: if the Launcher is up and // running with the
714 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
715 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -0400716 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -0700717 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800718 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -0400719
Joe Onorato36115782010-06-17 13:28:48 -0400720 if (mStopped) {
721 break keep_running;
722 }
723
Joe Onorato36115782010-06-17 13:28:48 -0400724 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -0400725
726 // second step
Winson Chung64359a52013-07-08 17:17:08 -0700727 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
728 loadAndBindAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700729
730 waitForIdle();
731
732 // third step
733 if (DEBUG_LOADERS) Log.d(TAG, "step 3: loading deep shortcuts");
734 loadAndBindDeepShortcuts();
Joe Onorato36115782010-06-17 13:28:48 -0400735 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400736
Joe Onorato36115782010-06-17 13:28:48 -0400737 // Clear out this reference, otherwise we end up holding it until all of the
738 // callback runnables are done.
739 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400740
Joe Onorato36115782010-06-17 13:28:48 -0400741 synchronized (mLock) {
742 // If we are still the last one to be scheduled, remove ourselves.
743 if (mLoaderTask == this) {
744 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400745 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700746 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700747 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -0400748 }
Joe Onorato36115782010-06-17 13:28:48 -0400749 }
750
751 public void stopLocked() {
752 synchronized (LoaderTask.this) {
753 mStopped = true;
754 this.notify();
755 }
756 }
757
758 /**
759 * Gets the callbacks object. If we've been stopped, or if the launcher object
760 * has somehow been garbage collected, return null instead. Pass in the Callbacks
761 * object that was around when the deferred message was scheduled, and if there's
762 * a new Callbacks object around then also return null. This will save us from
763 * calling onto it with data that will be ignored.
764 */
765 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
766 synchronized (mLock) {
767 if (mStopped) {
768 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400769 }
Joe Onorato36115782010-06-17 13:28:48 -0400770
771 if (mCallbacks == null) {
772 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400773 }
Joe Onorato36115782010-06-17 13:28:48 -0400774
775 final Callbacks callbacks = mCallbacks.get();
776 if (callbacks != oldCallbacks) {
777 return null;
778 }
779 if (callbacks == null) {
780 Log.w(TAG, "no mCallbacks");
781 return null;
782 }
783
784 return callbacks;
785 }
786 }
787
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800788 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -0700789 if (LauncherAppState.PROFILE_STARTUP) {
790 Trace.beginSection("Loading Workspace");
791 }
Joe Onorato36115782010-06-17 13:28:48 -0400792 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400793
Joe Onorato36115782010-06-17 13:28:48 -0400794 final Context context = mContext;
795 final ContentResolver contentResolver = context.getContentResolver();
796 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -0400797 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700798 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530799 final DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -0700800 final boolean isSdCardReady = Utilities.isBootCompleted();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800801 final MultiHashMap<UserHandle, String> pendingPackages = new MultiHashMap<>();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400802
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700803 boolean clearDb = false;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -0700804 try {
805 ImportDataTask.performImportIfPossible(context);
806 } catch (Exception e) {
807 // Migration failed. Clear workspace.
808 clearDb = true;
809 }
810
811 if (!clearDb && GridSizeMigrationTask.ENABLED &&
Sunny Goyalf076eae2016-01-11 12:25:10 -0800812 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
813 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700814 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -0700815 }
816
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700817 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -0700818 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -0700819 LauncherSettings.Settings.call(contentResolver,
820 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -0500821 }
822
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700823 Log.d(TAG, "loadWorkspace: loading default favorites");
824 LauncherSettings.Settings.call(contentResolver,
825 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -0700826
Sunny Goyale9956a72016-09-01 17:24:47 -0700827 synchronized (sBgDataModel) {
828 sBgDataModel.clear();
829
Sunny Goyal756adbc2015-04-16 15:20:51 -0700830 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -0700831 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyale9956a72016-09-01 17:24:47 -0700832 sBgDataModel.workspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -0800833
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700834 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800835 final LoaderCursor c = new LoaderCursor(contentResolver.query(
836 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null), mApp);
Daniel Sandler8802e962010-05-26 16:28:16 -0400837
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800838 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400839
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700840 try {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700841 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
842 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -0400843 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
844 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700845 final int spanXIndex = c.getColumnIndexOrThrow
846 (LauncherSettings.Favorites.SPANX);
847 final int spanYIndex = c.getColumnIndexOrThrow(
848 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800849 final int rankIndex = c.getColumnIndexOrThrow(
850 LauncherSettings.Favorites.RANK);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700851 final int optionsIndex = c.getColumnIndexOrThrow(
852 LauncherSettings.Favorites.OPTIONS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400853
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800854 final LongSparseArray<UserHandle> allUsers = c.allUsers;
Kenny Guyff05f432016-01-22 17:48:29 +0000855 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700856 final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800857 for (UserHandle user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +0000858 long serialNo = mUserManager.getSerialNumberForUser(user);
859 allUsers.put(serialNo, user);
860 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700861
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700862 boolean userUnlocked = mUserManager.isUserUnlocked(user);
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700863
864 // We can only query for shortcuts when the user is unlocked.
865 if (userUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -0700866 List<ShortcutInfoCompat> pinnedShortcuts =
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530867 shortcutManager.queryForPinnedShortcuts(null, user);
868 if (shortcutManager.wasLastCallSuccess()) {
Sunny Goyal49f4f032016-08-01 15:45:49 -0700869 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
870 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
871 shortcut);
872 }
873 } else {
874 // Shortcut manager can fail due to some race condition when the
875 // lock state changes too frequently. For the purpose of the loading
876 // shortcuts, consider the user is still locked.
877 userUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700878 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700879 }
Sunny Goyal49f4f032016-08-01 15:45:49 -0700880 unlockedUsers.put(serialNo, userUnlocked);
Sunny Goyal7f834d22015-04-21 10:10:23 -0700881 }
882
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700883 ShortcutInfo info;
884 String intentDescription;
885 LauncherAppWidgetInfo appWidgetInfo;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700886 Intent intent;
Sunny Goyal81e44912017-01-14 15:05:14 -0800887 String targetPkg;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400888
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700889 while (!mStopped && c.moveToNext()) {
890 try {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800891 if (c.user == null) {
892 // User has been deleted, remove the item.
893 c.markDeleted("User has been deleted");
894 continue;
895 }
896
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700897 boolean allowMissingTarget = false;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800898 switch (c.itemType) {
Sunny Goyal81e44912017-01-14 15:05:14 -0800899 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700900 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700901 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Sunny Goyal81e44912017-01-14 15:05:14 -0800902 intent = c.parseIntent();
903 if (intent == null) {
904 c.markDeleted("Invalid or null intent");
905 continue;
906 }
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800907
Sunny Goyal81e44912017-01-14 15:05:14 -0800908 int disabledState = quietMode.get(c.serialNumber) ?
909 ShortcutInfo.FLAG_DISABLED_QUIET_USER : 0;
910 ComponentName cn = intent.getComponent();
911 targetPkg = cn == null ? intent.getPackage() : cn.getPackageName();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700912
Sunny Goyal81e44912017-01-14 15:05:14 -0800913 if (!Process.myUserHandle().equals(c.user)) {
914 if (c.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
915 c.markDeleted("Legacy shortcuts are only allowed for default user");
916 continue;
917 } else if (c.restoreFlag != 0) {
918 // Don't restore items for other profiles.
919 c.markDeleted("Restore from managed profile not supported");
920 continue;
921 }
922 }
923 if (TextUtils.isEmpty(targetPkg) &&
924 c.itemType != LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
925 c.markDeleted("Only legacy shortcuts can have null package");
926 continue;
927 }
Sunny Goyal34942622014-08-29 17:20:55 -0700928
Sunny Goyal81e44912017-01-14 15:05:14 -0800929 // If there is no target package, its an implicit intent
930 // (legacy shortcut) which is always valid
931 boolean validTarget = TextUtils.isEmpty(targetPkg) ||
932 launcherApps.isPackageEnabledForProfile(targetPkg, c.user);
Sunny Goyal94485362014-09-18 16:13:58 -0700933
Sunny Goyal81e44912017-01-14 15:05:14 -0800934 if (cn != null && validTarget) {
935 // If the apk is present and the shortcut points to a specific
936 // component.
937
938 // If the component is already present
939 if (launcherApps.isActivityEnabledForProfile(cn, c.user)) {
940 // no special handling necessary for this item
941 c.markRestored();
942 } else {
943 if (c.hasRestoreFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
944 // We allow auto install apps to have their intent
945 // updated after an install.
946 intent = manager.getLaunchIntentForPackage(targetPkg);
947 if (intent != null) {
948 c.restoreFlag = 0;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800949 c.updater().put(
Sunny Goyal81e44912017-01-14 15:05:14 -0800950 LauncherSettings.Favorites.INTENT,
951 intent.toUri(0)).commit();
952 cn = intent.getComponent();
Sunny Goyalb05a00a2016-08-29 10:06:57 -0700953 } else {
Sunny Goyal81e44912017-01-14 15:05:14 -0800954 c.markDeleted("Unable to find a launch target");
Sunny Goyal94485362014-09-18 16:13:58 -0700955 continue;
956 }
Sunny Goyal1a745e82014-10-02 15:58:31 -0700957 } else {
Sunny Goyal81e44912017-01-14 15:05:14 -0800958 // The app is installed but the component is no
959 // longer available.
960 c.markDeleted("Invalid component removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -0700961 continue;
Winson Chungee055712013-07-30 14:46:24 -0700962 }
Winson Chungee055712013-07-30 14:46:24 -0700963 }
Sunny Goyal81e44912017-01-14 15:05:14 -0800964 }
965 // else if cn == null => can't infer much, leave it
966 // else if !validPkg => could be restored icon or missing sd-card
967
968 if (!TextUtils.isEmpty(targetPkg) && !validTarget) {
969 // Points to a valid app (superset of cn != null) but the apk
970 // is not available.
971
972 if (c.restoreFlag != 0) {
973 // Package is not yet available but might be
974 // installed later.
975 FileLog.d(TAG, "package not yet restored: " + targetPkg);
976
977 if (c.hasRestoreFlag(ShortcutInfo.FLAG_RESTORE_STARTED)) {
978 // Restore has started once.
979 } else if (installingPkgs.containsKey(targetPkg)) {
980 // App restore has started. Update the flag
981 c.restoreFlag |= ShortcutInfo.FLAG_RESTORE_STARTED;
982 c.updater().commit();
983 } else {
984 c.markDeleted("Unrestored app removed: " + targetPkg);
985 continue;
986 }
987 } else if (PackageManagerHelper.isAppOnSdcard(
988 manager, targetPkg)) {
989 // Package is present but not available.
990 disabledState |= ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
991 // Add the icon on the workspace anyway.
992 allowMissingTarget = true;
993 } else if (!isSdCardReady) {
994 // SdCard is not ready yet. Package might get available,
995 // once it is ready.
996 Log.d(TAG, "Missing pkg, will check later: " + targetPkg);
997 pendingPackages.addToList(c.user, targetPkg);
998 // Add the icon on the workspace anyway.
999 allowMissingTarget = true;
1000 } else {
1001 // Do not wait for external media load anymore.
1002 c.markDeleted("Invalid package removed: " + targetPkg);
1003 continue;
1004 }
1005 }
1006
1007 if (validTarget) {
1008 // The shortcut points to a valid target (either no target
1009 // or something which is ready to be used)
1010 c.markRestored();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001011 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001012
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001013 boolean useLowResIcon = !c.isOnWorkspaceOrHotseat() &&
Sunny Goyal34b65272015-03-11 16:56:52 -07001014 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1015
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001016 if (c.restoreFlag != 0) {
Sunny Goyal81e44912017-01-14 15:05:14 -08001017 // Already verified above that user is same as default user
1018 info = c.getRestoredItemInfo(intent);
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001019 } else if (c.itemType ==
Chris Wrenf4d08112014-01-16 18:13:56 -05001020 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001021 info = c.getAppShortcutInfo(
1022 intent, allowMissingTarget, useLowResIcon);
1023 } else if (c.itemType ==
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001024 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001025
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001026 ShortcutKey key = ShortcutKey.fromIntent(intent, c.user);
1027 if (unlockedUsers.get(c.serialNumber)) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001028 ShortcutInfoCompat pinnedShortcut =
1029 shortcutKeyToPinnedShortcuts.get(key);
1030 if (pinnedShortcut == null) {
1031 // The shortcut is no longer valid.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001032 c.markDeleted("Pinned shortcut not found");
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001033 continue;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001034 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001035 info = new ShortcutInfo(pinnedShortcut, context);
Sunny Goyal1b072632017-01-18 11:30:23 -08001036 info.iconBitmap = LauncherIcons
1037 .createShortcutIcon(pinnedShortcut, context);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001038 intent = info.intent;
1039 } else {
1040 // Create a shortcut info in disabled mode for now.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001041 info = c.loadSimpleShortcut();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001042 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001043 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001044 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001045 info = c.loadSimpleShortcut();
Michael Jurka96879562012-03-22 05:54:33 -07001046
Sunny Goyald09c3702016-04-06 16:18:20 -07001047 // Shortcuts are only available on the primary profile
Sunny Goyal81e44912017-01-14 15:05:14 -08001048 if (PackageManagerHelper.isAppSuspended(manager, targetPkg)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001049 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1050 }
1051
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001052 // App shortcuts that used to be automatically added to Launcher
1053 // didn't always have the correct intent flags set, so do that
1054 // here
1055 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001056 intent.getCategories() != null &&
1057 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001058 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001059 intent.addFlags(
1060 Intent.FLAG_ACTIVITY_NEW_TASK |
1061 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1062 }
Michael Jurka96879562012-03-22 05:54:33 -07001063 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001064
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001065 if (info != null) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001066 c.applyCommonProperties(info);
1067
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001068 info.intent = intent;
Sunny Goyal08f72612015-01-05 13:41:43 -08001069 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001070 info.spanX = 1;
1071 info.spanY = 1;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001072 // TODO: Remove this extra. Instead we should be using
1073 // itemInfo#user.
1074 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, c.serialNumber);
Sunny Goyald09c3702016-04-06 16:18:20 -07001075 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001076 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1077 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1078 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001079
Sunny Goyal81e44912017-01-14 15:05:14 -08001080 if (c.restoreFlag != 0 && !TextUtils.isEmpty(targetPkg)) {
1081 Integer progress = installingPkgs.get(targetPkg);
1082 if (progress != null) {
1083 info.setInstallProgress(progress);
1084 } else {
1085 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001086 }
1087 }
1088
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001089 c.checkAndAddItem(info, sBgDataModel);
Winson Chung1323b482013-08-05 12:41:55 -07001090 } else {
1091 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001092 }
1093 break;
1094
1095 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001096 FolderInfo folderInfo = sBgDataModel.findOrMakeFolder(c.id);
1097 c.applyCommonProperties(folderInfo);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001098
Sunny Goyala508e4f2015-05-21 09:33:57 -07001099 // Do not trim the folder label, as is was set by the user.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001100 folderInfo.title = c.getString(c.titleIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001101 folderInfo.spanX = 1;
1102 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001103 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001104
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001105 // no special handling required for restored folders
1106 c.markRestored();
Chris Wrenf4d08112014-01-16 18:13:56 -05001107
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001108 c.checkAndAddItem(folderInfo, sBgDataModel);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001109 break;
1110
1111 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08001112 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001113 // Read all Launcher-specific widget details
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001114 boolean customWidget = c.itemType ==
Adam Cohen59400422014-03-05 18:07:04 -08001115 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
1116
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001117 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001118 String savedProvider = c.getString(appWidgetProviderIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001119
Sunny Goyalff572272014-07-23 13:58:07 -07001120 final ComponentName component =
1121 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04001122
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001123 final boolean isIdValid = !c.hasRestoreFlag(
1124 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID);
1125 final boolean wasProviderReady = !c.hasRestoreFlag(
1126 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY);
Sunny Goyal651077b2014-06-30 14:15:31 -07001127
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001128 if (widgetProvidersMap == null) {
1129 widgetProvidersMap = AppWidgetManagerCompat
1130 .getInstance(mContext).getAllProvidersMap();
1131 }
1132 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
1133 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00001134 ComponentName.unflattenFromString(savedProvider),
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001135 c.user));
Sunny Goyalff572272014-07-23 13:58:07 -07001136
1137 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08001138 if (!isSafeMode && !customWidget &&
1139 wasProviderReady && !isProviderReady) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001140 c.markDeleted(
1141 "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07001142 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001143 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07001144 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07001145 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1146 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08001147
Sunny Goyal53f96722015-07-13 19:54:53 -07001148 // The provider is available. So the widget is either
1149 // available or not available. We do not need to track
1150 // any future restore updates.
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001151 int status = c.restoreFlag &
Sunny Goyal53f96722015-07-13 19:54:53 -07001152 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07001153 if (!wasProviderReady) {
1154 // If provider was not previously ready, update the
1155 // status and UI flag.
1156
1157 // Id would be valid only if the widget restore broadcast was received.
1158 if (isIdValid) {
Sunny Goyal86df1382016-08-10 15:03:22 -07001159 status |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07001160 } else {
1161 status &= ~LauncherAppWidgetInfo
1162 .FLAG_PROVIDER_NOT_READY;
1163 }
1164 }
1165 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07001166 } else {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001167 Log.v(TAG, "Widget restore pending id=" + c.id
Sunny Goyal651077b2014-06-30 14:15:31 -07001168 + " appWidgetId=" + appWidgetId
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001169 + " status =" + c.restoreFlag);
Sunny Goyal651077b2014-06-30 14:15:31 -07001170 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07001171 component);
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001172 appWidgetInfo.restoreStatus = c.restoreFlag;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001173 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07001174
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001175 if (c.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_RESTORE_STARTED)) {
Sunny Goyal94485362014-09-18 16:13:58 -07001176 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001177 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07001178 // App restore has started. Update the flag
1179 appWidgetInfo.restoreStatus |=
1180 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001181 } else if (!isSafeMode) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001182 c.markDeleted("Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07001183 continue;
1184 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07001185
1186 appWidgetInfo.installProgress =
1187 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07001188 }
Sunny Goyal86df1382016-08-10 15:03:22 -07001189 if (appWidgetInfo.hasRestoreFlag(
1190 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG)) {
Sunny Goyal81e44912017-01-14 15:05:14 -08001191 appWidgetInfo.bindOptions = c.parseIntent();
Sunny Goyal86df1382016-08-10 15:03:22 -07001192 }
Sunny Goyalff572272014-07-23 13:58:07 -07001193
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001194 c.applyCommonProperties(appWidgetInfo);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001195 appWidgetInfo.spanX = c.getInt(spanXIndex);
1196 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001197 appWidgetInfo.user = c.user;
Joe Onorato36115782010-06-17 13:28:48 -04001198
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001199 if (!c.isOnWorkspaceOrHotseat()) {
1200 c.markDeleted("Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001201 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001202 continue;
1203 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001204
Adam Cohen59400422014-03-05 18:07:04 -08001205 if (!customWidget) {
1206 String providerName =
1207 appWidgetInfo.providerName.flattenToString();
1208 if (!providerName.equals(savedProvider) ||
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001209 (appWidgetInfo.restoreStatus != c.restoreFlag)) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001210 c.updater()
1211 .put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1212 providerName)
1213 .put(LauncherSettings.Favorites.RESTORED,
1214 appWidgetInfo.restoreStatus)
1215 .commit();
Adam Cohen59400422014-03-05 18:07:04 -08001216 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001217 }
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001218 c.checkAndAddItem(appWidgetInfo, sBgDataModel);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001219 }
Joe Onorato36115782010-06-17 13:28:48 -04001220 break;
1221 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001222 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001223 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001224 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001225 }
1226 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001227 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001228 }
1229
Winson Chungba9c37f2013-08-30 14:11:37 -07001230 // Break early if we've stopped loading
1231 if (mStopped) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001232 sBgDataModel.clear();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001233 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07001234 }
1235
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001236 // Remove dead items
1237 if (c.commitDeleted()) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001238 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07001239 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
1240 .call(contentResolver,
1241 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
1242 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
1243 for (long folderId : deletedFolderIds) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001244 sBgDataModel.workspaceItems.remove(sBgDataModel.folders.get(folderId));
1245 sBgDataModel.folders.remove(folderId);
1246 sBgDataModel.itemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001247 }
1248 }
1249
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001250 // Unpin shortcuts that don't exist on the workspace.
Sunny Goyalf75baa92016-11-22 03:23:51 +05301251 HashSet<ShortcutKey> pendingShortcuts =
1252 InstallShortcutReceiver.getPendingShortcuts(context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001253 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001254 MutableInt numTimesPinned = sBgDataModel.pinnedShortcutCounts.get(key);
Sunny Goyalf75baa92016-11-22 03:23:51 +05301255 if ((numTimesPinned == null || numTimesPinned.value == 0)
1256 && !pendingShortcuts.contains(key)) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001257 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301258 shortcutManager.unpinShortcut(key);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001259 }
1260 }
1261
Sunny Goyal317698b2015-07-29 11:45:41 -07001262 // Sort all the folder items and make sure the first 3 items are high resolution.
Sunny Goyale9956a72016-09-01 17:24:47 -07001263 for (FolderInfo folder : sBgDataModel.folders) {
Sunny Goyal317698b2015-07-29 11:45:41 -07001264 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
1265 int pos = 0;
1266 for (ShortcutInfo info : folder.contents) {
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001267 if (info.usingLowResIcon &&
1268 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal3fe4a142016-12-15 17:40:07 -08001269 mIconCache.getTitleAndIcon(info, false);
Sunny Goyal317698b2015-07-29 11:45:41 -07001270 }
1271 pos ++;
1272 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
1273 break;
1274 }
1275 }
1276 }
1277
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001278 c.commitRestoredItems();
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001279 if (!isSdCardReady && !pendingPackages.isEmpty()) {
1280 context.registerReceiver(
1281 new SdCardAvailableReceiver(
1282 LauncherModel.this, mContext, pendingPackages),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001283 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001284 null,
1285 sWorker);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001286 }
1287
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001288 // Remove any empty screens
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001289 ArrayList<Long> unusedScreens = new ArrayList<>(sBgDataModel.workspaceScreens);
Sunny Goyale9956a72016-09-01 17:24:47 -07001290 for (ItemInfo item: sBgDataModel.itemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001291 long screenId = item.screenId;
1292 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1293 unusedScreens.contains(screenId)) {
1294 unusedScreens.remove(screenId);
1295 }
1296 }
1297
1298 // If there are any empty screens remove them, and update.
1299 if (unusedScreens.size() != 0) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001300 sBgDataModel.workspaceScreens.removeAll(unusedScreens);
1301 updateWorkspaceScreenOrder(context, sBgDataModel.workspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001302 }
Joe Onorato36115782010-06-17 13:28:48 -04001303 }
Sunny Goyale26d1002016-06-20 14:52:14 -07001304 if (LauncherAppState.PROFILE_STARTUP) {
1305 Trace.endSection();
1306 }
Adam Cohene25af792013-06-06 23:08:25 -07001307 }
1308
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001309 /** Filters the set of items who are directly or indirectly (via another container) on the
1310 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08001311 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001312 ArrayList<ItemInfo> allWorkspaceItems,
1313 ArrayList<ItemInfo> currentScreenItems,
1314 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001315 // Purge any null ItemInfos
1316 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
1317 while (iter.hasNext()) {
1318 ItemInfo i = iter.next();
1319 if (i == null) {
1320 iter.remove();
1321 }
1322 }
1323
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001324 // Order the set of items by their containers first, this allows use to walk through the
1325 // list sequentially, build up a list of containers that are in the specified screen,
1326 // as well as all items in those containers.
1327 Set<Long> itemsOnScreen = new HashSet<Long>();
1328 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
1329 @Override
1330 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07001331 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001332 }
1333 });
1334 for (ItemInfo info : allWorkspaceItems) {
1335 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001336 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001337 currentScreenItems.add(info);
1338 itemsOnScreen.add(info.id);
1339 } else {
1340 otherScreenItems.add(info);
1341 }
1342 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1343 currentScreenItems.add(info);
1344 itemsOnScreen.add(info.id);
1345 } else {
1346 if (itemsOnScreen.contains(info.container)) {
1347 currentScreenItems.add(info);
1348 itemsOnScreen.add(info.id);
1349 } else {
1350 otherScreenItems.add(info);
1351 }
1352 }
1353 }
1354 }
1355
1356 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08001357 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001358 ArrayList<LauncherAppWidgetInfo> appWidgets,
1359 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
1360 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001361
1362 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001363 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001364 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08001365 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001366 currentScreenWidgets.add(widget);
1367 } else {
1368 otherScreenWidgets.add(widget);
1369 }
1370 }
1371 }
1372
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001373 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
1374 * right) */
1375 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Sunny Goyal87f784c2017-01-11 10:48:34 -08001376 final InvariantDeviceProfile profile = mApp.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07001377 final int screenCols = profile.numColumns;
1378 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001379 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07001380 @Override
1381 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07001382 if (lhs.container == rhs.container) {
1383 // Within containers, order by their spatial position in that container
1384 switch ((int) lhs.container) {
1385 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
1386 long lr = (lhs.screenId * screenCellCount +
1387 lhs.cellY * screenCols + lhs.cellX);
1388 long rr = (rhs.screenId * screenCellCount +
1389 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07001390 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07001391 }
1392 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
1393 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07001394 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07001395 }
1396 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07001397 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07001398 throw new RuntimeException("Unexpected container type when " +
1399 "sorting workspace items.");
1400 }
1401 return 0;
1402 }
1403 } else {
1404 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07001405 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07001406 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001407 }
1408 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001409 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001410
Adam Cohendcd297f2013-06-18 13:13:40 -07001411 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
1412 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001413 final Runnable r = new Runnable() {
1414 @Override
1415 public void run() {
1416 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1417 if (callbacks != null) {
1418 callbacks.bindScreens(orderedScreens);
1419 }
1420 }
1421 };
Sunny Goyald33860f2015-04-23 16:02:20 -07001422 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07001423 }
1424
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001425 private void bindWorkspaceItems(final Callbacks oldCallbacks,
1426 final ArrayList<ItemInfo> workspaceItems,
1427 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07001428 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001429
1430 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07001431 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001432 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04001433 final int start = i;
1434 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001435 final Runnable r = new Runnable() {
1436 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001437 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001438 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001439 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07001440 callbacks.bindItems(workspaceItems, start, start+chunkSize,
1441 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001442 }
1443 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001444 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001445 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04001446 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001447
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001448 // Bind the widgets, one at a time
1449 N = appWidgets.size();
1450 for (int i = 0; i < N; i++) {
1451 final LauncherAppWidgetInfo widget = appWidgets.get(i);
1452 final Runnable r = new Runnable() {
1453 public void run() {
1454 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1455 if (callbacks != null) {
1456 callbacks.bindAppWidget(widget);
1457 }
1458 }
1459 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001460 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001461 }
1462 }
1463
1464 /**
1465 * Binds all loaded data to actual views on the main thread.
1466 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001467 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001468 final long t = SystemClock.uptimeMillis();
1469 Runnable r;
1470
1471 // Don't use these two variables in any of the callback runnables.
1472 // Otherwise we hold a reference to them.
1473 final Callbacks oldCallbacks = mCallbacks.get();
1474 if (oldCallbacks == null) {
1475 // This launcher has exited and nobody bothered to tell us. Just bail.
1476 Log.w(TAG, "LoaderTask running with no launcher");
1477 return;
1478 }
1479
Winson Chung9b9fb962013-11-15 15:39:34 -08001480 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07001481 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
1482 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
1483 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07001484
Sunny Goyale9956a72016-09-01 17:24:47 -07001485 synchronized (sBgDataModel) {
1486 workspaceItems.addAll(sBgDataModel.workspaceItems);
1487 appWidgets.addAll(sBgDataModel.appWidgets);
1488 orderedScreenIds.addAll(sBgDataModel.workspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001489 }
1490
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001491 final int currentScreen;
1492 {
1493 int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE
1494 ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen();
1495 if (currScreen >= orderedScreenIds.size()) {
1496 // There may be no workspace screens (just hotseat items and an empty page).
1497 currScreen = PagedView.INVALID_RESTORE_PAGE;
1498 }
1499 currentScreen = currScreen;
Winson Chung9b9fb962013-11-15 15:39:34 -08001500 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001501 final boolean validFirstPage = currentScreen >= 0;
1502 final long currentScreenId =
1503 validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID;
Winson Chung9b9fb962013-11-15 15:39:34 -08001504
Winson Chung9b9fb962013-11-15 15:39:34 -08001505 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07001506 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
1507 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
1508 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
1509 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001510
Winson Chung9b9fb962013-11-15 15:39:34 -08001511 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001512 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08001513 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001514 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001515 sortWorkspaceItemsSpatially(currentWorkspaceItems);
1516 sortWorkspaceItemsSpatially(otherWorkspaceItems);
1517
1518 // Tell the workspace that we're about to start binding items
1519 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001520 public void run() {
1521 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1522 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001523 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001524 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04001525 }
1526 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001527 };
Sunny Goyald33860f2015-04-23 16:02:20 -07001528 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001529
Adam Cohendcd297f2013-06-18 13:13:40 -07001530 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
1531
Sunny Goyal527c7d32015-08-28 15:19:36 -07001532 Executor mainExecutor = new DeferredMainThreadExecutor();
1533 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07001534 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001535
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001536 // In case of validFirstPage, only bind the first screen, and defer binding the
1537 // remaining screens after first onDraw (and an optional the fade animation whichever
1538 // happens later).
1539 // This ensures that the first screen is immediately visible (eg. during rotation)
1540 // In case of !validFirstPage, bind all pages one after other.
1541 final Executor deferredExecutor =
1542 validFirstPage ? new ViewOnDrawExecutor(mHandler) : mainExecutor;
1543
1544 mainExecutor.execute(new Runnable() {
1545 @Override
1546 public void run() {
1547 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1548 if (callbacks != null) {
1549 callbacks.finishFirstPageBind(
1550 validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null);
1551 }
1552 }
1553 });
Sunny Goyal527c7d32015-08-28 15:19:36 -07001554
Sunny Goyal44c06432016-04-02 10:56:02 -07001555 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001556
1557 // Tell the workspace that we're done binding items
1558 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001559 public void run() {
1560 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1561 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001562 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04001563 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001564
Sunny Goyal639e9062015-08-19 19:17:06 -07001565 mIsLoadingAndBindingWorkspace = false;
1566
1567 // Run all the bind complete runnables after workspace is bound.
1568 if (!mBindCompleteRunnables.isEmpty()) {
1569 synchronized (mBindCompleteRunnables) {
1570 for (final Runnable r : mBindCompleteRunnables) {
1571 runOnWorkerThread(r);
1572 }
1573 mBindCompleteRunnables.clear();
1574 }
1575 }
1576
Winson Chung98e030b2012-05-07 16:01:11 -07001577 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04001578 if (DEBUG_LOADERS) {
1579 Log.d(TAG, "bound workspace in "
1580 + (SystemClock.uptimeMillis()-t) + "ms");
1581 }
Winson Chung36a62fe2012-05-06 18:04:42 -07001582
Joe Onorato36115782010-06-17 13:28:48 -04001583 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001584 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001585 deferredExecutor.execute(r);
1586
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001587 if (validFirstPage) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001588 r = new Runnable() {
1589 public void run() {
1590 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1591 if (callbacks != null) {
1592 // We are loading synchronously, which means, some of the pages will be
1593 // bound after first draw. Inform the callbacks that page binding is
1594 // not complete, and schedule the remaining pages.
1595 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
1596 callbacks.onPageBoundSynchronously(currentScreen);
1597 }
1598 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
1599 }
1600 }
1601 };
Sunny Goyald33860f2015-04-23 16:02:20 -07001602 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07001603 }
Joe Onorato36115782010-06-17 13:28:48 -04001604 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001605
Joe Onorato36115782010-06-17 13:28:48 -04001606 private void loadAndBindAllApps() {
1607 if (DEBUG_LOADERS) {
1608 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1609 }
1610 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07001611 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001612 synchronized (LoaderTask.this) {
1613 if (mStopped) {
1614 return;
1615 }
1616 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001617 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07001618 synchronized (LoaderTask.this) {
1619 if (mStopped) {
1620 return;
1621 }
1622 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07001623 }
Joe Onorato36115782010-06-17 13:28:48 -04001624 } else {
1625 onlyBindAllApps();
1626 }
1627 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001628
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001629 private void updateIconCache() {
1630 // Ignore packages which have a promise icon.
1631 HashSet<String> packagesToIgnore = new HashSet<>();
Sunny Goyale9956a72016-09-01 17:24:47 -07001632 synchronized (sBgDataModel) {
1633 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001634 if (info instanceof ShortcutInfo) {
1635 ShortcutInfo si = (ShortcutInfo) info;
1636 if (si.isPromise() && si.getTargetComponent() != null) {
1637 packagesToIgnore.add(si.getTargetComponent().getPackageName());
1638 }
1639 } else if (info instanceof LauncherAppWidgetInfo) {
1640 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
1641 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
1642 packagesToIgnore.add(lawi.providerName.getPackageName());
1643 }
1644 }
1645 }
1646 }
1647 mIconCache.updateDbIcons(packagesToIgnore);
1648 }
1649
Joe Onorato36115782010-06-17 13:28:48 -04001650 private void onlyBindAllApps() {
1651 final Callbacks oldCallbacks = mCallbacks.get();
1652 if (oldCallbacks == null) {
1653 // This launcher has exited and nobody bothered to tell us. Just bail.
1654 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1655 return;
1656 }
1657
1658 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07001659 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001660 final ArrayList<AppInfo> list
1661 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07001662 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001663 public void run() {
1664 final long t = SystemClock.uptimeMillis();
1665 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1666 if (callbacks != null) {
1667 callbacks.bindAllApplications(list);
1668 }
1669 if (DEBUG_LOADERS) {
1670 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08001671 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04001672 }
1673 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07001674 };
Tony Wickham80f57872016-06-29 18:12:15 -07001675 runOnMainThread(r);
Joe Onorato36115782010-06-17 13:28:48 -04001676 }
1677
Winson Chung64359a52013-07-08 17:17:08 -07001678 private void loadAllApps() {
1679 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04001680
Joe Onorato36115782010-06-17 13:28:48 -04001681 final Callbacks oldCallbacks = mCallbacks.get();
1682 if (oldCallbacks == null) {
1683 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07001684 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04001685 return;
1686 }
1687
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001688 final List<UserHandle> profiles = mUserManager.getUserProfiles();
Kenny Guyed131872014-04-30 03:02:21 +01001689
Winson Chung64359a52013-07-08 17:17:08 -07001690 // Clear the list of apps
1691 mBgAllAppsList.clear();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001692 for (UserHandle user : profiles) {
Kenny Guyed131872014-04-30 03:02:21 +01001693 // Query for the set of apps
1694 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal3e9be432017-01-05 15:22:41 -08001695 final List<LauncherActivityInfo> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01001696 if (DEBUG_LOADERS) {
1697 Log.d(TAG, "getActivityList took "
1698 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
1699 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
1700 }
1701 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08001702 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01001703 if (apps == null || apps.isEmpty()) {
1704 return;
1705 }
Kenny Guyff05f432016-01-22 17:48:29 +00001706 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01001707 // Create the ApplicationInfos
1708 for (int i = 0; i < apps.size(); i++) {
Sunny Goyal3e9be432017-01-05 15:22:41 -08001709 LauncherActivityInfo app = apps.get(i);
Kenny Guyed131872014-04-30 03:02:21 +01001710 // This builds the icon bitmaps.
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001711 mBgAllAppsList.add(new AppInfo(mContext, app, user, quietMode), app);
Kenny Guyed131872014-04-30 03:02:21 +01001712 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08001713
Sunny Goyal756a28a2015-04-23 17:07:55 -07001714 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
1715 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07001716 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07001717
1718 @Override
1719 public void run() {
1720 heuristic.processUserApps(apps);
1721 }
Sunny Goyal639e9062015-08-19 19:17:06 -07001722 };
1723 runOnMainThread(new Runnable() {
1724
1725 @Override
1726 public void run() {
1727 // Check isLoadingWorkspace on the UI thread, as it is updated on
1728 // the UI thread.
1729 if (mIsLoadingAndBindingWorkspace) {
1730 synchronized (mBindCompleteRunnables) {
1731 mBindCompleteRunnables.add(r);
1732 }
1733 } else {
1734 runOnWorkerThread(r);
1735 }
1736 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07001737 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08001738 }
Winson Chung64359a52013-07-08 17:17:08 -07001739 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01001740 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001741 final ArrayList<AppInfo> added = mBgAllAppsList.added;
1742 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07001743
1744 // Post callback on main thread
1745 mHandler.post(new Runnable() {
1746 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07001747
Winson Chung64359a52013-07-08 17:17:08 -07001748 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07001749 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07001750 if (callbacks != null) {
1751 callbacks.bindAllApplications(added);
1752 if (DEBUG_LOADERS) {
1753 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001754 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07001755 }
1756 } else {
1757 Log.i(TAG, "not binding apps: no Launcher activity");
1758 }
1759 }
1760 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07001761 // Cleanup any data stored for a deleted user.
1762 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04001763 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07001764 Log.d(TAG, "Icons processed in "
1765 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08001766 }
1767 }
1768
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001769 private void loadAndBindDeepShortcuts() {
1770 if (DEBUG_LOADERS) {
1771 Log.d(TAG, "loadAndBindDeepShortcuts mDeepShortcutsLoaded=" + mDeepShortcutsLoaded);
1772 }
1773 if (!mDeepShortcutsLoaded) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07001774 sBgDataModel.deepShortcutMap.clear();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301775 DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(mContext);
1776 mHasShortcutHostPermission = shortcutManager.hasHostPermission();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001777 if (mHasShortcutHostPermission) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001778 for (UserHandle user : mUserManager.getUserProfiles()) {
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001779 if (mUserManager.isUserUnlocked(user)) {
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301780 List<ShortcutInfoCompat> shortcuts =
1781 shortcutManager.queryForAllShortcuts(user);
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07001782 sBgDataModel.updateDeepShortcutMap(null, user, shortcuts);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001783 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001784 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001785 }
1786 synchronized (LoaderTask.this) {
1787 if (mStopped) {
1788 return;
1789 }
1790 mDeepShortcutsLoaded = true;
1791 }
1792 }
Tony Wickham80f57872016-06-29 18:12:15 -07001793 bindDeepShortcuts();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001794 }
1795
Joe Onoratobe386092009-11-17 17:32:16 -08001796 public void dumpState() {
Sunny Goyale9956a72016-09-01 17:24:47 -07001797 synchronized (sBgDataModel) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001798 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001799 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
1800 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
Sunny Goyale9956a72016-09-01 17:24:47 -07001801 Log.d(TAG, "mItems size=" + sBgDataModel.workspaceItems.size());
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001802 }
Joe Onorato36115782010-06-17 13:28:48 -04001803 }
1804 }
1805
Tony Wickham80f57872016-06-29 18:12:15 -07001806 public void bindDeepShortcuts() {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07001807 final MultiHashMap<ComponentKey, String> shortcutMapCopy =
1808 sBgDataModel.deepShortcutMap.clone();
Tony Wickham80f57872016-06-29 18:12:15 -07001809 Runnable r = new Runnable() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001810 @Override
1811 public void run() {
1812 Callbacks callbacks = getCallback();
1813 if (callbacks != null) {
1814 callbacks.bindDeepShortcutMap(shortcutMapCopy);
1815 }
1816 }
Tony Wickham80f57872016-06-29 18:12:15 -07001817 };
1818 runOnMainThread(r);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001819 }
1820
Sunny Goyal75b0f552015-05-20 21:57:06 -07001821 /**
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001822 * Refreshes the cached shortcuts if the shortcut permission has changed.
1823 * Current implementation simply reloads the workspace, but it can be optimized to
1824 * use partial updates similar to {@link UserManagerCompat}
1825 */
1826 public void refreshShortcutsIfRequired() {
Sunny Goyalf5e37442016-11-02 10:31:24 -07001827 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001828 sWorker.removeCallbacks(mShortcutPermissionCheckRunnable);
1829 sWorker.post(mShortcutPermissionCheckRunnable);
1830 }
1831 }
1832
1833 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -07001834 * Called when the icons for packages have been updated in the icon cache.
1835 */
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001836 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandle user) {
Sunny Goyal75b0f552015-05-20 21:57:06 -07001837 // If any package icon has changed (app was updated while launcher was dead),
1838 // update the corresponding shortcuts.
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001839 enqueueModelUpdateTask(new CacheDataUpdatedTask(
1840 CacheDataUpdatedTask.OP_CACHE_UPDATE, user, updatedPackages));
Sunny Goyal75b0f552015-05-20 21:57:06 -07001841 }
1842
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001843 void enqueueModelUpdateTask(BaseModelUpdateTask task) {
1844 task.init(this);
1845 runOnWorkerThread(task);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001846 }
1847
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001848 /**
1849 * A task to be executed on the current callbacks on the UI thread.
1850 * If there is no current callbacks, the task is ignored.
1851 */
1852 public interface CallbackTask {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001853
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001854 void execute(Callbacks callbacks);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001855 }
1856
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001857 /**
1858 * A runnable which changes/updates the data model of the launcher based on certain events.
1859 */
1860 public static abstract class BaseModelUpdateTask implements Runnable {
Joe Onorato36115782010-06-17 13:28:48 -04001861
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001862 private LauncherModel mModel;
1863 private DeferredHandler mUiHandler;
Joe Onorato36115782010-06-17 13:28:48 -04001864
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001865 /* package private */
1866 void init(LauncherModel model) {
1867 mModel = model;
1868 mUiHandler = mModel.mHandler;
Joe Onorato36115782010-06-17 13:28:48 -04001869 }
1870
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001871 @Override
Joe Onorato36115782010-06-17 13:28:48 -04001872 public void run() {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001873 if (!mModel.mHasLoaderCompletedOnce) {
Sunny Goyalc905efc2015-05-06 09:54:53 -07001874 // Loader has not yet run.
1875 return;
1876 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001877 execute(mModel.mApp, sBgDataModel, mModel.mBgAllAppsList);
1878 }
Joe Onorato36115782010-06-17 13:28:48 -04001879
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001880 /**
1881 * Execute the actual task. Called on the worker thread.
1882 */
1883 public abstract void execute(
1884 LauncherAppState app, BgDataModel dataModel, AllAppsList apps);
Sunny Goyale0f58d72014-11-10 18:05:31 -08001885
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001886 /**
1887 * Schedules a {@param task} to be executed on the current callbacks.
1888 */
1889 public final void scheduleCallbackTask(final CallbackTask task) {
1890 final Callbacks callbacks = mModel.getCallback();
1891 mUiHandler.post(new Runnable() {
1892 public void run() {
1893 Callbacks cb = mModel.getCallback();
1894 if (callbacks == cb && cb != null) {
1895 task.execute(callbacks);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07001896 }
1897 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001898 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001899 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001900
1901 public ModelWriter getModelWriter() {
1902 // Updates from model task, do not deal with icon position in hotseat.
1903 return mModel.getWriter(false /* hasVerticalHotseat */);
1904 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001905 }
1906
Sunny Goyal1b072632017-01-18 11:30:23 -08001907 public void updateAndBindShortcutInfo(final ShortcutInfo si, final ShortcutInfoCompat info) {
1908 updateAndBindShortcutInfo(new Provider<ShortcutInfo>() {
1909 @Override
1910 public ShortcutInfo get() {
1911 si.updateFromDeepShortcutInfo(info, mApp.getContext());
1912 si.iconBitmap = LauncherIcons.createShortcutIcon(info, mApp.getContext());
1913 return si;
1914 }
1915 });
1916 }
1917
Sunny Goyal10923b32016-07-20 15:42:44 -07001918 /**
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001919 * Utility method to update a shortcut on the background thread.
Sunny Goyal10923b32016-07-20 15:42:44 -07001920 */
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001921 public void updateAndBindShortcutInfo(final Provider<ShortcutInfo> shortcutProvider) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001922 enqueueModelUpdateTask(new ExtendedModelTask() {
Sunny Goyal10923b32016-07-20 15:42:44 -07001923 @Override
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001924 public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001925 ShortcutInfo info = shortcutProvider.get();
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001926 ArrayList<ShortcutInfo> update = new ArrayList<>();
Sunny Goyal10923b32016-07-20 15:42:44 -07001927 update.add(info);
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001928 bindUpdatedShortcuts(update, info.user);
Sunny Goyal10923b32016-07-20 15:42:44 -07001929 }
1930 });
1931 }
1932
Sunny Goyald164b7f2016-10-12 20:49:31 -07001933 private void bindWidgetsModel(final Callbacks callbacks) {
1934 final MultiHashMap<PackageItemInfo, WidgetItem> widgets
1935 = mBgWidgetsModel.getWidgetsMap().clone();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001936 mHandler.post(new Runnable() {
1937 @Override
1938 public void run() {
1939 Callbacks cb = getCallback();
1940 if (callbacks == cb && cb != null) {
Sunny Goyald164b7f2016-10-12 20:49:31 -07001941 callbacks.bindAllWidgets(widgets);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001942 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07001943 }
1944 });
1945 }
1946
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001947 public void refreshAndBindWidgetsAndShortcuts(
1948 final Callbacks callbacks, final boolean bindFirst) {
1949 runOnWorkerThread(new Runnable() {
1950 @Override
1951 public void run() {
1952 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
Sunny Goyald164b7f2016-10-12 20:49:31 -07001953 bindWidgetsModel(callbacks);
Sunny Goyal31860582015-09-18 08:38:57 -07001954 }
Sunny Goyald164b7f2016-10-12 20:49:31 -07001955 ArrayList<WidgetItem> allWidgets = mBgWidgetsModel.update(mApp.getContext());
1956 bindWidgetsModel(callbacks);
1957
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001958 // update the Widget entries inside DB on the worker thread.
Sunny Goyal87f784c2017-01-11 10:48:34 -08001959 mApp.getWidgetCache().removeObsoletePreviews(allWidgets);
Sunny Goyal31860582015-09-18 08:38:57 -07001960 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001961 });
Michael Jurkac402cd92013-05-20 15:49:32 +02001962 }
1963
Sunny Goyal651077b2014-06-30 14:15:31 -07001964 static boolean isValidProvider(AppWidgetProviderInfo provider) {
1965 return (provider != null) && (provider.provider != null)
1966 && (provider.provider.getPackageName() != null);
1967 }
1968
Joe Onoratobe386092009-11-17 17:32:16 -08001969 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08001970 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001971 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
1972 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
1973 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
1974 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04001975 if (mLoaderTask != null) {
1976 mLoaderTask.dumpState();
1977 } else {
1978 Log.d(TAG, "mLoaderTask=null");
1979 }
Joe Onoratobe386092009-11-17 17:32:16 -08001980 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08001981
1982 public Callbacks getCallback() {
1983 return mCallbacks != null ? mCallbacks.get() : null;
1984 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07001985
1986 /**
1987 * @return {@link FolderInfo} if its already loaded.
1988 */
1989 public FolderInfo findFolderById(Long folderId) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001990 synchronized (sBgDataModel) {
1991 return sBgDataModel.folders.get(folderId);
Sunny Goyal18bf8e22015-04-08 18:13:46 -07001992 }
1993 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07001994
Sunny Goyal527c7d32015-08-28 15:19:36 -07001995 @Thunk class DeferredMainThreadExecutor implements Executor {
1996
1997 @Override
1998 public void execute(Runnable command) {
1999 runOnMainThread(command);
2000 }
2001 }
2002
Sunny Goyal756adbc2015-04-16 15:20:51 -07002003 /**
2004 * @return the looper for the worker thread which can be used to start background tasks.
2005 */
2006 public static Looper getWorkerLooper() {
2007 return sWorkerThread.getLooper();
2008 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009}