blob: 42c798325ce5c33c5f920f7f5606af2df6c309b0 [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;
Joe Onorato0589f0f2010-02-08 13:44:00 -080027import android.content.Intent.ShortcutIconResource;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070028import android.content.IntentFilter;
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.database.Cursor;
31import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.net.Uri;
Joe Onorato36115782010-06-17 13:28:48 -040033import android.os.Handler;
34import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070035import android.os.Looper;
Joe Onorato0589f0f2010-02-08 13:44:00 -080036import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040038import android.os.SystemClock;
Sunny Goyale26d1002016-06-20 14:52:14 -070039import android.os.Trace;
Chris Wrenc3919c02013-09-18 09:48:33 -040040import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080041import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070042import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080043import android.util.LongSparseArray;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070044import android.util.MutableInt;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010045
Sunny Goyalffe83f12014-08-14 17:39:34 -070046import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010047import com.android.launcher3.compat.LauncherActivityInfoCompat;
48import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070049import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070050import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010051import com.android.launcher3.compat.UserHandleCompat;
52import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070053import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -070054import com.android.launcher3.config.ProviderConfig;
Tony Wickham827cef22016-03-17 15:39:39 -070055import com.android.launcher3.dynamicui.ExtractionUtils;
Sunny Goyal26119432016-02-18 22:09:23 +000056import com.android.launcher3.folder.Folder;
57import com.android.launcher3.folder.FolderIcon;
Sunny Goyal10629b02016-09-01 12:50:11 -070058import com.android.launcher3.graphics.LauncherIcons;
Sunny Goyal1acb9e92016-05-16 12:41:09 -070059import com.android.launcher3.logging.FileLog;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070060import com.android.launcher3.model.AddWorkspaceItemsTask;
61import com.android.launcher3.model.ExtendedModelTask;
Sunny Goyale9956a72016-09-01 17:24:47 -070062import com.android.launcher3.model.BgDataModel;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070063import com.android.launcher3.model.CacheDataUpdatedTask;
Sunny Goyalf862a262015-12-14 14:27:38 -080064import com.android.launcher3.model.GridSizeMigrationTask;
Sunny Goyald164b7f2016-10-12 20:49:31 -070065import com.android.launcher3.model.PackageItemInfo;
Sunny Goyalc2936bc2016-09-01 15:50:36 -070066import com.android.launcher3.model.SdCardAvailableReceiver;
Sunny Goyald164b7f2016-10-12 20:49:31 -070067import com.android.launcher3.model.WidgetItem;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070068import com.android.launcher3.model.PackageInstallStateChangedTask;
69import com.android.launcher3.model.PackageUpdatedTask;
70import com.android.launcher3.model.ShortcutsChangedTask;
71import com.android.launcher3.model.UserLockStateChangedTask;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070072import com.android.launcher3.model.WidgetsModel;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -070073import com.android.launcher3.provider.ImportDataTask;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070074import com.android.launcher3.provider.LauncherDbUtils;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070075import com.android.launcher3.shortcuts.DeepShortcutManager;
76import com.android.launcher3.shortcuts.ShortcutInfoCompat;
77import com.android.launcher3.shortcuts.ShortcutKey;
Robin Lee26ace122015-03-16 19:41:43 +000078import com.android.launcher3.util.ComponentKey;
Sunny Goyal32f3dda2016-11-11 11:45:00 -080079import com.android.launcher3.util.ContentWriter;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070080import com.android.launcher3.util.CursorIconInfo;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070081import com.android.launcher3.util.GridOccupancy;
Sunny Goyal40452cf2016-09-01 15:17:46 -070082import com.android.launcher3.util.ItemInfoMatcher;
Sunny Goyale2df0622015-04-24 11:27:00 -070083import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070084import com.android.launcher3.util.ManagedProfileHeuristic;
Tony Wickhamd82a39d2016-07-01 15:44:13 -070085import com.android.launcher3.util.MultiHashMap;
Sunny Goyald09c3702016-04-06 16:18:20 -070086import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070087import com.android.launcher3.util.Preconditions;
Adam Cohen091440a2015-03-18 14:16:05 -070088import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070089import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080090
Michael Jurkac2f801e2011-07-12 14:19:46 -070091import java.lang.ref.WeakReference;
92import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070093import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070094import java.util.ArrayList;
95import java.util.Collections;
96import java.util.Comparator;
97import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070098import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070099import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -0700100import java.util.List;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700101import java.util.Map;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700102import java.util.Set;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700103import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -0700104
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800105/**
106 * Maintains in-memory state of the Launcher. It is expected that there should be only one
107 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700108 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109 */
Kenny Guyed131872014-04-30 03:02:21 +0100110public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +0100111 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800112 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -0400113 private static final boolean DEBUG_RECEIVER = false;
Chris Wrenb358f812014-04-16 13:37:00 -0400114
Joe Onorato9c1289c2009-08-17 11:03:03 -0400115 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700116
Joe Onorato36115782010-06-17 13:28:48 -0400117 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500118 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800119
Adam Cohen091440a2015-03-18 14:16:05 -0700120 @Thunk final LauncherAppState mApp;
121 @Thunk final Object mLock = new Object();
122 @Thunk DeferredHandler mHandler = new DeferredHandler();
123 @Thunk LoaderTask mLoaderTask;
124 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700125 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126
Adam Cohen091440a2015-03-18 14:16:05 -0700127 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700128 static {
129 sWorkerThread.start();
130 }
Adam Cohen091440a2015-03-18 14:16:05 -0700131 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700132
Joe Onoratocc67f472010-06-08 10:54:30 -0700133 // We start off with everything not loaded. After that, we assume that
134 // our monitoring of the package manager provides all updates and we never
135 // need to do a requery. These are only ever touched from the loader thread.
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700136 private boolean mWorkspaceLoaded;
137 private boolean mAllAppsLoaded;
138 private boolean mDeepShortcutsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700139
Sunny Goyal756a28a2015-04-23 17:07:55 -0700140 /**
141 * Set of runnables to be called on the background thread after the workspace binding
142 * is complete.
143 */
144 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
145
Adam Cohen091440a2015-03-18 14:16:05 -0700146 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700148 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800149 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700150 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800151 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800152
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700153 private boolean mHasShortcutHostPermission;
154 // Runnable to check if the shortcuts permission has changed.
155 private final Runnable mShortcutPermissionCheckRunnable = new Runnable() {
156 @Override
157 public void run() {
158 if (mDeepShortcutsLoaded) {
159 boolean hasShortcutHostPermission = mDeepShortcutManager.hasHostPermission();
160 if (hasShortcutHostPermission != mHasShortcutHostPermission) {
161 mApp.reloadWorkspace();
162 }
163 }
164 }
165 };
166
Sunny Goyale9956a72016-09-01 17:24:47 -0700167 /**
168 * All the static data should be accessed on the background thread, A lock should be acquired
169 * on this object when accessing any data from this model.
170 */
171 static final BgDataModel sBgDataModel = new BgDataModel();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700172
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700173 // </ only access in worker thread >
174
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700175 private final IconCache mIconCache;
176 private final DeepShortcutManager mDeepShortcutManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700178 private final LauncherAppsCompat mLauncherApps;
179 private final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100180
Joe Onorato9c1289c2009-08-17 11:03:03 -0400181 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700182 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400183 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700184 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400185 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700186 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
187 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700188 public void bindScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700189 public void finishFirstPageBind(ViewOnDrawExecutor executor);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800190 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400191 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200192 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700193 public void bindAppsAdded(ArrayList<Long> newScreens,
194 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700195 ArrayList<ItemInfo> addAnimated,
196 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200197 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700198 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
199 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
200 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700201 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700202 public void bindWorkspaceComponentsRemoved(
203 HashSet<String> packageNames, HashSet<ComponentName> components,
204 UserHandleCompat user);
205 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800206 public void notifyWidgetProvidersChanged();
Sunny Goyald164b7f2016-10-12 20:49:31 -0700207 public void bindAllWidgets(MultiHashMap<PackageItemInfo, WidgetItem> widgets);
Adam Cohen1462de32012-07-24 22:34:36 -0700208 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700209 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700210 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMap);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400211 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700213 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter,
214 DeepShortcutManager deepShortcutManager) {
Winson Chunga6945242014-01-08 14:04:34 -0800215 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400216 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100217 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Sunny Goyald164b7f2016-10-12 20:49:31 -0700218 mBgWidgetsModel = new WidgetsModel(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800219 mIconCache = iconCache;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700220 mDeepShortcutManager = deepShortcutManager;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800221
Kenny Guyed131872014-04-30 03:02:21 +0100222 mLauncherApps = LauncherAppsCompat.getInstance(context);
223 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800224 }
225
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700226 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
227 * posted on the main thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700228 private void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700229 if (sWorkerThread.getThreadId() == Process.myTid()) {
230 // If we are on the worker thread, post onto the main handler
231 mHandler.post(r);
232 } else {
233 r.run();
234 }
235 }
236
237 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
238 * posted on the worker thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700239 private static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700240 if (sWorkerThread.getThreadId() == Process.myTid()) {
241 r.run();
242 } else {
243 // If we are not on the worker thread, then post to the worker handler
244 sWorker.post(r);
245 }
246 }
247
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700248 public void setPackageState(PackageInstallInfo installInfo) {
249 enqueueModelUpdateTask(new PackageInstallStateChangedTask(installInfo));
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500250 }
251
Sunny Goyal756adbc2015-04-16 15:20:51 -0700252 /**
253 * Updates the icons and label of all pending icons for the provided package name.
254 */
255 public void updateSessionDisplayInfo(final String packageName) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700256 HashSet<String> packages = new HashSet<>();
257 packages.add(packageName);
258 enqueueModelUpdateTask(new CacheDataUpdatedTask(
259 CacheDataUpdatedTask.OP_SESSION_UPDATE, UserHandleCompat.myUserHandle(), packages));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800260 }
261
262 /**
263 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800264 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700265 public void addAndBindAddedWorkspaceItems(
Sunny Goyala214a632015-05-06 12:23:34 -0700266 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700267 enqueueModelUpdateTask(new AddWorkspaceItemsTask(workspaceApps));
Winson Chung64359a52013-07-08 17:17:08 -0700268 }
269
Joe Onorato9c1289c2009-08-17 11:03:03 -0400270 /**
271 * Adds an item to the DB if it was not created previously, or move it to a new
272 * <container, screen, cellX, cellY>
273 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800274 public static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700275 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400276 if (item.container == ItemInfo.NO_ID) {
277 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700278 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400279 } else {
280 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700281 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282 }
283 }
284
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700285 static void checkItemInfoLocked(
286 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
Sunny Goyale9956a72016-09-01 17:24:47 -0700287 ItemInfo modelItem = sBgDataModel.itemsIdMap.get(itemId);
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700288 if (modelItem != null && item != modelItem) {
289 // check all the data is consistent
290 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
291 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
292 ShortcutInfo shortcut = (ShortcutInfo) item;
293 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
294 modelShortcut.intent.filterEquals(shortcut.intent) &&
295 modelShortcut.id == shortcut.id &&
296 modelShortcut.itemType == shortcut.itemType &&
297 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700298 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700299 modelShortcut.cellX == shortcut.cellX &&
300 modelShortcut.cellY == shortcut.cellY &&
301 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700302 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700303 // For all intents and purposes, this is the same object
304 return;
305 }
306 }
307
308 // the modelItem needs to match up perfectly with item if our model is
309 // to be consistent with the database-- for now, just require
310 // modelItem == item or the equality check above
311 String msg = "item: " + ((item != null) ? item.toString() : "null") +
312 "modelItem: " +
313 ((modelItem != null) ? modelItem.toString() : "null") +
314 "Error: ItemInfo passed to checkItemInfo doesn't match original";
315 RuntimeException e = new RuntimeException(msg);
316 if (stackTrace != null) {
317 e.setStackTrace(stackTrace);
318 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800319 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700320 }
321 }
322
Michael Jurka816474f2012-06-25 14:49:02 -0700323 static void checkItemInfo(final ItemInfo item) {
324 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
325 final long itemId = item.id;
326 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700327 public void run() {
Sunny Goyale9956a72016-09-01 17:24:47 -0700328 synchronized (sBgDataModel) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700329 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700330 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700331 }
332 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700333 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700334 }
335
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800336 static void updateItemInDatabaseHelper(Context context, final ContentWriter writer,
Michael Jurkac9d95c52011-08-29 14:03:34 -0700337 final ItemInfo item, final String callingFunction) {
338 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700339 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700340 final ContentResolver cr = context.getContentResolver();
341
Adam Cohen487f7dd2012-06-28 18:12:10 -0700342 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700343 Runnable r = new Runnable() {
344 public void run() {
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800345 cr.update(uri, writer.getValues(), null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700346 updateItemArrays(item, itemId, stackTrace);
347 }
348 };
349 runOnWorkerThread(r);
350 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700351
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700352 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
353 final ArrayList<ItemInfo> items, final String callingFunction) {
354 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700355
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700356 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
357 Runnable r = new Runnable() {
358 public void run() {
359 ArrayList<ContentProviderOperation> ops =
360 new ArrayList<ContentProviderOperation>();
361 int count = items.size();
362 for (int i = 0; i < count; i++) {
363 ItemInfo item = items.get(i);
364 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700365 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700366 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700367
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700368 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
369 updateItemArrays(item, itemId, stackTrace);
370
371 }
372 try {
373 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
374 } catch (Exception e) {
375 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700376 }
377 }
378 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700379 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700380 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700381
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700382 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
383 // Lock on mBgLock *after* the db operation
Sunny Goyale9956a72016-09-01 17:24:47 -0700384 synchronized (sBgDataModel) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700385 checkItemInfoLocked(itemId, item, stackTrace);
386
387 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
388 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
389 // Item is in a folder, make sure this folder exists
Sunny Goyale9956a72016-09-01 17:24:47 -0700390 if (!sBgDataModel.folders.containsKey(item.container)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700391 // An items container is being set to a that of an item which is not in
392 // the list of Folders.
393 String msg = "item: " + item + " container being set to: " +
394 item.container + ", not in the list of folders";
395 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700396 }
397 }
398
399 // Items are added/removed from the corresponding FolderInfo elsewhere, such
400 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
401 // that are on the desktop, as appropriate
Sunny Goyale9956a72016-09-01 17:24:47 -0700402 ItemInfo modelItem = sBgDataModel.itemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800403 if (modelItem != null &&
404 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
405 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700406 switch (modelItem.itemType) {
407 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
408 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700409 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700410 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyale9956a72016-09-01 17:24:47 -0700411 if (!sBgDataModel.workspaceItems.contains(modelItem)) {
412 sBgDataModel.workspaceItems.add(modelItem);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700413 }
414 break;
415 default:
416 break;
417 }
418 } else {
Sunny Goyale9956a72016-09-01 17:24:47 -0700419 sBgDataModel.workspaceItems.remove(modelItem);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700420 }
421 }
422 }
423
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800424 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400425 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700426 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700427 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700428 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400429 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400430 item.cellX = cellX;
431 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700432
Winson Chung3d503fb2011-07-13 17:25:49 -0700433 // We store hotseat items in canonical form which is this orientation invariant position
434 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700435 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700436 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700437 item.screenId = Launcher.getLauncher(context).getHotseat()
438 .getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700439 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700440 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700441 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400442
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800443 final ContentWriter writer = new ContentWriter(context)
444 .put(LauncherSettings.Favorites.CONTAINER, item.container)
445 .put(LauncherSettings.Favorites.CELLX, item.cellX)
446 .put(LauncherSettings.Favorites.CELLY, item.cellY)
447 .put(LauncherSettings.Favorites.RANK, item.rank)
448 .put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400449
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800450 updateItemInDatabaseHelper(context, writer, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700451 }
452
453 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700454 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
455 * cellX, cellY have already been updated on the ItemInfos.
456 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800457 public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700458 final long container, final int screen) {
459
460 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
461 int count = items.size();
462
463 for (int i = 0; i < count; i++) {
464 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700465 item.container = container;
466
467 // We store hotseat items in canonical form which is this orientation invariant position
468 // in the hotseat
469 if (context instanceof Launcher && screen < 0 &&
470 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700471 item.screenId = Launcher.getLauncher(context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700472 item.cellY);
473 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700474 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700475 }
476
477 final ContentValues values = new ContentValues();
478 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
479 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
480 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800481 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700482 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700483
484 contentValues.add(values);
485 }
486 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
487 }
488
489 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700490 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800491 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700492 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700493 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700494 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800495 item.cellX = cellX;
496 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700497 item.spanX = spanX;
498 item.spanY = spanY;
499
500 // We store hotseat items in canonical form which is this orientation invariant position
501 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700502 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700503 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700504 item.screenId = Launcher.getLauncher(context).getHotseat()
505 .getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700506 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700507 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700508 }
Adam Cohend4844c32011-02-18 19:25:06 -0800509
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800510 final ContentWriter writer = new ContentWriter(context)
511 .put(LauncherSettings.Favorites.CONTAINER, item.container)
512 .put(LauncherSettings.Favorites.CELLX, item.cellX)
513 .put(LauncherSettings.Favorites.CELLY, item.cellY)
514 .put(LauncherSettings.Favorites.RANK, item.rank)
515 .put(LauncherSettings.Favorites.SPANX, item.spanX)
516 .put(LauncherSettings.Favorites.SPANY, item.spanY)
517 .put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800518
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800519 updateItemInDatabaseHelper(context, writer, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700520 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700521
522 /**
523 * Update an item to the database in a specified container.
524 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700525 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800526 ContentWriter writer = new ContentWriter(context);
527 item.onAddToDatabase(writer);
528 updateItemInDatabaseHelper(context, writer, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800529 }
530
Joe Onorato9c1289c2009-08-17 11:03:03 -0400531 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400532 * Add an item to the database in a specified container. Sets the container, screen, cellX and
533 * cellY fields of the item. Also assigns an ID to the item.
534 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700535 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700536 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400537 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400538 item.cellX = cellX;
539 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700540 // We store hotseat items in canonical form which is this orientation invariant position
541 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700542 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700543 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700544 item.screenId = Launcher.getLauncher(context).getHotseat()
545 .getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700546 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700547 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700548 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400549
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800550 final ContentWriter writer = new ContentWriter(context);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400551 final ContentResolver cr = context.getContentResolver();
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800552 item.onAddToDatabase(writer);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400553
Sunny Goyald2497482015-09-22 18:24:19 -0700554 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
555 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
556
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800557 writer.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700558
Jason Monk8e19cf22014-03-20 15:06:57 -0400559 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700560 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700561 public void run() {
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800562 cr.insert(LauncherSettings.Favorites.CONTENT_URI, writer.getValues());
Joe Onorato9c1289c2009-08-17 11:03:03 -0400563
Sunny Goyale9956a72016-09-01 17:24:47 -0700564 synchronized (sBgDataModel) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400565 checkItemInfoLocked(item.id, item, stackTrace);
Sunny Goyale9956a72016-09-01 17:24:47 -0700566 sBgDataModel.addItem(item, true);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700567 }
568 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700569 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700570 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700571 }
572
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700573 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700574 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -0400575 */
Sunny Goyalfa401a12015-04-10 13:45:42 -0700576 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyal40452cf2016-09-01 15:17:46 -0700577 ArrayList<ItemInfo> items = new ArrayList<>();
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700578 items.add(item);
579 deleteItemsFromDatabase(context, items);
580 }
581
582 /**
Sunny Goyal40452cf2016-09-01 15:17:46 -0700583 * Removes all the items from the database matching {@param matcher}.
584 */
585 public static void deleteItemsFromDatabase(Context context, ItemInfoMatcher matcher) {
Sunny Goyale9956a72016-09-01 17:24:47 -0700586 deleteItemsFromDatabase(context, matcher.filterItemInfos(sBgDataModel.itemsIdMap));
Sunny Goyal40452cf2016-09-01 15:17:46 -0700587 }
588
589 /**
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700590 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700591 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700592 public static void deleteItemsFromDatabase(Context context,
593 final Iterable<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400594 final ContentResolver cr = context.getContentResolver();
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 Goyale9956a72016-09-01 17:24:47 -0700601 sBgDataModel.removeItem(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();
661
Michael Jurkac9d95c52011-08-29 14:03:34 -0700662 Runnable r = new Runnable() {
663 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700664 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -0700665 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Sunny Goyale9956a72016-09-01 17:24:47 -0700666 sBgDataModel.removeItem(info.contents);
667 info.contents.clear();
668
669 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
670 sBgDataModel.removeItem(info);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700671 }
672 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700673 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400674 }
675
676 /**
677 * Set this as the current Launcher activity object for the loader.
678 */
679 public void initialize(Callbacks callbacks) {
680 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -0700681 Preconditions.assertUIThread();
682 // Remove any queued UI runnables
683 mHandler.cancelAll();
684 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400685 }
686 }
687
Kenny Guyed131872014-04-30 03:02:21 +0100688 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +0100689 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +0100690 int op = PackageUpdatedTask.OP_UPDATE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700691 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100692 }
693
694 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +0100695 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Sunny Goyalc2936bc2016-09-01 15:50:36 -0700696 onPackagesRemoved(user, packageName);
697 }
698
699 public void onPackagesRemoved(UserHandleCompat user, String... packages) {
Kenny Guyed131872014-04-30 03:02:21 +0100700 int op = PackageUpdatedTask.OP_REMOVE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700701 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packages));
Kenny Guyed131872014-04-30 03:02:21 +0100702 }
703
704 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +0100705 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +0100706 int op = PackageUpdatedTask.OP_ADD;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700707 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100708 }
709
710 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +0100711 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +0100712 boolean replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700713 enqueueModelUpdateTask(
714 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100715 }
716
717 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +0100718 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +0100719 boolean replacing) {
720 if (!replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700721 enqueueModelUpdateTask(new PackageUpdatedTask(
722 PackageUpdatedTask.OP_UNAVAILABLE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100723 }
Kenny Guyed131872014-04-30 03:02:21 +0100724 }
725
Kenny Guy44cba692016-01-21 19:50:02 +0000726 @Override
727 public void onPackagesSuspended(String[] packageNames, UserHandleCompat user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700728 enqueueModelUpdateTask(new PackageUpdatedTask(
729 PackageUpdatedTask.OP_SUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000730 }
731
732 @Override
733 public void onPackagesUnsuspended(String[] packageNames, UserHandleCompat user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700734 enqueueModelUpdateTask(new PackageUpdatedTask(
735 PackageUpdatedTask.OP_UNSUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000736 }
737
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700738 @Override
739 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
740 UserHandleCompat user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700741 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, true));
Sunny Goyal50941fb2016-08-04 12:03:52 -0700742 }
743
744 public void updatePinnedShortcuts(String packageName, List<ShortcutInfoCompat> shortcuts,
745 UserHandleCompat user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700746 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, false));
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700747 }
748
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700749 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400750 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
751 * ACTION_PACKAGE_CHANGED.
752 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100753 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400754 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -0400755 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700756
Joe Onorato36115782010-06-17 13:28:48 -0400757 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +0100758 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -0700759 // If we have changed locale we need to clear out the labels in all apps/workspace.
760 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700761 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
762 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -0700763 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -0700764 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700765 } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
766 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
767 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyalda891c12016-03-18 18:29:24 -0700768 UserHandleCompat user = UserHandleCompat.fromIntent(intent);
769 if (user != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700770 if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
771 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700772 enqueueModelUpdateTask(new PackageUpdatedTask(
773 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE, user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700774 }
775
776 // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so
777 // we need to run the state change task again.
778 if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
779 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700780 enqueueModelUpdateTask(new UserLockStateChangedTask(user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700781 }
Sunny Goyalda891c12016-03-18 18:29:24 -0700782 }
Tony Wickham827cef22016-03-17 15:39:39 -0700783 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
784 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700785 }
786 }
787
Amith Yamasani6cc806d2014-05-02 13:47:11 -0700788 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700789 resetLoadedState(true, true);
790
Reena Lee93f824a2011-09-23 17:20:28 -0700791 // Do this here because if the launcher activity is running it will be restarted.
792 // If it's not running startLoaderFromBackground will merely tell it that it needs
793 // to reload.
794 startLoaderFromBackground();
795 }
796
Winson Chungf0c6ae02012-03-21 16:10:31 -0700797 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
798 synchronized (mLock) {
799 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
800 // mWorkspaceLoaded to true later
801 stopLoaderLocked();
802 if (resetAllAppsLoaded) mAllAppsLoaded = false;
803 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700804 // Always reset deep shortcuts loaded.
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700805 // TODO: why?
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700806 mDeepShortcutsLoaded = false;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700807 }
808 }
809
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700810 /**
811 * When the launcher is in the background, it's possible for it to miss paired
812 * configuration changes. So whenever we trigger the loader from the background
813 * tell the launcher that it needs to re-run the loader when it comes back instead
814 * of doing it now.
815 */
816 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800817 Callbacks callbacks = getCallback();
818 if (callbacks != null) {
819 // Only actually run the loader if they're not paused.
820 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -0700821 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700822 }
823 }
Joe Onorato36115782010-06-17 13:28:48 -0400824 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400825
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700826 /**
827 * If there is already a loader task running, tell it to stop.
828 */
829 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -0700830 LoaderTask oldTask = mLoaderTask;
831 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -0700832 oldTask.stopLocked();
833 }
Reena Lee93f824a2011-09-23 17:20:28 -0700834 }
835
Adam Cohen1a85c582014-09-30 09:48:49 -0700836 public boolean isCurrentCallbacks(Callbacks callbacks) {
837 return (mCallbacks != null && mCallbacks.get() == callbacks);
838 }
839
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700840 /**
841 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
842 * @return true if the page could be bound synchronously.
843 */
844 public boolean startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700845 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
846 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -0400847 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -0400848 // Don't bother to start the thread if we know it's not going to do anything
849 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -0700850 final Callbacks oldCallbacks = mCallbacks.get();
851 // Clear any pending bind-runnables from the synchronized load process.
852 runOnMainThread(new Runnable() {
853 public void run() {
854 oldCallbacks.clearPendingBinds();
855 }
856 });
857
Joe Onorato36115782010-06-17 13:28:48 -0400858 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700859 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700860 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700861 // TODO: mDeepShortcutsLoaded does not need to be true for synchronous bind.
Tony Wickham80f57872016-06-29 18:12:15 -0700862 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE && mAllAppsLoaded
863 && mWorkspaceLoaded && mDeepShortcutsLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700864 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700865 return true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700866 } else {
867 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
868 sWorker.post(mLoaderTask);
869 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400870 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400871 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700872 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400873 }
874
Joe Onorato36115782010-06-17 13:28:48 -0400875 public void stopLoader() {
876 synchronized (mLock) {
877 if (mLoaderTask != null) {
878 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400879 }
880 }
Joe Onorato36115782010-06-17 13:28:48 -0400881 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400882
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800883 /**
884 * Loads the workspace screen ids in an ordered list.
885 */
Sunny Goyale5bb7052015-07-27 14:36:07 -0700886 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -0700887 final ContentResolver contentResolver = context.getContentResolver();
888 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -0700889
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800890 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -0700891 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
892 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -0700893 }
894
Joe Onorato36115782010-06-17 13:28:48 -0400895 /**
896 * Runnable for the thread that loads the contents of the launcher:
897 * - workspace icons
898 * - widgets
899 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700900 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -0400901 */
902 private class LoaderTask implements Runnable {
903 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -0700904 private int mPageToBindFirst;
905
Adam Cohen091440a2015-03-18 14:16:05 -0700906 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -0400907 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -0700908 @Thunk boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700909
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700910 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -0400911 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -0700912 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400913 }
914
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800915 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700916 mIsLoadingAndBindingWorkspace = true;
917
Joe Onorato36115782010-06-17 13:28:48 -0400918 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -0400919 if (DEBUG_LOADERS) {
920 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400921 }
Michael Jurka288a36b2011-07-12 16:53:48 -0700922
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700923 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800924 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -0700925 synchronized (LoaderTask.this) {
926 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800927 return;
Reena Lee93f824a2011-09-23 17:20:28 -0700928 }
929 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400930 }
931 }
932
Joe Onorato36115782010-06-17 13:28:48 -0400933 // Bind the workspace
Sunny Goyal93f878c2016-03-30 17:31:24 -0700934 bindWorkspace(mPageToBindFirst);
Joe Onorato36115782010-06-17 13:28:48 -0400935 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400936
Joe Onorato36115782010-06-17 13:28:48 -0400937 private void waitForIdle() {
938 // Wait until the either we're stopped or the other threads are done.
939 // This way we don't start loading all apps until the workspace has settled
940 // down.
941 synchronized (LoaderTask.this) {
942 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700943
Joe Onorato36115782010-06-17 13:28:48 -0400944 mHandler.postIdle(new Runnable() {
945 public void run() {
946 synchronized (LoaderTask.this) {
947 mLoadAndBindStepFinished = true;
948 if (DEBUG_LOADERS) {
949 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400950 }
Joe Onorato36115782010-06-17 13:28:48 -0400951 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400952 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400953 }
Joe Onorato36115782010-06-17 13:28:48 -0400954 });
955
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800956 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -0400957 try {
Michael Jurkac7700af2013-05-14 20:17:58 +0200958 // Just in case mFlushingWorkerThread changes but we aren't woken up,
959 // wait no longer than 1sec at a time
960 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -0400961 } catch (InterruptedException ex) {
962 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400963 }
964 }
Joe Onorato36115782010-06-17 13:28:48 -0400965 if (DEBUG_LOADERS) {
966 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700967 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400968 + "ms for previous step to finish binding");
969 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400970 }
Joe Onorato36115782010-06-17 13:28:48 -0400971 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400972
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700973 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -0500974 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700975 // Ensure that we have a valid page index to load synchronously
976 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
977 "valid page index");
978 }
979 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
980 // Ensure that we don't try and bind a specified page when the pages have not been
981 // loaded already (we should load everything asynchronously in that case)
982 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
983 }
984 synchronized (mLock) {
985 if (mIsLoaderTaskRunning) {
986 // Ensure that we are never running the background loading at this point since
987 // we also touch the background collections
988 throw new RuntimeException("Error! Background loading is already running");
989 }
990 }
991
992 // XXX: Throw an exception if we are already loading (since we touch the worker thread
993 // data structures, we can't allow any other thread to touch that data, but because
994 // this call is synchronous, we can get away with not locking).
995
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400996 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -0700997 // operations from the previous activity. We need to ensure that all queued operations
998 // are executed before any synchronous binding work is done.
999 mHandler.flush();
1000
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001001 // Divide the set of loaded items into those that we are binding synchronously, and
1002 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001003 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001004 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1005 // arise from that.
1006 onlyBindAllApps();
Tony Wickham80f57872016-06-29 18:12:15 -07001007
1008 bindDeepShortcuts();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001009 }
1010
Joe Onorato36115782010-06-17 13:28:48 -04001011 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001012 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001013 if (mStopped) {
1014 return;
1015 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001016 mIsLoaderTaskRunning = true;
1017 }
Joe Onorato36115782010-06-17 13:28:48 -04001018 // Optimize for end-user experience: if the Launcher is up and // running with the
1019 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1020 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001021 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001022 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001023 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001024
Joe Onorato36115782010-06-17 13:28:48 -04001025 if (mStopped) {
1026 break keep_running;
1027 }
1028
Joe Onorato36115782010-06-17 13:28:48 -04001029 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001030
1031 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001032 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1033 loadAndBindAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001034
1035 waitForIdle();
1036
1037 // third step
1038 if (DEBUG_LOADERS) Log.d(TAG, "step 3: loading deep shortcuts");
1039 loadAndBindDeepShortcuts();
Joe Onorato36115782010-06-17 13:28:48 -04001040 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001041
Joe Onorato36115782010-06-17 13:28:48 -04001042 // Clear out this reference, otherwise we end up holding it until all of the
1043 // callback runnables are done.
1044 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001045
Joe Onorato36115782010-06-17 13:28:48 -04001046 synchronized (mLock) {
1047 // If we are still the last one to be scheduled, remove ourselves.
1048 if (mLoaderTask == this) {
1049 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001050 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001051 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001052 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001053 }
Joe Onorato36115782010-06-17 13:28:48 -04001054 }
1055
1056 public void stopLocked() {
1057 synchronized (LoaderTask.this) {
1058 mStopped = true;
1059 this.notify();
1060 }
1061 }
1062
1063 /**
1064 * Gets the callbacks object. If we've been stopped, or if the launcher object
1065 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1066 * object that was around when the deferred message was scheduled, and if there's
1067 * a new Callbacks object around then also return null. This will save us from
1068 * calling onto it with data that will be ignored.
1069 */
1070 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1071 synchronized (mLock) {
1072 if (mStopped) {
1073 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001074 }
Joe Onorato36115782010-06-17 13:28:48 -04001075
1076 if (mCallbacks == null) {
1077 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001078 }
Joe Onorato36115782010-06-17 13:28:48 -04001079
1080 final Callbacks callbacks = mCallbacks.get();
1081 if (callbacks != oldCallbacks) {
1082 return null;
1083 }
1084 if (callbacks == null) {
1085 Log.w(TAG, "no mCallbacks");
1086 return null;
1087 }
1088
1089 return callbacks;
1090 }
1091 }
1092
1093 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001094 private boolean checkItemPlacement(LongArrayMap<GridOccupancy> occupied, ItemInfo item,
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001095 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001096 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001097 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung892c74d2013-08-22 16:15:50 -07001098
Adam Cohendcd297f2013-06-18 13:13:40 -07001099 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001100 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001101 // Return early if we detect that an item is under the hotseat button
Sunny Goyalbb011da2016-06-15 15:42:29 -07001102 if (!FeatureFlags.NO_ALL_APPS_ICON &&
1103 profile.isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001104 Log.e(TAG, "Error loading shortcut into hotseat " + item
1105 + " into position (" + item.screenId + ":" + item.cellX + ","
1106 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001107 return false;
1108 }
1109
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001110 final GridOccupancy hotseatOccupancy =
Dan Sandler295ae182013-12-10 16:05:47 -05001111 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1112
Adam Cohen2e6da152015-05-06 11:42:25 -07001113 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001114 Log.e(TAG, "Error loading shortcut " + item
1115 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001116 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001117 + ")");
1118 return false;
1119 }
1120
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001121 if (hotseatOccupancy != null) {
1122 if (hotseatOccupancy.cells[(int) item.screenId][0]) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001123 Log.e(TAG, "Error loading shortcut into hotseat " + item
1124 + " into position (" + item.screenId + ":" + item.cellX + ","
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001125 + item.cellY + ") already occupied");
Adam Cohendcd297f2013-06-18 13:13:40 -07001126 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001127 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001128 hotseatOccupancy.cells[(int) item.screenId][0] = true;
Dan Sandler295ae182013-12-10 16:05:47 -05001129 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001130 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001131 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001132 final GridOccupancy occupancy = new GridOccupancy(profile.numHotseatIcons, 1);
1133 occupancy.cells[(int) item.screenId][0] = true;
1134 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, occupancy);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001135 return true;
1136 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001137 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1138 if (!workspaceScreens.contains((Long) item.screenId)) {
1139 // The item has an invalid screen id.
1140 return false;
1141 }
1142 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001143 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001144 return true;
1145 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001146
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001147 final int countX = profile.numColumns;
1148 final int countY = profile.numRows;
Adam Cohenae4409d2013-11-26 10:34:59 -08001149 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1150 item.cellX < 0 || item.cellY < 0 ||
1151 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1152 Log.e(TAG, "Error loading shortcut " + item
1153 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1154 + item.cellX + "," + item.cellY
1155 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1156 return false;
1157 }
1158
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001159 if (!occupied.containsKey(item.screenId)) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001160 GridOccupancy screen = new GridOccupancy(countX + 1, countY + 1);
1161 if (item.screenId == Workspace.FIRST_SCREEN_ID) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001162 // Mark the first row as occupied (if the feature is enabled)
1163 // in order to account for the QSB.
1164 screen.markCells(0, 0, countX + 1, 1, FeatureFlags.QSB_ON_FIRST_SCREEN);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001165 }
1166 occupied.put(item.screenId, screen);
Joe Onorato36115782010-06-17 13:28:48 -04001167 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001168 final GridOccupancy occupancy = occupied.get(item.screenId);
Winson Chungf30ad5f2011-08-08 10:55:42 -07001169
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001170 // Check if any workspace icons overlap with each other
1171 if (occupancy.isRegionVacant(item.cellX, item.cellY, item.spanX, item.spanY)) {
1172 occupancy.markCells(item, true);
1173 return true;
1174 } else {
1175 Log.e(TAG, "Error loading shortcut " + item
1176 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1177 + item.cellX + "," + item.cellX + "," + item.spanX + "," + item.spanY
1178 + ") already occupied");
1179 return false;
1180 }
Joe Onorato36115782010-06-17 13:28:48 -04001181 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001182
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001183 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -07001184 if (LauncherAppState.PROFILE_STARTUP) {
1185 Trace.beginSection("Loading Workspace");
1186 }
Joe Onorato36115782010-06-17 13:28:48 -04001187 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001188
Joe Onorato36115782010-06-17 13:28:48 -04001189 final Context context = mContext;
1190 final ContentResolver contentResolver = context.getContentResolver();
1191 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001192 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001193 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001194 final boolean isSdCardReady = Utilities.isBootCompleted();
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001195 final MultiHashMap<UserHandleCompat, String> pendingPackages = new MultiHashMap<>();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001196
Winson Chung892c74d2013-08-22 16:15:50 -07001197 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001198 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001199 int countX = profile.numColumns;
1200 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001201
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001202 boolean clearDb = false;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -07001203 try {
1204 ImportDataTask.performImportIfPossible(context);
1205 } catch (Exception e) {
1206 // Migration failed. Clear workspace.
1207 clearDb = true;
1208 }
1209
1210 if (!clearDb && GridSizeMigrationTask.ENABLED &&
Sunny Goyalf076eae2016-01-11 12:25:10 -08001211 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1212 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001213 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001214 }
1215
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001216 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -07001217 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001218 LauncherSettings.Settings.call(contentResolver,
1219 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001220 }
1221
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001222 Log.d(TAG, "loadWorkspace: loading default favorites");
1223 LauncherSettings.Settings.call(contentResolver,
1224 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -07001225
Sunny Goyale9956a72016-09-01 17:24:47 -07001226 synchronized (sBgDataModel) {
1227 sBgDataModel.clear();
1228
Sunny Goyal756adbc2015-04-16 15:20:51 -07001229 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001230 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyale9956a72016-09-01 17:24:47 -07001231 sBgDataModel.workspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001232
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001233 final ArrayList<Long> itemsToRemove = new ArrayList<>();
1234 final ArrayList<Long> restoredRows = new ArrayList<>();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001235 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001236 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001237 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001238 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001239
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001240 // +1 for the hotseat (it can be larger than the workspace)
1241 // Load workspace in reverse order to ensure that latest items are loaded first (and
1242 // before any earlier duplicates)
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001243 final LongArrayMap<GridOccupancy> occupied = new LongArrayMap<>();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001244 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001245
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001246 try {
1247 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1248 final int intentIndex = c.getColumnIndexOrThrow
1249 (LauncherSettings.Favorites.INTENT);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001250 final int containerIndex = c.getColumnIndexOrThrow(
1251 LauncherSettings.Favorites.CONTAINER);
1252 final int itemTypeIndex = c.getColumnIndexOrThrow(
1253 LauncherSettings.Favorites.ITEM_TYPE);
1254 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1255 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001256 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1257 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001258 final int screenIndex = c.getColumnIndexOrThrow(
1259 LauncherSettings.Favorites.SCREEN);
1260 final int cellXIndex = c.getColumnIndexOrThrow
1261 (LauncherSettings.Favorites.CELLX);
1262 final int cellYIndex = c.getColumnIndexOrThrow
1263 (LauncherSettings.Favorites.CELLY);
1264 final int spanXIndex = c.getColumnIndexOrThrow
1265 (LauncherSettings.Favorites.SPANX);
1266 final int spanYIndex = c.getColumnIndexOrThrow(
1267 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001268 final int rankIndex = c.getColumnIndexOrThrow(
1269 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001270 final int restoredIndex = c.getColumnIndexOrThrow(
1271 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001272 final int profileIdIndex = c.getColumnIndexOrThrow(
1273 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001274 final int optionsIndex = c.getColumnIndexOrThrow(
1275 LauncherSettings.Favorites.OPTIONS);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001276 final CursorIconInfo cursorIconInfo = new CursorIconInfo(mContext, c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001277
Sunny Goyal7f834d22015-04-21 10:10:23 -07001278 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
Kenny Guyff05f432016-01-22 17:48:29 +00001279 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001280 final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>();
Sunny Goyal7f834d22015-04-21 10:10:23 -07001281 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001282 long serialNo = mUserManager.getSerialNumberForUser(user);
1283 allUsers.put(serialNo, user);
1284 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001285
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001286 boolean userUnlocked = mUserManager.isUserUnlocked(user);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001287
1288 // We can only query for shortcuts when the user is unlocked.
1289 if (userUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07001290 List<ShortcutInfoCompat> pinnedShortcuts =
1291 mDeepShortcutManager.queryForPinnedShortcuts(null, user);
1292 if (mDeepShortcutManager.wasLastCallSuccess()) {
1293 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
1294 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
1295 shortcut);
1296 }
1297 } else {
1298 // Shortcut manager can fail due to some race condition when the
1299 // lock state changes too frequently. For the purpose of the loading
1300 // shortcuts, consider the user is still locked.
1301 userUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001302 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001303 }
Sunny Goyal49f4f032016-08-01 15:45:49 -07001304 unlockedUsers.put(serialNo, userUnlocked);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001305 }
1306
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001307 ShortcutInfo info;
1308 String intentDescription;
1309 LauncherAppWidgetInfo appWidgetInfo;
1310 int container;
1311 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001312 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001313 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001314 UserHandleCompat user;
Sunny Goyald09c3702016-04-06 16:18:20 -07001315 String targetPackage;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001316
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001317 while (!mStopped && c.moveToNext()) {
1318 try {
1319 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001320 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001321 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001322 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001323
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001324 switch (itemType) {
1325 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1326 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001327 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001328 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001329 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001330 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001331 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001332 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001333 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001334 boolean itemReplaced = false;
Sunny Goyald09c3702016-04-06 16:18:20 -07001335 targetPackage = null;
Kenny Guyed131872014-04-30 03:02:21 +01001336 if (user == null) {
1337 // User has been deleted remove the item.
1338 itemsToRemove.add(id);
1339 continue;
1340 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001341 try {
1342 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001343 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001344 if (cn != null && cn.getPackageName() != null) {
1345 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1346 cn.getPackageName(), user);
1347 boolean validComponent = validPkg &&
1348 launcherApps.isActivityEnabledForProfile(cn, user);
Sunny Goyald09c3702016-04-06 16:18:20 -07001349 if (validPkg) {
1350 targetPackage = cn.getPackageName();
1351 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001352
1353 if (validComponent) {
1354 if (restored) {
1355 // no special handling necessary for this item
1356 restoredRows.add(id);
1357 restored = false;
1358 }
Kenny Guyff05f432016-01-22 17:48:29 +00001359 if (quietMode.get(serialNumber)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001360 disabledState = ShortcutInfo.FLAG_DISABLED_QUIET_USER;
Kenny Guyff05f432016-01-22 17:48:29 +00001361 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001362 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001363 intent = null;
1364 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1365 // We allow auto install apps to have their intent
1366 // updated after an install.
1367 intent = manager.getLaunchIntentForPackage(
1368 cn.getPackageName());
1369 if (intent != null) {
1370 ContentValues values = new ContentValues();
1371 values.put(LauncherSettings.Favorites.INTENT,
1372 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001373 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001374 }
1375 }
1376
1377 if (intent == null) {
1378 // The app is installed but the component is no
1379 // longer available.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001380 FileLog.d(TAG, "Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001381 itemsToRemove.add(id);
1382 continue;
1383 } else {
1384 // no special handling necessary for this item
1385 restoredRows.add(id);
1386 restored = false;
1387 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001388 } else if (restored) {
1389 // Package is not yet available but might be
1390 // installed later.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001391 FileLog.d(TAG, "package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001392
1393 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1394 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001395 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001396 // App restore has started. Update the flag
1397 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1398 ContentValues values = new ContentValues();
1399 values.put(LauncherSettings.Favorites.RESTORED,
1400 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001401 updateItem(id, values);
1402 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1403 // This is a common app. Try to replace this.
1404 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1405 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1406 if (parser.findDefaultApp()) {
1407 // Default app found. Replace it.
1408 intent = parser.parsedIntent;
1409 cn = intent.getComponent();
1410 ContentValues values = parser.parsedValues;
1411 values.put(LauncherSettings.Favorites.RESTORED, 0);
1412 updateItem(id, values);
1413 restored = false;
1414 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001415
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001416 } else {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001417 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001418 itemsToRemove.add(id);
1419 continue;
1420 }
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001421 } else {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001422 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001423 itemsToRemove.add(id);
1424 continue;
1425 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001426 } else if (PackageManagerHelper.isAppOnSdcard(
1427 manager, cn.getPackageName())) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07001428 // Package is present but not available.
1429 allowMissingTarget = true;
1430 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1431 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001432 // SdCard is not ready yet. Package might get available,
1433 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001434 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001435 pendingPackages.addToList(user, cn.getPackageName());
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001436 allowMissingTarget = true;
1437 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001438
1439 } else {
1440 // Do not wait for external media load anymore.
1441 // Log the invalid package, and remove it
Sunny Goyal713edfc2016-05-06 09:58:34 -07001442 FileLog.d(TAG, "Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001443 itemsToRemove.add(id);
1444 continue;
Winson Chungee055712013-07-30 14:46:24 -07001445 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001446 } else if (cn == null) {
1447 // For shortcuts with no component, keep them as they are
1448 restoredRows.add(id);
1449 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001450 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001451 } catch (URISyntaxException e) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001452 FileLog.d(TAG, "Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001453 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001454 continue;
1455 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001456
Sunny Goyal34b65272015-03-11 16:56:52 -07001457 boolean useLowResIcon = container >= 0 &&
1458 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1459
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001460 if (itemReplaced) {
1461 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001462 info = getAppShortcutInfo(intent, user, null,
1463 cursorIconInfo, false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001464 } else {
1465 // Don't replace items for other profiles.
1466 itemsToRemove.add(id);
1467 continue;
1468 }
1469 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001470 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001471 info = getRestoredItemInfo(c, intent,
1472 promiseType, itemType, cursorIconInfo);
Kenny Guyed131872014-04-30 03:02:21 +01001473 intent = getRestoredItemIntent(c, context, intent);
1474 } else {
1475 // Don't restore items for other profiles.
1476 itemsToRemove.add(id);
1477 continue;
1478 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001479 } else if (itemType ==
1480 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001481 info = getAppShortcutInfo(intent, user, c,
1482 cursorIconInfo, allowMissingTarget, useLowResIcon);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001483 } else if (itemType ==
1484 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001485
1486 ShortcutKey key = ShortcutKey.fromIntent(intent, user);
1487 if (unlockedUsers.get(serialNumber)) {
1488 ShortcutInfoCompat pinnedShortcut =
1489 shortcutKeyToPinnedShortcuts.get(key);
1490 if (pinnedShortcut == null) {
1491 // The shortcut is no longer valid.
1492 itemsToRemove.add(id);
1493 continue;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001494 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001495 info = new ShortcutInfo(pinnedShortcut, context);
1496 intent = info.intent;
1497 } else {
1498 // Create a shortcut info in disabled mode for now.
1499 info = new ShortcutInfo();
1500 info.user = user;
1501 info.itemType = itemType;
1502 loadInfoFromCursor(info, c, cursorIconInfo);
1503
1504 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001505 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001506 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001507 info = getShortcutInfo(c, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001508
Sunny Goyald09c3702016-04-06 16:18:20 -07001509 // Shortcuts are only available on the primary profile
1510 if (PackageManagerHelper.isAppSuspended(manager, targetPackage)) {
1511 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1512 }
1513
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001514 // App shortcuts that used to be automatically added to Launcher
1515 // didn't always have the correct intent flags set, so do that
1516 // here
1517 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001518 intent.getCategories() != null &&
1519 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001520 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001521 intent.addFlags(
1522 Intent.FLAG_ACTIVITY_NEW_TASK |
1523 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1524 }
Michael Jurka96879562012-03-22 05:54:33 -07001525 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001526
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001527 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001528 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001529 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001530 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001531 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001532 info.cellX = c.getInt(cellXIndex);
1533 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08001534 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001535 info.spanX = 1;
1536 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01001537 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001538 if (info.promisedIntent != null) {
1539 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
1540 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001541 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001542 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1543 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1544 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001545
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001546 // check & update map of what's occupied
Sunny Goyale9956a72016-09-01 17:24:47 -07001547 if (!checkItemPlacement(occupied, info, sBgDataModel.workspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001548 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001549 break;
1550 }
1551
Sunny Goyal756adbc2015-04-16 15:20:51 -07001552 if (restored) {
1553 ComponentName cn = info.getTargetComponent();
1554 if (cn != null) {
1555 Integer progress = installingPkgs.get(cn.getPackageName());
1556 if (progress != null) {
1557 info.setInstallProgress(progress);
1558 } else {
1559 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
1560 }
1561 }
1562 }
1563
Sunny Goyale9956a72016-09-01 17:24:47 -07001564 sBgDataModel.addItem(info, false);
Winson Chung1323b482013-08-05 12:41:55 -07001565 } else {
1566 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001567 }
1568 break;
1569
1570 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1571 id = c.getLong(idIndex);
Sunny Goyale9956a72016-09-01 17:24:47 -07001572 FolderInfo folderInfo = sBgDataModel.findOrMakeFolder(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001573
Sunny Goyala508e4f2015-05-21 09:33:57 -07001574 // Do not trim the folder label, as is was set by the user.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001575 folderInfo.title = c.getString(cursorIconInfo.titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001576 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001577 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001578 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001579 folderInfo.cellX = c.getInt(cellXIndex);
1580 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001581 folderInfo.spanX = 1;
1582 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001583 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001584
Daniel Sandler8802e962010-05-26 16:28:16 -04001585 // check & update map of what's occupied
Sunny Goyale9956a72016-09-01 17:24:47 -07001586 if (!checkItemPlacement(occupied, folderInfo, sBgDataModel.workspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001587 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001588 break;
1589 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001590 if (restored) {
1591 // no special handling required for restored folders
1592 restoredRows.add(id);
1593 }
1594
Sunny Goyale9956a72016-09-01 17:24:47 -07001595 sBgDataModel.addItem(folderInfo, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001596 break;
1597
1598 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08001599 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001600 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08001601 boolean customWidget = itemType ==
1602 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
1603
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001604 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001605 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001606 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001607 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001608 user = allUsers.get(serialNumber);
1609 if (user == null) {
1610 itemsToRemove.add(id);
1611 continue;
1612 }
1613
Sunny Goyalff572272014-07-23 13:58:07 -07001614 final ComponentName component =
1615 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04001616
Sunny Goyal651077b2014-06-30 14:15:31 -07001617 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07001618 final boolean isIdValid = (restoreStatus &
1619 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07001620 final boolean wasProviderReady = (restoreStatus &
1621 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07001622
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001623 if (widgetProvidersMap == null) {
1624 widgetProvidersMap = AppWidgetManagerCompat
1625 .getInstance(mContext).getAllProvidersMap();
1626 }
1627 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
1628 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00001629 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001630 user));
Sunny Goyalff572272014-07-23 13:58:07 -07001631
1632 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08001633 if (!isSafeMode && !customWidget &&
1634 wasProviderReady && !isProviderReady) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001635 FileLog.d(TAG, "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07001636 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001637 itemsToRemove.add(id);
1638 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07001639 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07001640 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1641 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08001642
Sunny Goyal53f96722015-07-13 19:54:53 -07001643 // The provider is available. So the widget is either
1644 // available or not available. We do not need to track
1645 // any future restore updates.
1646 int status = restoreStatus &
1647 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07001648 if (!wasProviderReady) {
1649 // If provider was not previously ready, update the
1650 // status and UI flag.
1651
1652 // Id would be valid only if the widget restore broadcast was received.
1653 if (isIdValid) {
Sunny Goyal86df1382016-08-10 15:03:22 -07001654 status |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07001655 } else {
1656 status &= ~LauncherAppWidgetInfo
1657 .FLAG_PROVIDER_NOT_READY;
1658 }
1659 }
1660 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07001661 } else {
1662 Log.v(TAG, "Widget restore pending id=" + id
1663 + " appWidgetId=" + appWidgetId
1664 + " status =" + restoreStatus);
1665 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07001666 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07001667 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001668 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07001669
1670 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
1671 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001672 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07001673 // App restore has started. Update the flag
1674 appWidgetInfo.restoreStatus |=
1675 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001676 } else if (!isSafeMode) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001677 FileLog.d(TAG, "Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07001678 itemsToRemove.add(id);
1679 continue;
1680 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07001681
1682 appWidgetInfo.installProgress =
1683 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07001684 }
Sunny Goyal86df1382016-08-10 15:03:22 -07001685 if (appWidgetInfo.hasRestoreFlag(
1686 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG)) {
1687 intentDescription = c.getString(intentIndex);
1688 if (!TextUtils.isEmpty(intentDescription)) {
1689 appWidgetInfo.bindOptions =
1690 Intent.parseUri(intentDescription, 0);
1691 }
1692 }
Sunny Goyalff572272014-07-23 13:58:07 -07001693
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001694 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07001695 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001696 appWidgetInfo.cellX = c.getInt(cellXIndex);
1697 appWidgetInfo.cellY = c.getInt(cellYIndex);
1698 appWidgetInfo.spanX = c.getInt(spanXIndex);
1699 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07001700 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04001701
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001702 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1703 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1704 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001705 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1706 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001707 continue;
1708 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001709
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001710 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001711 // check & update map of what's occupied
Sunny Goyale9956a72016-09-01 17:24:47 -07001712 if (!checkItemPlacement(occupied, appWidgetInfo, sBgDataModel.workspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001713 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001714 break;
1715 }
Sunny Goyal651077b2014-06-30 14:15:31 -07001716
Adam Cohen59400422014-03-05 18:07:04 -08001717 if (!customWidget) {
1718 String providerName =
1719 appWidgetInfo.providerName.flattenToString();
1720 if (!providerName.equals(savedProvider) ||
1721 (appWidgetInfo.restoreStatus != restoreStatus)) {
1722 ContentValues values = new ContentValues();
1723 values.put(
1724 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1725 providerName);
1726 values.put(LauncherSettings.Favorites.RESTORED,
1727 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001728 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08001729 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001730 }
Sunny Goyale9956a72016-09-01 17:24:47 -07001731 sBgDataModel.addItem(appWidgetInfo, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001732 }
Joe Onorato36115782010-06-17 13:28:48 -04001733 break;
1734 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001735 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001736 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001737 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001738 }
1739 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001740 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001741 }
1742
Winson Chungba9c37f2013-08-30 14:11:37 -07001743 // Break early if we've stopped loading
1744 if (mStopped) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001745 sBgDataModel.clear();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001746 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07001747 }
1748
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001749 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001750 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001751 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
1752 Utilities.createDbSelectionQuery(
1753 LauncherSettings.Favorites._ID, itemsToRemove), null);
1754 if (DEBUG_LOADERS) {
1755 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
1756 LauncherSettings.Favorites._ID, itemsToRemove));
1757 }
1758
1759 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07001760 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
1761 .call(contentResolver,
1762 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
1763 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
1764 for (long folderId : deletedFolderIds) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001765 sBgDataModel.workspaceItems.remove(sBgDataModel.folders.get(folderId));
1766 sBgDataModel.folders.remove(folderId);
1767 sBgDataModel.itemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001768 }
1769 }
1770
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001771 // Unpin shortcuts that don't exist on the workspace.
1772 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001773 MutableInt numTimesPinned = sBgDataModel.pinnedShortcutCounts.get(key);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001774 if (numTimesPinned == null || numTimesPinned.value == 0) {
1775 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
1776 mDeepShortcutManager.unpinShortcut(key);
1777 }
1778 }
1779
Sunny Goyal317698b2015-07-29 11:45:41 -07001780 // Sort all the folder items and make sure the first 3 items are high resolution.
Sunny Goyale9956a72016-09-01 17:24:47 -07001781 for (FolderInfo folder : sBgDataModel.folders) {
Sunny Goyal317698b2015-07-29 11:45:41 -07001782 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
1783 int pos = 0;
1784 for (ShortcutInfo info : folder.contents) {
1785 if (info.usingLowResIcon) {
1786 info.updateIcon(mIconCache, false);
1787 }
1788 pos ++;
1789 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
1790 break;
1791 }
1792 }
1793 }
1794
Chris Wrenf4d08112014-01-16 18:13:56 -05001795 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001796 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001797 ContentValues values = new ContentValues();
1798 values.put(LauncherSettings.Favorites.RESTORED, 0);
1799 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
1800 Utilities.createDbSelectionQuery(
1801 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05001802 }
1803
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001804 if (!isSdCardReady && !pendingPackages.isEmpty()) {
1805 context.registerReceiver(
1806 new SdCardAvailableReceiver(
1807 LauncherModel.this, mContext, pendingPackages),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001808 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001809 null,
1810 sWorker);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001811 }
1812
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001813 // Remove any empty screens
Sunny Goyale9956a72016-09-01 17:24:47 -07001814 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgDataModel.workspaceScreens);
1815 for (ItemInfo item: sBgDataModel.itemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001816 long screenId = item.screenId;
1817 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1818 unusedScreens.contains(screenId)) {
1819 unusedScreens.remove(screenId);
1820 }
1821 }
1822
1823 // If there are any empty screens remove them, and update.
1824 if (unusedScreens.size() != 0) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001825 sBgDataModel.workspaceScreens.removeAll(unusedScreens);
1826 updateWorkspaceScreenOrder(context, sBgDataModel.workspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001827 }
1828
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001829 if (DEBUG_LOADERS) {
1830 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1831 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07001832 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07001833 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001834 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07001835
Sunny Goyale2df0622015-04-24 11:27:00 -07001836 for (int i = 0; i < nScreens; i++) {
1837 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07001838 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001839 line += " | ";
1840 }
Joe Onorato36115782010-06-17 13:28:48 -04001841 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001842 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04001843 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001844 }
Joe Onorato36115782010-06-17 13:28:48 -04001845 }
Sunny Goyale26d1002016-06-20 14:52:14 -07001846 if (LauncherAppState.PROFILE_STARTUP) {
1847 Trace.endSection();
1848 }
Adam Cohene25af792013-06-06 23:08:25 -07001849 }
1850
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001851 /**
1852 * Partially updates the item without any notification. Must be called on the worker thread.
1853 */
1854 private void updateItem(long itemId, ContentValues update) {
1855 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001856 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001857 update,
1858 BaseColumns._ID + "= ?",
1859 new String[]{Long.toString(itemId)});
1860 }
1861
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001862 /** Filters the set of items who are directly or indirectly (via another container) on the
1863 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08001864 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001865 ArrayList<ItemInfo> allWorkspaceItems,
1866 ArrayList<ItemInfo> currentScreenItems,
1867 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001868 // Purge any null ItemInfos
1869 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
1870 while (iter.hasNext()) {
1871 ItemInfo i = iter.next();
1872 if (i == null) {
1873 iter.remove();
1874 }
1875 }
1876
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001877 // Order the set of items by their containers first, this allows use to walk through the
1878 // list sequentially, build up a list of containers that are in the specified screen,
1879 // as well as all items in those containers.
1880 Set<Long> itemsOnScreen = new HashSet<Long>();
1881 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
1882 @Override
1883 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07001884 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001885 }
1886 });
1887 for (ItemInfo info : allWorkspaceItems) {
1888 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001889 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001890 currentScreenItems.add(info);
1891 itemsOnScreen.add(info.id);
1892 } else {
1893 otherScreenItems.add(info);
1894 }
1895 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1896 currentScreenItems.add(info);
1897 itemsOnScreen.add(info.id);
1898 } else {
1899 if (itemsOnScreen.contains(info.container)) {
1900 currentScreenItems.add(info);
1901 itemsOnScreen.add(info.id);
1902 } else {
1903 otherScreenItems.add(info);
1904 }
1905 }
1906 }
1907 }
1908
1909 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08001910 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001911 ArrayList<LauncherAppWidgetInfo> appWidgets,
1912 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
1913 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001914
1915 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001916 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001917 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08001918 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001919 currentScreenWidgets.add(widget);
1920 } else {
1921 otherScreenWidgets.add(widget);
1922 }
1923 }
1924 }
1925
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001926 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
1927 * right) */
1928 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07001929 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001930 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07001931 final int screenCols = profile.numColumns;
1932 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001933 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07001934 @Override
1935 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07001936 if (lhs.container == rhs.container) {
1937 // Within containers, order by their spatial position in that container
1938 switch ((int) lhs.container) {
1939 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
1940 long lr = (lhs.screenId * screenCellCount +
1941 lhs.cellY * screenCols + lhs.cellX);
1942 long rr = (rhs.screenId * screenCellCount +
1943 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07001944 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07001945 }
1946 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
1947 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07001948 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07001949 }
1950 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07001951 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07001952 throw new RuntimeException("Unexpected container type when " +
1953 "sorting workspace items.");
1954 }
1955 return 0;
1956 }
1957 } else {
1958 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07001959 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07001960 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001961 }
1962 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001963 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001964
Adam Cohendcd297f2013-06-18 13:13:40 -07001965 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
1966 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001967 final Runnable r = new Runnable() {
1968 @Override
1969 public void run() {
1970 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1971 if (callbacks != null) {
1972 callbacks.bindScreens(orderedScreens);
1973 }
1974 }
1975 };
Sunny Goyald33860f2015-04-23 16:02:20 -07001976 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07001977 }
1978
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001979 private void bindWorkspaceItems(final Callbacks oldCallbacks,
1980 final ArrayList<ItemInfo> workspaceItems,
1981 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07001982 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001983
1984 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07001985 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001986 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04001987 final int start = i;
1988 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001989 final Runnable r = new Runnable() {
1990 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001991 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001992 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001993 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07001994 callbacks.bindItems(workspaceItems, start, start+chunkSize,
1995 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001996 }
1997 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001998 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001999 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002000 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002001
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002002 // Bind the widgets, one at a time
2003 N = appWidgets.size();
2004 for (int i = 0; i < N; i++) {
2005 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2006 final Runnable r = new Runnable() {
2007 public void run() {
2008 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2009 if (callbacks != null) {
2010 callbacks.bindAppWidget(widget);
2011 }
2012 }
2013 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002014 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002015 }
2016 }
2017
2018 /**
2019 * Binds all loaded data to actual views on the main thread.
2020 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002021 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002022 final long t = SystemClock.uptimeMillis();
2023 Runnable r;
2024
2025 // Don't use these two variables in any of the callback runnables.
2026 // Otherwise we hold a reference to them.
2027 final Callbacks oldCallbacks = mCallbacks.get();
2028 if (oldCallbacks == null) {
2029 // This launcher has exited and nobody bothered to tell us. Just bail.
2030 Log.w(TAG, "LoaderTask running with no launcher");
2031 return;
2032 }
2033
Winson Chung9b9fb962013-11-15 15:39:34 -08002034 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07002035 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
2036 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
2037 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002038
Sunny Goyale9956a72016-09-01 17:24:47 -07002039 synchronized (sBgDataModel) {
2040 workspaceItems.addAll(sBgDataModel.workspaceItems);
2041 appWidgets.addAll(sBgDataModel.appWidgets);
2042 orderedScreenIds.addAll(sBgDataModel.workspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002043 }
2044
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002045 final int currentScreen;
2046 {
2047 int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE
2048 ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen();
2049 if (currScreen >= orderedScreenIds.size()) {
2050 // There may be no workspace screens (just hotseat items and an empty page).
2051 currScreen = PagedView.INVALID_RESTORE_PAGE;
2052 }
2053 currentScreen = currScreen;
Winson Chung9b9fb962013-11-15 15:39:34 -08002054 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002055 final boolean validFirstPage = currentScreen >= 0;
2056 final long currentScreenId =
2057 validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID;
Winson Chung9b9fb962013-11-15 15:39:34 -08002058
Winson Chung9b9fb962013-11-15 15:39:34 -08002059 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07002060 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
2061 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
2062 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
2063 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002064
Winson Chung9b9fb962013-11-15 15:39:34 -08002065 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002066 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002067 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002068 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002069 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2070 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2071
2072 // Tell the workspace that we're about to start binding items
2073 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002074 public void run() {
2075 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2076 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002077 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002078 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002079 }
2080 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002081 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002082 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002083
Adam Cohendcd297f2013-06-18 13:13:40 -07002084 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2085
Sunny Goyal527c7d32015-08-28 15:19:36 -07002086 Executor mainExecutor = new DeferredMainThreadExecutor();
2087 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07002088 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002089
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002090 // In case of validFirstPage, only bind the first screen, and defer binding the
2091 // remaining screens after first onDraw (and an optional the fade animation whichever
2092 // happens later).
2093 // This ensures that the first screen is immediately visible (eg. during rotation)
2094 // In case of !validFirstPage, bind all pages one after other.
2095 final Executor deferredExecutor =
2096 validFirstPage ? new ViewOnDrawExecutor(mHandler) : mainExecutor;
2097
2098 mainExecutor.execute(new Runnable() {
2099 @Override
2100 public void run() {
2101 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2102 if (callbacks != null) {
2103 callbacks.finishFirstPageBind(
2104 validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null);
2105 }
2106 }
2107 });
Sunny Goyal527c7d32015-08-28 15:19:36 -07002108
Sunny Goyal44c06432016-04-02 10:56:02 -07002109 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002110
2111 // Tell the workspace that we're done binding items
2112 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002113 public void run() {
2114 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2115 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002116 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002117 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002118
Sunny Goyal639e9062015-08-19 19:17:06 -07002119 mIsLoadingAndBindingWorkspace = false;
2120
2121 // Run all the bind complete runnables after workspace is bound.
2122 if (!mBindCompleteRunnables.isEmpty()) {
2123 synchronized (mBindCompleteRunnables) {
2124 for (final Runnable r : mBindCompleteRunnables) {
2125 runOnWorkerThread(r);
2126 }
2127 mBindCompleteRunnables.clear();
2128 }
2129 }
2130
Winson Chung98e030b2012-05-07 16:01:11 -07002131 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002132 if (DEBUG_LOADERS) {
2133 Log.d(TAG, "bound workspace in "
2134 + (SystemClock.uptimeMillis()-t) + "ms");
2135 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002136
Joe Onorato36115782010-06-17 13:28:48 -04002137 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002138 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002139 deferredExecutor.execute(r);
2140
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002141 if (validFirstPage) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002142 r = new Runnable() {
2143 public void run() {
2144 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2145 if (callbacks != null) {
2146 // We are loading synchronously, which means, some of the pages will be
2147 // bound after first draw. Inform the callbacks that page binding is
2148 // not complete, and schedule the remaining pages.
2149 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2150 callbacks.onPageBoundSynchronously(currentScreen);
2151 }
2152 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2153 }
2154 }
2155 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002156 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002157 }
Joe Onorato36115782010-06-17 13:28:48 -04002158 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002159
Joe Onorato36115782010-06-17 13:28:48 -04002160 private void loadAndBindAllApps() {
2161 if (DEBUG_LOADERS) {
2162 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2163 }
2164 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002165 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002166 synchronized (LoaderTask.this) {
2167 if (mStopped) {
2168 return;
2169 }
2170 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002171 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002172 synchronized (LoaderTask.this) {
2173 if (mStopped) {
2174 return;
2175 }
2176 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002177 }
Joe Onorato36115782010-06-17 13:28:48 -04002178 } else {
2179 onlyBindAllApps();
2180 }
2181 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002182
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002183 private void updateIconCache() {
2184 // Ignore packages which have a promise icon.
2185 HashSet<String> packagesToIgnore = new HashSet<>();
Sunny Goyale9956a72016-09-01 17:24:47 -07002186 synchronized (sBgDataModel) {
2187 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002188 if (info instanceof ShortcutInfo) {
2189 ShortcutInfo si = (ShortcutInfo) info;
2190 if (si.isPromise() && si.getTargetComponent() != null) {
2191 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2192 }
2193 } else if (info instanceof LauncherAppWidgetInfo) {
2194 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2195 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2196 packagesToIgnore.add(lawi.providerName.getPackageName());
2197 }
2198 }
2199 }
2200 }
2201 mIconCache.updateDbIcons(packagesToIgnore);
2202 }
2203
Joe Onorato36115782010-06-17 13:28:48 -04002204 private void onlyBindAllApps() {
2205 final Callbacks oldCallbacks = mCallbacks.get();
2206 if (oldCallbacks == null) {
2207 // This launcher has exited and nobody bothered to tell us. Just bail.
2208 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2209 return;
2210 }
2211
2212 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002213 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002214 final ArrayList<AppInfo> list
2215 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002216 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002217 public void run() {
2218 final long t = SystemClock.uptimeMillis();
2219 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2220 if (callbacks != null) {
2221 callbacks.bindAllApplications(list);
2222 }
2223 if (DEBUG_LOADERS) {
2224 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002225 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04002226 }
2227 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002228 };
Tony Wickham80f57872016-06-29 18:12:15 -07002229 runOnMainThread(r);
Joe Onorato36115782010-06-17 13:28:48 -04002230 }
2231
Winson Chung64359a52013-07-08 17:17:08 -07002232 private void loadAllApps() {
2233 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002234
Joe Onorato36115782010-06-17 13:28:48 -04002235 final Callbacks oldCallbacks = mCallbacks.get();
2236 if (oldCallbacks == null) {
2237 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002238 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002239 return;
2240 }
2241
Kenny Guyed131872014-04-30 03:02:21 +01002242 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2243
Winson Chung64359a52013-07-08 17:17:08 -07002244 // Clear the list of apps
2245 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002246 for (UserHandleCompat user : profiles) {
2247 // Query for the set of apps
2248 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002249 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002250 if (DEBUG_LOADERS) {
2251 Log.d(TAG, "getActivityList took "
2252 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2253 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2254 }
2255 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002256 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002257 if (apps == null || apps.isEmpty()) {
2258 return;
2259 }
Kenny Guyff05f432016-01-22 17:48:29 +00002260 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002261 // Create the ApplicationInfos
2262 for (int i = 0; i < apps.size(); i++) {
2263 LauncherActivityInfoCompat app = apps.get(i);
2264 // This builds the icon bitmaps.
Kenny Guyff05f432016-01-22 17:48:29 +00002265 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode));
Kenny Guyed131872014-04-30 03:02:21 +01002266 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002267
Sunny Goyal756a28a2015-04-23 17:07:55 -07002268 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2269 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002270 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002271
2272 @Override
2273 public void run() {
2274 heuristic.processUserApps(apps);
2275 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002276 };
2277 runOnMainThread(new Runnable() {
2278
2279 @Override
2280 public void run() {
2281 // Check isLoadingWorkspace on the UI thread, as it is updated on
2282 // the UI thread.
2283 if (mIsLoadingAndBindingWorkspace) {
2284 synchronized (mBindCompleteRunnables) {
2285 mBindCompleteRunnables.add(r);
2286 }
2287 } else {
2288 runOnWorkerThread(r);
2289 }
2290 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002291 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002292 }
Winson Chung64359a52013-07-08 17:17:08 -07002293 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002294 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002295 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2296 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002297
2298 // Post callback on main thread
2299 mHandler.post(new Runnable() {
2300 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002301
Winson Chung64359a52013-07-08 17:17:08 -07002302 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002303 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002304 if (callbacks != null) {
2305 callbacks.bindAllApplications(added);
2306 if (DEBUG_LOADERS) {
2307 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002308 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07002309 }
2310 } else {
2311 Log.i(TAG, "not binding apps: no Launcher activity");
2312 }
2313 }
2314 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002315 // Cleanup any data stored for a deleted user.
2316 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04002317 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002318 Log.d(TAG, "Icons processed in "
2319 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002320 }
2321 }
2322
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002323 private void loadAndBindDeepShortcuts() {
2324 if (DEBUG_LOADERS) {
2325 Log.d(TAG, "loadAndBindDeepShortcuts mDeepShortcutsLoaded=" + mDeepShortcutsLoaded);
2326 }
2327 if (!mDeepShortcutsLoaded) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07002328 sBgDataModel.deepShortcutMap.clear();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002329 mHasShortcutHostPermission = mDeepShortcutManager.hasHostPermission();
2330 if (mHasShortcutHostPermission) {
2331 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
2332 if (mUserManager.isUserUnlocked(user)) {
2333 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager
2334 .queryForAllShortcuts(user);
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07002335 sBgDataModel.updateDeepShortcutMap(null, user, shortcuts);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002336 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002337 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002338 }
2339 synchronized (LoaderTask.this) {
2340 if (mStopped) {
2341 return;
2342 }
2343 mDeepShortcutsLoaded = true;
2344 }
2345 }
Tony Wickham80f57872016-06-29 18:12:15 -07002346 bindDeepShortcuts();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002347 }
2348
Joe Onoratobe386092009-11-17 17:32:16 -08002349 public void dumpState() {
Sunny Goyale9956a72016-09-01 17:24:47 -07002350 synchronized (sBgDataModel) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002351 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002352 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2353 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
Sunny Goyale9956a72016-09-01 17:24:47 -07002354 Log.d(TAG, "mItems size=" + sBgDataModel.workspaceItems.size());
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002355 }
Joe Onorato36115782010-06-17 13:28:48 -04002356 }
2357 }
2358
Tony Wickham80f57872016-06-29 18:12:15 -07002359 public void bindDeepShortcuts() {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07002360 final MultiHashMap<ComponentKey, String> shortcutMapCopy =
2361 sBgDataModel.deepShortcutMap.clone();
Tony Wickham80f57872016-06-29 18:12:15 -07002362 Runnable r = new Runnable() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002363 @Override
2364 public void run() {
2365 Callbacks callbacks = getCallback();
2366 if (callbacks != null) {
2367 callbacks.bindDeepShortcutMap(shortcutMapCopy);
2368 }
2369 }
Tony Wickham80f57872016-06-29 18:12:15 -07002370 };
2371 runOnMainThread(r);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002372 }
2373
Sunny Goyal75b0f552015-05-20 21:57:06 -07002374 /**
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002375 * Refreshes the cached shortcuts if the shortcut permission has changed.
2376 * Current implementation simply reloads the workspace, but it can be optimized to
2377 * use partial updates similar to {@link UserManagerCompat}
2378 */
2379 public void refreshShortcutsIfRequired() {
Sunny Goyalf5e37442016-11-02 10:31:24 -07002380 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002381 sWorker.removeCallbacks(mShortcutPermissionCheckRunnable);
2382 sWorker.post(mShortcutPermissionCheckRunnable);
2383 }
2384 }
2385
2386 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -07002387 * Called when the icons for packages have been updated in the icon cache.
2388 */
2389 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
Sunny Goyal75b0f552015-05-20 21:57:06 -07002390 // If any package icon has changed (app was updated while launcher was dead),
2391 // update the corresponding shortcuts.
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002392 enqueueModelUpdateTask(new CacheDataUpdatedTask(
2393 CacheDataUpdatedTask.OP_CACHE_UPDATE, user, updatedPackages));
Sunny Goyal75b0f552015-05-20 21:57:06 -07002394 }
2395
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002396 void enqueueModelUpdateTask(BaseModelUpdateTask task) {
2397 task.init(this);
2398 runOnWorkerThread(task);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002399 }
2400
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002401 /**
2402 * A task to be executed on the current callbacks on the UI thread.
2403 * If there is no current callbacks, the task is ignored.
2404 */
2405 public interface CallbackTask {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002406
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002407 void execute(Callbacks callbacks);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002408 }
2409
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002410 /**
2411 * A runnable which changes/updates the data model of the launcher based on certain events.
2412 */
2413 public static abstract class BaseModelUpdateTask implements Runnable {
Joe Onorato36115782010-06-17 13:28:48 -04002414
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002415 private LauncherModel mModel;
2416 private DeferredHandler mUiHandler;
Joe Onorato36115782010-06-17 13:28:48 -04002417
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002418 /* package private */
2419 void init(LauncherModel model) {
2420 mModel = model;
2421 mUiHandler = mModel.mHandler;
Joe Onorato36115782010-06-17 13:28:48 -04002422 }
2423
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002424 @Override
Joe Onorato36115782010-06-17 13:28:48 -04002425 public void run() {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002426 if (!mModel.mHasLoaderCompletedOnce) {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002427 // Loader has not yet run.
2428 return;
2429 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002430 execute(mModel.mApp, sBgDataModel, mModel.mBgAllAppsList);
2431 }
Joe Onorato36115782010-06-17 13:28:48 -04002432
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002433 /**
2434 * Execute the actual task. Called on the worker thread.
2435 */
2436 public abstract void execute(
2437 LauncherAppState app, BgDataModel dataModel, AllAppsList apps);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002438
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002439 /**
2440 * Schedules a {@param task} to be executed on the current callbacks.
2441 */
2442 public final void scheduleCallbackTask(final CallbackTask task) {
2443 final Callbacks callbacks = mModel.getCallback();
2444 mUiHandler.post(new Runnable() {
2445 public void run() {
2446 Callbacks cb = mModel.getCallback();
2447 if (callbacks == cb && cb != null) {
2448 task.execute(callbacks);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002449 }
2450 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002451 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002452 }
2453 }
2454
Sunny Goyal10923b32016-07-20 15:42:44 -07002455 /**
2456 * Repopulates the shortcut info, possibly updating any icon already on the workspace.
2457 */
2458 public void updateShortcutInfo(final ShortcutInfoCompat fullDetail, final ShortcutInfo info) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002459 enqueueModelUpdateTask(new ExtendedModelTask() {
Sunny Goyal10923b32016-07-20 15:42:44 -07002460 @Override
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002461 public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
2462 info.updateFromDeepShortcutInfo(fullDetail, app.getContext());
2463
2464 ArrayList<ShortcutInfo> update = new ArrayList<>();
Sunny Goyal10923b32016-07-20 15:42:44 -07002465 update.add(info);
2466 bindUpdatedShortcuts(update, fullDetail.getUserHandle());
2467 }
2468 });
2469 }
2470
Sunny Goyald164b7f2016-10-12 20:49:31 -07002471 private void bindWidgetsModel(final Callbacks callbacks) {
2472 final MultiHashMap<PackageItemInfo, WidgetItem> widgets
2473 = mBgWidgetsModel.getWidgetsMap().clone();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002474 mHandler.post(new Runnable() {
2475 @Override
2476 public void run() {
2477 Callbacks cb = getCallback();
2478 if (callbacks == cb && cb != null) {
Sunny Goyald164b7f2016-10-12 20:49:31 -07002479 callbacks.bindAllWidgets(widgets);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002480 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07002481 }
2482 });
2483 }
2484
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002485 public void refreshAndBindWidgetsAndShortcuts(
2486 final Callbacks callbacks, final boolean bindFirst) {
2487 runOnWorkerThread(new Runnable() {
2488 @Override
2489 public void run() {
2490 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
Sunny Goyald164b7f2016-10-12 20:49:31 -07002491 bindWidgetsModel(callbacks);
Sunny Goyal31860582015-09-18 08:38:57 -07002492 }
Sunny Goyald164b7f2016-10-12 20:49:31 -07002493 ArrayList<WidgetItem> allWidgets = mBgWidgetsModel.update(mApp.getContext());
2494 bindWidgetsModel(callbacks);
2495
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002496 // update the Widget entries inside DB on the worker thread.
Sunny Goyald164b7f2016-10-12 20:49:31 -07002497 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(allWidgets);
Sunny Goyal31860582015-09-18 08:38:57 -07002498 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002499 });
Michael Jurkac402cd92013-05-20 15:49:32 +02002500 }
2501
Joe Onorato9c1289c2009-08-17 11:03:03 -04002502 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05002503 * Make an ShortcutInfo object for a restored application or shortcut item that points
2504 * to a package that is not yet installed on the system.
2505 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002506 public ShortcutInfo getRestoredItemInfo(Cursor c, Intent intent,
2507 int promiseType, int itemType, CursorIconInfo iconInfo) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002508 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01002509 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002510
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002511 Bitmap icon = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002512 // the fallback icon
2513 if (icon == null) {
2514 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
2515 } else {
2516 info.setIcon(icon);
2517 }
Sunny Goyal34942622014-08-29 17:20:55 -07002518
2519 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002520 String title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07002521 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07002522 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07002523 }
Sunny Goyal34942622014-08-29 17:20:55 -07002524 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
2525 if (TextUtils.isEmpty(info.title)) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002526 info.title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07002527 }
Sunny Goyal34942622014-08-29 17:20:55 -07002528 } else {
2529 throw new InvalidParameterException("Invalid restoreType " + promiseType);
2530 }
2531
Winson Chung82b016c2015-05-08 17:00:10 -07002532 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07002533 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07002534 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002535 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05002536 return info;
2537 }
2538
2539 /**
2540 * Make an Intent object for a restored application or shortcut item that points
2541 * to the market page for the item.
2542 */
Adam Cohen091440a2015-03-18 14:16:05 -07002543 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002544 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002545 return getMarketIntent(componentName.getPackageName());
2546 }
2547
2548 static Intent getMarketIntent(String packageName) {
2549 return new Intent(Intent.ACTION_VIEW)
2550 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05002551 .scheme("market")
2552 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002553 .appendQueryParameter("id", packageName)
2554 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05002555 }
2556
2557 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002558 * Make an ShortcutInfo object for a shortcut that is an application.
2559 *
2560 * If c is not null, then it will be used to fill in missing data like the title and icon.
2561 */
Sunny Goyald09c3702016-04-06 16:18:20 -07002562 public ShortcutInfo getAppShortcutInfo(Intent intent,
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002563 UserHandleCompat user, Cursor c, CursorIconInfo iconInfo,
Sunny Goyal34b65272015-03-11 16:56:52 -07002564 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01002565 if (user == null) {
2566 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002567 return null;
2568 }
2569
Kenny Guyed131872014-04-30 03:02:21 +01002570 ComponentName componentName = intent.getComponent();
2571 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08002572 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01002573 return null;
2574 }
2575
2576 Intent newIntent = new Intent(intent.getAction(), null);
2577 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2578 newIntent.setComponent(componentName);
2579 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002580 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01002581 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
2582 return null;
2583 }
2584
2585 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07002586 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002587 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002588 Bitmap icon = iconInfo.loadIcon(c);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002589 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01002590 }
2591
Sunny Goyald09c3702016-04-06 16:18:20 -07002592 if (lai != null && PackageManagerHelper.isAppSuspended(lai.getApplicationInfo())) {
2593 info.isDisabled = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
2594 }
2595
Joe Onorato56d82912010-03-07 14:32:10 -05002596 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002597 if (TextUtils.isEmpty(info.title) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002598 info.title = iconInfo.getTitle(c);
Joe Onorato56d82912010-03-07 14:32:10 -05002599 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002600
Joe Onorato56d82912010-03-07 14:32:10 -05002601 // fall back to the class name of the activity
2602 if (info.title == null) {
2603 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002604 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002605
Joe Onorato9c1289c2009-08-17 11:03:03 -04002606 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01002607 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07002608 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002609 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002610 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07002611
Sunny Goyal1a745e82014-10-02 15:58:31 -07002612 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08002613 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002614 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002615 @Thunk ShortcutInfo getShortcutInfo(Cursor c, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002616 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01002617 // Non-app shortcuts are only supported for current user.
2618 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002619 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002620
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002621 // TODO: If there's an explicit component and we can't install that, delete it.
2622
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002623 loadInfoFromCursor(info, c, iconInfo);
2624 return info;
2625 }
Joe Onorato56d82912010-03-07 14:32:10 -05002626
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002627 /**
2628 * Make an ShortcutInfo object for a shortcut that isn't an application.
2629 */
2630 public void loadInfoFromCursor(ShortcutInfo info, Cursor c, CursorIconInfo iconInfo) {
2631 info.title = iconInfo.getTitle(c);
2632 Bitmap icon = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002633 // the fallback icon
2634 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01002635 icon = mIconCache.getDefaultIcon(info.user);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002636 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08002637 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002638 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002639
Sunny Goyal2350bc92014-10-14 16:42:54 -07002640 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08002641 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
2642 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
2643 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
2644
Adam Cohend9198822011-11-22 16:42:47 -08002645 if (intent == null) {
2646 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
2647 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
2648 return null;
2649 }
2650
Joe Onorato0589f0f2010-02-08 13:44:00 -08002651 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08002652 ShortcutIconResource iconResource = null;
2653
Sunny Goyal2fce90c2014-10-07 12:01:58 -07002654 if (bitmap instanceof Bitmap) {
Sunny Goyal10629b02016-09-01 12:50:11 -07002655 icon = LauncherIcons.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002656 } else {
2657 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002658 if (extra instanceof ShortcutIconResource) {
2659 iconResource = (ShortcutIconResource) extra;
Sunny Goyal10629b02016-09-01 12:50:11 -07002660 icon = LauncherIcons.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07002661 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002662 }
2663 }
2664
Michael Jurkac9d95c52011-08-29 14:03:34 -07002665 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05002666
Kenny Guyed131872014-04-30 03:02:21 +01002667 // Only support intents for current user for now. Intents sent from other
2668 // users wouldn't get here without intent forwarding anyway.
2669 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05002670 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07002671 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05002672 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08002673 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05002674
Winson Chung82b016c2015-05-08 17:00:10 -07002675 info.title = Utilities.trim(name);
2676 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002677 info.intent = intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -08002678 info.iconResource = iconResource;
2679
2680 return info;
2681 }
2682
Sunny Goyal651077b2014-06-30 14:15:31 -07002683 static boolean isValidProvider(AppWidgetProviderInfo provider) {
2684 return (provider != null) && (provider.provider != null)
2685 && (provider.provider.getPackageName() != null);
2686 }
2687
Joe Onoratobe386092009-11-17 17:32:16 -08002688 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08002689 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002690 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
2691 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
2692 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
2693 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04002694 if (mLoaderTask != null) {
2695 mLoaderTask.dumpState();
2696 } else {
2697 Log.d(TAG, "mLoaderTask=null");
2698 }
Joe Onoratobe386092009-11-17 17:32:16 -08002699 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002700
2701 public Callbacks getCallback() {
2702 return mCallbacks != null ? mCallbacks.get() : null;
2703 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002704
2705 /**
2706 * @return {@link FolderInfo} if its already loaded.
2707 */
2708 public FolderInfo findFolderById(Long folderId) {
Sunny Goyale9956a72016-09-01 17:24:47 -07002709 synchronized (sBgDataModel) {
2710 return sBgDataModel.folders.get(folderId);
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002711 }
2712 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002713
Sunny Goyal527c7d32015-08-28 15:19:36 -07002714 @Thunk class DeferredMainThreadExecutor implements Executor {
2715
2716 @Override
2717 public void execute(Runnable command) {
2718 runOnMainThread(command);
2719 }
2720 }
2721
Sunny Goyal756adbc2015-04-16 15:20:51 -07002722 /**
2723 * @return the looper for the worker thread which can be used to start background tasks.
2724 */
2725 public static Looper getWorkerLooper() {
2726 return sWorkerThread.getLooper();
2727 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002728}