blob: 247a295d6590a5ddcf9037f008d0c9c271ffb426 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Romain Guy629de3e2010-01-13 12:20:59 -080019import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070020import android.content.BroadcastReceiver;
21import android.content.ComponentName;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070022import android.content.ContentProviderOperation;
23import android.content.ContentResolver;
24import android.content.ContentValues;
25import android.content.Context;
26import android.content.Intent;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070027import android.content.IntentFilter;
Sunny Goyal3e9be432017-01-05 15:22:41 -080028import android.content.pm.LauncherActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.content.pm.PackageManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.net.Uri;
Joe Onorato36115782010-06-17 13:28:48 -040031import android.os.Handler;
32import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070033import android.os.Looper;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040035import android.os.SystemClock;
Sunny Goyale26d1002016-06-20 14:52:14 -070036import android.os.Trace;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080037import android.os.UserHandle;
Winson Chunga90303b2013-11-15 13:05:06 -080038import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070039import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080040import android.util.LongSparseArray;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070041import android.util.MutableInt;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010042
Sunny Goyalffe83f12014-08-14 17:39:34 -070043import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010044import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070045import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070046import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010047import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyal6c56c682015-07-16 14:09:05 -070048import com.android.launcher3.config.ProviderConfig;
Tony Wickham827cef22016-03-17 15:39:39 -070049import com.android.launcher3.dynamicui.ExtractionUtils;
Sunny Goyal26119432016-02-18 22:09:23 +000050import com.android.launcher3.folder.Folder;
51import com.android.launcher3.folder.FolderIcon;
Sunny Goyal1b072632017-01-18 11:30:23 -080052import com.android.launcher3.graphics.LauncherIcons;
Sunny Goyal1acb9e92016-05-16 12:41:09 -070053import com.android.launcher3.logging.FileLog;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070054import com.android.launcher3.model.AddWorkspaceItemsTask;
Sunny Goyale9956a72016-09-01 17:24:47 -070055import com.android.launcher3.model.BgDataModel;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070056import com.android.launcher3.model.CacheDataUpdatedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080057import com.android.launcher3.model.ExtendedModelTask;
Sunny Goyalf862a262015-12-14 14:27:38 -080058import com.android.launcher3.model.GridSizeMigrationTask;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -080059import com.android.launcher3.model.LoaderCursor;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070060import com.android.launcher3.model.PackageInstallStateChangedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080061import com.android.launcher3.model.PackageItemInfo;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070062import com.android.launcher3.model.PackageUpdatedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080063import com.android.launcher3.model.SdCardAvailableReceiver;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070064import com.android.launcher3.model.ShortcutsChangedTask;
65import com.android.launcher3.model.UserLockStateChangedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080066import com.android.launcher3.model.WidgetItem;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070067import com.android.launcher3.model.WidgetsModel;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -070068import com.android.launcher3.provider.ImportDataTask;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070069import com.android.launcher3.provider.LauncherDbUtils;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070070import com.android.launcher3.shortcuts.DeepShortcutManager;
71import com.android.launcher3.shortcuts.ShortcutInfoCompat;
72import com.android.launcher3.shortcuts.ShortcutKey;
Robin Lee26ace122015-03-16 19:41:43 +000073import com.android.launcher3.util.ComponentKey;
Sunny Goyal32f3dda2016-11-11 11:45:00 -080074import com.android.launcher3.util.ContentWriter;
Sunny Goyal40452cf2016-09-01 15:17:46 -070075import com.android.launcher3.util.ItemInfoMatcher;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070076import com.android.launcher3.util.ManagedProfileHeuristic;
Tony Wickhamd82a39d2016-07-01 15:44:13 -070077import com.android.launcher3.util.MultiHashMap;
Sunny Goyald09c3702016-04-06 16:18:20 -070078import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070079import com.android.launcher3.util.Preconditions;
Sunny Goyal2bcbe132016-11-16 09:23:42 -080080import com.android.launcher3.util.Provider;
Adam Cohen091440a2015-03-18 14:16:05 -070081import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070082import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080083
Michael Jurkac2f801e2011-07-12 14:19:46 -070084import java.lang.ref.WeakReference;
85import java.net.URISyntaxException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070086import java.util.ArrayList;
87import java.util.Collections;
88import java.util.Comparator;
89import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070090import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070091import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070092import java.util.List;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070093import java.util.Map;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070094import java.util.Set;
Sunny Goyal527c7d32015-08-28 15:19:36 -070095import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -070096
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097/**
98 * Maintains in-memory state of the Launcher. It is expected that there should be only one
99 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700100 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101 */
Kenny Guyed131872014-04-30 03:02:21 +0100102public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +0100103 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800104 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -0400105 private static final boolean DEBUG_RECEIVER = false;
Chris Wrenb358f812014-04-16 13:37:00 -0400106
Joe Onorato9c1289c2009-08-17 11:03:03 -0400107 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700108
Joe Onorato36115782010-06-17 13:28:48 -0400109 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500110 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800111
Adam Cohen091440a2015-03-18 14:16:05 -0700112 @Thunk final LauncherAppState mApp;
113 @Thunk final Object mLock = new Object();
114 @Thunk DeferredHandler mHandler = new DeferredHandler();
115 @Thunk LoaderTask mLoaderTask;
116 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700117 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118
Adam Cohen091440a2015-03-18 14:16:05 -0700119 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700120 static {
121 sWorkerThread.start();
122 }
Adam Cohen091440a2015-03-18 14:16:05 -0700123 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700124
Joe Onoratocc67f472010-06-08 10:54:30 -0700125 // We start off with everything not loaded. After that, we assume that
126 // our monitoring of the package manager provides all updates and we never
127 // need to do a requery. These are only ever touched from the loader thread.
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700128 private boolean mWorkspaceLoaded;
129 private boolean mAllAppsLoaded;
130 private boolean mDeepShortcutsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700131
Sunny Goyal756a28a2015-04-23 17:07:55 -0700132 /**
133 * Set of runnables to be called on the background thread after the workspace binding
134 * is complete.
135 */
136 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
137
Adam Cohen091440a2015-03-18 14:16:05 -0700138 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700140 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800141 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700142 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800143 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800144
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700145 private boolean mHasShortcutHostPermission;
146 // Runnable to check if the shortcuts permission has changed.
147 private final Runnable mShortcutPermissionCheckRunnable = new Runnable() {
148 @Override
149 public void run() {
150 if (mDeepShortcutsLoaded) {
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530151 boolean hasShortcutHostPermission =
152 DeepShortcutManager.getInstance(mApp.getContext()).hasHostPermission();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700153 if (hasShortcutHostPermission != mHasShortcutHostPermission) {
154 mApp.reloadWorkspace();
155 }
156 }
157 }
158 };
159
Sunny Goyale9956a72016-09-01 17:24:47 -0700160 /**
161 * All the static data should be accessed on the background thread, A lock should be acquired
162 * on this object when accessing any data from this model.
163 */
164 static final BgDataModel sBgDataModel = new BgDataModel();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700165
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700166 // </ only access in worker thread >
167
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700168 private final IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700170 private final LauncherAppsCompat mLauncherApps;
171 private final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100172
Joe Onorato9c1289c2009-08-17 11:03:03 -0400173 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700174 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400175 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700176 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400177 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700178 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
179 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700180 public void bindScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700181 public void finishFirstPageBind(ViewOnDrawExecutor executor);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800182 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400183 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200184 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700185 public void bindAppsAdded(ArrayList<Long> newScreens,
186 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700187 ArrayList<ItemInfo> addAnimated,
188 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200189 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700190 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800191 ArrayList<ShortcutInfo> removed, UserHandle user);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700192 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700193 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700194 public void bindWorkspaceComponentsRemoved(
195 HashSet<String> packageNames, HashSet<ComponentName> components,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800196 UserHandle user);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700197 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800198 public void notifyWidgetProvidersChanged();
Sunny Goyald164b7f2016-10-12 20:49:31 -0700199 public void bindAllWidgets(MultiHashMap<PackageItemInfo, WidgetItem> widgets);
Adam Cohen1462de32012-07-24 22:34:36 -0700200 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700201 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700202 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMap);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400203 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530205 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800206 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400207 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100208 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Sunny Goyald164b7f2016-10-12 20:49:31 -0700209 mBgWidgetsModel = new WidgetsModel(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800210 mIconCache = iconCache;
211
Kenny Guyed131872014-04-30 03:02:21 +0100212 mLauncherApps = LauncherAppsCompat.getInstance(context);
213 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800214 }
215
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700216 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
217 * posted on the main thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700218 private void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700219 if (sWorkerThread.getThreadId() == Process.myTid()) {
220 // If we are on the worker thread, post onto the main handler
221 mHandler.post(r);
222 } else {
223 r.run();
224 }
225 }
226
227 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
228 * posted on the worker thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700229 private static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700230 if (sWorkerThread.getThreadId() == Process.myTid()) {
231 r.run();
232 } else {
233 // If we are not on the worker thread, then post to the worker handler
234 sWorker.post(r);
235 }
236 }
237
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700238 public void setPackageState(PackageInstallInfo installInfo) {
239 enqueueModelUpdateTask(new PackageInstallStateChangedTask(installInfo));
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500240 }
241
Sunny Goyal756adbc2015-04-16 15:20:51 -0700242 /**
243 * Updates the icons and label of all pending icons for the provided package name.
244 */
245 public void updateSessionDisplayInfo(final String packageName) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700246 HashSet<String> packages = new HashSet<>();
247 packages.add(packageName);
248 enqueueModelUpdateTask(new CacheDataUpdatedTask(
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800249 CacheDataUpdatedTask.OP_SESSION_UPDATE, Process.myUserHandle(), packages));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800250 }
251
252 /**
253 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800254 */
Sunny Goyal2bcbe132016-11-16 09:23:42 -0800255 public void addAndBindAddedWorkspaceItems(List<ItemInfo> workspaceApps) {
256 addAndBindAddedWorkspaceItems(Provider.of(workspaceApps));
257 }
258
259 /**
260 * Adds the provided items to the workspace.
261 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700262 public void addAndBindAddedWorkspaceItems(
Sunny Goyal2bcbe132016-11-16 09:23:42 -0800263 Provider<List<ItemInfo>> appsProvider) {
264 enqueueModelUpdateTask(new AddWorkspaceItemsTask(appsProvider));
Winson Chung64359a52013-07-08 17:17:08 -0700265 }
266
Joe Onorato9c1289c2009-08-17 11:03:03 -0400267 /**
268 * Adds an item to the DB if it was not created previously, or move it to a new
269 * <container, screen, cellX, cellY>
270 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800271 public static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700272 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400273 if (item.container == ItemInfo.NO_ID) {
274 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700275 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400276 } else {
277 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700278 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800279 }
280 }
281
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700282 static void checkItemInfoLocked(
283 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
Sunny Goyale9956a72016-09-01 17:24:47 -0700284 ItemInfo modelItem = sBgDataModel.itemsIdMap.get(itemId);
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700285 if (modelItem != null && item != modelItem) {
286 // check all the data is consistent
287 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
288 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
289 ShortcutInfo shortcut = (ShortcutInfo) item;
290 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
291 modelShortcut.intent.filterEquals(shortcut.intent) &&
292 modelShortcut.id == shortcut.id &&
293 modelShortcut.itemType == shortcut.itemType &&
294 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700295 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700296 modelShortcut.cellX == shortcut.cellX &&
297 modelShortcut.cellY == shortcut.cellY &&
298 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700299 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700300 // For all intents and purposes, this is the same object
301 return;
302 }
303 }
304
305 // the modelItem needs to match up perfectly with item if our model is
306 // to be consistent with the database-- for now, just require
307 // modelItem == item or the equality check above
308 String msg = "item: " + ((item != null) ? item.toString() : "null") +
309 "modelItem: " +
310 ((modelItem != null) ? modelItem.toString() : "null") +
311 "Error: ItemInfo passed to checkItemInfo doesn't match original";
312 RuntimeException e = new RuntimeException(msg);
313 if (stackTrace != null) {
314 e.setStackTrace(stackTrace);
315 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800316 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700317 }
318 }
319
Michael Jurka816474f2012-06-25 14:49:02 -0700320 static void checkItemInfo(final ItemInfo item) {
321 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
322 final long itemId = item.id;
323 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700324 public void run() {
Sunny Goyale9956a72016-09-01 17:24:47 -0700325 synchronized (sBgDataModel) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700326 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700327 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700328 }
329 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700330 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700331 }
332
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800333 static void updateItemInDatabaseHelper(Context context, final ContentWriter writer,
Michael Jurkac9d95c52011-08-29 14:03:34 -0700334 final ItemInfo item, final String callingFunction) {
335 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700336 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700337 final ContentResolver cr = context.getContentResolver();
338
Adam Cohen487f7dd2012-06-28 18:12:10 -0700339 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Sunny Goyal87f784c2017-01-11 10:48:34 -0800340 final Context appContext = context.getApplicationContext();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700341 Runnable r = new Runnable() {
342 public void run() {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800343 cr.update(uri, writer.getValues(appContext), null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700344 updateItemArrays(item, itemId, stackTrace);
345 }
346 };
347 runOnWorkerThread(r);
348 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700349
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700350 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
351 final ArrayList<ItemInfo> items, final String callingFunction) {
352 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700353
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700354 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
355 Runnable r = new Runnable() {
356 public void run() {
357 ArrayList<ContentProviderOperation> ops =
358 new ArrayList<ContentProviderOperation>();
359 int count = items.size();
360 for (int i = 0; i < count; i++) {
361 ItemInfo item = items.get(i);
362 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700363 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700364 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700365
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700366 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
367 updateItemArrays(item, itemId, stackTrace);
368
369 }
370 try {
371 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
372 } catch (Exception e) {
373 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700374 }
375 }
376 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700377 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700378 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700379
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700380 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
381 // Lock on mBgLock *after* the db operation
Sunny Goyale9956a72016-09-01 17:24:47 -0700382 synchronized (sBgDataModel) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700383 checkItemInfoLocked(itemId, item, stackTrace);
384
385 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
386 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
387 // Item is in a folder, make sure this folder exists
Sunny Goyale9956a72016-09-01 17:24:47 -0700388 if (!sBgDataModel.folders.containsKey(item.container)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700389 // An items container is being set to a that of an item which is not in
390 // the list of Folders.
391 String msg = "item: " + item + " container being set to: " +
392 item.container + ", not in the list of folders";
393 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700394 }
395 }
396
397 // Items are added/removed from the corresponding FolderInfo elsewhere, such
398 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
399 // that are on the desktop, as appropriate
Sunny Goyale9956a72016-09-01 17:24:47 -0700400 ItemInfo modelItem = sBgDataModel.itemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800401 if (modelItem != null &&
402 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
403 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700404 switch (modelItem.itemType) {
405 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
406 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700407 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700408 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyale9956a72016-09-01 17:24:47 -0700409 if (!sBgDataModel.workspaceItems.contains(modelItem)) {
410 sBgDataModel.workspaceItems.add(modelItem);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700411 }
412 break;
413 default:
414 break;
415 }
416 } else {
Sunny Goyale9956a72016-09-01 17:24:47 -0700417 sBgDataModel.workspaceItems.remove(modelItem);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700418 }
419 }
420 }
421
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800422 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400423 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700424 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700425 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700426 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400427 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400428 item.cellX = cellX;
429 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700430
Winson Chung3d503fb2011-07-13 17:25:49 -0700431 // We store hotseat items in canonical form which is this orientation invariant position
432 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700433 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700434 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700435 item.screenId = Launcher.getLauncher(context).getHotseat()
436 .getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700437 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700438 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700439 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400440
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800441 final ContentWriter writer = new ContentWriter(context)
442 .put(LauncherSettings.Favorites.CONTAINER, item.container)
443 .put(LauncherSettings.Favorites.CELLX, item.cellX)
444 .put(LauncherSettings.Favorites.CELLY, item.cellY)
445 .put(LauncherSettings.Favorites.RANK, item.rank)
446 .put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400447
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800448 updateItemInDatabaseHelper(context, writer, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700449 }
450
451 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700452 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
453 * cellX, cellY have already been updated on the ItemInfos.
454 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800455 public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700456 final long container, final int screen) {
457
458 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
459 int count = items.size();
460
461 for (int i = 0; i < count; i++) {
462 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700463 item.container = container;
464
465 // We store hotseat items in canonical form which is this orientation invariant position
466 // in the hotseat
467 if (context instanceof Launcher && screen < 0 &&
468 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700469 item.screenId = Launcher.getLauncher(context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700470 item.cellY);
471 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700472 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700473 }
474
475 final ContentValues values = new ContentValues();
476 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
477 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
478 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800479 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700480 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700481
482 contentValues.add(values);
483 }
484 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
485 }
486
487 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700488 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800489 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700490 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700491 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700492 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800493 item.cellX = cellX;
494 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700495 item.spanX = spanX;
496 item.spanY = spanY;
497
498 // We store hotseat items in canonical form which is this orientation invariant position
499 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700500 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700501 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700502 item.screenId = Launcher.getLauncher(context).getHotseat()
503 .getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700504 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700505 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700506 }
Adam Cohend4844c32011-02-18 19:25:06 -0800507
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800508 final ContentWriter writer = new ContentWriter(context)
509 .put(LauncherSettings.Favorites.CONTAINER, item.container)
510 .put(LauncherSettings.Favorites.CELLX, item.cellX)
511 .put(LauncherSettings.Favorites.CELLY, item.cellY)
512 .put(LauncherSettings.Favorites.RANK, item.rank)
513 .put(LauncherSettings.Favorites.SPANX, item.spanX)
514 .put(LauncherSettings.Favorites.SPANY, item.spanY)
515 .put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800516
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800517 updateItemInDatabaseHelper(context, writer, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700518 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700519
520 /**
521 * Update an item to the database in a specified container.
522 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700523 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800524 ContentWriter writer = new ContentWriter(context);
525 item.onAddToDatabase(writer);
526 updateItemInDatabaseHelper(context, writer, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800527 }
528
Joe Onorato9c1289c2009-08-17 11:03:03 -0400529 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400530 * Add an item to the database in a specified container. Sets the container, screen, cellX and
531 * cellY fields of the item. Also assigns an ID to the item.
532 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700533 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700534 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400535 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400536 item.cellX = cellX;
537 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700538 // We store hotseat items in canonical form which is this orientation invariant position
539 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700540 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700541 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700542 item.screenId = Launcher.getLauncher(context).getHotseat()
543 .getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700544 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700545 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700546 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400547
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800548 final ContentWriter writer = new ContentWriter(context);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400549 final ContentResolver cr = context.getContentResolver();
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800550 item.onAddToDatabase(writer);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400551
Sunny Goyald2497482015-09-22 18:24:19 -0700552 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
553 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
554
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800555 writer.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700556
Jason Monk8e19cf22014-03-20 15:06:57 -0400557 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Sunny Goyal87f784c2017-01-11 10:48:34 -0800558 final Context appContext = context.getApplicationContext();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700559 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700560 public void run() {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800561 cr.insert(LauncherSettings.Favorites.CONTENT_URI, writer.getValues(appContext));
Joe Onorato9c1289c2009-08-17 11:03:03 -0400562
Sunny Goyale9956a72016-09-01 17:24:47 -0700563 synchronized (sBgDataModel) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400564 checkItemInfoLocked(item.id, item, stackTrace);
Sunny Goyal87f784c2017-01-11 10:48:34 -0800565 sBgDataModel.addItem(appContext, item, true);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700566 }
567 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700568 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700569 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700570 }
571
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700572 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700573 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -0400574 */
Sunny Goyalfa401a12015-04-10 13:45:42 -0700575 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyal40452cf2016-09-01 15:17:46 -0700576 ArrayList<ItemInfo> items = new ArrayList<>();
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700577 items.add(item);
578 deleteItemsFromDatabase(context, items);
579 }
580
581 /**
Sunny Goyal40452cf2016-09-01 15:17:46 -0700582 * Removes all the items from the database matching {@param matcher}.
583 */
584 public static void deleteItemsFromDatabase(Context context, ItemInfoMatcher matcher) {
Sunny Goyale9956a72016-09-01 17:24:47 -0700585 deleteItemsFromDatabase(context, matcher.filterItemInfos(sBgDataModel.itemsIdMap));
Sunny Goyal40452cf2016-09-01 15:17:46 -0700586 }
587
588 /**
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700589 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700590 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700591 public static void deleteItemsFromDatabase(Context context,
592 final Iterable<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400593 final ContentResolver cr = context.getContentResolver();
Sunny Goyal87f784c2017-01-11 10:48:34 -0800594 final Context appContext = context.getApplicationContext();
Michael Jurka83df1882011-08-31 20:59:26 -0700595 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700596 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700597 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700598 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700599 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700600
Sunny Goyal87f784c2017-01-11 10:48:34 -0800601 sBgDataModel.removeItem(appContext, item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700602 }
603 }
Michael Jurka83df1882011-08-31 20:59:26 -0700604 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700605 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400606 }
607
608 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700609 * Update the order of the workspace screens in the database. The array list contains
610 * a list of screen ids in the order that they should appear.
611 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700612 public static void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -0700613 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700614 final ContentResolver cr = context.getContentResolver();
615 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
616
617 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700618 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700619 while (iter.hasNext()) {
620 long id = iter.next();
621 if (id < 0) {
622 iter.remove();
623 }
624 }
625
626 Runnable r = new Runnable() {
627 @Override
628 public void run() {
Yura085c8532014-02-11 15:15:29 +0000629 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700630 // Clear the table
Yura085c8532014-02-11 15:15:29 +0000631 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -0700632 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700633 for (int i = 0; i < count; i++) {
634 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700635 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700636 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
637 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +0000638 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -0700639 }
Yura085c8532014-02-11 15:15:29 +0000640
641 try {
642 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
643 } catch (Exception ex) {
644 throw new RuntimeException(ex);
645 }
Winson Chung9e6a0a22013-08-27 11:58:12 -0700646
Sunny Goyale9956a72016-09-01 17:24:47 -0700647 synchronized (sBgDataModel) {
648 sBgDataModel.workspaceScreens.clear();
649 sBgDataModel.workspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -0700650 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700651 }
652 };
653 runOnWorkerThread(r);
654 }
655
656 /**
Winsonc0b52fe2015-09-09 16:38:15 -0700657 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -0400658 */
Winsonc0b52fe2015-09-09 16:38:15 -0700659 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400660 final ContentResolver cr = context.getContentResolver();
Sunny Goyal87f784c2017-01-11 10:48:34 -0800661 final Context appContext = context.getApplicationContext();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400662
Michael Jurkac9d95c52011-08-29 14:03:34 -0700663 Runnable r = new Runnable() {
664 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700665 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -0700666 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Sunny Goyal87f784c2017-01-11 10:48:34 -0800667 sBgDataModel.removeItem(appContext, info.contents);
Sunny Goyale9956a72016-09-01 17:24:47 -0700668 info.contents.clear();
669
670 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Sunny Goyal87f784c2017-01-11 10:48:34 -0800671 sBgDataModel.removeItem(appContext, info);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700672 }
673 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700674 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400675 }
676
677 /**
678 * Set this as the current Launcher activity object for the loader.
679 */
680 public void initialize(Callbacks callbacks) {
681 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -0700682 Preconditions.assertUIThread();
683 // Remove any queued UI runnables
684 mHandler.cancelAll();
685 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400686 }
687 }
688
Kenny Guyed131872014-04-30 03:02:21 +0100689 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800690 public void onPackageChanged(String packageName, UserHandle user) {
Kenny Guyed131872014-04-30 03:02:21 +0100691 int op = PackageUpdatedTask.OP_UPDATE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700692 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100693 }
694
695 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800696 public void onPackageRemoved(String packageName, UserHandle user) {
Sunny Goyalc2936bc2016-09-01 15:50:36 -0700697 onPackagesRemoved(user, packageName);
698 }
699
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800700 public void onPackagesRemoved(UserHandle user, String... packages) {
Kenny Guyed131872014-04-30 03:02:21 +0100701 int op = PackageUpdatedTask.OP_REMOVE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700702 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packages));
Kenny Guyed131872014-04-30 03:02:21 +0100703 }
704
705 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800706 public void onPackageAdded(String packageName, UserHandle user) {
Kenny Guyed131872014-04-30 03:02:21 +0100707 int op = PackageUpdatedTask.OP_ADD;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700708 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100709 }
710
711 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800712 public void onPackagesAvailable(String[] packageNames, UserHandle user,
Kenny Guyed131872014-04-30 03:02:21 +0100713 boolean replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700714 enqueueModelUpdateTask(
715 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100716 }
717
718 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800719 public void onPackagesUnavailable(String[] packageNames, UserHandle user,
Kenny Guyed131872014-04-30 03:02:21 +0100720 boolean replacing) {
721 if (!replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700722 enqueueModelUpdateTask(new PackageUpdatedTask(
723 PackageUpdatedTask.OP_UNAVAILABLE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100724 }
Kenny Guyed131872014-04-30 03:02:21 +0100725 }
726
Kenny Guy44cba692016-01-21 19:50:02 +0000727 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800728 public void onPackagesSuspended(String[] packageNames, UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700729 enqueueModelUpdateTask(new PackageUpdatedTask(
730 PackageUpdatedTask.OP_SUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000731 }
732
733 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800734 public void onPackagesUnsuspended(String[] packageNames, UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700735 enqueueModelUpdateTask(new PackageUpdatedTask(
736 PackageUpdatedTask.OP_UNSUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000737 }
738
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700739 @Override
740 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800741 UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700742 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, true));
Sunny Goyal50941fb2016-08-04 12:03:52 -0700743 }
744
745 public void updatePinnedShortcuts(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800746 UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700747 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, false));
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700748 }
749
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700750 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400751 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
752 * ACTION_PACKAGE_CHANGED.
753 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100754 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400755 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -0400756 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700757
Joe Onorato36115782010-06-17 13:28:48 -0400758 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +0100759 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -0700760 // If we have changed locale we need to clear out the labels in all apps/workspace.
761 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700762 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
763 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -0700764 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -0700765 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700766 } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
767 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
768 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800769 UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER);
Sunny Goyalda891c12016-03-18 18:29:24 -0700770 if (user != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700771 if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
772 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700773 enqueueModelUpdateTask(new PackageUpdatedTask(
774 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE, user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700775 }
776
777 // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so
778 // we need to run the state change task again.
779 if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
780 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700781 enqueueModelUpdateTask(new UserLockStateChangedTask(user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700782 }
Sunny Goyalda891c12016-03-18 18:29:24 -0700783 }
Tony Wickham827cef22016-03-17 15:39:39 -0700784 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
785 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700786 }
787 }
788
Amith Yamasani6cc806d2014-05-02 13:47:11 -0700789 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700790 resetLoadedState(true, true);
791
Reena Lee93f824a2011-09-23 17:20:28 -0700792 // Do this here because if the launcher activity is running it will be restarted.
793 // If it's not running startLoaderFromBackground will merely tell it that it needs
794 // to reload.
795 startLoaderFromBackground();
796 }
797
Winson Chungf0c6ae02012-03-21 16:10:31 -0700798 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
799 synchronized (mLock) {
800 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
801 // mWorkspaceLoaded to true later
802 stopLoaderLocked();
803 if (resetAllAppsLoaded) mAllAppsLoaded = false;
804 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700805 // Always reset deep shortcuts loaded.
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700806 // TODO: why?
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700807 mDeepShortcutsLoaded = false;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700808 }
809 }
810
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700811 /**
812 * When the launcher is in the background, it's possible for it to miss paired
813 * configuration changes. So whenever we trigger the loader from the background
814 * tell the launcher that it needs to re-run the loader when it comes back instead
815 * of doing it now.
816 */
817 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800818 Callbacks callbacks = getCallback();
819 if (callbacks != null) {
820 // Only actually run the loader if they're not paused.
821 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -0700822 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700823 }
824 }
Joe Onorato36115782010-06-17 13:28:48 -0400825 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400826
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700827 /**
828 * If there is already a loader task running, tell it to stop.
829 */
830 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -0700831 LoaderTask oldTask = mLoaderTask;
832 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -0700833 oldTask.stopLocked();
834 }
Reena Lee93f824a2011-09-23 17:20:28 -0700835 }
836
Adam Cohen1a85c582014-09-30 09:48:49 -0700837 public boolean isCurrentCallbacks(Callbacks callbacks) {
838 return (mCallbacks != null && mCallbacks.get() == callbacks);
839 }
840
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700841 /**
842 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
843 * @return true if the page could be bound synchronously.
844 */
845 public boolean startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700846 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
847 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -0400848 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -0400849 // Don't bother to start the thread if we know it's not going to do anything
850 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -0700851 final Callbacks oldCallbacks = mCallbacks.get();
852 // Clear any pending bind-runnables from the synchronized load process.
853 runOnMainThread(new Runnable() {
854 public void run() {
855 oldCallbacks.clearPendingBinds();
856 }
857 });
858
Joe Onorato36115782010-06-17 13:28:48 -0400859 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700860 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700861 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700862 // TODO: mDeepShortcutsLoaded does not need to be true for synchronous bind.
Tony Wickham80f57872016-06-29 18:12:15 -0700863 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE && mAllAppsLoaded
864 && mWorkspaceLoaded && mDeepShortcutsLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700865 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700866 return true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700867 } else {
868 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
869 sWorker.post(mLoaderTask);
870 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400871 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400872 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700873 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400874 }
875
Joe Onorato36115782010-06-17 13:28:48 -0400876 public void stopLoader() {
877 synchronized (mLock) {
878 if (mLoaderTask != null) {
879 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400880 }
881 }
Joe Onorato36115782010-06-17 13:28:48 -0400882 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400883
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800884 /**
885 * Loads the workspace screen ids in an ordered list.
886 */
Sunny Goyale5bb7052015-07-27 14:36:07 -0700887 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -0700888 final ContentResolver contentResolver = context.getContentResolver();
889 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -0700890
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800891 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -0700892 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
893 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -0700894 }
895
Joe Onorato36115782010-06-17 13:28:48 -0400896 /**
897 * Runnable for the thread that loads the contents of the launcher:
898 * - workspace icons
899 * - widgets
900 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700901 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -0400902 */
903 private class LoaderTask implements Runnable {
904 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -0700905 private int mPageToBindFirst;
906
Adam Cohen091440a2015-03-18 14:16:05 -0700907 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -0400908 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -0700909 @Thunk boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700910
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700911 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -0400912 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -0700913 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400914 }
915
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800916 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700917 mIsLoadingAndBindingWorkspace = true;
918
Joe Onorato36115782010-06-17 13:28:48 -0400919 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -0400920 if (DEBUG_LOADERS) {
921 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400922 }
Michael Jurka288a36b2011-07-12 16:53:48 -0700923
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700924 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800925 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -0700926 synchronized (LoaderTask.this) {
927 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800928 return;
Reena Lee93f824a2011-09-23 17:20:28 -0700929 }
930 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400931 }
932 }
933
Joe Onorato36115782010-06-17 13:28:48 -0400934 // Bind the workspace
Sunny Goyal93f878c2016-03-30 17:31:24 -0700935 bindWorkspace(mPageToBindFirst);
Joe Onorato36115782010-06-17 13:28:48 -0400936 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400937
Joe Onorato36115782010-06-17 13:28:48 -0400938 private void waitForIdle() {
939 // Wait until the either we're stopped or the other threads are done.
940 // This way we don't start loading all apps until the workspace has settled
941 // down.
942 synchronized (LoaderTask.this) {
943 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700944
Joe Onorato36115782010-06-17 13:28:48 -0400945 mHandler.postIdle(new Runnable() {
946 public void run() {
947 synchronized (LoaderTask.this) {
948 mLoadAndBindStepFinished = true;
949 if (DEBUG_LOADERS) {
950 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400951 }
Joe Onorato36115782010-06-17 13:28:48 -0400952 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400953 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400954 }
Joe Onorato36115782010-06-17 13:28:48 -0400955 });
956
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800957 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -0400958 try {
Michael Jurkac7700af2013-05-14 20:17:58 +0200959 // Just in case mFlushingWorkerThread changes but we aren't woken up,
960 // wait no longer than 1sec at a time
961 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -0400962 } catch (InterruptedException ex) {
963 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400964 }
965 }
Joe Onorato36115782010-06-17 13:28:48 -0400966 if (DEBUG_LOADERS) {
967 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700968 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400969 + "ms for previous step to finish binding");
970 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400971 }
Joe Onorato36115782010-06-17 13:28:48 -0400972 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400973
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700974 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -0500975 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700976 // Ensure that we have a valid page index to load synchronously
977 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
978 "valid page index");
979 }
980 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
981 // Ensure that we don't try and bind a specified page when the pages have not been
982 // loaded already (we should load everything asynchronously in that case)
983 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
984 }
985 synchronized (mLock) {
986 if (mIsLoaderTaskRunning) {
987 // Ensure that we are never running the background loading at this point since
988 // we also touch the background collections
989 throw new RuntimeException("Error! Background loading is already running");
990 }
991 }
992
993 // XXX: Throw an exception if we are already loading (since we touch the worker thread
994 // data structures, we can't allow any other thread to touch that data, but because
995 // this call is synchronous, we can get away with not locking).
996
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400997 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -0700998 // operations from the previous activity. We need to ensure that all queued operations
999 // are executed before any synchronous binding work is done.
1000 mHandler.flush();
1001
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001002 // Divide the set of loaded items into those that we are binding synchronously, and
1003 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001004 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001005 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1006 // arise from that.
1007 onlyBindAllApps();
Tony Wickham80f57872016-06-29 18:12:15 -07001008
1009 bindDeepShortcuts();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001010 }
1011
Joe Onorato36115782010-06-17 13:28:48 -04001012 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001013 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001014 if (mStopped) {
1015 return;
1016 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001017 mIsLoaderTaskRunning = true;
1018 }
Joe Onorato36115782010-06-17 13:28:48 -04001019 // Optimize for end-user experience: if the Launcher is up and // running with the
1020 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1021 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001022 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001023 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001024 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001025
Joe Onorato36115782010-06-17 13:28:48 -04001026 if (mStopped) {
1027 break keep_running;
1028 }
1029
Joe Onorato36115782010-06-17 13:28:48 -04001030 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001031
1032 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001033 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1034 loadAndBindAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001035
1036 waitForIdle();
1037
1038 // third step
1039 if (DEBUG_LOADERS) Log.d(TAG, "step 3: loading deep shortcuts");
1040 loadAndBindDeepShortcuts();
Joe Onorato36115782010-06-17 13:28:48 -04001041 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001042
Joe Onorato36115782010-06-17 13:28:48 -04001043 // Clear out this reference, otherwise we end up holding it until all of the
1044 // callback runnables are done.
1045 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001046
Joe Onorato36115782010-06-17 13:28:48 -04001047 synchronized (mLock) {
1048 // If we are still the last one to be scheduled, remove ourselves.
1049 if (mLoaderTask == this) {
1050 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001051 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001052 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001053 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001054 }
Joe Onorato36115782010-06-17 13:28:48 -04001055 }
1056
1057 public void stopLocked() {
1058 synchronized (LoaderTask.this) {
1059 mStopped = true;
1060 this.notify();
1061 }
1062 }
1063
1064 /**
1065 * Gets the callbacks object. If we've been stopped, or if the launcher object
1066 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1067 * object that was around when the deferred message was scheduled, and if there's
1068 * a new Callbacks object around then also return null. This will save us from
1069 * calling onto it with data that will be ignored.
1070 */
1071 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1072 synchronized (mLock) {
1073 if (mStopped) {
1074 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001075 }
Joe Onorato36115782010-06-17 13:28:48 -04001076
1077 if (mCallbacks == null) {
1078 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001079 }
Joe Onorato36115782010-06-17 13:28:48 -04001080
1081 final Callbacks callbacks = mCallbacks.get();
1082 if (callbacks != oldCallbacks) {
1083 return null;
1084 }
1085 if (callbacks == null) {
1086 Log.w(TAG, "no mCallbacks");
1087 return null;
1088 }
1089
1090 return callbacks;
1091 }
1092 }
1093
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001094 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -07001095 if (LauncherAppState.PROFILE_STARTUP) {
1096 Trace.beginSection("Loading Workspace");
1097 }
Joe Onorato36115782010-06-17 13:28:48 -04001098 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001099
Joe Onorato36115782010-06-17 13:28:48 -04001100 final Context context = mContext;
1101 final ContentResolver contentResolver = context.getContentResolver();
1102 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001103 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001104 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301105 final DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001106 final boolean isSdCardReady = Utilities.isBootCompleted();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001107 final MultiHashMap<UserHandle, String> pendingPackages = new MultiHashMap<>();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001108
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001109 boolean clearDb = false;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -07001110 try {
1111 ImportDataTask.performImportIfPossible(context);
1112 } catch (Exception e) {
1113 // Migration failed. Clear workspace.
1114 clearDb = true;
1115 }
1116
1117 if (!clearDb && GridSizeMigrationTask.ENABLED &&
Sunny Goyalf076eae2016-01-11 12:25:10 -08001118 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1119 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001120 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001121 }
1122
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001123 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -07001124 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001125 LauncherSettings.Settings.call(contentResolver,
1126 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001127 }
1128
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001129 Log.d(TAG, "loadWorkspace: loading default favorites");
1130 LauncherSettings.Settings.call(contentResolver,
1131 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -07001132
Sunny Goyale9956a72016-09-01 17:24:47 -07001133 synchronized (sBgDataModel) {
1134 sBgDataModel.clear();
1135
Sunny Goyal756adbc2015-04-16 15:20:51 -07001136 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001137 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyale9956a72016-09-01 17:24:47 -07001138 sBgDataModel.workspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001139
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001140 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001141 final LoaderCursor c = new LoaderCursor(contentResolver.query(
1142 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null), mApp);
Daniel Sandler8802e962010-05-26 16:28:16 -04001143
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001144 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001145
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001146 try {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001147 final int intentIndex = c.getColumnIndexOrThrow
1148 (LauncherSettings.Favorites.INTENT);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001149 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1150 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001151 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1152 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001153 final int spanXIndex = c.getColumnIndexOrThrow
1154 (LauncherSettings.Favorites.SPANX);
1155 final int spanYIndex = c.getColumnIndexOrThrow(
1156 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001157 final int rankIndex = c.getColumnIndexOrThrow(
1158 LauncherSettings.Favorites.RANK);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001159 final int optionsIndex = c.getColumnIndexOrThrow(
1160 LauncherSettings.Favorites.OPTIONS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001161
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001162 final LongSparseArray<UserHandle> allUsers = c.allUsers;
Kenny Guyff05f432016-01-22 17:48:29 +00001163 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001164 final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001165 for (UserHandle user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001166 long serialNo = mUserManager.getSerialNumberForUser(user);
1167 allUsers.put(serialNo, user);
1168 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001169
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001170 boolean userUnlocked = mUserManager.isUserUnlocked(user);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001171
1172 // We can only query for shortcuts when the user is unlocked.
1173 if (userUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07001174 List<ShortcutInfoCompat> pinnedShortcuts =
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301175 shortcutManager.queryForPinnedShortcuts(null, user);
1176 if (shortcutManager.wasLastCallSuccess()) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07001177 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
1178 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
1179 shortcut);
1180 }
1181 } else {
1182 // Shortcut manager can fail due to some race condition when the
1183 // lock state changes too frequently. For the purpose of the loading
1184 // shortcuts, consider the user is still locked.
1185 userUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001186 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001187 }
Sunny Goyal49f4f032016-08-01 15:45:49 -07001188 unlockedUsers.put(serialNo, userUnlocked);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001189 }
1190
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001191 ShortcutInfo info;
1192 String intentDescription;
1193 LauncherAppWidgetInfo appWidgetInfo;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001194 Intent intent;
Sunny Goyald09c3702016-04-06 16:18:20 -07001195 String targetPackage;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001196
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001197 while (!mStopped && c.moveToNext()) {
1198 try {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001199 if (c.user == null) {
1200 // User has been deleted, remove the item.
1201 c.markDeleted("User has been deleted");
1202 continue;
1203 }
1204
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001205 boolean allowMissingTarget = false;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001206 switch (c.itemType) {
1207 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: {
1208 if (!Process.myUserHandle().equals(c.user)) {
1209 c.markDeleted("Legacy shortcuts are only allowed for default user");
1210 continue;
1211 }
1212 // Follow through.
1213 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001214 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001215 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001216 intentDescription = c.getString(intentIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001217 int disabledState = 0;
Sunny Goyald09c3702016-04-06 16:18:20 -07001218 targetPackage = null;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001219
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001220 try {
1221 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001222 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001223 if (cn != null && cn.getPackageName() != null) {
1224 boolean validPkg = launcherApps.isPackageEnabledForProfile(
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001225 cn.getPackageName(), c.user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001226 boolean validComponent = validPkg &&
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001227 launcherApps.isActivityEnabledForProfile(cn, c.user);
Sunny Goyald09c3702016-04-06 16:18:20 -07001228 if (validPkg) {
1229 targetPackage = cn.getPackageName();
1230 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001231
1232 if (validComponent) {
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001233 // no special handling necessary for this item
1234 c.markRestored();
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001235 if (quietMode.get(c.serialNumber)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001236 disabledState = ShortcutInfo.FLAG_DISABLED_QUIET_USER;
Kenny Guyff05f432016-01-22 17:48:29 +00001237 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001238 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001239 intent = null;
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001240 if (c.hasRestoreFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
Sunny Goyal34942622014-08-29 17:20:55 -07001241 // We allow auto install apps to have their intent
1242 // updated after an install.
1243 intent = manager.getLaunchIntentForPackage(
1244 cn.getPackageName());
1245 if (intent != null) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001246 c.updater().put(
1247 LauncherSettings.Favorites.INTENT,
1248 intent.toUri(0)).commit();
Sunny Goyal34942622014-08-29 17:20:55 -07001249 }
1250 }
1251
1252 if (intent == null) {
1253 // The app is installed but the component is no
1254 // longer available.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001255 c.markDeleted("Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001256 continue;
1257 } else {
1258 // no special handling necessary for this item
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001259 c.markRestored();
Sunny Goyal34942622014-08-29 17:20:55 -07001260 }
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001261 } else if (c.restoreFlag != 0) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001262 // Package is not yet available but might be
1263 // installed later.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001264 FileLog.d(TAG, "package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001265
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001266 if (c.hasRestoreFlag(ShortcutInfo.FLAG_RESTORE_STARTED)) {
Sunny Goyal94485362014-09-18 16:13:58 -07001267 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001268 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001269 // App restore has started. Update the flag
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001270 c.restoreFlag |= ShortcutInfo.FLAG_RESTORE_STARTED;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001271 c.updater().put(
1272 LauncherSettings.Favorites.RESTORED,
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001273 c.restoreFlag).commit();
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001274 } else {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001275 c.markDeleted("Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001276 continue;
1277 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001278 } else if (PackageManagerHelper.isAppOnSdcard(
1279 manager, cn.getPackageName())) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07001280 // Package is present but not available.
1281 allowMissingTarget = true;
1282 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1283 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001284 // SdCard is not ready yet. Package might get available,
1285 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001286 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001287 pendingPackages.addToList(c.user, cn.getPackageName());
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001288 allowMissingTarget = true;
1289 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001290
1291 } else {
1292 // Do not wait for external media load anymore.
1293 // Log the invalid package, and remove it
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001294 c.markDeleted("Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001295 continue;
Winson Chungee055712013-07-30 14:46:24 -07001296 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001297 } else if (cn == null) {
1298 // For shortcuts with no component, keep them as they are
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001299 c.markRestored();
Winson Chungee055712013-07-30 14:46:24 -07001300 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001301 } catch (URISyntaxException e) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001302 c.markDeleted("Invalid uri: " + intentDescription);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001303 continue;
1304 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001305
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001306 boolean useLowResIcon = !c.isOnWorkspaceOrHotseat() &&
Sunny Goyal34b65272015-03-11 16:56:52 -07001307 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1308
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001309 if (c.restoreFlag != 0) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001310 if (c.user.equals(Process.myUserHandle())) {
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001311 info = c.getRestoredItemInfo(intent);
Kenny Guyed131872014-04-30 03:02:21 +01001312 } else {
1313 // Don't restore items for other profiles.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001314 c.markDeleted("Restore from managed profile not supported");
Kenny Guyed131872014-04-30 03:02:21 +01001315 continue;
1316 }
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001317 } else if (c.itemType ==
Chris Wrenf4d08112014-01-16 18:13:56 -05001318 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001319 info = c.getAppShortcutInfo(
1320 intent, allowMissingTarget, useLowResIcon);
1321 } else if (c.itemType ==
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001322 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001323
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001324 ShortcutKey key = ShortcutKey.fromIntent(intent, c.user);
1325 if (unlockedUsers.get(c.serialNumber)) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001326 ShortcutInfoCompat pinnedShortcut =
1327 shortcutKeyToPinnedShortcuts.get(key);
1328 if (pinnedShortcut == null) {
1329 // The shortcut is no longer valid.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001330 c.markDeleted("Pinned shortcut not found");
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001331 continue;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001332 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001333 info = new ShortcutInfo(pinnedShortcut, context);
Sunny Goyal1b072632017-01-18 11:30:23 -08001334 info.iconBitmap = LauncherIcons
1335 .createShortcutIcon(pinnedShortcut, context);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001336 intent = info.intent;
1337 } else {
1338 // Create a shortcut info in disabled mode for now.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001339 info = c.loadSimpleShortcut();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001340 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001341 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001342 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001343 info = c.loadSimpleShortcut();
Michael Jurka96879562012-03-22 05:54:33 -07001344
Sunny Goyald09c3702016-04-06 16:18:20 -07001345 // Shortcuts are only available on the primary profile
1346 if (PackageManagerHelper.isAppSuspended(manager, targetPackage)) {
1347 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1348 }
1349
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001350 // App shortcuts that used to be automatically added to Launcher
1351 // didn't always have the correct intent flags set, so do that
1352 // here
1353 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001354 intent.getCategories() != null &&
1355 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001356 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001357 intent.addFlags(
1358 Intent.FLAG_ACTIVITY_NEW_TASK |
1359 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1360 }
Michael Jurka96879562012-03-22 05:54:33 -07001361 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001362
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001363 if (info != null) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001364 c.applyCommonProperties(info);
1365
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001366 info.intent = intent;
Sunny Goyal08f72612015-01-05 13:41:43 -08001367 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001368 info.spanX = 1;
1369 info.spanY = 1;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001370 // TODO: Remove this extra. Instead we should be using
1371 // itemInfo#user.
1372 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, c.serialNumber);
Sunny Goyald09c3702016-04-06 16:18:20 -07001373 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001374 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1375 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1376 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001377
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001378 if (c.restoreFlag != 0) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001379 ComponentName cn = info.getTargetComponent();
1380 if (cn != null) {
1381 Integer progress = installingPkgs.get(cn.getPackageName());
1382 if (progress != null) {
1383 info.setInstallProgress(progress);
1384 } else {
1385 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
1386 }
1387 }
1388 }
1389
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001390 c.checkAndAddItem(info, sBgDataModel);
Winson Chung1323b482013-08-05 12:41:55 -07001391 } else {
1392 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001393 }
1394 break;
1395
1396 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001397 FolderInfo folderInfo = sBgDataModel.findOrMakeFolder(c.id);
1398 c.applyCommonProperties(folderInfo);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001399
Sunny Goyala508e4f2015-05-21 09:33:57 -07001400 // Do not trim the folder label, as is was set by the user.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001401 folderInfo.title = c.getString(c.titleIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001402 folderInfo.spanX = 1;
1403 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001404 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001405
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001406 // no special handling required for restored folders
1407 c.markRestored();
Chris Wrenf4d08112014-01-16 18:13:56 -05001408
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001409 c.checkAndAddItem(folderInfo, sBgDataModel);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001410 break;
1411
1412 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08001413 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001414 // Read all Launcher-specific widget details
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001415 boolean customWidget = c.itemType ==
Adam Cohen59400422014-03-05 18:07:04 -08001416 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
1417
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001418 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001419 String savedProvider = c.getString(appWidgetProviderIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001420
Sunny Goyalff572272014-07-23 13:58:07 -07001421 final ComponentName component =
1422 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04001423
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001424 final boolean isIdValid = !c.hasRestoreFlag(
1425 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID);
1426 final boolean wasProviderReady = !c.hasRestoreFlag(
1427 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY);
Sunny Goyal651077b2014-06-30 14:15:31 -07001428
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001429 if (widgetProvidersMap == null) {
1430 widgetProvidersMap = AppWidgetManagerCompat
1431 .getInstance(mContext).getAllProvidersMap();
1432 }
1433 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
1434 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00001435 ComponentName.unflattenFromString(savedProvider),
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001436 c.user));
Sunny Goyalff572272014-07-23 13:58:07 -07001437
1438 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08001439 if (!isSafeMode && !customWidget &&
1440 wasProviderReady && !isProviderReady) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001441 c.markDeleted(
1442 "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07001443 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001444 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07001445 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07001446 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1447 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08001448
Sunny Goyal53f96722015-07-13 19:54:53 -07001449 // The provider is available. So the widget is either
1450 // available or not available. We do not need to track
1451 // any future restore updates.
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001452 int status = c.restoreFlag &
Sunny Goyal53f96722015-07-13 19:54:53 -07001453 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07001454 if (!wasProviderReady) {
1455 // If provider was not previously ready, update the
1456 // status and UI flag.
1457
1458 // Id would be valid only if the widget restore broadcast was received.
1459 if (isIdValid) {
Sunny Goyal86df1382016-08-10 15:03:22 -07001460 status |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07001461 } else {
1462 status &= ~LauncherAppWidgetInfo
1463 .FLAG_PROVIDER_NOT_READY;
1464 }
1465 }
1466 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07001467 } else {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001468 Log.v(TAG, "Widget restore pending id=" + c.id
Sunny Goyal651077b2014-06-30 14:15:31 -07001469 + " appWidgetId=" + appWidgetId
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001470 + " status =" + c.restoreFlag);
Sunny Goyal651077b2014-06-30 14:15:31 -07001471 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07001472 component);
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001473 appWidgetInfo.restoreStatus = c.restoreFlag;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001474 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07001475
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001476 if (c.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_RESTORE_STARTED)) {
Sunny Goyal94485362014-09-18 16:13:58 -07001477 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001478 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07001479 // App restore has started. Update the flag
1480 appWidgetInfo.restoreStatus |=
1481 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001482 } else if (!isSafeMode) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001483 c.markDeleted("Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07001484 continue;
1485 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07001486
1487 appWidgetInfo.installProgress =
1488 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07001489 }
Sunny Goyal86df1382016-08-10 15:03:22 -07001490 if (appWidgetInfo.hasRestoreFlag(
1491 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG)) {
1492 intentDescription = c.getString(intentIndex);
1493 if (!TextUtils.isEmpty(intentDescription)) {
1494 appWidgetInfo.bindOptions =
1495 Intent.parseUri(intentDescription, 0);
1496 }
1497 }
Sunny Goyalff572272014-07-23 13:58:07 -07001498
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001499 c.applyCommonProperties(appWidgetInfo);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001500 appWidgetInfo.spanX = c.getInt(spanXIndex);
1501 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001502 appWidgetInfo.user = c.user;
Joe Onorato36115782010-06-17 13:28:48 -04001503
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001504 if (!c.isOnWorkspaceOrHotseat()) {
1505 c.markDeleted("Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001506 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001507 continue;
1508 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001509
Adam Cohen59400422014-03-05 18:07:04 -08001510 if (!customWidget) {
1511 String providerName =
1512 appWidgetInfo.providerName.flattenToString();
1513 if (!providerName.equals(savedProvider) ||
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001514 (appWidgetInfo.restoreStatus != c.restoreFlag)) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001515 c.updater()
1516 .put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1517 providerName)
1518 .put(LauncherSettings.Favorites.RESTORED,
1519 appWidgetInfo.restoreStatus)
1520 .commit();
Adam Cohen59400422014-03-05 18:07:04 -08001521 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001522 }
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001523 c.checkAndAddItem(appWidgetInfo, sBgDataModel);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001524 }
Joe Onorato36115782010-06-17 13:28:48 -04001525 break;
1526 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001527 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001528 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001529 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001530 }
1531 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001532 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001533 }
1534
Winson Chungba9c37f2013-08-30 14:11:37 -07001535 // Break early if we've stopped loading
1536 if (mStopped) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001537 sBgDataModel.clear();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001538 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07001539 }
1540
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001541 // Remove dead items
1542 if (c.commitDeleted()) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001543 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07001544 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
1545 .call(contentResolver,
1546 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
1547 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
1548 for (long folderId : deletedFolderIds) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001549 sBgDataModel.workspaceItems.remove(sBgDataModel.folders.get(folderId));
1550 sBgDataModel.folders.remove(folderId);
1551 sBgDataModel.itemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001552 }
1553 }
1554
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001555 // Unpin shortcuts that don't exist on the workspace.
Sunny Goyalf75baa92016-11-22 03:23:51 +05301556 HashSet<ShortcutKey> pendingShortcuts =
1557 InstallShortcutReceiver.getPendingShortcuts(context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001558 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001559 MutableInt numTimesPinned = sBgDataModel.pinnedShortcutCounts.get(key);
Sunny Goyalf75baa92016-11-22 03:23:51 +05301560 if ((numTimesPinned == null || numTimesPinned.value == 0)
1561 && !pendingShortcuts.contains(key)) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001562 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301563 shortcutManager.unpinShortcut(key);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001564 }
1565 }
1566
Sunny Goyal317698b2015-07-29 11:45:41 -07001567 // Sort all the folder items and make sure the first 3 items are high resolution.
Sunny Goyale9956a72016-09-01 17:24:47 -07001568 for (FolderInfo folder : sBgDataModel.folders) {
Sunny Goyal317698b2015-07-29 11:45:41 -07001569 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
1570 int pos = 0;
1571 for (ShortcutInfo info : folder.contents) {
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001572 if (info.usingLowResIcon &&
1573 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal3fe4a142016-12-15 17:40:07 -08001574 mIconCache.getTitleAndIcon(info, false);
Sunny Goyal317698b2015-07-29 11:45:41 -07001575 }
1576 pos ++;
1577 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
1578 break;
1579 }
1580 }
1581 }
1582
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001583 c.commitRestoredItems();
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001584 if (!isSdCardReady && !pendingPackages.isEmpty()) {
1585 context.registerReceiver(
1586 new SdCardAvailableReceiver(
1587 LauncherModel.this, mContext, pendingPackages),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001588 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001589 null,
1590 sWorker);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001591 }
1592
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001593 // Remove any empty screens
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001594 ArrayList<Long> unusedScreens = new ArrayList<>(sBgDataModel.workspaceScreens);
Sunny Goyale9956a72016-09-01 17:24:47 -07001595 for (ItemInfo item: sBgDataModel.itemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001596 long screenId = item.screenId;
1597 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1598 unusedScreens.contains(screenId)) {
1599 unusedScreens.remove(screenId);
1600 }
1601 }
1602
1603 // If there are any empty screens remove them, and update.
1604 if (unusedScreens.size() != 0) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001605 sBgDataModel.workspaceScreens.removeAll(unusedScreens);
1606 updateWorkspaceScreenOrder(context, sBgDataModel.workspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001607 }
Joe Onorato36115782010-06-17 13:28:48 -04001608 }
Sunny Goyale26d1002016-06-20 14:52:14 -07001609 if (LauncherAppState.PROFILE_STARTUP) {
1610 Trace.endSection();
1611 }
Adam Cohene25af792013-06-06 23:08:25 -07001612 }
1613
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001614 /** Filters the set of items who are directly or indirectly (via another container) on the
1615 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08001616 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001617 ArrayList<ItemInfo> allWorkspaceItems,
1618 ArrayList<ItemInfo> currentScreenItems,
1619 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001620 // Purge any null ItemInfos
1621 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
1622 while (iter.hasNext()) {
1623 ItemInfo i = iter.next();
1624 if (i == null) {
1625 iter.remove();
1626 }
1627 }
1628
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001629 // Order the set of items by their containers first, this allows use to walk through the
1630 // list sequentially, build up a list of containers that are in the specified screen,
1631 // as well as all items in those containers.
1632 Set<Long> itemsOnScreen = new HashSet<Long>();
1633 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
1634 @Override
1635 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07001636 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001637 }
1638 });
1639 for (ItemInfo info : allWorkspaceItems) {
1640 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001641 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001642 currentScreenItems.add(info);
1643 itemsOnScreen.add(info.id);
1644 } else {
1645 otherScreenItems.add(info);
1646 }
1647 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1648 currentScreenItems.add(info);
1649 itemsOnScreen.add(info.id);
1650 } else {
1651 if (itemsOnScreen.contains(info.container)) {
1652 currentScreenItems.add(info);
1653 itemsOnScreen.add(info.id);
1654 } else {
1655 otherScreenItems.add(info);
1656 }
1657 }
1658 }
1659 }
1660
1661 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08001662 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001663 ArrayList<LauncherAppWidgetInfo> appWidgets,
1664 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
1665 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001666
1667 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001668 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001669 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08001670 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001671 currentScreenWidgets.add(widget);
1672 } else {
1673 otherScreenWidgets.add(widget);
1674 }
1675 }
1676 }
1677
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001678 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
1679 * right) */
1680 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Sunny Goyal87f784c2017-01-11 10:48:34 -08001681 final InvariantDeviceProfile profile = mApp.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07001682 final int screenCols = profile.numColumns;
1683 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001684 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07001685 @Override
1686 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07001687 if (lhs.container == rhs.container) {
1688 // Within containers, order by their spatial position in that container
1689 switch ((int) lhs.container) {
1690 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
1691 long lr = (lhs.screenId * screenCellCount +
1692 lhs.cellY * screenCols + lhs.cellX);
1693 long rr = (rhs.screenId * screenCellCount +
1694 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07001695 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07001696 }
1697 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
1698 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07001699 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07001700 }
1701 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07001702 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07001703 throw new RuntimeException("Unexpected container type when " +
1704 "sorting workspace items.");
1705 }
1706 return 0;
1707 }
1708 } else {
1709 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07001710 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07001711 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001712 }
1713 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001714 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001715
Adam Cohendcd297f2013-06-18 13:13:40 -07001716 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
1717 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001718 final Runnable r = new Runnable() {
1719 @Override
1720 public void run() {
1721 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1722 if (callbacks != null) {
1723 callbacks.bindScreens(orderedScreens);
1724 }
1725 }
1726 };
Sunny Goyald33860f2015-04-23 16:02:20 -07001727 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07001728 }
1729
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001730 private void bindWorkspaceItems(final Callbacks oldCallbacks,
1731 final ArrayList<ItemInfo> workspaceItems,
1732 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07001733 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001734
1735 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07001736 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001737 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04001738 final int start = i;
1739 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001740 final Runnable r = new Runnable() {
1741 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001742 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001743 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001744 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07001745 callbacks.bindItems(workspaceItems, start, start+chunkSize,
1746 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001747 }
1748 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001749 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001750 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04001751 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001752
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001753 // Bind the widgets, one at a time
1754 N = appWidgets.size();
1755 for (int i = 0; i < N; i++) {
1756 final LauncherAppWidgetInfo widget = appWidgets.get(i);
1757 final Runnable r = new Runnable() {
1758 public void run() {
1759 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1760 if (callbacks != null) {
1761 callbacks.bindAppWidget(widget);
1762 }
1763 }
1764 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001765 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001766 }
1767 }
1768
1769 /**
1770 * Binds all loaded data to actual views on the main thread.
1771 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001772 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001773 final long t = SystemClock.uptimeMillis();
1774 Runnable r;
1775
1776 // Don't use these two variables in any of the callback runnables.
1777 // Otherwise we hold a reference to them.
1778 final Callbacks oldCallbacks = mCallbacks.get();
1779 if (oldCallbacks == null) {
1780 // This launcher has exited and nobody bothered to tell us. Just bail.
1781 Log.w(TAG, "LoaderTask running with no launcher");
1782 return;
1783 }
1784
Winson Chung9b9fb962013-11-15 15:39:34 -08001785 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07001786 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
1787 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
1788 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07001789
Sunny Goyale9956a72016-09-01 17:24:47 -07001790 synchronized (sBgDataModel) {
1791 workspaceItems.addAll(sBgDataModel.workspaceItems);
1792 appWidgets.addAll(sBgDataModel.appWidgets);
1793 orderedScreenIds.addAll(sBgDataModel.workspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001794 }
1795
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001796 final int currentScreen;
1797 {
1798 int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE
1799 ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen();
1800 if (currScreen >= orderedScreenIds.size()) {
1801 // There may be no workspace screens (just hotseat items and an empty page).
1802 currScreen = PagedView.INVALID_RESTORE_PAGE;
1803 }
1804 currentScreen = currScreen;
Winson Chung9b9fb962013-11-15 15:39:34 -08001805 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001806 final boolean validFirstPage = currentScreen >= 0;
1807 final long currentScreenId =
1808 validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID;
Winson Chung9b9fb962013-11-15 15:39:34 -08001809
Winson Chung9b9fb962013-11-15 15:39:34 -08001810 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07001811 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
1812 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
1813 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
1814 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001815
Winson Chung9b9fb962013-11-15 15:39:34 -08001816 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001817 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08001818 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001819 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001820 sortWorkspaceItemsSpatially(currentWorkspaceItems);
1821 sortWorkspaceItemsSpatially(otherWorkspaceItems);
1822
1823 // Tell the workspace that we're about to start binding items
1824 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001825 public void run() {
1826 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1827 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001828 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001829 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04001830 }
1831 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001832 };
Sunny Goyald33860f2015-04-23 16:02:20 -07001833 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001834
Adam Cohendcd297f2013-06-18 13:13:40 -07001835 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
1836
Sunny Goyal527c7d32015-08-28 15:19:36 -07001837 Executor mainExecutor = new DeferredMainThreadExecutor();
1838 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07001839 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001840
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001841 // In case of validFirstPage, only bind the first screen, and defer binding the
1842 // remaining screens after first onDraw (and an optional the fade animation whichever
1843 // happens later).
1844 // This ensures that the first screen is immediately visible (eg. during rotation)
1845 // In case of !validFirstPage, bind all pages one after other.
1846 final Executor deferredExecutor =
1847 validFirstPage ? new ViewOnDrawExecutor(mHandler) : mainExecutor;
1848
1849 mainExecutor.execute(new Runnable() {
1850 @Override
1851 public void run() {
1852 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1853 if (callbacks != null) {
1854 callbacks.finishFirstPageBind(
1855 validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null);
1856 }
1857 }
1858 });
Sunny Goyal527c7d32015-08-28 15:19:36 -07001859
Sunny Goyal44c06432016-04-02 10:56:02 -07001860 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001861
1862 // Tell the workspace that we're done binding items
1863 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001864 public void run() {
1865 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1866 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001867 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04001868 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001869
Sunny Goyal639e9062015-08-19 19:17:06 -07001870 mIsLoadingAndBindingWorkspace = false;
1871
1872 // Run all the bind complete runnables after workspace is bound.
1873 if (!mBindCompleteRunnables.isEmpty()) {
1874 synchronized (mBindCompleteRunnables) {
1875 for (final Runnable r : mBindCompleteRunnables) {
1876 runOnWorkerThread(r);
1877 }
1878 mBindCompleteRunnables.clear();
1879 }
1880 }
1881
Winson Chung98e030b2012-05-07 16:01:11 -07001882 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04001883 if (DEBUG_LOADERS) {
1884 Log.d(TAG, "bound workspace in "
1885 + (SystemClock.uptimeMillis()-t) + "ms");
1886 }
Winson Chung36a62fe2012-05-06 18:04:42 -07001887
Joe Onorato36115782010-06-17 13:28:48 -04001888 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001889 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001890 deferredExecutor.execute(r);
1891
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001892 if (validFirstPage) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001893 r = new Runnable() {
1894 public void run() {
1895 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1896 if (callbacks != null) {
1897 // We are loading synchronously, which means, some of the pages will be
1898 // bound after first draw. Inform the callbacks that page binding is
1899 // not complete, and schedule the remaining pages.
1900 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
1901 callbacks.onPageBoundSynchronously(currentScreen);
1902 }
1903 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
1904 }
1905 }
1906 };
Sunny Goyald33860f2015-04-23 16:02:20 -07001907 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07001908 }
Joe Onorato36115782010-06-17 13:28:48 -04001909 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001910
Joe Onorato36115782010-06-17 13:28:48 -04001911 private void loadAndBindAllApps() {
1912 if (DEBUG_LOADERS) {
1913 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1914 }
1915 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07001916 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001917 synchronized (LoaderTask.this) {
1918 if (mStopped) {
1919 return;
1920 }
1921 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001922 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07001923 synchronized (LoaderTask.this) {
1924 if (mStopped) {
1925 return;
1926 }
1927 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07001928 }
Joe Onorato36115782010-06-17 13:28:48 -04001929 } else {
1930 onlyBindAllApps();
1931 }
1932 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001933
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001934 private void updateIconCache() {
1935 // Ignore packages which have a promise icon.
1936 HashSet<String> packagesToIgnore = new HashSet<>();
Sunny Goyale9956a72016-09-01 17:24:47 -07001937 synchronized (sBgDataModel) {
1938 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001939 if (info instanceof ShortcutInfo) {
1940 ShortcutInfo si = (ShortcutInfo) info;
1941 if (si.isPromise() && si.getTargetComponent() != null) {
1942 packagesToIgnore.add(si.getTargetComponent().getPackageName());
1943 }
1944 } else if (info instanceof LauncherAppWidgetInfo) {
1945 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
1946 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
1947 packagesToIgnore.add(lawi.providerName.getPackageName());
1948 }
1949 }
1950 }
1951 }
1952 mIconCache.updateDbIcons(packagesToIgnore);
1953 }
1954
Joe Onorato36115782010-06-17 13:28:48 -04001955 private void onlyBindAllApps() {
1956 final Callbacks oldCallbacks = mCallbacks.get();
1957 if (oldCallbacks == null) {
1958 // This launcher has exited and nobody bothered to tell us. Just bail.
1959 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1960 return;
1961 }
1962
1963 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07001964 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001965 final ArrayList<AppInfo> list
1966 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07001967 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001968 public void run() {
1969 final long t = SystemClock.uptimeMillis();
1970 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1971 if (callbacks != null) {
1972 callbacks.bindAllApplications(list);
1973 }
1974 if (DEBUG_LOADERS) {
1975 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08001976 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04001977 }
1978 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07001979 };
Tony Wickham80f57872016-06-29 18:12:15 -07001980 runOnMainThread(r);
Joe Onorato36115782010-06-17 13:28:48 -04001981 }
1982
Winson Chung64359a52013-07-08 17:17:08 -07001983 private void loadAllApps() {
1984 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04001985
Joe Onorato36115782010-06-17 13:28:48 -04001986 final Callbacks oldCallbacks = mCallbacks.get();
1987 if (oldCallbacks == null) {
1988 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07001989 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04001990 return;
1991 }
1992
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001993 final List<UserHandle> profiles = mUserManager.getUserProfiles();
Kenny Guyed131872014-04-30 03:02:21 +01001994
Winson Chung64359a52013-07-08 17:17:08 -07001995 // Clear the list of apps
1996 mBgAllAppsList.clear();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001997 for (UserHandle user : profiles) {
Kenny Guyed131872014-04-30 03:02:21 +01001998 // Query for the set of apps
1999 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal3e9be432017-01-05 15:22:41 -08002000 final List<LauncherActivityInfo> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002001 if (DEBUG_LOADERS) {
2002 Log.d(TAG, "getActivityList took "
2003 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2004 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2005 }
2006 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002007 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002008 if (apps == null || apps.isEmpty()) {
2009 return;
2010 }
Kenny Guyff05f432016-01-22 17:48:29 +00002011 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002012 // Create the ApplicationInfos
2013 for (int i = 0; i < apps.size(); i++) {
Sunny Goyal3e9be432017-01-05 15:22:41 -08002014 LauncherActivityInfo app = apps.get(i);
Kenny Guyed131872014-04-30 03:02:21 +01002015 // This builds the icon bitmaps.
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08002016 mBgAllAppsList.add(new AppInfo(mContext, app, user, quietMode), app);
Kenny Guyed131872014-04-30 03:02:21 +01002017 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002018
Sunny Goyal756a28a2015-04-23 17:07:55 -07002019 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2020 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002021 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002022
2023 @Override
2024 public void run() {
2025 heuristic.processUserApps(apps);
2026 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002027 };
2028 runOnMainThread(new Runnable() {
2029
2030 @Override
2031 public void run() {
2032 // Check isLoadingWorkspace on the UI thread, as it is updated on
2033 // the UI thread.
2034 if (mIsLoadingAndBindingWorkspace) {
2035 synchronized (mBindCompleteRunnables) {
2036 mBindCompleteRunnables.add(r);
2037 }
2038 } else {
2039 runOnWorkerThread(r);
2040 }
2041 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002042 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002043 }
Winson Chung64359a52013-07-08 17:17:08 -07002044 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002045 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002046 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2047 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002048
2049 // Post callback on main thread
2050 mHandler.post(new Runnable() {
2051 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002052
Winson Chung64359a52013-07-08 17:17:08 -07002053 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002054 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002055 if (callbacks != null) {
2056 callbacks.bindAllApplications(added);
2057 if (DEBUG_LOADERS) {
2058 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002059 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07002060 }
2061 } else {
2062 Log.i(TAG, "not binding apps: no Launcher activity");
2063 }
2064 }
2065 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002066 // Cleanup any data stored for a deleted user.
2067 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04002068 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002069 Log.d(TAG, "Icons processed in "
2070 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002071 }
2072 }
2073
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002074 private void loadAndBindDeepShortcuts() {
2075 if (DEBUG_LOADERS) {
2076 Log.d(TAG, "loadAndBindDeepShortcuts mDeepShortcutsLoaded=" + mDeepShortcutsLoaded);
2077 }
2078 if (!mDeepShortcutsLoaded) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07002079 sBgDataModel.deepShortcutMap.clear();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302080 DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(mContext);
2081 mHasShortcutHostPermission = shortcutManager.hasHostPermission();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002082 if (mHasShortcutHostPermission) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002083 for (UserHandle user : mUserManager.getUserProfiles()) {
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002084 if (mUserManager.isUserUnlocked(user)) {
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302085 List<ShortcutInfoCompat> shortcuts =
2086 shortcutManager.queryForAllShortcuts(user);
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07002087 sBgDataModel.updateDeepShortcutMap(null, user, shortcuts);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002088 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002089 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002090 }
2091 synchronized (LoaderTask.this) {
2092 if (mStopped) {
2093 return;
2094 }
2095 mDeepShortcutsLoaded = true;
2096 }
2097 }
Tony Wickham80f57872016-06-29 18:12:15 -07002098 bindDeepShortcuts();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002099 }
2100
Joe Onoratobe386092009-11-17 17:32:16 -08002101 public void dumpState() {
Sunny Goyale9956a72016-09-01 17:24:47 -07002102 synchronized (sBgDataModel) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002103 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002104 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2105 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
Sunny Goyale9956a72016-09-01 17:24:47 -07002106 Log.d(TAG, "mItems size=" + sBgDataModel.workspaceItems.size());
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002107 }
Joe Onorato36115782010-06-17 13:28:48 -04002108 }
2109 }
2110
Tony Wickham80f57872016-06-29 18:12:15 -07002111 public void bindDeepShortcuts() {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07002112 final MultiHashMap<ComponentKey, String> shortcutMapCopy =
2113 sBgDataModel.deepShortcutMap.clone();
Tony Wickham80f57872016-06-29 18:12:15 -07002114 Runnable r = new Runnable() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002115 @Override
2116 public void run() {
2117 Callbacks callbacks = getCallback();
2118 if (callbacks != null) {
2119 callbacks.bindDeepShortcutMap(shortcutMapCopy);
2120 }
2121 }
Tony Wickham80f57872016-06-29 18:12:15 -07002122 };
2123 runOnMainThread(r);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002124 }
2125
Sunny Goyal75b0f552015-05-20 21:57:06 -07002126 /**
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002127 * Refreshes the cached shortcuts if the shortcut permission has changed.
2128 * Current implementation simply reloads the workspace, but it can be optimized to
2129 * use partial updates similar to {@link UserManagerCompat}
2130 */
2131 public void refreshShortcutsIfRequired() {
Sunny Goyalf5e37442016-11-02 10:31:24 -07002132 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002133 sWorker.removeCallbacks(mShortcutPermissionCheckRunnable);
2134 sWorker.post(mShortcutPermissionCheckRunnable);
2135 }
2136 }
2137
2138 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -07002139 * Called when the icons for packages have been updated in the icon cache.
2140 */
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002141 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandle user) {
Sunny Goyal75b0f552015-05-20 21:57:06 -07002142 // If any package icon has changed (app was updated while launcher was dead),
2143 // update the corresponding shortcuts.
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002144 enqueueModelUpdateTask(new CacheDataUpdatedTask(
2145 CacheDataUpdatedTask.OP_CACHE_UPDATE, user, updatedPackages));
Sunny Goyal75b0f552015-05-20 21:57:06 -07002146 }
2147
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002148 void enqueueModelUpdateTask(BaseModelUpdateTask task) {
2149 task.init(this);
2150 runOnWorkerThread(task);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002151 }
2152
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002153 /**
2154 * A task to be executed on the current callbacks on the UI thread.
2155 * If there is no current callbacks, the task is ignored.
2156 */
2157 public interface CallbackTask {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002158
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002159 void execute(Callbacks callbacks);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002160 }
2161
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002162 /**
2163 * A runnable which changes/updates the data model of the launcher based on certain events.
2164 */
2165 public static abstract class BaseModelUpdateTask implements Runnable {
Joe Onorato36115782010-06-17 13:28:48 -04002166
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002167 private LauncherModel mModel;
2168 private DeferredHandler mUiHandler;
Joe Onorato36115782010-06-17 13:28:48 -04002169
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002170 /* package private */
2171 void init(LauncherModel model) {
2172 mModel = model;
2173 mUiHandler = mModel.mHandler;
Joe Onorato36115782010-06-17 13:28:48 -04002174 }
2175
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002176 @Override
Joe Onorato36115782010-06-17 13:28:48 -04002177 public void run() {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002178 if (!mModel.mHasLoaderCompletedOnce) {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002179 // Loader has not yet run.
2180 return;
2181 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002182 execute(mModel.mApp, sBgDataModel, mModel.mBgAllAppsList);
2183 }
Joe Onorato36115782010-06-17 13:28:48 -04002184
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002185 /**
2186 * Execute the actual task. Called on the worker thread.
2187 */
2188 public abstract void execute(
2189 LauncherAppState app, BgDataModel dataModel, AllAppsList apps);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002190
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002191 /**
2192 * Schedules a {@param task} to be executed on the current callbacks.
2193 */
2194 public final void scheduleCallbackTask(final CallbackTask task) {
2195 final Callbacks callbacks = mModel.getCallback();
2196 mUiHandler.post(new Runnable() {
2197 public void run() {
2198 Callbacks cb = mModel.getCallback();
2199 if (callbacks == cb && cb != null) {
2200 task.execute(callbacks);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002201 }
2202 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002203 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002204 }
2205 }
2206
Sunny Goyal1b072632017-01-18 11:30:23 -08002207 public void updateAndBindShortcutInfo(final ShortcutInfo si, final ShortcutInfoCompat info) {
2208 updateAndBindShortcutInfo(new Provider<ShortcutInfo>() {
2209 @Override
2210 public ShortcutInfo get() {
2211 si.updateFromDeepShortcutInfo(info, mApp.getContext());
2212 si.iconBitmap = LauncherIcons.createShortcutIcon(info, mApp.getContext());
2213 return si;
2214 }
2215 });
2216 }
2217
Sunny Goyal10923b32016-07-20 15:42:44 -07002218 /**
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08002219 * Utility method to update a shortcut on the background thread.
Sunny Goyal10923b32016-07-20 15:42:44 -07002220 */
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08002221 public void updateAndBindShortcutInfo(final Provider<ShortcutInfo> shortcutProvider) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002222 enqueueModelUpdateTask(new ExtendedModelTask() {
Sunny Goyal10923b32016-07-20 15:42:44 -07002223 @Override
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002224 public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08002225 ShortcutInfo info = shortcutProvider.get();
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002226 ArrayList<ShortcutInfo> update = new ArrayList<>();
Sunny Goyal10923b32016-07-20 15:42:44 -07002227 update.add(info);
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08002228 bindUpdatedShortcuts(update, info.user);
Sunny Goyal10923b32016-07-20 15:42:44 -07002229 }
2230 });
2231 }
2232
Sunny Goyald164b7f2016-10-12 20:49:31 -07002233 private void bindWidgetsModel(final Callbacks callbacks) {
2234 final MultiHashMap<PackageItemInfo, WidgetItem> widgets
2235 = mBgWidgetsModel.getWidgetsMap().clone();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002236 mHandler.post(new Runnable() {
2237 @Override
2238 public void run() {
2239 Callbacks cb = getCallback();
2240 if (callbacks == cb && cb != null) {
Sunny Goyald164b7f2016-10-12 20:49:31 -07002241 callbacks.bindAllWidgets(widgets);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002242 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07002243 }
2244 });
2245 }
2246
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002247 public void refreshAndBindWidgetsAndShortcuts(
2248 final Callbacks callbacks, final boolean bindFirst) {
2249 runOnWorkerThread(new Runnable() {
2250 @Override
2251 public void run() {
2252 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
Sunny Goyald164b7f2016-10-12 20:49:31 -07002253 bindWidgetsModel(callbacks);
Sunny Goyal31860582015-09-18 08:38:57 -07002254 }
Sunny Goyald164b7f2016-10-12 20:49:31 -07002255 ArrayList<WidgetItem> allWidgets = mBgWidgetsModel.update(mApp.getContext());
2256 bindWidgetsModel(callbacks);
2257
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002258 // update the Widget entries inside DB on the worker thread.
Sunny Goyal87f784c2017-01-11 10:48:34 -08002259 mApp.getWidgetCache().removeObsoletePreviews(allWidgets);
Sunny Goyal31860582015-09-18 08:38:57 -07002260 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002261 });
Michael Jurkac402cd92013-05-20 15:49:32 +02002262 }
2263
Sunny Goyal651077b2014-06-30 14:15:31 -07002264 static boolean isValidProvider(AppWidgetProviderInfo provider) {
2265 return (provider != null) && (provider.provider != null)
2266 && (provider.provider.getPackageName() != null);
2267 }
2268
Joe Onoratobe386092009-11-17 17:32:16 -08002269 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08002270 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002271 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
2272 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
2273 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
2274 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04002275 if (mLoaderTask != null) {
2276 mLoaderTask.dumpState();
2277 } else {
2278 Log.d(TAG, "mLoaderTask=null");
2279 }
Joe Onoratobe386092009-11-17 17:32:16 -08002280 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002281
2282 public Callbacks getCallback() {
2283 return mCallbacks != null ? mCallbacks.get() : null;
2284 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002285
2286 /**
2287 * @return {@link FolderInfo} if its already loaded.
2288 */
2289 public FolderInfo findFolderById(Long folderId) {
Sunny Goyale9956a72016-09-01 17:24:47 -07002290 synchronized (sBgDataModel) {
2291 return sBgDataModel.folders.get(folderId);
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002292 }
2293 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002294
Sunny Goyal527c7d32015-08-28 15:19:36 -07002295 @Thunk class DeferredMainThreadExecutor implements Executor {
2296
2297 @Override
2298 public void execute(Runnable command) {
2299 runOnMainThread(command);
2300 }
2301 }
2302
Sunny Goyal756adbc2015-04-16 15:20:51 -07002303 /**
2304 * @return the looper for the worker thread which can be used to start background tasks.
2305 */
2306 public static Looper getWorkerLooper() {
2307 return sWorkerThread.getLooper();
2308 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002309}