blob: af22ab749457931c0da65e2576fb27c95e382d21 [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.pm.PackageManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.net.Uri;
Joe Onorato36115782010-06-17 13:28:48 -040030import android.os.Handler;
31import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070032import android.os.Looper;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040034import android.os.SystemClock;
Sunny Goyale26d1002016-06-20 14:52:14 -070035import android.os.Trace;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080036import android.os.UserHandle;
Winson Chunga90303b2013-11-15 13:05:06 -080037import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070038import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080039import android.util.LongSparseArray;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070040import android.util.MutableInt;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010041
Sunny Goyalffe83f12014-08-14 17:39:34 -070042import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010043import com.android.launcher3.compat.LauncherActivityInfoCompat;
44import 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 Goyal1acb9e92016-05-16 12:41:09 -070052import com.android.launcher3.logging.FileLog;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070053import com.android.launcher3.model.AddWorkspaceItemsTask;
Sunny Goyale9956a72016-09-01 17:24:47 -070054import com.android.launcher3.model.BgDataModel;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070055import com.android.launcher3.model.CacheDataUpdatedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080056import com.android.launcher3.model.ExtendedModelTask;
Sunny Goyalf862a262015-12-14 14:27:38 -080057import com.android.launcher3.model.GridSizeMigrationTask;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -080058import com.android.launcher3.model.LoaderCursor;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070059import com.android.launcher3.model.PackageInstallStateChangedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080060import com.android.launcher3.model.PackageItemInfo;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070061import com.android.launcher3.model.PackageUpdatedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080062import com.android.launcher3.model.SdCardAvailableReceiver;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070063import com.android.launcher3.model.ShortcutsChangedTask;
64import com.android.launcher3.model.UserLockStateChangedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080065import com.android.launcher3.model.WidgetItem;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070066import com.android.launcher3.model.WidgetsModel;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -070067import com.android.launcher3.provider.ImportDataTask;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070068import com.android.launcher3.provider.LauncherDbUtils;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070069import com.android.launcher3.shortcuts.DeepShortcutManager;
70import com.android.launcher3.shortcuts.ShortcutInfoCompat;
71import com.android.launcher3.shortcuts.ShortcutKey;
Robin Lee26ace122015-03-16 19:41:43 +000072import com.android.launcher3.util.ComponentKey;
Sunny Goyal32f3dda2016-11-11 11:45:00 -080073import com.android.launcher3.util.ContentWriter;
Sunny Goyal40452cf2016-09-01 15:17:46 -070074import com.android.launcher3.util.ItemInfoMatcher;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070075import com.android.launcher3.util.ManagedProfileHeuristic;
Tony Wickhamd82a39d2016-07-01 15:44:13 -070076import com.android.launcher3.util.MultiHashMap;
Sunny Goyald09c3702016-04-06 16:18:20 -070077import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070078import com.android.launcher3.util.Preconditions;
Sunny Goyal2bcbe132016-11-16 09:23:42 -080079import com.android.launcher3.util.Provider;
Adam Cohen091440a2015-03-18 14:16:05 -070080import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070081import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080082
Michael Jurkac2f801e2011-07-12 14:19:46 -070083import java.lang.ref.WeakReference;
84import java.net.URISyntaxException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070085import java.util.ArrayList;
86import java.util.Collections;
87import java.util.Comparator;
88import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070089import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070090import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070091import java.util.List;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070092import java.util.Map;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070093import java.util.Set;
Sunny Goyal527c7d32015-08-28 15:19:36 -070094import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -070095
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096/**
97 * Maintains in-memory state of the Launcher. It is expected that there should be only one
98 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070099 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800100 */
Kenny Guyed131872014-04-30 03:02:21 +0100101public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +0100102 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800103 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -0400104 private static final boolean DEBUG_RECEIVER = false;
Chris Wrenb358f812014-04-16 13:37:00 -0400105
Joe Onorato9c1289c2009-08-17 11:03:03 -0400106 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700107
Joe Onorato36115782010-06-17 13:28:48 -0400108 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500109 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800110
Adam Cohen091440a2015-03-18 14:16:05 -0700111 @Thunk final LauncherAppState mApp;
112 @Thunk final Object mLock = new Object();
113 @Thunk DeferredHandler mHandler = new DeferredHandler();
114 @Thunk LoaderTask mLoaderTask;
115 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700116 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117
Adam Cohen091440a2015-03-18 14:16:05 -0700118 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700119 static {
120 sWorkerThread.start();
121 }
Adam Cohen091440a2015-03-18 14:16:05 -0700122 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700123
Joe Onoratocc67f472010-06-08 10:54:30 -0700124 // We start off with everything not loaded. After that, we assume that
125 // our monitoring of the package manager provides all updates and we never
126 // need to do a requery. These are only ever touched from the loader thread.
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700127 private boolean mWorkspaceLoaded;
128 private boolean mAllAppsLoaded;
129 private boolean mDeepShortcutsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700130
Sunny Goyal756a28a2015-04-23 17:07:55 -0700131 /**
132 * Set of runnables to be called on the background thread after the workspace binding
133 * is complete.
134 */
135 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
136
Adam Cohen091440a2015-03-18 14:16:05 -0700137 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700139 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800140 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700141 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800142 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800143
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700144 private boolean mHasShortcutHostPermission;
145 // Runnable to check if the shortcuts permission has changed.
146 private final Runnable mShortcutPermissionCheckRunnable = new Runnable() {
147 @Override
148 public void run() {
149 if (mDeepShortcutsLoaded) {
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530150 boolean hasShortcutHostPermission =
151 DeepShortcutManager.getInstance(mApp.getContext()).hasHostPermission();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700152 if (hasShortcutHostPermission != mHasShortcutHostPermission) {
153 mApp.reloadWorkspace();
154 }
155 }
156 }
157 };
158
Sunny Goyale9956a72016-09-01 17:24:47 -0700159 /**
160 * All the static data should be accessed on the background thread, A lock should be acquired
161 * on this object when accessing any data from this model.
162 */
163 static final BgDataModel sBgDataModel = new BgDataModel();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700164
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700165 // </ only access in worker thread >
166
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700167 private final IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700169 private final LauncherAppsCompat mLauncherApps;
170 private final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100171
Joe Onorato9c1289c2009-08-17 11:03:03 -0400172 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700173 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400174 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700175 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400176 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700177 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
178 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700179 public void bindScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700180 public void finishFirstPageBind(ViewOnDrawExecutor executor);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800181 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400182 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200183 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700184 public void bindAppsAdded(ArrayList<Long> newScreens,
185 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700186 ArrayList<ItemInfo> addAnimated,
187 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200188 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700189 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800190 ArrayList<ShortcutInfo> removed, UserHandle user);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700191 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700192 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700193 public void bindWorkspaceComponentsRemoved(
194 HashSet<String> packageNames, HashSet<ComponentName> components,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800195 UserHandle user);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700196 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800197 public void notifyWidgetProvidersChanged();
Sunny Goyald164b7f2016-10-12 20:49:31 -0700198 public void bindAllWidgets(MultiHashMap<PackageItemInfo, WidgetItem> widgets);
Adam Cohen1462de32012-07-24 22:34:36 -0700199 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700200 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700201 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMap);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400202 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530204 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800205 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400206 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100207 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Sunny Goyald164b7f2016-10-12 20:49:31 -0700208 mBgWidgetsModel = new WidgetsModel(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800209 mIconCache = iconCache;
210
Kenny Guyed131872014-04-30 03:02:21 +0100211 mLauncherApps = LauncherAppsCompat.getInstance(context);
212 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800213 }
214
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700215 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
216 * posted on the main thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700217 private void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700218 if (sWorkerThread.getThreadId() == Process.myTid()) {
219 // If we are on the worker thread, post onto the main handler
220 mHandler.post(r);
221 } else {
222 r.run();
223 }
224 }
225
226 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
227 * posted on the worker thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700228 private static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700229 if (sWorkerThread.getThreadId() == Process.myTid()) {
230 r.run();
231 } else {
232 // If we are not on the worker thread, then post to the worker handler
233 sWorker.post(r);
234 }
235 }
236
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700237 public void setPackageState(PackageInstallInfo installInfo) {
238 enqueueModelUpdateTask(new PackageInstallStateChangedTask(installInfo));
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500239 }
240
Sunny Goyal756adbc2015-04-16 15:20:51 -0700241 /**
242 * Updates the icons and label of all pending icons for the provided package name.
243 */
244 public void updateSessionDisplayInfo(final String packageName) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700245 HashSet<String> packages = new HashSet<>();
246 packages.add(packageName);
247 enqueueModelUpdateTask(new CacheDataUpdatedTask(
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800248 CacheDataUpdatedTask.OP_SESSION_UPDATE, Process.myUserHandle(), packages));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800249 }
250
251 /**
252 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800253 */
Sunny Goyal2bcbe132016-11-16 09:23:42 -0800254 public void addAndBindAddedWorkspaceItems(List<ItemInfo> workspaceApps) {
255 addAndBindAddedWorkspaceItems(Provider.of(workspaceApps));
256 }
257
258 /**
259 * Adds the provided items to the workspace.
260 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700261 public void addAndBindAddedWorkspaceItems(
Sunny Goyal2bcbe132016-11-16 09:23:42 -0800262 Provider<List<ItemInfo>> appsProvider) {
263 enqueueModelUpdateTask(new AddWorkspaceItemsTask(appsProvider));
Winson Chung64359a52013-07-08 17:17:08 -0700264 }
265
Joe Onorato9c1289c2009-08-17 11:03:03 -0400266 /**
267 * Adds an item to the DB if it was not created previously, or move it to a new
268 * <container, screen, cellX, cellY>
269 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800270 public static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700271 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400272 if (item.container == ItemInfo.NO_ID) {
273 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700274 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400275 } else {
276 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700277 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278 }
279 }
280
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700281 static void checkItemInfoLocked(
282 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
Sunny Goyale9956a72016-09-01 17:24:47 -0700283 ItemInfo modelItem = sBgDataModel.itemsIdMap.get(itemId);
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700284 if (modelItem != null && item != modelItem) {
285 // check all the data is consistent
286 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
287 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
288 ShortcutInfo shortcut = (ShortcutInfo) item;
289 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
290 modelShortcut.intent.filterEquals(shortcut.intent) &&
291 modelShortcut.id == shortcut.id &&
292 modelShortcut.itemType == shortcut.itemType &&
293 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700294 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700295 modelShortcut.cellX == shortcut.cellX &&
296 modelShortcut.cellY == shortcut.cellY &&
297 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700298 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700299 // For all intents and purposes, this is the same object
300 return;
301 }
302 }
303
304 // the modelItem needs to match up perfectly with item if our model is
305 // to be consistent with the database-- for now, just require
306 // modelItem == item or the equality check above
307 String msg = "item: " + ((item != null) ? item.toString() : "null") +
308 "modelItem: " +
309 ((modelItem != null) ? modelItem.toString() : "null") +
310 "Error: ItemInfo passed to checkItemInfo doesn't match original";
311 RuntimeException e = new RuntimeException(msg);
312 if (stackTrace != null) {
313 e.setStackTrace(stackTrace);
314 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800315 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700316 }
317 }
318
Michael Jurka816474f2012-06-25 14:49:02 -0700319 static void checkItemInfo(final ItemInfo item) {
320 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
321 final long itemId = item.id;
322 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700323 public void run() {
Sunny Goyale9956a72016-09-01 17:24:47 -0700324 synchronized (sBgDataModel) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700325 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700326 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700327 }
328 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700329 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700330 }
331
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800332 static void updateItemInDatabaseHelper(Context context, final ContentWriter writer,
Michael Jurkac9d95c52011-08-29 14:03:34 -0700333 final ItemInfo item, final String callingFunction) {
334 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700335 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700336 final ContentResolver cr = context.getContentResolver();
337
Adam Cohen487f7dd2012-06-28 18:12:10 -0700338 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Sunny Goyal87f784c2017-01-11 10:48:34 -0800339 final Context appContext = context.getApplicationContext();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700340 Runnable r = new Runnable() {
341 public void run() {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800342 cr.update(uri, writer.getValues(appContext), null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700343 updateItemArrays(item, itemId, stackTrace);
344 }
345 };
346 runOnWorkerThread(r);
347 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700348
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700349 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
350 final ArrayList<ItemInfo> items, final String callingFunction) {
351 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700352
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700353 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
354 Runnable r = new Runnable() {
355 public void run() {
356 ArrayList<ContentProviderOperation> ops =
357 new ArrayList<ContentProviderOperation>();
358 int count = items.size();
359 for (int i = 0; i < count; i++) {
360 ItemInfo item = items.get(i);
361 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700362 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700363 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700364
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700365 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
366 updateItemArrays(item, itemId, stackTrace);
367
368 }
369 try {
370 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
371 } catch (Exception e) {
372 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700373 }
374 }
375 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700376 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700377 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700378
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700379 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
380 // Lock on mBgLock *after* the db operation
Sunny Goyale9956a72016-09-01 17:24:47 -0700381 synchronized (sBgDataModel) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700382 checkItemInfoLocked(itemId, item, stackTrace);
383
384 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
385 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
386 // Item is in a folder, make sure this folder exists
Sunny Goyale9956a72016-09-01 17:24:47 -0700387 if (!sBgDataModel.folders.containsKey(item.container)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700388 // An items container is being set to a that of an item which is not in
389 // the list of Folders.
390 String msg = "item: " + item + " container being set to: " +
391 item.container + ", not in the list of folders";
392 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700393 }
394 }
395
396 // Items are added/removed from the corresponding FolderInfo elsewhere, such
397 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
398 // that are on the desktop, as appropriate
Sunny Goyale9956a72016-09-01 17:24:47 -0700399 ItemInfo modelItem = sBgDataModel.itemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800400 if (modelItem != null &&
401 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
402 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700403 switch (modelItem.itemType) {
404 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
405 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700406 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700407 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyale9956a72016-09-01 17:24:47 -0700408 if (!sBgDataModel.workspaceItems.contains(modelItem)) {
409 sBgDataModel.workspaceItems.add(modelItem);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700410 }
411 break;
412 default:
413 break;
414 }
415 } else {
Sunny Goyale9956a72016-09-01 17:24:47 -0700416 sBgDataModel.workspaceItems.remove(modelItem);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700417 }
418 }
419 }
420
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800421 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400422 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700423 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700424 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700425 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400426 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400427 item.cellX = cellX;
428 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700429
Winson Chung3d503fb2011-07-13 17:25:49 -0700430 // We store hotseat items in canonical form which is this orientation invariant position
431 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700432 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700433 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700434 item.screenId = Launcher.getLauncher(context).getHotseat()
435 .getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700436 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700437 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700438 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400439
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800440 final ContentWriter writer = new ContentWriter(context)
441 .put(LauncherSettings.Favorites.CONTAINER, item.container)
442 .put(LauncherSettings.Favorites.CELLX, item.cellX)
443 .put(LauncherSettings.Favorites.CELLY, item.cellY)
444 .put(LauncherSettings.Favorites.RANK, item.rank)
445 .put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400446
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800447 updateItemInDatabaseHelper(context, writer, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700448 }
449
450 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700451 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
452 * cellX, cellY have already been updated on the ItemInfos.
453 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800454 public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700455 final long container, final int screen) {
456
457 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
458 int count = items.size();
459
460 for (int i = 0; i < count; i++) {
461 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700462 item.container = container;
463
464 // We store hotseat items in canonical form which is this orientation invariant position
465 // in the hotseat
466 if (context instanceof Launcher && screen < 0 &&
467 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700468 item.screenId = Launcher.getLauncher(context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700469 item.cellY);
470 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700471 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700472 }
473
474 final ContentValues values = new ContentValues();
475 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
476 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
477 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800478 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700479 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700480
481 contentValues.add(values);
482 }
483 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
484 }
485
486 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700487 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800488 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700489 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700490 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700491 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800492 item.cellX = cellX;
493 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700494 item.spanX = spanX;
495 item.spanY = spanY;
496
497 // We store hotseat items in canonical form which is this orientation invariant position
498 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700499 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700500 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700501 item.screenId = Launcher.getLauncher(context).getHotseat()
502 .getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700503 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700504 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700505 }
Adam Cohend4844c32011-02-18 19:25:06 -0800506
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800507 final ContentWriter writer = new ContentWriter(context)
508 .put(LauncherSettings.Favorites.CONTAINER, item.container)
509 .put(LauncherSettings.Favorites.CELLX, item.cellX)
510 .put(LauncherSettings.Favorites.CELLY, item.cellY)
511 .put(LauncherSettings.Favorites.RANK, item.rank)
512 .put(LauncherSettings.Favorites.SPANX, item.spanX)
513 .put(LauncherSettings.Favorites.SPANY, item.spanY)
514 .put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800515
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800516 updateItemInDatabaseHelper(context, writer, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700517 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700518
519 /**
520 * Update an item to the database in a specified container.
521 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700522 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800523 ContentWriter writer = new ContentWriter(context);
524 item.onAddToDatabase(writer);
525 updateItemInDatabaseHelper(context, writer, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800526 }
527
Joe Onorato9c1289c2009-08-17 11:03:03 -0400528 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400529 * Add an item to the database in a specified container. Sets the container, screen, cellX and
530 * cellY fields of the item. Also assigns an ID to the item.
531 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700532 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700533 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400534 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400535 item.cellX = cellX;
536 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700537 // We store hotseat items in canonical form which is this orientation invariant position
538 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700539 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700540 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700541 item.screenId = Launcher.getLauncher(context).getHotseat()
542 .getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700543 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700544 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700545 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400546
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800547 final ContentWriter writer = new ContentWriter(context);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400548 final ContentResolver cr = context.getContentResolver();
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800549 item.onAddToDatabase(writer);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400550
Sunny Goyald2497482015-09-22 18:24:19 -0700551 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
552 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
553
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800554 writer.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700555
Jason Monk8e19cf22014-03-20 15:06:57 -0400556 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Sunny Goyal87f784c2017-01-11 10:48:34 -0800557 final Context appContext = context.getApplicationContext();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700558 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700559 public void run() {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800560 cr.insert(LauncherSettings.Favorites.CONTENT_URI, writer.getValues(appContext));
Joe Onorato9c1289c2009-08-17 11:03:03 -0400561
Sunny Goyale9956a72016-09-01 17:24:47 -0700562 synchronized (sBgDataModel) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400563 checkItemInfoLocked(item.id, item, stackTrace);
Sunny Goyal87f784c2017-01-11 10:48:34 -0800564 sBgDataModel.addItem(appContext, item, true);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700565 }
566 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700567 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700568 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700569 }
570
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700571 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700572 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -0400573 */
Sunny Goyalfa401a12015-04-10 13:45:42 -0700574 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyal40452cf2016-09-01 15:17:46 -0700575 ArrayList<ItemInfo> items = new ArrayList<>();
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700576 items.add(item);
577 deleteItemsFromDatabase(context, items);
578 }
579
580 /**
Sunny Goyal40452cf2016-09-01 15:17:46 -0700581 * Removes all the items from the database matching {@param matcher}.
582 */
583 public static void deleteItemsFromDatabase(Context context, ItemInfoMatcher matcher) {
Sunny Goyale9956a72016-09-01 17:24:47 -0700584 deleteItemsFromDatabase(context, matcher.filterItemInfos(sBgDataModel.itemsIdMap));
Sunny Goyal40452cf2016-09-01 15:17:46 -0700585 }
586
587 /**
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700588 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700589 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700590 public static void deleteItemsFromDatabase(Context context,
591 final Iterable<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400592 final ContentResolver cr = context.getContentResolver();
Sunny Goyal87f784c2017-01-11 10:48:34 -0800593 final Context appContext = context.getApplicationContext();
Michael Jurka83df1882011-08-31 20:59:26 -0700594 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700595 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700596 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700597 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700598 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700599
Sunny Goyal87f784c2017-01-11 10:48:34 -0800600 sBgDataModel.removeItem(appContext, item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700601 }
602 }
Michael Jurka83df1882011-08-31 20:59:26 -0700603 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700604 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400605 }
606
607 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700608 * Update the order of the workspace screens in the database. The array list contains
609 * a list of screen ids in the order that they should appear.
610 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700611 public static void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -0700612 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700613 final ContentResolver cr = context.getContentResolver();
614 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
615
616 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700617 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700618 while (iter.hasNext()) {
619 long id = iter.next();
620 if (id < 0) {
621 iter.remove();
622 }
623 }
624
625 Runnable r = new Runnable() {
626 @Override
627 public void run() {
Yura085c8532014-02-11 15:15:29 +0000628 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700629 // Clear the table
Yura085c8532014-02-11 15:15:29 +0000630 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -0700631 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700632 for (int i = 0; i < count; i++) {
633 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700634 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700635 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
636 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +0000637 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -0700638 }
Yura085c8532014-02-11 15:15:29 +0000639
640 try {
641 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
642 } catch (Exception ex) {
643 throw new RuntimeException(ex);
644 }
Winson Chung9e6a0a22013-08-27 11:58:12 -0700645
Sunny Goyale9956a72016-09-01 17:24:47 -0700646 synchronized (sBgDataModel) {
647 sBgDataModel.workspaceScreens.clear();
648 sBgDataModel.workspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -0700649 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700650 }
651 };
652 runOnWorkerThread(r);
653 }
654
655 /**
Winsonc0b52fe2015-09-09 16:38:15 -0700656 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -0400657 */
Winsonc0b52fe2015-09-09 16:38:15 -0700658 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400659 final ContentResolver cr = context.getContentResolver();
Sunny Goyal87f784c2017-01-11 10:48:34 -0800660 final Context appContext = context.getApplicationContext();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400661
Michael Jurkac9d95c52011-08-29 14:03:34 -0700662 Runnable r = new Runnable() {
663 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700664 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -0700665 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Sunny Goyal87f784c2017-01-11 10:48:34 -0800666 sBgDataModel.removeItem(appContext, info.contents);
Sunny Goyale9956a72016-09-01 17:24:47 -0700667 info.contents.clear();
668
669 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Sunny Goyal87f784c2017-01-11 10:48:34 -0800670 sBgDataModel.removeItem(appContext, info);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700671 }
672 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700673 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400674 }
675
676 /**
677 * Set this as the current Launcher activity object for the loader.
678 */
679 public void initialize(Callbacks callbacks) {
680 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -0700681 Preconditions.assertUIThread();
682 // Remove any queued UI runnables
683 mHandler.cancelAll();
684 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400685 }
686 }
687
Kenny Guyed131872014-04-30 03:02:21 +0100688 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800689 public void onPackageChanged(String packageName, UserHandle user) {
Kenny Guyed131872014-04-30 03:02:21 +0100690 int op = PackageUpdatedTask.OP_UPDATE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700691 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100692 }
693
694 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800695 public void onPackageRemoved(String packageName, UserHandle user) {
Sunny Goyalc2936bc2016-09-01 15:50:36 -0700696 onPackagesRemoved(user, packageName);
697 }
698
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800699 public void onPackagesRemoved(UserHandle user, String... packages) {
Kenny Guyed131872014-04-30 03:02:21 +0100700 int op = PackageUpdatedTask.OP_REMOVE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700701 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packages));
Kenny Guyed131872014-04-30 03:02:21 +0100702 }
703
704 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800705 public void onPackageAdded(String packageName, UserHandle user) {
Kenny Guyed131872014-04-30 03:02:21 +0100706 int op = PackageUpdatedTask.OP_ADD;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700707 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100708 }
709
710 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800711 public void onPackagesAvailable(String[] packageNames, UserHandle user,
Kenny Guyed131872014-04-30 03:02:21 +0100712 boolean replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700713 enqueueModelUpdateTask(
714 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100715 }
716
717 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800718 public void onPackagesUnavailable(String[] packageNames, UserHandle user,
Kenny Guyed131872014-04-30 03:02:21 +0100719 boolean replacing) {
720 if (!replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700721 enqueueModelUpdateTask(new PackageUpdatedTask(
722 PackageUpdatedTask.OP_UNAVAILABLE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100723 }
Kenny Guyed131872014-04-30 03:02:21 +0100724 }
725
Kenny Guy44cba692016-01-21 19:50:02 +0000726 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800727 public void onPackagesSuspended(String[] packageNames, UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700728 enqueueModelUpdateTask(new PackageUpdatedTask(
729 PackageUpdatedTask.OP_SUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000730 }
731
732 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800733 public void onPackagesUnsuspended(String[] packageNames, UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700734 enqueueModelUpdateTask(new PackageUpdatedTask(
735 PackageUpdatedTask.OP_UNSUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000736 }
737
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700738 @Override
739 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800740 UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700741 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, true));
Sunny Goyal50941fb2016-08-04 12:03:52 -0700742 }
743
744 public void updatePinnedShortcuts(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800745 UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700746 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, false));
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700747 }
748
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700749 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400750 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
751 * ACTION_PACKAGE_CHANGED.
752 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100753 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400754 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -0400755 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700756
Joe Onorato36115782010-06-17 13:28:48 -0400757 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +0100758 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -0700759 // If we have changed locale we need to clear out the labels in all apps/workspace.
760 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700761 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
762 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -0700763 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -0700764 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700765 } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
766 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
767 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800768 UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER);
Sunny Goyalda891c12016-03-18 18:29:24 -0700769 if (user != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700770 if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
771 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700772 enqueueModelUpdateTask(new PackageUpdatedTask(
773 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE, user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700774 }
775
776 // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so
777 // we need to run the state change task again.
778 if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
779 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700780 enqueueModelUpdateTask(new UserLockStateChangedTask(user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700781 }
Sunny Goyalda891c12016-03-18 18:29:24 -0700782 }
Tony Wickham827cef22016-03-17 15:39:39 -0700783 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
784 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700785 }
786 }
787
Amith Yamasani6cc806d2014-05-02 13:47:11 -0700788 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700789 resetLoadedState(true, true);
790
Reena Lee93f824a2011-09-23 17:20:28 -0700791 // Do this here because if the launcher activity is running it will be restarted.
792 // If it's not running startLoaderFromBackground will merely tell it that it needs
793 // to reload.
794 startLoaderFromBackground();
795 }
796
Winson Chungf0c6ae02012-03-21 16:10:31 -0700797 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
798 synchronized (mLock) {
799 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
800 // mWorkspaceLoaded to true later
801 stopLoaderLocked();
802 if (resetAllAppsLoaded) mAllAppsLoaded = false;
803 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700804 // Always reset deep shortcuts loaded.
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700805 // TODO: why?
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700806 mDeepShortcutsLoaded = false;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700807 }
808 }
809
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700810 /**
811 * When the launcher is in the background, it's possible for it to miss paired
812 * configuration changes. So whenever we trigger the loader from the background
813 * tell the launcher that it needs to re-run the loader when it comes back instead
814 * of doing it now.
815 */
816 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800817 Callbacks callbacks = getCallback();
818 if (callbacks != null) {
819 // Only actually run the loader if they're not paused.
820 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -0700821 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700822 }
823 }
Joe Onorato36115782010-06-17 13:28:48 -0400824 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400825
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700826 /**
827 * If there is already a loader task running, tell it to stop.
828 */
829 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -0700830 LoaderTask oldTask = mLoaderTask;
831 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -0700832 oldTask.stopLocked();
833 }
Reena Lee93f824a2011-09-23 17:20:28 -0700834 }
835
Adam Cohen1a85c582014-09-30 09:48:49 -0700836 public boolean isCurrentCallbacks(Callbacks callbacks) {
837 return (mCallbacks != null && mCallbacks.get() == callbacks);
838 }
839
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700840 /**
841 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
842 * @return true if the page could be bound synchronously.
843 */
844 public boolean startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700845 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
846 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -0400847 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -0400848 // Don't bother to start the thread if we know it's not going to do anything
849 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -0700850 final Callbacks oldCallbacks = mCallbacks.get();
851 // Clear any pending bind-runnables from the synchronized load process.
852 runOnMainThread(new Runnable() {
853 public void run() {
854 oldCallbacks.clearPendingBinds();
855 }
856 });
857
Joe Onorato36115782010-06-17 13:28:48 -0400858 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700859 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700860 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700861 // TODO: mDeepShortcutsLoaded does not need to be true for synchronous bind.
Tony Wickham80f57872016-06-29 18:12:15 -0700862 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE && mAllAppsLoaded
863 && mWorkspaceLoaded && mDeepShortcutsLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700864 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700865 return true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700866 } else {
867 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
868 sWorker.post(mLoaderTask);
869 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400870 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400871 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700872 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400873 }
874
Joe Onorato36115782010-06-17 13:28:48 -0400875 public void stopLoader() {
876 synchronized (mLock) {
877 if (mLoaderTask != null) {
878 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400879 }
880 }
Joe Onorato36115782010-06-17 13:28:48 -0400881 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400882
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800883 /**
884 * Loads the workspace screen ids in an ordered list.
885 */
Sunny Goyale5bb7052015-07-27 14:36:07 -0700886 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -0700887 final ContentResolver contentResolver = context.getContentResolver();
888 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -0700889
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800890 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -0700891 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
892 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -0700893 }
894
Joe Onorato36115782010-06-17 13:28:48 -0400895 /**
896 * Runnable for the thread that loads the contents of the launcher:
897 * - workspace icons
898 * - widgets
899 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700900 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -0400901 */
902 private class LoaderTask implements Runnable {
903 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -0700904 private int mPageToBindFirst;
905
Adam Cohen091440a2015-03-18 14:16:05 -0700906 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -0400907 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -0700908 @Thunk boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700909
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700910 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -0400911 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -0700912 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400913 }
914
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800915 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700916 mIsLoadingAndBindingWorkspace = true;
917
Joe Onorato36115782010-06-17 13:28:48 -0400918 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -0400919 if (DEBUG_LOADERS) {
920 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400921 }
Michael Jurka288a36b2011-07-12 16:53:48 -0700922
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700923 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800924 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -0700925 synchronized (LoaderTask.this) {
926 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800927 return;
Reena Lee93f824a2011-09-23 17:20:28 -0700928 }
929 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400930 }
931 }
932
Joe Onorato36115782010-06-17 13:28:48 -0400933 // Bind the workspace
Sunny Goyal93f878c2016-03-30 17:31:24 -0700934 bindWorkspace(mPageToBindFirst);
Joe Onorato36115782010-06-17 13:28:48 -0400935 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400936
Joe Onorato36115782010-06-17 13:28:48 -0400937 private void waitForIdle() {
938 // Wait until the either we're stopped or the other threads are done.
939 // This way we don't start loading all apps until the workspace has settled
940 // down.
941 synchronized (LoaderTask.this) {
942 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700943
Joe Onorato36115782010-06-17 13:28:48 -0400944 mHandler.postIdle(new Runnable() {
945 public void run() {
946 synchronized (LoaderTask.this) {
947 mLoadAndBindStepFinished = true;
948 if (DEBUG_LOADERS) {
949 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400950 }
Joe Onorato36115782010-06-17 13:28:48 -0400951 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400952 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400953 }
Joe Onorato36115782010-06-17 13:28:48 -0400954 });
955
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800956 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -0400957 try {
Michael Jurkac7700af2013-05-14 20:17:58 +0200958 // Just in case mFlushingWorkerThread changes but we aren't woken up,
959 // wait no longer than 1sec at a time
960 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -0400961 } catch (InterruptedException ex) {
962 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400963 }
964 }
Joe Onorato36115782010-06-17 13:28:48 -0400965 if (DEBUG_LOADERS) {
966 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700967 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400968 + "ms for previous step to finish binding");
969 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400970 }
Joe Onorato36115782010-06-17 13:28:48 -0400971 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400972
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700973 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -0500974 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700975 // Ensure that we have a valid page index to load synchronously
976 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
977 "valid page index");
978 }
979 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
980 // Ensure that we don't try and bind a specified page when the pages have not been
981 // loaded already (we should load everything asynchronously in that case)
982 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
983 }
984 synchronized (mLock) {
985 if (mIsLoaderTaskRunning) {
986 // Ensure that we are never running the background loading at this point since
987 // we also touch the background collections
988 throw new RuntimeException("Error! Background loading is already running");
989 }
990 }
991
992 // XXX: Throw an exception if we are already loading (since we touch the worker thread
993 // data structures, we can't allow any other thread to touch that data, but because
994 // this call is synchronous, we can get away with not locking).
995
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400996 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -0700997 // operations from the previous activity. We need to ensure that all queued operations
998 // are executed before any synchronous binding work is done.
999 mHandler.flush();
1000
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001001 // Divide the set of loaded items into those that we are binding synchronously, and
1002 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001003 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001004 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1005 // arise from that.
1006 onlyBindAllApps();
Tony Wickham80f57872016-06-29 18:12:15 -07001007
1008 bindDeepShortcuts();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001009 }
1010
Joe Onorato36115782010-06-17 13:28:48 -04001011 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001012 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001013 if (mStopped) {
1014 return;
1015 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001016 mIsLoaderTaskRunning = true;
1017 }
Joe Onorato36115782010-06-17 13:28:48 -04001018 // Optimize for end-user experience: if the Launcher is up and // running with the
1019 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1020 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001021 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001022 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001023 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001024
Joe Onorato36115782010-06-17 13:28:48 -04001025 if (mStopped) {
1026 break keep_running;
1027 }
1028
Joe Onorato36115782010-06-17 13:28:48 -04001029 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001030
1031 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001032 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1033 loadAndBindAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001034
1035 waitForIdle();
1036
1037 // third step
1038 if (DEBUG_LOADERS) Log.d(TAG, "step 3: loading deep shortcuts");
1039 loadAndBindDeepShortcuts();
Joe Onorato36115782010-06-17 13:28:48 -04001040 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001041
Joe Onorato36115782010-06-17 13:28:48 -04001042 // Clear out this reference, otherwise we end up holding it until all of the
1043 // callback runnables are done.
1044 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001045
Joe Onorato36115782010-06-17 13:28:48 -04001046 synchronized (mLock) {
1047 // If we are still the last one to be scheduled, remove ourselves.
1048 if (mLoaderTask == this) {
1049 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001050 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001051 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001052 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001053 }
Joe Onorato36115782010-06-17 13:28:48 -04001054 }
1055
1056 public void stopLocked() {
1057 synchronized (LoaderTask.this) {
1058 mStopped = true;
1059 this.notify();
1060 }
1061 }
1062
1063 /**
1064 * Gets the callbacks object. If we've been stopped, or if the launcher object
1065 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1066 * object that was around when the deferred message was scheduled, and if there's
1067 * a new Callbacks object around then also return null. This will save us from
1068 * calling onto it with data that will be ignored.
1069 */
1070 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1071 synchronized (mLock) {
1072 if (mStopped) {
1073 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001074 }
Joe Onorato36115782010-06-17 13:28:48 -04001075
1076 if (mCallbacks == null) {
1077 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001078 }
Joe Onorato36115782010-06-17 13:28:48 -04001079
1080 final Callbacks callbacks = mCallbacks.get();
1081 if (callbacks != oldCallbacks) {
1082 return null;
1083 }
1084 if (callbacks == null) {
1085 Log.w(TAG, "no mCallbacks");
1086 return null;
1087 }
1088
1089 return callbacks;
1090 }
1091 }
1092
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001093 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -07001094 if (LauncherAppState.PROFILE_STARTUP) {
1095 Trace.beginSection("Loading Workspace");
1096 }
Joe Onorato36115782010-06-17 13:28:48 -04001097 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001098
Joe Onorato36115782010-06-17 13:28:48 -04001099 final Context context = mContext;
1100 final ContentResolver contentResolver = context.getContentResolver();
1101 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001102 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001103 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301104 final DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001105 final boolean isSdCardReady = Utilities.isBootCompleted();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001106 final MultiHashMap<UserHandle, String> pendingPackages = new MultiHashMap<>();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001107
Sunny Goyal87f784c2017-01-11 10:48:34 -08001108 InvariantDeviceProfile profile = mApp.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001109 int countX = profile.numColumns;
1110 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001111
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001112 boolean clearDb = false;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -07001113 try {
1114 ImportDataTask.performImportIfPossible(context);
1115 } catch (Exception e) {
1116 // Migration failed. Clear workspace.
1117 clearDb = true;
1118 }
1119
1120 if (!clearDb && GridSizeMigrationTask.ENABLED &&
Sunny Goyalf076eae2016-01-11 12:25:10 -08001121 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1122 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001123 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001124 }
1125
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001126 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -07001127 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001128 LauncherSettings.Settings.call(contentResolver,
1129 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001130 }
1131
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001132 Log.d(TAG, "loadWorkspace: loading default favorites");
1133 LauncherSettings.Settings.call(contentResolver,
1134 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -07001135
Sunny Goyale9956a72016-09-01 17:24:47 -07001136 synchronized (sBgDataModel) {
1137 sBgDataModel.clear();
1138
Sunny Goyal756adbc2015-04-16 15:20:51 -07001139 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001140 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyale9956a72016-09-01 17:24:47 -07001141 sBgDataModel.workspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001142
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001143 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001144 final LoaderCursor c = new LoaderCursor(contentResolver.query(
1145 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null), mApp);
Daniel Sandler8802e962010-05-26 16:28:16 -04001146
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001147 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001148
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001149 try {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001150 final int intentIndex = c.getColumnIndexOrThrow
1151 (LauncherSettings.Favorites.INTENT);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001152 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1153 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001154 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1155 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001156 final int spanXIndex = c.getColumnIndexOrThrow
1157 (LauncherSettings.Favorites.SPANX);
1158 final int spanYIndex = c.getColumnIndexOrThrow(
1159 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001160 final int rankIndex = c.getColumnIndexOrThrow(
1161 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001162 final int restoredIndex = c.getColumnIndexOrThrow(
1163 LauncherSettings.Favorites.RESTORED);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001164 final int optionsIndex = c.getColumnIndexOrThrow(
1165 LauncherSettings.Favorites.OPTIONS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001166
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001167 final LongSparseArray<UserHandle> allUsers = c.allUsers;
Kenny Guyff05f432016-01-22 17:48:29 +00001168 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001169 final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001170 for (UserHandle user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001171 long serialNo = mUserManager.getSerialNumberForUser(user);
1172 allUsers.put(serialNo, user);
1173 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001174
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001175 boolean userUnlocked = mUserManager.isUserUnlocked(user);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001176
1177 // We can only query for shortcuts when the user is unlocked.
1178 if (userUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07001179 List<ShortcutInfoCompat> pinnedShortcuts =
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301180 shortcutManager.queryForPinnedShortcuts(null, user);
1181 if (shortcutManager.wasLastCallSuccess()) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07001182 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
1183 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
1184 shortcut);
1185 }
1186 } else {
1187 // Shortcut manager can fail due to some race condition when the
1188 // lock state changes too frequently. For the purpose of the loading
1189 // shortcuts, consider the user is still locked.
1190 userUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001191 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001192 }
Sunny Goyal49f4f032016-08-01 15:45:49 -07001193 unlockedUsers.put(serialNo, userUnlocked);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001194 }
1195
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001196 ShortcutInfo info;
1197 String intentDescription;
1198 LauncherAppWidgetInfo appWidgetInfo;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001199 Intent intent;
Sunny Goyald09c3702016-04-06 16:18:20 -07001200 String targetPackage;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001201
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001202 while (!mStopped && c.moveToNext()) {
1203 try {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001204 if (c.user == null) {
1205 // User has been deleted, remove the item.
1206 c.markDeleted("User has been deleted");
1207 continue;
1208 }
1209
Chris Wrenf4d08112014-01-16 18:13:56 -05001210 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001211 boolean allowMissingTarget = false;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001212 switch (c.itemType) {
1213 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: {
1214 if (!Process.myUserHandle().equals(c.user)) {
1215 c.markDeleted("Legacy shortcuts are only allowed for default user");
1216 continue;
1217 }
1218 // Follow through.
1219 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001220 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001221 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001222 intentDescription = c.getString(intentIndex);
Sunny Goyal34942622014-08-29 17:20:55 -07001223 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001224 int disabledState = 0;
Sunny Goyald09c3702016-04-06 16:18:20 -07001225 targetPackage = null;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001226
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001227 try {
1228 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001229 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001230 if (cn != null && cn.getPackageName() != null) {
1231 boolean validPkg = launcherApps.isPackageEnabledForProfile(
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001232 cn.getPackageName(), c.user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001233 boolean validComponent = validPkg &&
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001234 launcherApps.isActivityEnabledForProfile(cn, c.user);
Sunny Goyald09c3702016-04-06 16:18:20 -07001235 if (validPkg) {
1236 targetPackage = cn.getPackageName();
1237 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001238
1239 if (validComponent) {
1240 if (restored) {
1241 // no special handling necessary for this item
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001242 c.markRestored();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001243 restored = false;
1244 }
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001245 if (quietMode.get(c.serialNumber)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001246 disabledState = ShortcutInfo.FLAG_DISABLED_QUIET_USER;
Kenny Guyff05f432016-01-22 17:48:29 +00001247 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001248 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001249 intent = null;
1250 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1251 // We allow auto install apps to have their intent
1252 // updated after an install.
1253 intent = manager.getLaunchIntentForPackage(
1254 cn.getPackageName());
1255 if (intent != null) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001256 c.updater().put(
1257 LauncherSettings.Favorites.INTENT,
1258 intent.toUri(0)).commit();
Sunny Goyal34942622014-08-29 17:20:55 -07001259 }
1260 }
1261
1262 if (intent == null) {
1263 // The app is installed but the component is no
1264 // longer available.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001265 c.markDeleted("Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001266 continue;
1267 } else {
1268 // no special handling necessary for this item
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001269 c.markRestored();
Sunny Goyal34942622014-08-29 17:20:55 -07001270 restored = false;
1271 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001272 } else if (restored) {
1273 // Package is not yet available but might be
1274 // installed later.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001275 FileLog.d(TAG, "package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001276
1277 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1278 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001279 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001280 // App restore has started. Update the flag
1281 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001282 c.updater().put(
1283 LauncherSettings.Favorites.RESTORED,
1284 promiseType).commit();
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001285 } else {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001286 c.markDeleted("Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001287 continue;
1288 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001289 } else if (PackageManagerHelper.isAppOnSdcard(
1290 manager, cn.getPackageName())) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07001291 // Package is present but not available.
1292 allowMissingTarget = true;
1293 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1294 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001295 // SdCard is not ready yet. Package might get available,
1296 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001297 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001298 pendingPackages.addToList(c.user, cn.getPackageName());
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001299 allowMissingTarget = true;
1300 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001301
1302 } else {
1303 // Do not wait for external media load anymore.
1304 // Log the invalid package, and remove it
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001305 c.markDeleted("Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001306 continue;
Winson Chungee055712013-07-30 14:46:24 -07001307 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001308 } else if (cn == null) {
1309 // For shortcuts with no component, keep them as they are
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001310 c.markRestored();
Sunny Goyal938a53d2014-09-05 03:17:45 -07001311 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001312 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001313 } catch (URISyntaxException e) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001314 c.markDeleted("Invalid uri: " + intentDescription);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001315 continue;
1316 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001317
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001318 boolean useLowResIcon = !c.isOnWorkspaceOrHotseat() &&
Sunny Goyal34b65272015-03-11 16:56:52 -07001319 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1320
Sunny Goyala32bf9b2017-01-05 14:03:59 -08001321 if (restored) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001322 if (c.user.equals(Process.myUserHandle())) {
1323 info = c.getRestoredItemInfo(intent, promiseType);
1324 intent = PackageManagerHelper.getMarketIntent(
1325 intent.getComponent().getPackageName());
Kenny Guyed131872014-04-30 03:02:21 +01001326 } else {
1327 // Don't restore items for other profiles.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001328 c.markDeleted("Restore from managed profile not supported");
Kenny Guyed131872014-04-30 03:02:21 +01001329 continue;
1330 }
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001331 } else if (c.itemType ==
Chris Wrenf4d08112014-01-16 18:13:56 -05001332 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001333 info = c.getAppShortcutInfo(
1334 intent, allowMissingTarget, useLowResIcon);
1335 } else if (c.itemType ==
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001336 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001337
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001338 ShortcutKey key = ShortcutKey.fromIntent(intent, c.user);
1339 if (unlockedUsers.get(c.serialNumber)) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001340 ShortcutInfoCompat pinnedShortcut =
1341 shortcutKeyToPinnedShortcuts.get(key);
1342 if (pinnedShortcut == null) {
1343 // The shortcut is no longer valid.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001344 c.markDeleted("Pinned shortcut not found");
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001345 continue;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001346 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001347 info = new ShortcutInfo(pinnedShortcut, context);
1348 intent = info.intent;
1349 } else {
1350 // Create a shortcut info in disabled mode for now.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001351 info = c.loadSimpleShortcut();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001352 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001353 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001354 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001355 info = c.loadSimpleShortcut();
Michael Jurka96879562012-03-22 05:54:33 -07001356
Sunny Goyald09c3702016-04-06 16:18:20 -07001357 // Shortcuts are only available on the primary profile
1358 if (PackageManagerHelper.isAppSuspended(manager, targetPackage)) {
1359 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1360 }
1361
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001362 // App shortcuts that used to be automatically added to Launcher
1363 // didn't always have the correct intent flags set, so do that
1364 // here
1365 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001366 intent.getCategories() != null &&
1367 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001368 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001369 intent.addFlags(
1370 Intent.FLAG_ACTIVITY_NEW_TASK |
1371 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1372 }
Michael Jurka96879562012-03-22 05:54:33 -07001373 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001374
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001375 if (info != null) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001376 c.applyCommonProperties(info);
1377
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001378 info.intent = intent;
Sunny Goyal08f72612015-01-05 13:41:43 -08001379 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001380 info.spanX = 1;
1381 info.spanY = 1;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001382 // TODO: Remove this extra. Instead we should be using
1383 // itemInfo#user.
1384 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, c.serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001385 if (info.promisedIntent != null) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001386 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, c.serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001387 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001388 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001389 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1390 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1391 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001392
Sunny Goyal756adbc2015-04-16 15:20:51 -07001393 if (restored) {
1394 ComponentName cn = info.getTargetComponent();
1395 if (cn != null) {
1396 Integer progress = installingPkgs.get(cn.getPackageName());
1397 if (progress != null) {
1398 info.setInstallProgress(progress);
1399 } else {
1400 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
1401 }
1402 }
1403 }
1404
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001405 c.checkAndAddItem(info, sBgDataModel);
Winson Chung1323b482013-08-05 12:41:55 -07001406 } else {
1407 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001408 }
1409 break;
1410
1411 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001412 FolderInfo folderInfo = sBgDataModel.findOrMakeFolder(c.id);
1413 c.applyCommonProperties(folderInfo);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001414
Sunny Goyala508e4f2015-05-21 09:33:57 -07001415 // Do not trim the folder label, as is was set by the user.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001416 folderInfo.title = c.getString(c.titleIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001417 folderInfo.spanX = 1;
1418 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001419 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001420
Chris Wrenf4d08112014-01-16 18:13:56 -05001421 if (restored) {
1422 // no special handling required for restored folders
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001423 c.markRestored();
Chris Wrenf4d08112014-01-16 18:13:56 -05001424 }
1425
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001426 c.checkAndAddItem(folderInfo, sBgDataModel);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001427 break;
1428
1429 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08001430 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001431 // Read all Launcher-specific widget details
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001432 boolean customWidget = c.itemType ==
Adam Cohen59400422014-03-05 18:07:04 -08001433 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
1434
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001435 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001436 String savedProvider = c.getString(appWidgetProviderIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001437
Sunny Goyalff572272014-07-23 13:58:07 -07001438 final ComponentName component =
1439 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04001440
Sunny Goyal651077b2014-06-30 14:15:31 -07001441 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07001442 final boolean isIdValid = (restoreStatus &
1443 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07001444 final boolean wasProviderReady = (restoreStatus &
1445 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07001446
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001447 if (widgetProvidersMap == null) {
1448 widgetProvidersMap = AppWidgetManagerCompat
1449 .getInstance(mContext).getAllProvidersMap();
1450 }
1451 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
1452 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00001453 ComponentName.unflattenFromString(savedProvider),
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001454 c.user));
Sunny Goyalff572272014-07-23 13:58:07 -07001455
1456 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08001457 if (!isSafeMode && !customWidget &&
1458 wasProviderReady && !isProviderReady) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001459 c.markDeleted(
1460 "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07001461 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001462 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07001463 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07001464 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1465 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08001466
Sunny Goyal53f96722015-07-13 19:54:53 -07001467 // The provider is available. So the widget is either
1468 // available or not available. We do not need to track
1469 // any future restore updates.
1470 int status = restoreStatus &
1471 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07001472 if (!wasProviderReady) {
1473 // If provider was not previously ready, update the
1474 // status and UI flag.
1475
1476 // Id would be valid only if the widget restore broadcast was received.
1477 if (isIdValid) {
Sunny Goyal86df1382016-08-10 15:03:22 -07001478 status |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07001479 } else {
1480 status &= ~LauncherAppWidgetInfo
1481 .FLAG_PROVIDER_NOT_READY;
1482 }
1483 }
1484 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07001485 } else {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001486 Log.v(TAG, "Widget restore pending id=" + c.id
Sunny Goyal651077b2014-06-30 14:15:31 -07001487 + " appWidgetId=" + appWidgetId
1488 + " status =" + restoreStatus);
1489 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07001490 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07001491 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001492 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07001493
1494 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
1495 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001496 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07001497 // App restore has started. Update the flag
1498 appWidgetInfo.restoreStatus |=
1499 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001500 } else if (!isSafeMode) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001501 c.markDeleted("Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07001502 continue;
1503 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07001504
1505 appWidgetInfo.installProgress =
1506 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07001507 }
Sunny Goyal86df1382016-08-10 15:03:22 -07001508 if (appWidgetInfo.hasRestoreFlag(
1509 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG)) {
1510 intentDescription = c.getString(intentIndex);
1511 if (!TextUtils.isEmpty(intentDescription)) {
1512 appWidgetInfo.bindOptions =
1513 Intent.parseUri(intentDescription, 0);
1514 }
1515 }
Sunny Goyalff572272014-07-23 13:58:07 -07001516
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001517 c.applyCommonProperties(appWidgetInfo);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001518 appWidgetInfo.spanX = c.getInt(spanXIndex);
1519 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001520 appWidgetInfo.user = c.user;
Joe Onorato36115782010-06-17 13:28:48 -04001521
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001522 if (!c.isOnWorkspaceOrHotseat()) {
1523 c.markDeleted("Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001524 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001525 continue;
1526 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001527
Adam Cohen59400422014-03-05 18:07:04 -08001528 if (!customWidget) {
1529 String providerName =
1530 appWidgetInfo.providerName.flattenToString();
1531 if (!providerName.equals(savedProvider) ||
1532 (appWidgetInfo.restoreStatus != restoreStatus)) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001533 c.updater()
1534 .put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1535 providerName)
1536 .put(LauncherSettings.Favorites.RESTORED,
1537 appWidgetInfo.restoreStatus)
1538 .commit();
Adam Cohen59400422014-03-05 18:07:04 -08001539 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001540 }
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001541 c.checkAndAddItem(appWidgetInfo, sBgDataModel);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001542 }
Joe Onorato36115782010-06-17 13:28:48 -04001543 break;
1544 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001545 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001546 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001547 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001548 }
1549 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001550 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001551 }
1552
Winson Chungba9c37f2013-08-30 14:11:37 -07001553 // Break early if we've stopped loading
1554 if (mStopped) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001555 sBgDataModel.clear();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001556 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07001557 }
1558
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001559 // Remove dead items
1560 if (c.commitDeleted()) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001561 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07001562 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
1563 .call(contentResolver,
1564 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
1565 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
1566 for (long folderId : deletedFolderIds) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001567 sBgDataModel.workspaceItems.remove(sBgDataModel.folders.get(folderId));
1568 sBgDataModel.folders.remove(folderId);
1569 sBgDataModel.itemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001570 }
1571 }
1572
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001573 // Unpin shortcuts that don't exist on the workspace.
Sunny Goyalf75baa92016-11-22 03:23:51 +05301574 HashSet<ShortcutKey> pendingShortcuts =
1575 InstallShortcutReceiver.getPendingShortcuts(context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001576 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001577 MutableInt numTimesPinned = sBgDataModel.pinnedShortcutCounts.get(key);
Sunny Goyalf75baa92016-11-22 03:23:51 +05301578 if ((numTimesPinned == null || numTimesPinned.value == 0)
1579 && !pendingShortcuts.contains(key)) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001580 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301581 shortcutManager.unpinShortcut(key);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001582 }
1583 }
1584
Sunny Goyal317698b2015-07-29 11:45:41 -07001585 // Sort all the folder items and make sure the first 3 items are high resolution.
Sunny Goyale9956a72016-09-01 17:24:47 -07001586 for (FolderInfo folder : sBgDataModel.folders) {
Sunny Goyal317698b2015-07-29 11:45:41 -07001587 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
1588 int pos = 0;
1589 for (ShortcutInfo info : folder.contents) {
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001590 if (info.usingLowResIcon &&
1591 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal3fe4a142016-12-15 17:40:07 -08001592 mIconCache.getTitleAndIcon(info, false);
Sunny Goyal317698b2015-07-29 11:45:41 -07001593 }
1594 pos ++;
1595 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
1596 break;
1597 }
1598 }
1599 }
1600
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001601 c.commitRestoredItems();
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001602 if (!isSdCardReady && !pendingPackages.isEmpty()) {
1603 context.registerReceiver(
1604 new SdCardAvailableReceiver(
1605 LauncherModel.this, mContext, pendingPackages),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001606 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001607 null,
1608 sWorker);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001609 }
1610
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001611 // Remove any empty screens
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001612 ArrayList<Long> unusedScreens = new ArrayList<>(sBgDataModel.workspaceScreens);
Sunny Goyale9956a72016-09-01 17:24:47 -07001613 for (ItemInfo item: sBgDataModel.itemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001614 long screenId = item.screenId;
1615 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1616 unusedScreens.contains(screenId)) {
1617 unusedScreens.remove(screenId);
1618 }
1619 }
1620
1621 // If there are any empty screens remove them, and update.
1622 if (unusedScreens.size() != 0) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001623 sBgDataModel.workspaceScreens.removeAll(unusedScreens);
1624 updateWorkspaceScreenOrder(context, sBgDataModel.workspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001625 }
Joe Onorato36115782010-06-17 13:28:48 -04001626 }
Sunny Goyale26d1002016-06-20 14:52:14 -07001627 if (LauncherAppState.PROFILE_STARTUP) {
1628 Trace.endSection();
1629 }
Adam Cohene25af792013-06-06 23:08:25 -07001630 }
1631
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001632 /** Filters the set of items who are directly or indirectly (via another container) on the
1633 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08001634 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001635 ArrayList<ItemInfo> allWorkspaceItems,
1636 ArrayList<ItemInfo> currentScreenItems,
1637 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001638 // Purge any null ItemInfos
1639 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
1640 while (iter.hasNext()) {
1641 ItemInfo i = iter.next();
1642 if (i == null) {
1643 iter.remove();
1644 }
1645 }
1646
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001647 // Order the set of items by their containers first, this allows use to walk through the
1648 // list sequentially, build up a list of containers that are in the specified screen,
1649 // as well as all items in those containers.
1650 Set<Long> itemsOnScreen = new HashSet<Long>();
1651 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
1652 @Override
1653 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07001654 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001655 }
1656 });
1657 for (ItemInfo info : allWorkspaceItems) {
1658 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001659 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001660 currentScreenItems.add(info);
1661 itemsOnScreen.add(info.id);
1662 } else {
1663 otherScreenItems.add(info);
1664 }
1665 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1666 currentScreenItems.add(info);
1667 itemsOnScreen.add(info.id);
1668 } else {
1669 if (itemsOnScreen.contains(info.container)) {
1670 currentScreenItems.add(info);
1671 itemsOnScreen.add(info.id);
1672 } else {
1673 otherScreenItems.add(info);
1674 }
1675 }
1676 }
1677 }
1678
1679 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08001680 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001681 ArrayList<LauncherAppWidgetInfo> appWidgets,
1682 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
1683 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001684
1685 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001686 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001687 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08001688 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001689 currentScreenWidgets.add(widget);
1690 } else {
1691 otherScreenWidgets.add(widget);
1692 }
1693 }
1694 }
1695
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001696 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
1697 * right) */
1698 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Sunny Goyal87f784c2017-01-11 10:48:34 -08001699 final InvariantDeviceProfile profile = mApp.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07001700 final int screenCols = profile.numColumns;
1701 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001702 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07001703 @Override
1704 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07001705 if (lhs.container == rhs.container) {
1706 // Within containers, order by their spatial position in that container
1707 switch ((int) lhs.container) {
1708 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
1709 long lr = (lhs.screenId * screenCellCount +
1710 lhs.cellY * screenCols + lhs.cellX);
1711 long rr = (rhs.screenId * screenCellCount +
1712 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07001713 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07001714 }
1715 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
1716 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07001717 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07001718 }
1719 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07001720 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07001721 throw new RuntimeException("Unexpected container type when " +
1722 "sorting workspace items.");
1723 }
1724 return 0;
1725 }
1726 } else {
1727 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07001728 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07001729 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001730 }
1731 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001732 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001733
Adam Cohendcd297f2013-06-18 13:13:40 -07001734 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
1735 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001736 final Runnable r = new Runnable() {
1737 @Override
1738 public void run() {
1739 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1740 if (callbacks != null) {
1741 callbacks.bindScreens(orderedScreens);
1742 }
1743 }
1744 };
Sunny Goyald33860f2015-04-23 16:02:20 -07001745 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07001746 }
1747
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001748 private void bindWorkspaceItems(final Callbacks oldCallbacks,
1749 final ArrayList<ItemInfo> workspaceItems,
1750 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07001751 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001752
1753 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07001754 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001755 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04001756 final int start = i;
1757 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001758 final Runnable r = new Runnable() {
1759 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001760 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001761 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001762 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07001763 callbacks.bindItems(workspaceItems, start, start+chunkSize,
1764 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001765 }
1766 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001767 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001768 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04001769 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001770
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001771 // Bind the widgets, one at a time
1772 N = appWidgets.size();
1773 for (int i = 0; i < N; i++) {
1774 final LauncherAppWidgetInfo widget = appWidgets.get(i);
1775 final Runnable r = new Runnable() {
1776 public void run() {
1777 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1778 if (callbacks != null) {
1779 callbacks.bindAppWidget(widget);
1780 }
1781 }
1782 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001783 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001784 }
1785 }
1786
1787 /**
1788 * Binds all loaded data to actual views on the main thread.
1789 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001790 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001791 final long t = SystemClock.uptimeMillis();
1792 Runnable r;
1793
1794 // Don't use these two variables in any of the callback runnables.
1795 // Otherwise we hold a reference to them.
1796 final Callbacks oldCallbacks = mCallbacks.get();
1797 if (oldCallbacks == null) {
1798 // This launcher has exited and nobody bothered to tell us. Just bail.
1799 Log.w(TAG, "LoaderTask running with no launcher");
1800 return;
1801 }
1802
Winson Chung9b9fb962013-11-15 15:39:34 -08001803 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07001804 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
1805 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
1806 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07001807
Sunny Goyale9956a72016-09-01 17:24:47 -07001808 synchronized (sBgDataModel) {
1809 workspaceItems.addAll(sBgDataModel.workspaceItems);
1810 appWidgets.addAll(sBgDataModel.appWidgets);
1811 orderedScreenIds.addAll(sBgDataModel.workspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001812 }
1813
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001814 final int currentScreen;
1815 {
1816 int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE
1817 ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen();
1818 if (currScreen >= orderedScreenIds.size()) {
1819 // There may be no workspace screens (just hotseat items and an empty page).
1820 currScreen = PagedView.INVALID_RESTORE_PAGE;
1821 }
1822 currentScreen = currScreen;
Winson Chung9b9fb962013-11-15 15:39:34 -08001823 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001824 final boolean validFirstPage = currentScreen >= 0;
1825 final long currentScreenId =
1826 validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID;
Winson Chung9b9fb962013-11-15 15:39:34 -08001827
Winson Chung9b9fb962013-11-15 15:39:34 -08001828 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07001829 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
1830 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
1831 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
1832 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001833
Winson Chung9b9fb962013-11-15 15:39:34 -08001834 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001835 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08001836 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001837 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001838 sortWorkspaceItemsSpatially(currentWorkspaceItems);
1839 sortWorkspaceItemsSpatially(otherWorkspaceItems);
1840
1841 // Tell the workspace that we're about to start binding items
1842 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001843 public void run() {
1844 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1845 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001846 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001847 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04001848 }
1849 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001850 };
Sunny Goyald33860f2015-04-23 16:02:20 -07001851 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001852
Adam Cohendcd297f2013-06-18 13:13:40 -07001853 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
1854
Sunny Goyal527c7d32015-08-28 15:19:36 -07001855 Executor mainExecutor = new DeferredMainThreadExecutor();
1856 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07001857 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001858
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001859 // In case of validFirstPage, only bind the first screen, and defer binding the
1860 // remaining screens after first onDraw (and an optional the fade animation whichever
1861 // happens later).
1862 // This ensures that the first screen is immediately visible (eg. during rotation)
1863 // In case of !validFirstPage, bind all pages one after other.
1864 final Executor deferredExecutor =
1865 validFirstPage ? new ViewOnDrawExecutor(mHandler) : mainExecutor;
1866
1867 mainExecutor.execute(new Runnable() {
1868 @Override
1869 public void run() {
1870 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1871 if (callbacks != null) {
1872 callbacks.finishFirstPageBind(
1873 validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null);
1874 }
1875 }
1876 });
Sunny Goyal527c7d32015-08-28 15:19:36 -07001877
Sunny Goyal44c06432016-04-02 10:56:02 -07001878 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001879
1880 // Tell the workspace that we're done binding items
1881 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001882 public void run() {
1883 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1884 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001885 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04001886 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001887
Sunny Goyal639e9062015-08-19 19:17:06 -07001888 mIsLoadingAndBindingWorkspace = false;
1889
1890 // Run all the bind complete runnables after workspace is bound.
1891 if (!mBindCompleteRunnables.isEmpty()) {
1892 synchronized (mBindCompleteRunnables) {
1893 for (final Runnable r : mBindCompleteRunnables) {
1894 runOnWorkerThread(r);
1895 }
1896 mBindCompleteRunnables.clear();
1897 }
1898 }
1899
Winson Chung98e030b2012-05-07 16:01:11 -07001900 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04001901 if (DEBUG_LOADERS) {
1902 Log.d(TAG, "bound workspace in "
1903 + (SystemClock.uptimeMillis()-t) + "ms");
1904 }
Winson Chung36a62fe2012-05-06 18:04:42 -07001905
Joe Onorato36115782010-06-17 13:28:48 -04001906 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001907 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001908 deferredExecutor.execute(r);
1909
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001910 if (validFirstPage) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001911 r = new Runnable() {
1912 public void run() {
1913 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1914 if (callbacks != null) {
1915 // We are loading synchronously, which means, some of the pages will be
1916 // bound after first draw. Inform the callbacks that page binding is
1917 // not complete, and schedule the remaining pages.
1918 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
1919 callbacks.onPageBoundSynchronously(currentScreen);
1920 }
1921 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
1922 }
1923 }
1924 };
Sunny Goyald33860f2015-04-23 16:02:20 -07001925 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07001926 }
Joe Onorato36115782010-06-17 13:28:48 -04001927 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001928
Joe Onorato36115782010-06-17 13:28:48 -04001929 private void loadAndBindAllApps() {
1930 if (DEBUG_LOADERS) {
1931 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1932 }
1933 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07001934 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001935 synchronized (LoaderTask.this) {
1936 if (mStopped) {
1937 return;
1938 }
1939 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001940 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07001941 synchronized (LoaderTask.this) {
1942 if (mStopped) {
1943 return;
1944 }
1945 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07001946 }
Joe Onorato36115782010-06-17 13:28:48 -04001947 } else {
1948 onlyBindAllApps();
1949 }
1950 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001951
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001952 private void updateIconCache() {
1953 // Ignore packages which have a promise icon.
1954 HashSet<String> packagesToIgnore = new HashSet<>();
Sunny Goyale9956a72016-09-01 17:24:47 -07001955 synchronized (sBgDataModel) {
1956 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001957 if (info instanceof ShortcutInfo) {
1958 ShortcutInfo si = (ShortcutInfo) info;
1959 if (si.isPromise() && si.getTargetComponent() != null) {
1960 packagesToIgnore.add(si.getTargetComponent().getPackageName());
1961 }
1962 } else if (info instanceof LauncherAppWidgetInfo) {
1963 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
1964 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
1965 packagesToIgnore.add(lawi.providerName.getPackageName());
1966 }
1967 }
1968 }
1969 }
1970 mIconCache.updateDbIcons(packagesToIgnore);
1971 }
1972
Joe Onorato36115782010-06-17 13:28:48 -04001973 private void onlyBindAllApps() {
1974 final Callbacks oldCallbacks = mCallbacks.get();
1975 if (oldCallbacks == null) {
1976 // This launcher has exited and nobody bothered to tell us. Just bail.
1977 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1978 return;
1979 }
1980
1981 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07001982 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001983 final ArrayList<AppInfo> list
1984 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07001985 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001986 public void run() {
1987 final long t = SystemClock.uptimeMillis();
1988 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1989 if (callbacks != null) {
1990 callbacks.bindAllApplications(list);
1991 }
1992 if (DEBUG_LOADERS) {
1993 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08001994 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04001995 }
1996 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07001997 };
Tony Wickham80f57872016-06-29 18:12:15 -07001998 runOnMainThread(r);
Joe Onorato36115782010-06-17 13:28:48 -04001999 }
2000
Winson Chung64359a52013-07-08 17:17:08 -07002001 private void loadAllApps() {
2002 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002003
Joe Onorato36115782010-06-17 13:28:48 -04002004 final Callbacks oldCallbacks = mCallbacks.get();
2005 if (oldCallbacks == null) {
2006 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002007 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002008 return;
2009 }
2010
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002011 final List<UserHandle> profiles = mUserManager.getUserProfiles();
Kenny Guyed131872014-04-30 03:02:21 +01002012
Winson Chung64359a52013-07-08 17:17:08 -07002013 // Clear the list of apps
2014 mBgAllAppsList.clear();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002015 for (UserHandle user : profiles) {
Kenny Guyed131872014-04-30 03:02:21 +01002016 // Query for the set of apps
2017 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002018 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002019 if (DEBUG_LOADERS) {
2020 Log.d(TAG, "getActivityList took "
2021 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2022 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2023 }
2024 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002025 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002026 if (apps == null || apps.isEmpty()) {
2027 return;
2028 }
Kenny Guyff05f432016-01-22 17:48:29 +00002029 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002030 // Create the ApplicationInfos
2031 for (int i = 0; i < apps.size(); i++) {
2032 LauncherActivityInfoCompat app = apps.get(i);
2033 // This builds the icon bitmaps.
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08002034 mBgAllAppsList.add(new AppInfo(mContext, app, user, quietMode), app);
Kenny Guyed131872014-04-30 03:02:21 +01002035 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002036
Sunny Goyal756a28a2015-04-23 17:07:55 -07002037 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2038 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002039 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002040
2041 @Override
2042 public void run() {
2043 heuristic.processUserApps(apps);
2044 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002045 };
2046 runOnMainThread(new Runnable() {
2047
2048 @Override
2049 public void run() {
2050 // Check isLoadingWorkspace on the UI thread, as it is updated on
2051 // the UI thread.
2052 if (mIsLoadingAndBindingWorkspace) {
2053 synchronized (mBindCompleteRunnables) {
2054 mBindCompleteRunnables.add(r);
2055 }
2056 } else {
2057 runOnWorkerThread(r);
2058 }
2059 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002060 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002061 }
Winson Chung64359a52013-07-08 17:17:08 -07002062 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002063 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002064 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2065 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002066
2067 // Post callback on main thread
2068 mHandler.post(new Runnable() {
2069 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002070
Winson Chung64359a52013-07-08 17:17:08 -07002071 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002072 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002073 if (callbacks != null) {
2074 callbacks.bindAllApplications(added);
2075 if (DEBUG_LOADERS) {
2076 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002077 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07002078 }
2079 } else {
2080 Log.i(TAG, "not binding apps: no Launcher activity");
2081 }
2082 }
2083 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002084 // Cleanup any data stored for a deleted user.
2085 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04002086 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002087 Log.d(TAG, "Icons processed in "
2088 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002089 }
2090 }
2091
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002092 private void loadAndBindDeepShortcuts() {
2093 if (DEBUG_LOADERS) {
2094 Log.d(TAG, "loadAndBindDeepShortcuts mDeepShortcutsLoaded=" + mDeepShortcutsLoaded);
2095 }
2096 if (!mDeepShortcutsLoaded) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07002097 sBgDataModel.deepShortcutMap.clear();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302098 DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(mContext);
2099 mHasShortcutHostPermission = shortcutManager.hasHostPermission();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002100 if (mHasShortcutHostPermission) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002101 for (UserHandle user : mUserManager.getUserProfiles()) {
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002102 if (mUserManager.isUserUnlocked(user)) {
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302103 List<ShortcutInfoCompat> shortcuts =
2104 shortcutManager.queryForAllShortcuts(user);
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07002105 sBgDataModel.updateDeepShortcutMap(null, user, shortcuts);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002106 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002107 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002108 }
2109 synchronized (LoaderTask.this) {
2110 if (mStopped) {
2111 return;
2112 }
2113 mDeepShortcutsLoaded = true;
2114 }
2115 }
Tony Wickham80f57872016-06-29 18:12:15 -07002116 bindDeepShortcuts();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002117 }
2118
Joe Onoratobe386092009-11-17 17:32:16 -08002119 public void dumpState() {
Sunny Goyale9956a72016-09-01 17:24:47 -07002120 synchronized (sBgDataModel) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002121 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002122 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2123 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
Sunny Goyale9956a72016-09-01 17:24:47 -07002124 Log.d(TAG, "mItems size=" + sBgDataModel.workspaceItems.size());
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002125 }
Joe Onorato36115782010-06-17 13:28:48 -04002126 }
2127 }
2128
Tony Wickham80f57872016-06-29 18:12:15 -07002129 public void bindDeepShortcuts() {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07002130 final MultiHashMap<ComponentKey, String> shortcutMapCopy =
2131 sBgDataModel.deepShortcutMap.clone();
Tony Wickham80f57872016-06-29 18:12:15 -07002132 Runnable r = new Runnable() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002133 @Override
2134 public void run() {
2135 Callbacks callbacks = getCallback();
2136 if (callbacks != null) {
2137 callbacks.bindDeepShortcutMap(shortcutMapCopy);
2138 }
2139 }
Tony Wickham80f57872016-06-29 18:12:15 -07002140 };
2141 runOnMainThread(r);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002142 }
2143
Sunny Goyal75b0f552015-05-20 21:57:06 -07002144 /**
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002145 * Refreshes the cached shortcuts if the shortcut permission has changed.
2146 * Current implementation simply reloads the workspace, but it can be optimized to
2147 * use partial updates similar to {@link UserManagerCompat}
2148 */
2149 public void refreshShortcutsIfRequired() {
Sunny Goyalf5e37442016-11-02 10:31:24 -07002150 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002151 sWorker.removeCallbacks(mShortcutPermissionCheckRunnable);
2152 sWorker.post(mShortcutPermissionCheckRunnable);
2153 }
2154 }
2155
2156 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -07002157 * Called when the icons for packages have been updated in the icon cache.
2158 */
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002159 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandle user) {
Sunny Goyal75b0f552015-05-20 21:57:06 -07002160 // If any package icon has changed (app was updated while launcher was dead),
2161 // update the corresponding shortcuts.
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002162 enqueueModelUpdateTask(new CacheDataUpdatedTask(
2163 CacheDataUpdatedTask.OP_CACHE_UPDATE, user, updatedPackages));
Sunny Goyal75b0f552015-05-20 21:57:06 -07002164 }
2165
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002166 void enqueueModelUpdateTask(BaseModelUpdateTask task) {
2167 task.init(this);
2168 runOnWorkerThread(task);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002169 }
2170
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002171 /**
2172 * A task to be executed on the current callbacks on the UI thread.
2173 * If there is no current callbacks, the task is ignored.
2174 */
2175 public interface CallbackTask {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002176
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002177 void execute(Callbacks callbacks);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002178 }
2179
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002180 /**
2181 * A runnable which changes/updates the data model of the launcher based on certain events.
2182 */
2183 public static abstract class BaseModelUpdateTask implements Runnable {
Joe Onorato36115782010-06-17 13:28:48 -04002184
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002185 private LauncherModel mModel;
2186 private DeferredHandler mUiHandler;
Joe Onorato36115782010-06-17 13:28:48 -04002187
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002188 /* package private */
2189 void init(LauncherModel model) {
2190 mModel = model;
2191 mUiHandler = mModel.mHandler;
Joe Onorato36115782010-06-17 13:28:48 -04002192 }
2193
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002194 @Override
Joe Onorato36115782010-06-17 13:28:48 -04002195 public void run() {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002196 if (!mModel.mHasLoaderCompletedOnce) {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002197 // Loader has not yet run.
2198 return;
2199 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002200 execute(mModel.mApp, sBgDataModel, mModel.mBgAllAppsList);
2201 }
Joe Onorato36115782010-06-17 13:28:48 -04002202
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002203 /**
2204 * Execute the actual task. Called on the worker thread.
2205 */
2206 public abstract void execute(
2207 LauncherAppState app, BgDataModel dataModel, AllAppsList apps);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002208
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002209 /**
2210 * Schedules a {@param task} to be executed on the current callbacks.
2211 */
2212 public final void scheduleCallbackTask(final CallbackTask task) {
2213 final Callbacks callbacks = mModel.getCallback();
2214 mUiHandler.post(new Runnable() {
2215 public void run() {
2216 Callbacks cb = mModel.getCallback();
2217 if (callbacks == cb && cb != null) {
2218 task.execute(callbacks);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002219 }
2220 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002221 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002222 }
2223 }
2224
Sunny Goyal10923b32016-07-20 15:42:44 -07002225 /**
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08002226 * Utility method to update a shortcut on the background thread.
Sunny Goyal10923b32016-07-20 15:42:44 -07002227 */
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08002228 public void updateAndBindShortcutInfo(final Provider<ShortcutInfo> shortcutProvider) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002229 enqueueModelUpdateTask(new ExtendedModelTask() {
Sunny Goyal10923b32016-07-20 15:42:44 -07002230 @Override
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002231 public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08002232 ShortcutInfo info = shortcutProvider.get();
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002233 ArrayList<ShortcutInfo> update = new ArrayList<>();
Sunny Goyal10923b32016-07-20 15:42:44 -07002234 update.add(info);
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08002235 bindUpdatedShortcuts(update, info.user);
Sunny Goyal10923b32016-07-20 15:42:44 -07002236 }
2237 });
2238 }
2239
Sunny Goyald164b7f2016-10-12 20:49:31 -07002240 private void bindWidgetsModel(final Callbacks callbacks) {
2241 final MultiHashMap<PackageItemInfo, WidgetItem> widgets
2242 = mBgWidgetsModel.getWidgetsMap().clone();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002243 mHandler.post(new Runnable() {
2244 @Override
2245 public void run() {
2246 Callbacks cb = getCallback();
2247 if (callbacks == cb && cb != null) {
Sunny Goyald164b7f2016-10-12 20:49:31 -07002248 callbacks.bindAllWidgets(widgets);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002249 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07002250 }
2251 });
2252 }
2253
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002254 public void refreshAndBindWidgetsAndShortcuts(
2255 final Callbacks callbacks, final boolean bindFirst) {
2256 runOnWorkerThread(new Runnable() {
2257 @Override
2258 public void run() {
2259 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
Sunny Goyald164b7f2016-10-12 20:49:31 -07002260 bindWidgetsModel(callbacks);
Sunny Goyal31860582015-09-18 08:38:57 -07002261 }
Sunny Goyald164b7f2016-10-12 20:49:31 -07002262 ArrayList<WidgetItem> allWidgets = mBgWidgetsModel.update(mApp.getContext());
2263 bindWidgetsModel(callbacks);
2264
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002265 // update the Widget entries inside DB on the worker thread.
Sunny Goyal87f784c2017-01-11 10:48:34 -08002266 mApp.getWidgetCache().removeObsoletePreviews(allWidgets);
Sunny Goyal31860582015-09-18 08:38:57 -07002267 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002268 });
Michael Jurkac402cd92013-05-20 15:49:32 +02002269 }
2270
Sunny Goyal651077b2014-06-30 14:15:31 -07002271 static boolean isValidProvider(AppWidgetProviderInfo provider) {
2272 return (provider != null) && (provider.provider != null)
2273 && (provider.provider.getPackageName() != null);
2274 }
2275
Joe Onoratobe386092009-11-17 17:32:16 -08002276 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08002277 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002278 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
2279 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
2280 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
2281 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04002282 if (mLoaderTask != null) {
2283 mLoaderTask.dumpState();
2284 } else {
2285 Log.d(TAG, "mLoaderTask=null");
2286 }
Joe Onoratobe386092009-11-17 17:32:16 -08002287 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002288
2289 public Callbacks getCallback() {
2290 return mCallbacks != null ? mCallbacks.get() : null;
2291 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002292
2293 /**
2294 * @return {@link FolderInfo} if its already loaded.
2295 */
2296 public FolderInfo findFolderById(Long folderId) {
Sunny Goyale9956a72016-09-01 17:24:47 -07002297 synchronized (sBgDataModel) {
2298 return sBgDataModel.folders.get(folderId);
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002299 }
2300 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002301
Sunny Goyal527c7d32015-08-28 15:19:36 -07002302 @Thunk class DeferredMainThreadExecutor implements Executor {
2303
2304 @Override
2305 public void execute(Runnable command) {
2306 runOnMainThread(command);
2307 }
2308 }
2309
Sunny Goyal756adbc2015-04-16 15:20:51 -07002310 /**
2311 * @return the looper for the worker thread which can be used to start background tasks.
2312 */
2313 public static Looper getWorkerLooper() {
2314 return sWorkerThread.getLooper();
2315 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002316}