blob: c70a47595d3f9e5dd19430dcef8c66d22372ae64 [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 Goyal4e5cc642015-06-25 16:37:44 -070079import com.android.launcher3.util.CursorIconInfo;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070080import com.android.launcher3.util.GridOccupancy;
Sunny Goyal40452cf2016-09-01 15:17:46 -070081import com.android.launcher3.util.ItemInfoMatcher;
Sunny Goyale2df0622015-04-24 11:27:00 -070082import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070083import com.android.launcher3.util.ManagedProfileHeuristic;
Tony Wickhamd82a39d2016-07-01 15:44:13 -070084import com.android.launcher3.util.MultiHashMap;
Sunny Goyald09c3702016-04-06 16:18:20 -070085import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070086import com.android.launcher3.util.Preconditions;
Adam Cohen091440a2015-03-18 14:16:05 -070087import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070088import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080089
Michael Jurkac2f801e2011-07-12 14:19:46 -070090import java.lang.ref.WeakReference;
91import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070092import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070093import java.util.ArrayList;
94import java.util.Collections;
95import java.util.Comparator;
96import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070097import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070098import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070099import java.util.List;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700100import java.util.Map;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700101import java.util.Set;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700102import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -0700103
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800104/**
105 * Maintains in-memory state of the Launcher. It is expected that there should be only one
106 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700107 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108 */
Kenny Guyed131872014-04-30 03:02:21 +0100109public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +0100110 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800111 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -0400112 private static final boolean DEBUG_RECEIVER = false;
Chris Wrenb358f812014-04-16 13:37:00 -0400113
Joe Onorato9c1289c2009-08-17 11:03:03 -0400114 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700115
Joe Onorato36115782010-06-17 13:28:48 -0400116 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500117 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800118
Adam Cohen091440a2015-03-18 14:16:05 -0700119 @Thunk final LauncherAppState mApp;
120 @Thunk final Object mLock = new Object();
121 @Thunk DeferredHandler mHandler = new DeferredHandler();
122 @Thunk LoaderTask mLoaderTask;
123 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700124 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
Adam Cohen091440a2015-03-18 14:16:05 -0700126 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700127 static {
128 sWorkerThread.start();
129 }
Adam Cohen091440a2015-03-18 14:16:05 -0700130 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700131
Joe Onoratocc67f472010-06-08 10:54:30 -0700132 // We start off with everything not loaded. After that, we assume that
133 // our monitoring of the package manager provides all updates and we never
134 // need to do a requery. These are only ever touched from the loader thread.
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700135 private boolean mWorkspaceLoaded;
136 private boolean mAllAppsLoaded;
137 private boolean mDeepShortcutsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700138
Sunny Goyal756a28a2015-04-23 17:07:55 -0700139 /**
140 * Set of runnables to be called on the background thread after the workspace binding
141 * is complete.
142 */
143 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
144
Adam Cohen091440a2015-03-18 14:16:05 -0700145 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700147 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800148 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700149 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800150 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800151
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700152 private boolean mHasShortcutHostPermission;
153 // Runnable to check if the shortcuts permission has changed.
154 private final Runnable mShortcutPermissionCheckRunnable = new Runnable() {
155 @Override
156 public void run() {
157 if (mDeepShortcutsLoaded) {
158 boolean hasShortcutHostPermission = mDeepShortcutManager.hasHostPermission();
159 if (hasShortcutHostPermission != mHasShortcutHostPermission) {
160 mApp.reloadWorkspace();
161 }
162 }
163 }
164 };
165
Sunny Goyale9956a72016-09-01 17:24:47 -0700166 /**
167 * All the static data should be accessed on the background thread, A lock should be acquired
168 * on this object when accessing any data from this model.
169 */
170 static final BgDataModel sBgDataModel = new BgDataModel();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700171
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700172 // </ only access in worker thread >
173
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700174 private final IconCache mIconCache;
175 private final DeepShortcutManager mDeepShortcutManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700177 private final LauncherAppsCompat mLauncherApps;
178 private final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100179
Joe Onorato9c1289c2009-08-17 11:03:03 -0400180 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700181 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400182 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700183 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400184 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700185 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
186 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700187 public void bindScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700188 public void finishFirstPageBind(ViewOnDrawExecutor executor);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800189 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400190 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200191 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700192 public void bindAppsAdded(ArrayList<Long> newScreens,
193 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700194 ArrayList<ItemInfo> addAnimated,
195 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200196 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700197 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
198 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
199 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700200 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700201 public void bindWorkspaceComponentsRemoved(
202 HashSet<String> packageNames, HashSet<ComponentName> components,
203 UserHandleCompat user);
204 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800205 public void notifyWidgetProvidersChanged();
Sunny Goyald164b7f2016-10-12 20:49:31 -0700206 public void bindAllWidgets(MultiHashMap<PackageItemInfo, WidgetItem> widgets);
Adam Cohen1462de32012-07-24 22:34:36 -0700207 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700208 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700209 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMap);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400210 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700212 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter,
213 DeepShortcutManager deepShortcutManager) {
Winson Chunga6945242014-01-08 14:04:34 -0800214 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400215 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100216 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Sunny Goyald164b7f2016-10-12 20:49:31 -0700217 mBgWidgetsModel = new WidgetsModel(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800218 mIconCache = iconCache;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700219 mDeepShortcutManager = deepShortcutManager;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800220
Kenny Guyed131872014-04-30 03:02:21 +0100221 mLauncherApps = LauncherAppsCompat.getInstance(context);
222 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800223 }
224
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700225 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
226 * posted on the main thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700227 private void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700228 if (sWorkerThread.getThreadId() == Process.myTid()) {
229 // If we are on the worker thread, post onto the main handler
230 mHandler.post(r);
231 } else {
232 r.run();
233 }
234 }
235
236 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
237 * posted on the worker thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700238 private static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700239 if (sWorkerThread.getThreadId() == Process.myTid()) {
240 r.run();
241 } else {
242 // If we are not on the worker thread, then post to the worker handler
243 sWorker.post(r);
244 }
245 }
246
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700247 public void setPackageState(PackageInstallInfo installInfo) {
248 enqueueModelUpdateTask(new PackageInstallStateChangedTask(installInfo));
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500249 }
250
Sunny Goyal756adbc2015-04-16 15:20:51 -0700251 /**
252 * Updates the icons and label of all pending icons for the provided package name.
253 */
254 public void updateSessionDisplayInfo(final String packageName) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700255 HashSet<String> packages = new HashSet<>();
256 packages.add(packageName);
257 enqueueModelUpdateTask(new CacheDataUpdatedTask(
258 CacheDataUpdatedTask.OP_SESSION_UPDATE, UserHandleCompat.myUserHandle(), packages));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800259 }
260
261 /**
262 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800263 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700264 public void addAndBindAddedWorkspaceItems(
Sunny Goyala214a632015-05-06 12:23:34 -0700265 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700266 enqueueModelUpdateTask(new AddWorkspaceItemsTask(workspaceApps));
Winson Chung64359a52013-07-08 17:17:08 -0700267 }
268
Joe Onorato9c1289c2009-08-17 11:03:03 -0400269 /**
270 * Adds an item to the DB if it was not created previously, or move it to a new
271 * <container, screen, cellX, cellY>
272 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800273 public static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700274 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400275 if (item.container == ItemInfo.NO_ID) {
276 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700277 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400278 } else {
279 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700280 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281 }
282 }
283
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700284 static void checkItemInfoLocked(
285 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
Sunny Goyale9956a72016-09-01 17:24:47 -0700286 ItemInfo modelItem = sBgDataModel.itemsIdMap.get(itemId);
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700287 if (modelItem != null && item != modelItem) {
288 // check all the data is consistent
289 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
290 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
291 ShortcutInfo shortcut = (ShortcutInfo) item;
292 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
293 modelShortcut.intent.filterEquals(shortcut.intent) &&
294 modelShortcut.id == shortcut.id &&
295 modelShortcut.itemType == shortcut.itemType &&
296 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700297 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700298 modelShortcut.cellX == shortcut.cellX &&
299 modelShortcut.cellY == shortcut.cellY &&
300 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700301 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700302 // For all intents and purposes, this is the same object
303 return;
304 }
305 }
306
307 // the modelItem needs to match up perfectly with item if our model is
308 // to be consistent with the database-- for now, just require
309 // modelItem == item or the equality check above
310 String msg = "item: " + ((item != null) ? item.toString() : "null") +
311 "modelItem: " +
312 ((modelItem != null) ? modelItem.toString() : "null") +
313 "Error: ItemInfo passed to checkItemInfo doesn't match original";
314 RuntimeException e = new RuntimeException(msg);
315 if (stackTrace != null) {
316 e.setStackTrace(stackTrace);
317 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800318 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700319 }
320 }
321
Michael Jurka816474f2012-06-25 14:49:02 -0700322 static void checkItemInfo(final ItemInfo item) {
323 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
324 final long itemId = item.id;
325 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700326 public void run() {
Sunny Goyale9956a72016-09-01 17:24:47 -0700327 synchronized (sBgDataModel) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700328 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700329 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700330 }
331 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700332 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700333 }
334
Michael Jurkac9d95c52011-08-29 14:03:34 -0700335 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
336 final ItemInfo item, final String callingFunction) {
337 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700338 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700339 final ContentResolver cr = context.getContentResolver();
340
Adam Cohen487f7dd2012-06-28 18:12:10 -0700341 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700342 Runnable r = new Runnable() {
343 public void run() {
344 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700345 updateItemArrays(item, itemId, stackTrace);
346 }
347 };
348 runOnWorkerThread(r);
349 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700350
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700351 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
352 final ArrayList<ItemInfo> items, final String callingFunction) {
353 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700354
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700355 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
356 Runnable r = new Runnable() {
357 public void run() {
358 ArrayList<ContentProviderOperation> ops =
359 new ArrayList<ContentProviderOperation>();
360 int count = items.size();
361 for (int i = 0; i < count; i++) {
362 ItemInfo item = items.get(i);
363 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700364 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700365 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700366
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700367 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
368 updateItemArrays(item, itemId, stackTrace);
369
370 }
371 try {
372 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
373 } catch (Exception e) {
374 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700375 }
376 }
377 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700378 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700379 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700380
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700381 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
382 // Lock on mBgLock *after* the db operation
Sunny Goyale9956a72016-09-01 17:24:47 -0700383 synchronized (sBgDataModel) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700384 checkItemInfoLocked(itemId, item, stackTrace);
385
386 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
387 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
388 // Item is in a folder, make sure this folder exists
Sunny Goyale9956a72016-09-01 17:24:47 -0700389 if (!sBgDataModel.folders.containsKey(item.container)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700390 // An items container is being set to a that of an item which is not in
391 // the list of Folders.
392 String msg = "item: " + item + " container being set to: " +
393 item.container + ", not in the list of folders";
394 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700395 }
396 }
397
398 // Items are added/removed from the corresponding FolderInfo elsewhere, such
399 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
400 // that are on the desktop, as appropriate
Sunny Goyale9956a72016-09-01 17:24:47 -0700401 ItemInfo modelItem = sBgDataModel.itemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800402 if (modelItem != null &&
403 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
404 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700405 switch (modelItem.itemType) {
406 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
407 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700408 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700409 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyale9956a72016-09-01 17:24:47 -0700410 if (!sBgDataModel.workspaceItems.contains(modelItem)) {
411 sBgDataModel.workspaceItems.add(modelItem);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700412 }
413 break;
414 default:
415 break;
416 }
417 } else {
Sunny Goyale9956a72016-09-01 17:24:47 -0700418 sBgDataModel.workspaceItems.remove(modelItem);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700419 }
420 }
421 }
422
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400424 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700425 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700426 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700427 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400428 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400429 item.cellX = cellX;
430 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700431
Winson Chung3d503fb2011-07-13 17:25:49 -0700432 // We store hotseat items in canonical form which is this orientation invariant position
433 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700434 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700435 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700436 item.screenId = Launcher.getLauncher(context).getHotseat()
437 .getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700438 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700439 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700440 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400441
442 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400443 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700444 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
445 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800446 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700447 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400448
Michael Jurkac9d95c52011-08-29 14:03:34 -0700449 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700450 }
451
452 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700453 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
454 * cellX, cellY have already been updated on the ItemInfos.
455 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800456 public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700457 final long container, final int screen) {
458
459 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
460 int count = items.size();
461
462 for (int i = 0; i < count; i++) {
463 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700464 item.container = container;
465
466 // We store hotseat items in canonical form which is this orientation invariant position
467 // in the hotseat
468 if (context instanceof Launcher && screen < 0 &&
469 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700470 item.screenId = Launcher.getLauncher(context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700471 item.cellY);
472 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700473 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700474 }
475
476 final ContentValues values = new ContentValues();
477 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
478 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
479 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800480 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700481 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700482
483 contentValues.add(values);
484 }
485 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
486 }
487
488 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700489 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800490 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700491 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700492 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700493 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800494 item.cellX = cellX;
495 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700496 item.spanX = spanX;
497 item.spanY = spanY;
498
499 // We store hotseat items in canonical form which is this orientation invariant position
500 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700501 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700502 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700503 item.screenId = Launcher.getLauncher(context).getHotseat()
504 .getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700505 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700506 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700507 }
Adam Cohend4844c32011-02-18 19:25:06 -0800508
Adam Cohend4844c32011-02-18 19:25:06 -0800509 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800510 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700511 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
512 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800513 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700514 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
515 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700516 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800517
Michael Jurka816474f2012-06-25 14:49:02 -0700518 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700519 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700520
521 /**
522 * Update an item to the database in a specified container.
523 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700524 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700525 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100526 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700527 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800528 }
529
Joe Onorato9c1289c2009-08-17 11:03:03 -0400530 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400531 * Add an item to the database in a specified container. Sets the container, screen, cellX and
532 * cellY fields of the item. Also assigns an ID to the item.
533 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700534 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700535 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400536 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400537 item.cellX = cellX;
538 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700539 // We store hotseat items in canonical form which is this orientation invariant position
540 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700541 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700542 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700543 item.screenId = Launcher.getLauncher(context).getHotseat()
544 .getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700545 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700546 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700547 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400548
549 final ContentValues values = new ContentValues();
550 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100551 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400552
Sunny Goyald2497482015-09-22 18:24:19 -0700553 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
554 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
555
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700556 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700557
Jason Monk8e19cf22014-03-20 15:06:57 -0400558 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700559 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700560 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700561 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400562
Sunny Goyale9956a72016-09-01 17:24:47 -0700563 synchronized (sBgDataModel) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400564 checkItemInfoLocked(item.id, item, stackTrace);
Sunny Goyale9956a72016-09-01 17:24:47 -0700565 sBgDataModel.addItem(item, true);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700566 }
567 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700568 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700569 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700570 }
571
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700572 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700573 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -0400574 */
Sunny Goyalfa401a12015-04-10 13:45:42 -0700575 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyal40452cf2016-09-01 15:17:46 -0700576 ArrayList<ItemInfo> items = new ArrayList<>();
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700577 items.add(item);
578 deleteItemsFromDatabase(context, items);
579 }
580
581 /**
Sunny Goyal40452cf2016-09-01 15:17:46 -0700582 * Removes all the items from the database matching {@param matcher}.
583 */
584 public static void deleteItemsFromDatabase(Context context, ItemInfoMatcher matcher) {
Sunny Goyale9956a72016-09-01 17:24:47 -0700585 deleteItemsFromDatabase(context, matcher.filterItemInfos(sBgDataModel.itemsIdMap));
Sunny Goyal40452cf2016-09-01 15:17:46 -0700586 }
587
588 /**
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700589 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700590 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700591 public static void deleteItemsFromDatabase(Context context,
592 final Iterable<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400593 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -0700594 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700595 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700596 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700597 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700598 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700599
Sunny Goyale9956a72016-09-01 17:24:47 -0700600 sBgDataModel.removeItem(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700601 }
602 }
Michael Jurka83df1882011-08-31 20:59:26 -0700603 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700604 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400605 }
606
607 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700608 * Update the order of the workspace screens in the database. The array list contains
609 * a list of screen ids in the order that they should appear.
610 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700611 public static void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -0700612 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700613 final ContentResolver cr = context.getContentResolver();
614 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
615
616 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700617 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700618 while (iter.hasNext()) {
619 long id = iter.next();
620 if (id < 0) {
621 iter.remove();
622 }
623 }
624
625 Runnable r = new Runnable() {
626 @Override
627 public void run() {
Yura085c8532014-02-11 15:15:29 +0000628 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700629 // Clear the table
Yura085c8532014-02-11 15:15:29 +0000630 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -0700631 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700632 for (int i = 0; i < count; i++) {
633 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700634 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700635 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
636 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +0000637 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -0700638 }
Yura085c8532014-02-11 15:15:29 +0000639
640 try {
641 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
642 } catch (Exception ex) {
643 throw new RuntimeException(ex);
644 }
Winson Chung9e6a0a22013-08-27 11:58:12 -0700645
Sunny Goyale9956a72016-09-01 17:24:47 -0700646 synchronized (sBgDataModel) {
647 sBgDataModel.workspaceScreens.clear();
648 sBgDataModel.workspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -0700649 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700650 }
651 };
652 runOnWorkerThread(r);
653 }
654
655 /**
Winsonc0b52fe2015-09-09 16:38:15 -0700656 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -0400657 */
Winsonc0b52fe2015-09-09 16:38:15 -0700658 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400659 final ContentResolver cr = context.getContentResolver();
660
Michael Jurkac9d95c52011-08-29 14:03:34 -0700661 Runnable r = new Runnable() {
662 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700663 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -0700664 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Sunny Goyale9956a72016-09-01 17:24:47 -0700665 sBgDataModel.removeItem(info.contents);
666 info.contents.clear();
667
668 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
669 sBgDataModel.removeItem(info);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700670 }
671 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700672 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400673 }
674
675 /**
676 * Set this as the current Launcher activity object for the loader.
677 */
678 public void initialize(Callbacks callbacks) {
679 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -0700680 Preconditions.assertUIThread();
681 // Remove any queued UI runnables
682 mHandler.cancelAll();
683 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400684 }
685 }
686
Kenny Guyed131872014-04-30 03:02:21 +0100687 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +0100688 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +0100689 int op = PackageUpdatedTask.OP_UPDATE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700690 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100691 }
692
693 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +0100694 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Sunny Goyalc2936bc2016-09-01 15:50:36 -0700695 onPackagesRemoved(user, packageName);
696 }
697
698 public void onPackagesRemoved(UserHandleCompat user, String... packages) {
Kenny Guyed131872014-04-30 03:02:21 +0100699 int op = PackageUpdatedTask.OP_REMOVE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700700 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packages));
Kenny Guyed131872014-04-30 03:02:21 +0100701 }
702
703 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +0100704 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +0100705 int op = PackageUpdatedTask.OP_ADD;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700706 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100707 }
708
709 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +0100710 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +0100711 boolean replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700712 enqueueModelUpdateTask(
713 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100714 }
715
716 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +0100717 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +0100718 boolean replacing) {
719 if (!replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700720 enqueueModelUpdateTask(new PackageUpdatedTask(
721 PackageUpdatedTask.OP_UNAVAILABLE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100722 }
Kenny Guyed131872014-04-30 03:02:21 +0100723 }
724
Kenny Guy44cba692016-01-21 19:50:02 +0000725 @Override
726 public void onPackagesSuspended(String[] packageNames, UserHandleCompat user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700727 enqueueModelUpdateTask(new PackageUpdatedTask(
728 PackageUpdatedTask.OP_SUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000729 }
730
731 @Override
732 public void onPackagesUnsuspended(String[] packageNames, UserHandleCompat user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700733 enqueueModelUpdateTask(new PackageUpdatedTask(
734 PackageUpdatedTask.OP_UNSUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000735 }
736
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700737 @Override
738 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
739 UserHandleCompat user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700740 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, true));
Sunny Goyal50941fb2016-08-04 12:03:52 -0700741 }
742
743 public void updatePinnedShortcuts(String packageName, List<ShortcutInfoCompat> shortcuts,
744 UserHandleCompat user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700745 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, false));
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700746 }
747
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700748 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400749 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
750 * ACTION_PACKAGE_CHANGED.
751 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100752 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400753 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -0400754 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700755
Joe Onorato36115782010-06-17 13:28:48 -0400756 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +0100757 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -0700758 // If we have changed locale we need to clear out the labels in all apps/workspace.
759 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700760 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
761 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -0700762 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -0700763 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700764 } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
765 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
766 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyalda891c12016-03-18 18:29:24 -0700767 UserHandleCompat user = UserHandleCompat.fromIntent(intent);
768 if (user != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700769 if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
770 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700771 enqueueModelUpdateTask(new PackageUpdatedTask(
772 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE, user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700773 }
774
775 // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so
776 // we need to run the state change task again.
777 if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
778 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700779 enqueueModelUpdateTask(new UserLockStateChangedTask(user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700780 }
Sunny Goyalda891c12016-03-18 18:29:24 -0700781 }
Tony Wickham827cef22016-03-17 15:39:39 -0700782 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
783 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700784 }
785 }
786
Amith Yamasani6cc806d2014-05-02 13:47:11 -0700787 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700788 resetLoadedState(true, true);
789
Reena Lee93f824a2011-09-23 17:20:28 -0700790 // Do this here because if the launcher activity is running it will be restarted.
791 // If it's not running startLoaderFromBackground will merely tell it that it needs
792 // to reload.
793 startLoaderFromBackground();
794 }
795
Winson Chungf0c6ae02012-03-21 16:10:31 -0700796 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
797 synchronized (mLock) {
798 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
799 // mWorkspaceLoaded to true later
800 stopLoaderLocked();
801 if (resetAllAppsLoaded) mAllAppsLoaded = false;
802 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700803 // Always reset deep shortcuts loaded.
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700804 // TODO: why?
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700805 mDeepShortcutsLoaded = false;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700806 }
807 }
808
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700809 /**
810 * When the launcher is in the background, it's possible for it to miss paired
811 * configuration changes. So whenever we trigger the loader from the background
812 * tell the launcher that it needs to re-run the loader when it comes back instead
813 * of doing it now.
814 */
815 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800816 Callbacks callbacks = getCallback();
817 if (callbacks != null) {
818 // Only actually run the loader if they're not paused.
819 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -0700820 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700821 }
822 }
Joe Onorato36115782010-06-17 13:28:48 -0400823 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400824
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700825 /**
826 * If there is already a loader task running, tell it to stop.
827 */
828 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -0700829 LoaderTask oldTask = mLoaderTask;
830 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -0700831 oldTask.stopLocked();
832 }
Reena Lee93f824a2011-09-23 17:20:28 -0700833 }
834
Adam Cohen1a85c582014-09-30 09:48:49 -0700835 public boolean isCurrentCallbacks(Callbacks callbacks) {
836 return (mCallbacks != null && mCallbacks.get() == callbacks);
837 }
838
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700839 /**
840 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
841 * @return true if the page could be bound synchronously.
842 */
843 public boolean startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700844 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
845 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -0400846 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -0400847 // Don't bother to start the thread if we know it's not going to do anything
848 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -0700849 final Callbacks oldCallbacks = mCallbacks.get();
850 // Clear any pending bind-runnables from the synchronized load process.
851 runOnMainThread(new Runnable() {
852 public void run() {
853 oldCallbacks.clearPendingBinds();
854 }
855 });
856
Joe Onorato36115782010-06-17 13:28:48 -0400857 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700858 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700859 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700860 // TODO: mDeepShortcutsLoaded does not need to be true for synchronous bind.
Tony Wickham80f57872016-06-29 18:12:15 -0700861 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE && mAllAppsLoaded
862 && mWorkspaceLoaded && mDeepShortcutsLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700863 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700864 return true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700865 } else {
866 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
867 sWorker.post(mLoaderTask);
868 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400869 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400870 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700871 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400872 }
873
Joe Onorato36115782010-06-17 13:28:48 -0400874 public void stopLoader() {
875 synchronized (mLock) {
876 if (mLoaderTask != null) {
877 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400878 }
879 }
Joe Onorato36115782010-06-17 13:28:48 -0400880 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400881
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800882 /**
883 * Loads the workspace screen ids in an ordered list.
884 */
Sunny Goyale5bb7052015-07-27 14:36:07 -0700885 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -0700886 final ContentResolver contentResolver = context.getContentResolver();
887 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -0700888
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800889 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -0700890 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
891 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -0700892 }
893
Joe Onorato36115782010-06-17 13:28:48 -0400894 /**
895 * Runnable for the thread that loads the contents of the launcher:
896 * - workspace icons
897 * - widgets
898 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700899 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -0400900 */
901 private class LoaderTask implements Runnable {
902 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -0700903 private int mPageToBindFirst;
904
Adam Cohen091440a2015-03-18 14:16:05 -0700905 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -0400906 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -0700907 @Thunk boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700908
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700909 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -0400910 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -0700911 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400912 }
913
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800914 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700915 mIsLoadingAndBindingWorkspace = true;
916
Joe Onorato36115782010-06-17 13:28:48 -0400917 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -0400918 if (DEBUG_LOADERS) {
919 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400920 }
Michael Jurka288a36b2011-07-12 16:53:48 -0700921
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700922 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800923 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -0700924 synchronized (LoaderTask.this) {
925 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800926 return;
Reena Lee93f824a2011-09-23 17:20:28 -0700927 }
928 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400929 }
930 }
931
Joe Onorato36115782010-06-17 13:28:48 -0400932 // Bind the workspace
Sunny Goyal93f878c2016-03-30 17:31:24 -0700933 bindWorkspace(mPageToBindFirst);
Joe Onorato36115782010-06-17 13:28:48 -0400934 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400935
Joe Onorato36115782010-06-17 13:28:48 -0400936 private void waitForIdle() {
937 // Wait until the either we're stopped or the other threads are done.
938 // This way we don't start loading all apps until the workspace has settled
939 // down.
940 synchronized (LoaderTask.this) {
941 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700942
Joe Onorato36115782010-06-17 13:28:48 -0400943 mHandler.postIdle(new Runnable() {
944 public void run() {
945 synchronized (LoaderTask.this) {
946 mLoadAndBindStepFinished = true;
947 if (DEBUG_LOADERS) {
948 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400949 }
Joe Onorato36115782010-06-17 13:28:48 -0400950 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400951 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400952 }
Joe Onorato36115782010-06-17 13:28:48 -0400953 });
954
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800955 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -0400956 try {
Michael Jurkac7700af2013-05-14 20:17:58 +0200957 // Just in case mFlushingWorkerThread changes but we aren't woken up,
958 // wait no longer than 1sec at a time
959 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -0400960 } catch (InterruptedException ex) {
961 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400962 }
963 }
Joe Onorato36115782010-06-17 13:28:48 -0400964 if (DEBUG_LOADERS) {
965 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700966 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400967 + "ms for previous step to finish binding");
968 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400969 }
Joe Onorato36115782010-06-17 13:28:48 -0400970 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400971
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700972 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -0500973 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700974 // Ensure that we have a valid page index to load synchronously
975 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
976 "valid page index");
977 }
978 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
979 // Ensure that we don't try and bind a specified page when the pages have not been
980 // loaded already (we should load everything asynchronously in that case)
981 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
982 }
983 synchronized (mLock) {
984 if (mIsLoaderTaskRunning) {
985 // Ensure that we are never running the background loading at this point since
986 // we also touch the background collections
987 throw new RuntimeException("Error! Background loading is already running");
988 }
989 }
990
991 // XXX: Throw an exception if we are already loading (since we touch the worker thread
992 // data structures, we can't allow any other thread to touch that data, but because
993 // this call is synchronous, we can get away with not locking).
994
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400995 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -0700996 // operations from the previous activity. We need to ensure that all queued operations
997 // are executed before any synchronous binding work is done.
998 mHandler.flush();
999
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001000 // Divide the set of loaded items into those that we are binding synchronously, and
1001 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001002 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001003 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1004 // arise from that.
1005 onlyBindAllApps();
Tony Wickham80f57872016-06-29 18:12:15 -07001006
1007 bindDeepShortcuts();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001008 }
1009
Joe Onorato36115782010-06-17 13:28:48 -04001010 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001011 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001012 if (mStopped) {
1013 return;
1014 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001015 mIsLoaderTaskRunning = true;
1016 }
Joe Onorato36115782010-06-17 13:28:48 -04001017 // Optimize for end-user experience: if the Launcher is up and // running with the
1018 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1019 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001020 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001021 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001022 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001023
Joe Onorato36115782010-06-17 13:28:48 -04001024 if (mStopped) {
1025 break keep_running;
1026 }
1027
Joe Onorato36115782010-06-17 13:28:48 -04001028 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001029
1030 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001031 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1032 loadAndBindAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001033
1034 waitForIdle();
1035
1036 // third step
1037 if (DEBUG_LOADERS) Log.d(TAG, "step 3: loading deep shortcuts");
1038 loadAndBindDeepShortcuts();
Joe Onorato36115782010-06-17 13:28:48 -04001039 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001040
Joe Onorato36115782010-06-17 13:28:48 -04001041 // Clear out this reference, otherwise we end up holding it until all of the
1042 // callback runnables are done.
1043 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001044
Joe Onorato36115782010-06-17 13:28:48 -04001045 synchronized (mLock) {
1046 // If we are still the last one to be scheduled, remove ourselves.
1047 if (mLoaderTask == this) {
1048 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001049 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001050 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001051 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001052 }
Joe Onorato36115782010-06-17 13:28:48 -04001053 }
1054
1055 public void stopLocked() {
1056 synchronized (LoaderTask.this) {
1057 mStopped = true;
1058 this.notify();
1059 }
1060 }
1061
1062 /**
1063 * Gets the callbacks object. If we've been stopped, or if the launcher object
1064 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1065 * object that was around when the deferred message was scheduled, and if there's
1066 * a new Callbacks object around then also return null. This will save us from
1067 * calling onto it with data that will be ignored.
1068 */
1069 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1070 synchronized (mLock) {
1071 if (mStopped) {
1072 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001073 }
Joe Onorato36115782010-06-17 13:28:48 -04001074
1075 if (mCallbacks == null) {
1076 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001077 }
Joe Onorato36115782010-06-17 13:28:48 -04001078
1079 final Callbacks callbacks = mCallbacks.get();
1080 if (callbacks != oldCallbacks) {
1081 return null;
1082 }
1083 if (callbacks == null) {
1084 Log.w(TAG, "no mCallbacks");
1085 return null;
1086 }
1087
1088 return callbacks;
1089 }
1090 }
1091
1092 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001093 private boolean checkItemPlacement(LongArrayMap<GridOccupancy> occupied, ItemInfo item,
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001094 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001095 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001096 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung892c74d2013-08-22 16:15:50 -07001097
Adam Cohendcd297f2013-06-18 13:13:40 -07001098 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001099 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001100 // Return early if we detect that an item is under the hotseat button
Sunny Goyalbb011da2016-06-15 15:42:29 -07001101 if (!FeatureFlags.NO_ALL_APPS_ICON &&
1102 profile.isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001103 Log.e(TAG, "Error loading shortcut into hotseat " + item
1104 + " into position (" + item.screenId + ":" + item.cellX + ","
1105 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001106 return false;
1107 }
1108
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001109 final GridOccupancy hotseatOccupancy =
Dan Sandler295ae182013-12-10 16:05:47 -05001110 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1111
Adam Cohen2e6da152015-05-06 11:42:25 -07001112 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001113 Log.e(TAG, "Error loading shortcut " + item
1114 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001115 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001116 + ")");
1117 return false;
1118 }
1119
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001120 if (hotseatOccupancy != null) {
1121 if (hotseatOccupancy.cells[(int) item.screenId][0]) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001122 Log.e(TAG, "Error loading shortcut into hotseat " + item
1123 + " into position (" + item.screenId + ":" + item.cellX + ","
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001124 + item.cellY + ") already occupied");
Adam Cohendcd297f2013-06-18 13:13:40 -07001125 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001126 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001127 hotseatOccupancy.cells[(int) item.screenId][0] = true;
Dan Sandler295ae182013-12-10 16:05:47 -05001128 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001129 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001130 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001131 final GridOccupancy occupancy = new GridOccupancy(profile.numHotseatIcons, 1);
1132 occupancy.cells[(int) item.screenId][0] = true;
1133 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, occupancy);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001134 return true;
1135 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001136 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1137 if (!workspaceScreens.contains((Long) item.screenId)) {
1138 // The item has an invalid screen id.
1139 return false;
1140 }
1141 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001142 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001143 return true;
1144 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001145
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001146 final int countX = profile.numColumns;
1147 final int countY = profile.numRows;
Adam Cohenae4409d2013-11-26 10:34:59 -08001148 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1149 item.cellX < 0 || item.cellY < 0 ||
1150 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1151 Log.e(TAG, "Error loading shortcut " + item
1152 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1153 + item.cellX + "," + item.cellY
1154 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1155 return false;
1156 }
1157
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001158 if (!occupied.containsKey(item.screenId)) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001159 GridOccupancy screen = new GridOccupancy(countX + 1, countY + 1);
1160 if (item.screenId == Workspace.FIRST_SCREEN_ID) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001161 // Mark the first row as occupied (if the feature is enabled)
1162 // in order to account for the QSB.
1163 screen.markCells(0, 0, countX + 1, 1, FeatureFlags.QSB_ON_FIRST_SCREEN);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001164 }
1165 occupied.put(item.screenId, screen);
Joe Onorato36115782010-06-17 13:28:48 -04001166 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001167 final GridOccupancy occupancy = occupied.get(item.screenId);
Winson Chungf30ad5f2011-08-08 10:55:42 -07001168
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001169 // Check if any workspace icons overlap with each other
1170 if (occupancy.isRegionVacant(item.cellX, item.cellY, item.spanX, item.spanY)) {
1171 occupancy.markCells(item, true);
1172 return true;
1173 } else {
1174 Log.e(TAG, "Error loading shortcut " + item
1175 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1176 + item.cellX + "," + item.cellX + "," + item.spanX + "," + item.spanY
1177 + ") already occupied");
1178 return false;
1179 }
Joe Onorato36115782010-06-17 13:28:48 -04001180 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001181
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001182 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -07001183 if (LauncherAppState.PROFILE_STARTUP) {
1184 Trace.beginSection("Loading Workspace");
1185 }
Joe Onorato36115782010-06-17 13:28:48 -04001186 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001187
Joe Onorato36115782010-06-17 13:28:48 -04001188 final Context context = mContext;
1189 final ContentResolver contentResolver = context.getContentResolver();
1190 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001191 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001192 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001193 final boolean isSdCardReady = Utilities.isBootCompleted();
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001194 final MultiHashMap<UserHandleCompat, String> pendingPackages = new MultiHashMap<>();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001195
Winson Chung892c74d2013-08-22 16:15:50 -07001196 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001197 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001198 int countX = profile.numColumns;
1199 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001200
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001201 boolean clearDb = false;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -07001202 try {
1203 ImportDataTask.performImportIfPossible(context);
1204 } catch (Exception e) {
1205 // Migration failed. Clear workspace.
1206 clearDb = true;
1207 }
1208
1209 if (!clearDb && GridSizeMigrationTask.ENABLED &&
Sunny Goyalf076eae2016-01-11 12:25:10 -08001210 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1211 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001212 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001213 }
1214
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001215 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -07001216 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001217 LauncherSettings.Settings.call(contentResolver,
1218 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001219 }
1220
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001221 Log.d(TAG, "loadWorkspace: loading default favorites");
1222 LauncherSettings.Settings.call(contentResolver,
1223 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -07001224
Sunny Goyale9956a72016-09-01 17:24:47 -07001225 synchronized (sBgDataModel) {
1226 sBgDataModel.clear();
1227
Sunny Goyal756adbc2015-04-16 15:20:51 -07001228 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001229 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyale9956a72016-09-01 17:24:47 -07001230 sBgDataModel.workspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001231
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001232 final ArrayList<Long> itemsToRemove = new ArrayList<>();
1233 final ArrayList<Long> restoredRows = new ArrayList<>();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001234 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001235 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001236 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001237 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001238
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001239 // +1 for the hotseat (it can be larger than the workspace)
1240 // Load workspace in reverse order to ensure that latest items are loaded first (and
1241 // before any earlier duplicates)
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001242 final LongArrayMap<GridOccupancy> occupied = new LongArrayMap<>();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001243 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001244
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001245 try {
1246 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1247 final int intentIndex = c.getColumnIndexOrThrow
1248 (LauncherSettings.Favorites.INTENT);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001249 final int containerIndex = c.getColumnIndexOrThrow(
1250 LauncherSettings.Favorites.CONTAINER);
1251 final int itemTypeIndex = c.getColumnIndexOrThrow(
1252 LauncherSettings.Favorites.ITEM_TYPE);
1253 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1254 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001255 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1256 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001257 final int screenIndex = c.getColumnIndexOrThrow(
1258 LauncherSettings.Favorites.SCREEN);
1259 final int cellXIndex = c.getColumnIndexOrThrow
1260 (LauncherSettings.Favorites.CELLX);
1261 final int cellYIndex = c.getColumnIndexOrThrow
1262 (LauncherSettings.Favorites.CELLY);
1263 final int spanXIndex = c.getColumnIndexOrThrow
1264 (LauncherSettings.Favorites.SPANX);
1265 final int spanYIndex = c.getColumnIndexOrThrow(
1266 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001267 final int rankIndex = c.getColumnIndexOrThrow(
1268 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001269 final int restoredIndex = c.getColumnIndexOrThrow(
1270 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001271 final int profileIdIndex = c.getColumnIndexOrThrow(
1272 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001273 final int optionsIndex = c.getColumnIndexOrThrow(
1274 LauncherSettings.Favorites.OPTIONS);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001275 final CursorIconInfo cursorIconInfo = new CursorIconInfo(mContext, c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001276
Sunny Goyal7f834d22015-04-21 10:10:23 -07001277 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
Kenny Guyff05f432016-01-22 17:48:29 +00001278 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001279 final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>();
Sunny Goyal7f834d22015-04-21 10:10:23 -07001280 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001281 long serialNo = mUserManager.getSerialNumberForUser(user);
1282 allUsers.put(serialNo, user);
1283 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001284
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001285 boolean userUnlocked = mUserManager.isUserUnlocked(user);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001286
1287 // We can only query for shortcuts when the user is unlocked.
1288 if (userUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07001289 List<ShortcutInfoCompat> pinnedShortcuts =
1290 mDeepShortcutManager.queryForPinnedShortcuts(null, user);
1291 if (mDeepShortcutManager.wasLastCallSuccess()) {
1292 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
1293 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
1294 shortcut);
1295 }
1296 } else {
1297 // Shortcut manager can fail due to some race condition when the
1298 // lock state changes too frequently. For the purpose of the loading
1299 // shortcuts, consider the user is still locked.
1300 userUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001301 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001302 }
Sunny Goyal49f4f032016-08-01 15:45:49 -07001303 unlockedUsers.put(serialNo, userUnlocked);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001304 }
1305
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001306 ShortcutInfo info;
1307 String intentDescription;
1308 LauncherAppWidgetInfo appWidgetInfo;
1309 int container;
1310 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001311 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001312 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001313 UserHandleCompat user;
Sunny Goyald09c3702016-04-06 16:18:20 -07001314 String targetPackage;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001315
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001316 while (!mStopped && c.moveToNext()) {
1317 try {
1318 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001319 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001320 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001321 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001322
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001323 switch (itemType) {
1324 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1325 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001326 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001327 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001328 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001329 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001330 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001331 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001332 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001333 boolean itemReplaced = false;
Sunny Goyald09c3702016-04-06 16:18:20 -07001334 targetPackage = null;
Kenny Guyed131872014-04-30 03:02:21 +01001335 if (user == null) {
1336 // User has been deleted remove the item.
1337 itemsToRemove.add(id);
1338 continue;
1339 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001340 try {
1341 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001342 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001343 if (cn != null && cn.getPackageName() != null) {
1344 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1345 cn.getPackageName(), user);
1346 boolean validComponent = validPkg &&
1347 launcherApps.isActivityEnabledForProfile(cn, user);
Sunny Goyald09c3702016-04-06 16:18:20 -07001348 if (validPkg) {
1349 targetPackage = cn.getPackageName();
1350 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001351
1352 if (validComponent) {
1353 if (restored) {
1354 // no special handling necessary for this item
1355 restoredRows.add(id);
1356 restored = false;
1357 }
Kenny Guyff05f432016-01-22 17:48:29 +00001358 if (quietMode.get(serialNumber)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001359 disabledState = ShortcutInfo.FLAG_DISABLED_QUIET_USER;
Kenny Guyff05f432016-01-22 17:48:29 +00001360 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001361 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001362 intent = null;
1363 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1364 // We allow auto install apps to have their intent
1365 // updated after an install.
1366 intent = manager.getLaunchIntentForPackage(
1367 cn.getPackageName());
1368 if (intent != null) {
1369 ContentValues values = new ContentValues();
1370 values.put(LauncherSettings.Favorites.INTENT,
1371 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001372 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001373 }
1374 }
1375
1376 if (intent == null) {
1377 // The app is installed but the component is no
1378 // longer available.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001379 FileLog.d(TAG, "Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001380 itemsToRemove.add(id);
1381 continue;
1382 } else {
1383 // no special handling necessary for this item
1384 restoredRows.add(id);
1385 restored = false;
1386 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001387 } else if (restored) {
1388 // Package is not yet available but might be
1389 // installed later.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001390 FileLog.d(TAG, "package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001391
1392 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1393 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001394 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001395 // App restore has started. Update the flag
1396 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1397 ContentValues values = new ContentValues();
1398 values.put(LauncherSettings.Favorites.RESTORED,
1399 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001400 updateItem(id, values);
1401 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1402 // This is a common app. Try to replace this.
1403 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1404 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1405 if (parser.findDefaultApp()) {
1406 // Default app found. Replace it.
1407 intent = parser.parsedIntent;
1408 cn = intent.getComponent();
1409 ContentValues values = parser.parsedValues;
1410 values.put(LauncherSettings.Favorites.RESTORED, 0);
1411 updateItem(id, values);
1412 restored = false;
1413 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001414
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001415 } else {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001416 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001417 itemsToRemove.add(id);
1418 continue;
1419 }
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001420 } else {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001421 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001422 itemsToRemove.add(id);
1423 continue;
1424 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001425 } else if (PackageManagerHelper.isAppOnSdcard(
1426 manager, cn.getPackageName())) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07001427 // Package is present but not available.
1428 allowMissingTarget = true;
1429 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1430 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001431 // SdCard is not ready yet. Package might get available,
1432 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001433 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001434 pendingPackages.addToList(user, cn.getPackageName());
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001435 allowMissingTarget = true;
1436 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001437
1438 } else {
1439 // Do not wait for external media load anymore.
1440 // Log the invalid package, and remove it
Sunny Goyal713edfc2016-05-06 09:58:34 -07001441 FileLog.d(TAG, "Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001442 itemsToRemove.add(id);
1443 continue;
Winson Chungee055712013-07-30 14:46:24 -07001444 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001445 } else if (cn == null) {
1446 // For shortcuts with no component, keep them as they are
1447 restoredRows.add(id);
1448 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001449 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001450 } catch (URISyntaxException e) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001451 FileLog.d(TAG, "Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001452 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001453 continue;
1454 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001455
Sunny Goyal34b65272015-03-11 16:56:52 -07001456 boolean useLowResIcon = container >= 0 &&
1457 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1458
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001459 if (itemReplaced) {
1460 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001461 info = getAppShortcutInfo(intent, user, null,
1462 cursorIconInfo, false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001463 } else {
1464 // Don't replace items for other profiles.
1465 itemsToRemove.add(id);
1466 continue;
1467 }
1468 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001469 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001470 info = getRestoredItemInfo(c, intent,
1471 promiseType, itemType, cursorIconInfo);
Kenny Guyed131872014-04-30 03:02:21 +01001472 intent = getRestoredItemIntent(c, context, intent);
1473 } else {
1474 // Don't restore items for other profiles.
1475 itemsToRemove.add(id);
1476 continue;
1477 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001478 } else if (itemType ==
1479 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001480 info = getAppShortcutInfo(intent, user, c,
1481 cursorIconInfo, allowMissingTarget, useLowResIcon);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001482 } else if (itemType ==
1483 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001484
1485 ShortcutKey key = ShortcutKey.fromIntent(intent, user);
1486 if (unlockedUsers.get(serialNumber)) {
1487 ShortcutInfoCompat pinnedShortcut =
1488 shortcutKeyToPinnedShortcuts.get(key);
1489 if (pinnedShortcut == null) {
1490 // The shortcut is no longer valid.
1491 itemsToRemove.add(id);
1492 continue;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001493 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001494 info = new ShortcutInfo(pinnedShortcut, context);
1495 intent = info.intent;
1496 } else {
1497 // Create a shortcut info in disabled mode for now.
1498 info = new ShortcutInfo();
1499 info.user = user;
1500 info.itemType = itemType;
1501 loadInfoFromCursor(info, c, cursorIconInfo);
1502
1503 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001504 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001505 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001506 info = getShortcutInfo(c, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001507
Sunny Goyald09c3702016-04-06 16:18:20 -07001508 // Shortcuts are only available on the primary profile
1509 if (PackageManagerHelper.isAppSuspended(manager, targetPackage)) {
1510 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1511 }
1512
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001513 // App shortcuts that used to be automatically added to Launcher
1514 // didn't always have the correct intent flags set, so do that
1515 // here
1516 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001517 intent.getCategories() != null &&
1518 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001519 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001520 intent.addFlags(
1521 Intent.FLAG_ACTIVITY_NEW_TASK |
1522 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1523 }
Michael Jurka96879562012-03-22 05:54:33 -07001524 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001525
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001526 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001527 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001528 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001529 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001530 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001531 info.cellX = c.getInt(cellXIndex);
1532 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08001533 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001534 info.spanX = 1;
1535 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01001536 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001537 if (info.promisedIntent != null) {
1538 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
1539 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001540 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001541 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1542 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1543 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001544
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001545 // check & update map of what's occupied
Sunny Goyale9956a72016-09-01 17:24:47 -07001546 if (!checkItemPlacement(occupied, info, sBgDataModel.workspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001547 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001548 break;
1549 }
1550
Sunny Goyal756adbc2015-04-16 15:20:51 -07001551 if (restored) {
1552 ComponentName cn = info.getTargetComponent();
1553 if (cn != null) {
1554 Integer progress = installingPkgs.get(cn.getPackageName());
1555 if (progress != null) {
1556 info.setInstallProgress(progress);
1557 } else {
1558 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
1559 }
1560 }
1561 }
1562
Sunny Goyale9956a72016-09-01 17:24:47 -07001563 sBgDataModel.addItem(info, false);
Winson Chung1323b482013-08-05 12:41:55 -07001564 } else {
1565 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001566 }
1567 break;
1568
1569 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1570 id = c.getLong(idIndex);
Sunny Goyale9956a72016-09-01 17:24:47 -07001571 FolderInfo folderInfo = sBgDataModel.findOrMakeFolder(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001572
Sunny Goyala508e4f2015-05-21 09:33:57 -07001573 // Do not trim the folder label, as is was set by the user.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001574 folderInfo.title = c.getString(cursorIconInfo.titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001575 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001576 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001577 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001578 folderInfo.cellX = c.getInt(cellXIndex);
1579 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001580 folderInfo.spanX = 1;
1581 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001582 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001583
Daniel Sandler8802e962010-05-26 16:28:16 -04001584 // check & update map of what's occupied
Sunny Goyale9956a72016-09-01 17:24:47 -07001585 if (!checkItemPlacement(occupied, folderInfo, sBgDataModel.workspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001586 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001587 break;
1588 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001589 if (restored) {
1590 // no special handling required for restored folders
1591 restoredRows.add(id);
1592 }
1593
Sunny Goyale9956a72016-09-01 17:24:47 -07001594 sBgDataModel.addItem(folderInfo, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001595 break;
1596
1597 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08001598 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001599 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08001600 boolean customWidget = itemType ==
1601 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
1602
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001603 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001604 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001605 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001606 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001607 user = allUsers.get(serialNumber);
1608 if (user == null) {
1609 itemsToRemove.add(id);
1610 continue;
1611 }
1612
Sunny Goyalff572272014-07-23 13:58:07 -07001613 final ComponentName component =
1614 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04001615
Sunny Goyal651077b2014-06-30 14:15:31 -07001616 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07001617 final boolean isIdValid = (restoreStatus &
1618 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07001619 final boolean wasProviderReady = (restoreStatus &
1620 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07001621
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001622 if (widgetProvidersMap == null) {
1623 widgetProvidersMap = AppWidgetManagerCompat
1624 .getInstance(mContext).getAllProvidersMap();
1625 }
1626 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
1627 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00001628 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001629 user));
Sunny Goyalff572272014-07-23 13:58:07 -07001630
1631 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08001632 if (!isSafeMode && !customWidget &&
1633 wasProviderReady && !isProviderReady) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001634 FileLog.d(TAG, "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07001635 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001636 itemsToRemove.add(id);
1637 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07001638 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07001639 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1640 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08001641
Sunny Goyal53f96722015-07-13 19:54:53 -07001642 // The provider is available. So the widget is either
1643 // available or not available. We do not need to track
1644 // any future restore updates.
1645 int status = restoreStatus &
1646 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07001647 if (!wasProviderReady) {
1648 // If provider was not previously ready, update the
1649 // status and UI flag.
1650
1651 // Id would be valid only if the widget restore broadcast was received.
1652 if (isIdValid) {
Sunny Goyal86df1382016-08-10 15:03:22 -07001653 status |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07001654 } else {
1655 status &= ~LauncherAppWidgetInfo
1656 .FLAG_PROVIDER_NOT_READY;
1657 }
1658 }
1659 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07001660 } else {
1661 Log.v(TAG, "Widget restore pending id=" + id
1662 + " appWidgetId=" + appWidgetId
1663 + " status =" + restoreStatus);
1664 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07001665 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07001666 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001667 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07001668
1669 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
1670 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001671 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07001672 // App restore has started. Update the flag
1673 appWidgetInfo.restoreStatus |=
1674 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001675 } else if (!isSafeMode) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001676 FileLog.d(TAG, "Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07001677 itemsToRemove.add(id);
1678 continue;
1679 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07001680
1681 appWidgetInfo.installProgress =
1682 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07001683 }
Sunny Goyal86df1382016-08-10 15:03:22 -07001684 if (appWidgetInfo.hasRestoreFlag(
1685 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG)) {
1686 intentDescription = c.getString(intentIndex);
1687 if (!TextUtils.isEmpty(intentDescription)) {
1688 appWidgetInfo.bindOptions =
1689 Intent.parseUri(intentDescription, 0);
1690 }
1691 }
Sunny Goyalff572272014-07-23 13:58:07 -07001692
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001693 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07001694 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001695 appWidgetInfo.cellX = c.getInt(cellXIndex);
1696 appWidgetInfo.cellY = c.getInt(cellYIndex);
1697 appWidgetInfo.spanX = c.getInt(spanXIndex);
1698 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07001699 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04001700
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001701 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1702 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1703 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001704 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1705 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001706 continue;
1707 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001708
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001709 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001710 // check & update map of what's occupied
Sunny Goyale9956a72016-09-01 17:24:47 -07001711 if (!checkItemPlacement(occupied, appWidgetInfo, sBgDataModel.workspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001712 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001713 break;
1714 }
Sunny Goyal651077b2014-06-30 14:15:31 -07001715
Adam Cohen59400422014-03-05 18:07:04 -08001716 if (!customWidget) {
1717 String providerName =
1718 appWidgetInfo.providerName.flattenToString();
1719 if (!providerName.equals(savedProvider) ||
1720 (appWidgetInfo.restoreStatus != restoreStatus)) {
1721 ContentValues values = new ContentValues();
1722 values.put(
1723 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1724 providerName);
1725 values.put(LauncherSettings.Favorites.RESTORED,
1726 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001727 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08001728 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001729 }
Sunny Goyale9956a72016-09-01 17:24:47 -07001730 sBgDataModel.addItem(appWidgetInfo, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001731 }
Joe Onorato36115782010-06-17 13:28:48 -04001732 break;
1733 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001734 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001735 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001736 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001737 }
1738 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001739 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001740 }
1741
Winson Chungba9c37f2013-08-30 14:11:37 -07001742 // Break early if we've stopped loading
1743 if (mStopped) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001744 sBgDataModel.clear();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001745 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07001746 }
1747
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001748 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001749 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001750 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
1751 Utilities.createDbSelectionQuery(
1752 LauncherSettings.Favorites._ID, itemsToRemove), null);
1753 if (DEBUG_LOADERS) {
1754 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
1755 LauncherSettings.Favorites._ID, itemsToRemove));
1756 }
1757
1758 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07001759 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
1760 .call(contentResolver,
1761 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
1762 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
1763 for (long folderId : deletedFolderIds) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001764 sBgDataModel.workspaceItems.remove(sBgDataModel.folders.get(folderId));
1765 sBgDataModel.folders.remove(folderId);
1766 sBgDataModel.itemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001767 }
1768 }
1769
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001770 // Unpin shortcuts that don't exist on the workspace.
1771 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001772 MutableInt numTimesPinned = sBgDataModel.pinnedShortcutCounts.get(key);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001773 if (numTimesPinned == null || numTimesPinned.value == 0) {
1774 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
1775 mDeepShortcutManager.unpinShortcut(key);
1776 }
1777 }
1778
Sunny Goyal317698b2015-07-29 11:45:41 -07001779 // Sort all the folder items and make sure the first 3 items are high resolution.
Sunny Goyale9956a72016-09-01 17:24:47 -07001780 for (FolderInfo folder : sBgDataModel.folders) {
Sunny Goyal317698b2015-07-29 11:45:41 -07001781 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
1782 int pos = 0;
1783 for (ShortcutInfo info : folder.contents) {
1784 if (info.usingLowResIcon) {
1785 info.updateIcon(mIconCache, false);
1786 }
1787 pos ++;
1788 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
1789 break;
1790 }
1791 }
1792 }
1793
Chris Wrenf4d08112014-01-16 18:13:56 -05001794 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001795 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001796 ContentValues values = new ContentValues();
1797 values.put(LauncherSettings.Favorites.RESTORED, 0);
1798 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
1799 Utilities.createDbSelectionQuery(
1800 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05001801 }
1802
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001803 if (!isSdCardReady && !pendingPackages.isEmpty()) {
1804 context.registerReceiver(
1805 new SdCardAvailableReceiver(
1806 LauncherModel.this, mContext, pendingPackages),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001807 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001808 null,
1809 sWorker);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001810 }
1811
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001812 // Remove any empty screens
Sunny Goyale9956a72016-09-01 17:24:47 -07001813 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgDataModel.workspaceScreens);
1814 for (ItemInfo item: sBgDataModel.itemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001815 long screenId = item.screenId;
1816 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1817 unusedScreens.contains(screenId)) {
1818 unusedScreens.remove(screenId);
1819 }
1820 }
1821
1822 // If there are any empty screens remove them, and update.
1823 if (unusedScreens.size() != 0) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001824 sBgDataModel.workspaceScreens.removeAll(unusedScreens);
1825 updateWorkspaceScreenOrder(context, sBgDataModel.workspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001826 }
1827
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001828 if (DEBUG_LOADERS) {
1829 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1830 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07001831 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07001832 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001833 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07001834
Sunny Goyale2df0622015-04-24 11:27:00 -07001835 for (int i = 0; i < nScreens; i++) {
1836 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07001837 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001838 line += " | ";
1839 }
Joe Onorato36115782010-06-17 13:28:48 -04001840 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001841 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04001842 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001843 }
Joe Onorato36115782010-06-17 13:28:48 -04001844 }
Sunny Goyale26d1002016-06-20 14:52:14 -07001845 if (LauncherAppState.PROFILE_STARTUP) {
1846 Trace.endSection();
1847 }
Adam Cohene25af792013-06-06 23:08:25 -07001848 }
1849
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001850 /**
1851 * Partially updates the item without any notification. Must be called on the worker thread.
1852 */
1853 private void updateItem(long itemId, ContentValues update) {
1854 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001855 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001856 update,
1857 BaseColumns._ID + "= ?",
1858 new String[]{Long.toString(itemId)});
1859 }
1860
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001861 /** Filters the set of items who are directly or indirectly (via another container) on the
1862 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08001863 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001864 ArrayList<ItemInfo> allWorkspaceItems,
1865 ArrayList<ItemInfo> currentScreenItems,
1866 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001867 // Purge any null ItemInfos
1868 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
1869 while (iter.hasNext()) {
1870 ItemInfo i = iter.next();
1871 if (i == null) {
1872 iter.remove();
1873 }
1874 }
1875
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001876 // Order the set of items by their containers first, this allows use to walk through the
1877 // list sequentially, build up a list of containers that are in the specified screen,
1878 // as well as all items in those containers.
1879 Set<Long> itemsOnScreen = new HashSet<Long>();
1880 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
1881 @Override
1882 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07001883 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001884 }
1885 });
1886 for (ItemInfo info : allWorkspaceItems) {
1887 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001888 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001889 currentScreenItems.add(info);
1890 itemsOnScreen.add(info.id);
1891 } else {
1892 otherScreenItems.add(info);
1893 }
1894 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1895 currentScreenItems.add(info);
1896 itemsOnScreen.add(info.id);
1897 } else {
1898 if (itemsOnScreen.contains(info.container)) {
1899 currentScreenItems.add(info);
1900 itemsOnScreen.add(info.id);
1901 } else {
1902 otherScreenItems.add(info);
1903 }
1904 }
1905 }
1906 }
1907
1908 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08001909 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001910 ArrayList<LauncherAppWidgetInfo> appWidgets,
1911 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
1912 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001913
1914 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001915 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001916 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08001917 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001918 currentScreenWidgets.add(widget);
1919 } else {
1920 otherScreenWidgets.add(widget);
1921 }
1922 }
1923 }
1924
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001925 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
1926 * right) */
1927 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07001928 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001929 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07001930 final int screenCols = profile.numColumns;
1931 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001932 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07001933 @Override
1934 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07001935 if (lhs.container == rhs.container) {
1936 // Within containers, order by their spatial position in that container
1937 switch ((int) lhs.container) {
1938 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
1939 long lr = (lhs.screenId * screenCellCount +
1940 lhs.cellY * screenCols + lhs.cellX);
1941 long rr = (rhs.screenId * screenCellCount +
1942 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07001943 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07001944 }
1945 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
1946 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07001947 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07001948 }
1949 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07001950 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07001951 throw new RuntimeException("Unexpected container type when " +
1952 "sorting workspace items.");
1953 }
1954 return 0;
1955 }
1956 } else {
1957 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07001958 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07001959 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001960 }
1961 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001962 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001963
Adam Cohendcd297f2013-06-18 13:13:40 -07001964 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
1965 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001966 final Runnable r = new Runnable() {
1967 @Override
1968 public void run() {
1969 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1970 if (callbacks != null) {
1971 callbacks.bindScreens(orderedScreens);
1972 }
1973 }
1974 };
Sunny Goyald33860f2015-04-23 16:02:20 -07001975 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07001976 }
1977
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001978 private void bindWorkspaceItems(final Callbacks oldCallbacks,
1979 final ArrayList<ItemInfo> workspaceItems,
1980 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07001981 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001982
1983 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07001984 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001985 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04001986 final int start = i;
1987 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001988 final Runnable r = new Runnable() {
1989 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001990 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001991 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001992 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07001993 callbacks.bindItems(workspaceItems, start, start+chunkSize,
1994 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001995 }
1996 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001997 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001998 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04001999 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002000
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002001 // Bind the widgets, one at a time
2002 N = appWidgets.size();
2003 for (int i = 0; i < N; i++) {
2004 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2005 final Runnable r = new Runnable() {
2006 public void run() {
2007 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2008 if (callbacks != null) {
2009 callbacks.bindAppWidget(widget);
2010 }
2011 }
2012 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002013 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002014 }
2015 }
2016
2017 /**
2018 * Binds all loaded data to actual views on the main thread.
2019 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002020 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002021 final long t = SystemClock.uptimeMillis();
2022 Runnable r;
2023
2024 // Don't use these two variables in any of the callback runnables.
2025 // Otherwise we hold a reference to them.
2026 final Callbacks oldCallbacks = mCallbacks.get();
2027 if (oldCallbacks == null) {
2028 // This launcher has exited and nobody bothered to tell us. Just bail.
2029 Log.w(TAG, "LoaderTask running with no launcher");
2030 return;
2031 }
2032
Winson Chung9b9fb962013-11-15 15:39:34 -08002033 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07002034 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
2035 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
2036 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002037
Sunny Goyale9956a72016-09-01 17:24:47 -07002038 synchronized (sBgDataModel) {
2039 workspaceItems.addAll(sBgDataModel.workspaceItems);
2040 appWidgets.addAll(sBgDataModel.appWidgets);
2041 orderedScreenIds.addAll(sBgDataModel.workspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002042 }
2043
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002044 final int currentScreen;
2045 {
2046 int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE
2047 ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen();
2048 if (currScreen >= orderedScreenIds.size()) {
2049 // There may be no workspace screens (just hotseat items and an empty page).
2050 currScreen = PagedView.INVALID_RESTORE_PAGE;
2051 }
2052 currentScreen = currScreen;
Winson Chung9b9fb962013-11-15 15:39:34 -08002053 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002054 final boolean validFirstPage = currentScreen >= 0;
2055 final long currentScreenId =
2056 validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID;
Winson Chung9b9fb962013-11-15 15:39:34 -08002057
Winson Chung9b9fb962013-11-15 15:39:34 -08002058 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07002059 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
2060 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
2061 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
2062 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002063
Winson Chung9b9fb962013-11-15 15:39:34 -08002064 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002065 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002066 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002067 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002068 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2069 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2070
2071 // Tell the workspace that we're about to start binding items
2072 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002073 public void run() {
2074 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2075 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002076 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002077 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002078 }
2079 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002080 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002081 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002082
Adam Cohendcd297f2013-06-18 13:13:40 -07002083 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2084
Sunny Goyal527c7d32015-08-28 15:19:36 -07002085 Executor mainExecutor = new DeferredMainThreadExecutor();
2086 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07002087 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002088
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002089 // In case of validFirstPage, only bind the first screen, and defer binding the
2090 // remaining screens after first onDraw (and an optional the fade animation whichever
2091 // happens later).
2092 // This ensures that the first screen is immediately visible (eg. during rotation)
2093 // In case of !validFirstPage, bind all pages one after other.
2094 final Executor deferredExecutor =
2095 validFirstPage ? new ViewOnDrawExecutor(mHandler) : mainExecutor;
2096
2097 mainExecutor.execute(new Runnable() {
2098 @Override
2099 public void run() {
2100 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2101 if (callbacks != null) {
2102 callbacks.finishFirstPageBind(
2103 validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null);
2104 }
2105 }
2106 });
Sunny Goyal527c7d32015-08-28 15:19:36 -07002107
Sunny Goyal44c06432016-04-02 10:56:02 -07002108 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002109
2110 // Tell the workspace that we're done binding items
2111 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002112 public void run() {
2113 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2114 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002115 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002116 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002117
Sunny Goyal639e9062015-08-19 19:17:06 -07002118 mIsLoadingAndBindingWorkspace = false;
2119
2120 // Run all the bind complete runnables after workspace is bound.
2121 if (!mBindCompleteRunnables.isEmpty()) {
2122 synchronized (mBindCompleteRunnables) {
2123 for (final Runnable r : mBindCompleteRunnables) {
2124 runOnWorkerThread(r);
2125 }
2126 mBindCompleteRunnables.clear();
2127 }
2128 }
2129
Winson Chung98e030b2012-05-07 16:01:11 -07002130 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002131 if (DEBUG_LOADERS) {
2132 Log.d(TAG, "bound workspace in "
2133 + (SystemClock.uptimeMillis()-t) + "ms");
2134 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002135
Joe Onorato36115782010-06-17 13:28:48 -04002136 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002137 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002138 deferredExecutor.execute(r);
2139
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002140 if (validFirstPage) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002141 r = new Runnable() {
2142 public void run() {
2143 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2144 if (callbacks != null) {
2145 // We are loading synchronously, which means, some of the pages will be
2146 // bound after first draw. Inform the callbacks that page binding is
2147 // not complete, and schedule the remaining pages.
2148 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2149 callbacks.onPageBoundSynchronously(currentScreen);
2150 }
2151 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2152 }
2153 }
2154 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002155 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002156 }
Joe Onorato36115782010-06-17 13:28:48 -04002157 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002158
Joe Onorato36115782010-06-17 13:28:48 -04002159 private void loadAndBindAllApps() {
2160 if (DEBUG_LOADERS) {
2161 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2162 }
2163 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002164 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002165 synchronized (LoaderTask.this) {
2166 if (mStopped) {
2167 return;
2168 }
2169 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002170 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002171 synchronized (LoaderTask.this) {
2172 if (mStopped) {
2173 return;
2174 }
2175 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002176 }
Joe Onorato36115782010-06-17 13:28:48 -04002177 } else {
2178 onlyBindAllApps();
2179 }
2180 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002181
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002182 private void updateIconCache() {
2183 // Ignore packages which have a promise icon.
2184 HashSet<String> packagesToIgnore = new HashSet<>();
Sunny Goyale9956a72016-09-01 17:24:47 -07002185 synchronized (sBgDataModel) {
2186 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002187 if (info instanceof ShortcutInfo) {
2188 ShortcutInfo si = (ShortcutInfo) info;
2189 if (si.isPromise() && si.getTargetComponent() != null) {
2190 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2191 }
2192 } else if (info instanceof LauncherAppWidgetInfo) {
2193 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2194 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2195 packagesToIgnore.add(lawi.providerName.getPackageName());
2196 }
2197 }
2198 }
2199 }
2200 mIconCache.updateDbIcons(packagesToIgnore);
2201 }
2202
Joe Onorato36115782010-06-17 13:28:48 -04002203 private void onlyBindAllApps() {
2204 final Callbacks oldCallbacks = mCallbacks.get();
2205 if (oldCallbacks == null) {
2206 // This launcher has exited and nobody bothered to tell us. Just bail.
2207 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2208 return;
2209 }
2210
2211 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002212 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002213 final ArrayList<AppInfo> list
2214 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002215 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002216 public void run() {
2217 final long t = SystemClock.uptimeMillis();
2218 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2219 if (callbacks != null) {
2220 callbacks.bindAllApplications(list);
2221 }
2222 if (DEBUG_LOADERS) {
2223 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002224 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04002225 }
2226 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002227 };
Tony Wickham80f57872016-06-29 18:12:15 -07002228 runOnMainThread(r);
Joe Onorato36115782010-06-17 13:28:48 -04002229 }
2230
Winson Chung64359a52013-07-08 17:17:08 -07002231 private void loadAllApps() {
2232 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002233
Joe Onorato36115782010-06-17 13:28:48 -04002234 final Callbacks oldCallbacks = mCallbacks.get();
2235 if (oldCallbacks == null) {
2236 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002237 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002238 return;
2239 }
2240
Kenny Guyed131872014-04-30 03:02:21 +01002241 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2242
Winson Chung64359a52013-07-08 17:17:08 -07002243 // Clear the list of apps
2244 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002245 for (UserHandleCompat user : profiles) {
2246 // Query for the set of apps
2247 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002248 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002249 if (DEBUG_LOADERS) {
2250 Log.d(TAG, "getActivityList took "
2251 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2252 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2253 }
2254 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002255 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002256 if (apps == null || apps.isEmpty()) {
2257 return;
2258 }
Kenny Guyff05f432016-01-22 17:48:29 +00002259 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002260 // Create the ApplicationInfos
2261 for (int i = 0; i < apps.size(); i++) {
2262 LauncherActivityInfoCompat app = apps.get(i);
2263 // This builds the icon bitmaps.
Kenny Guyff05f432016-01-22 17:48:29 +00002264 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode));
Kenny Guyed131872014-04-30 03:02:21 +01002265 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002266
Sunny Goyal756a28a2015-04-23 17:07:55 -07002267 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2268 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002269 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002270
2271 @Override
2272 public void run() {
2273 heuristic.processUserApps(apps);
2274 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002275 };
2276 runOnMainThread(new Runnable() {
2277
2278 @Override
2279 public void run() {
2280 // Check isLoadingWorkspace on the UI thread, as it is updated on
2281 // the UI thread.
2282 if (mIsLoadingAndBindingWorkspace) {
2283 synchronized (mBindCompleteRunnables) {
2284 mBindCompleteRunnables.add(r);
2285 }
2286 } else {
2287 runOnWorkerThread(r);
2288 }
2289 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002290 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002291 }
Winson Chung64359a52013-07-08 17:17:08 -07002292 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002293 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002294 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2295 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002296
2297 // Post callback on main thread
2298 mHandler.post(new Runnable() {
2299 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002300
Winson Chung64359a52013-07-08 17:17:08 -07002301 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002302 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002303 if (callbacks != null) {
2304 callbacks.bindAllApplications(added);
2305 if (DEBUG_LOADERS) {
2306 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002307 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07002308 }
2309 } else {
2310 Log.i(TAG, "not binding apps: no Launcher activity");
2311 }
2312 }
2313 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002314 // Cleanup any data stored for a deleted user.
2315 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04002316 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002317 Log.d(TAG, "Icons processed in "
2318 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002319 }
2320 }
2321
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002322 private void loadAndBindDeepShortcuts() {
2323 if (DEBUG_LOADERS) {
2324 Log.d(TAG, "loadAndBindDeepShortcuts mDeepShortcutsLoaded=" + mDeepShortcutsLoaded);
2325 }
2326 if (!mDeepShortcutsLoaded) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07002327 sBgDataModel.deepShortcutMap.clear();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002328 mHasShortcutHostPermission = mDeepShortcutManager.hasHostPermission();
2329 if (mHasShortcutHostPermission) {
2330 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
2331 if (mUserManager.isUserUnlocked(user)) {
2332 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager
2333 .queryForAllShortcuts(user);
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07002334 sBgDataModel.updateDeepShortcutMap(null, user, shortcuts);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002335 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002336 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002337 }
2338 synchronized (LoaderTask.this) {
2339 if (mStopped) {
2340 return;
2341 }
2342 mDeepShortcutsLoaded = true;
2343 }
2344 }
Tony Wickham80f57872016-06-29 18:12:15 -07002345 bindDeepShortcuts();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002346 }
2347
Joe Onoratobe386092009-11-17 17:32:16 -08002348 public void dumpState() {
Sunny Goyale9956a72016-09-01 17:24:47 -07002349 synchronized (sBgDataModel) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002350 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002351 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2352 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
Sunny Goyale9956a72016-09-01 17:24:47 -07002353 Log.d(TAG, "mItems size=" + sBgDataModel.workspaceItems.size());
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002354 }
Joe Onorato36115782010-06-17 13:28:48 -04002355 }
2356 }
2357
Tony Wickham80f57872016-06-29 18:12:15 -07002358 public void bindDeepShortcuts() {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07002359 final MultiHashMap<ComponentKey, String> shortcutMapCopy =
2360 sBgDataModel.deepShortcutMap.clone();
Tony Wickham80f57872016-06-29 18:12:15 -07002361 Runnable r = new Runnable() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002362 @Override
2363 public void run() {
2364 Callbacks callbacks = getCallback();
2365 if (callbacks != null) {
2366 callbacks.bindDeepShortcutMap(shortcutMapCopy);
2367 }
2368 }
Tony Wickham80f57872016-06-29 18:12:15 -07002369 };
2370 runOnMainThread(r);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002371 }
2372
Sunny Goyal75b0f552015-05-20 21:57:06 -07002373 /**
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002374 * Refreshes the cached shortcuts if the shortcut permission has changed.
2375 * Current implementation simply reloads the workspace, but it can be optimized to
2376 * use partial updates similar to {@link UserManagerCompat}
2377 */
2378 public void refreshShortcutsIfRequired() {
Sunny Goyalf5e37442016-11-02 10:31:24 -07002379 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002380 sWorker.removeCallbacks(mShortcutPermissionCheckRunnable);
2381 sWorker.post(mShortcutPermissionCheckRunnable);
2382 }
2383 }
2384
2385 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -07002386 * Called when the icons for packages have been updated in the icon cache.
2387 */
2388 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
Sunny Goyal75b0f552015-05-20 21:57:06 -07002389 // If any package icon has changed (app was updated while launcher was dead),
2390 // update the corresponding shortcuts.
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002391 enqueueModelUpdateTask(new CacheDataUpdatedTask(
2392 CacheDataUpdatedTask.OP_CACHE_UPDATE, user, updatedPackages));
Sunny Goyal75b0f552015-05-20 21:57:06 -07002393 }
2394
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002395 void enqueueModelUpdateTask(BaseModelUpdateTask task) {
2396 task.init(this);
2397 runOnWorkerThread(task);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002398 }
2399
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002400 /**
2401 * A task to be executed on the current callbacks on the UI thread.
2402 * If there is no current callbacks, the task is ignored.
2403 */
2404 public interface CallbackTask {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002405
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002406 void execute(Callbacks callbacks);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002407 }
2408
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002409 /**
2410 * A runnable which changes/updates the data model of the launcher based on certain events.
2411 */
2412 public static abstract class BaseModelUpdateTask implements Runnable {
Joe Onorato36115782010-06-17 13:28:48 -04002413
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002414 private LauncherModel mModel;
2415 private DeferredHandler mUiHandler;
Joe Onorato36115782010-06-17 13:28:48 -04002416
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002417 /* package private */
2418 void init(LauncherModel model) {
2419 mModel = model;
2420 mUiHandler = mModel.mHandler;
Joe Onorato36115782010-06-17 13:28:48 -04002421 }
2422
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002423 @Override
Joe Onorato36115782010-06-17 13:28:48 -04002424 public void run() {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002425 if (!mModel.mHasLoaderCompletedOnce) {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002426 // Loader has not yet run.
2427 return;
2428 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002429 execute(mModel.mApp, sBgDataModel, mModel.mBgAllAppsList);
2430 }
Joe Onorato36115782010-06-17 13:28:48 -04002431
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002432 /**
2433 * Execute the actual task. Called on the worker thread.
2434 */
2435 public abstract void execute(
2436 LauncherAppState app, BgDataModel dataModel, AllAppsList apps);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002437
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002438 /**
2439 * Schedules a {@param task} to be executed on the current callbacks.
2440 */
2441 public final void scheduleCallbackTask(final CallbackTask task) {
2442 final Callbacks callbacks = mModel.getCallback();
2443 mUiHandler.post(new Runnable() {
2444 public void run() {
2445 Callbacks cb = mModel.getCallback();
2446 if (callbacks == cb && cb != null) {
2447 task.execute(callbacks);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002448 }
2449 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002450 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002451 }
2452 }
2453
Sunny Goyal10923b32016-07-20 15:42:44 -07002454 /**
2455 * Repopulates the shortcut info, possibly updating any icon already on the workspace.
2456 */
2457 public void updateShortcutInfo(final ShortcutInfoCompat fullDetail, final ShortcutInfo info) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002458 enqueueModelUpdateTask(new ExtendedModelTask() {
Sunny Goyal10923b32016-07-20 15:42:44 -07002459 @Override
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002460 public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
2461 info.updateFromDeepShortcutInfo(fullDetail, app.getContext());
2462
2463 ArrayList<ShortcutInfo> update = new ArrayList<>();
Sunny Goyal10923b32016-07-20 15:42:44 -07002464 update.add(info);
2465 bindUpdatedShortcuts(update, fullDetail.getUserHandle());
2466 }
2467 });
2468 }
2469
Sunny Goyald164b7f2016-10-12 20:49:31 -07002470 private void bindWidgetsModel(final Callbacks callbacks) {
2471 final MultiHashMap<PackageItemInfo, WidgetItem> widgets
2472 = mBgWidgetsModel.getWidgetsMap().clone();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002473 mHandler.post(new Runnable() {
2474 @Override
2475 public void run() {
2476 Callbacks cb = getCallback();
2477 if (callbacks == cb && cb != null) {
Sunny Goyald164b7f2016-10-12 20:49:31 -07002478 callbacks.bindAllWidgets(widgets);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002479 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07002480 }
2481 });
2482 }
2483
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002484 public void refreshAndBindWidgetsAndShortcuts(
2485 final Callbacks callbacks, final boolean bindFirst) {
2486 runOnWorkerThread(new Runnable() {
2487 @Override
2488 public void run() {
2489 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
Sunny Goyald164b7f2016-10-12 20:49:31 -07002490 bindWidgetsModel(callbacks);
Sunny Goyal31860582015-09-18 08:38:57 -07002491 }
Sunny Goyald164b7f2016-10-12 20:49:31 -07002492 ArrayList<WidgetItem> allWidgets = mBgWidgetsModel.update(mApp.getContext());
2493 bindWidgetsModel(callbacks);
2494
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002495 // update the Widget entries inside DB on the worker thread.
Sunny Goyald164b7f2016-10-12 20:49:31 -07002496 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(allWidgets);
Sunny Goyal31860582015-09-18 08:38:57 -07002497 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002498 });
Michael Jurkac402cd92013-05-20 15:49:32 +02002499 }
2500
Joe Onorato9c1289c2009-08-17 11:03:03 -04002501 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05002502 * Make an ShortcutInfo object for a restored application or shortcut item that points
2503 * to a package that is not yet installed on the system.
2504 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002505 public ShortcutInfo getRestoredItemInfo(Cursor c, Intent intent,
2506 int promiseType, int itemType, CursorIconInfo iconInfo) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002507 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01002508 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002509
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002510 Bitmap icon = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002511 // the fallback icon
2512 if (icon == null) {
2513 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
2514 } else {
2515 info.setIcon(icon);
2516 }
Sunny Goyal34942622014-08-29 17:20:55 -07002517
2518 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002519 String title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07002520 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07002521 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07002522 }
Sunny Goyal34942622014-08-29 17:20:55 -07002523 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
2524 if (TextUtils.isEmpty(info.title)) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002525 info.title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07002526 }
Sunny Goyal34942622014-08-29 17:20:55 -07002527 } else {
2528 throw new InvalidParameterException("Invalid restoreType " + promiseType);
2529 }
2530
Winson Chung82b016c2015-05-08 17:00:10 -07002531 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07002532 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07002533 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002534 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05002535 return info;
2536 }
2537
2538 /**
2539 * Make an Intent object for a restored application or shortcut item that points
2540 * to the market page for the item.
2541 */
Adam Cohen091440a2015-03-18 14:16:05 -07002542 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002543 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002544 return getMarketIntent(componentName.getPackageName());
2545 }
2546
2547 static Intent getMarketIntent(String packageName) {
2548 return new Intent(Intent.ACTION_VIEW)
2549 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05002550 .scheme("market")
2551 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002552 .appendQueryParameter("id", packageName)
2553 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05002554 }
2555
2556 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002557 * Make an ShortcutInfo object for a shortcut that is an application.
2558 *
2559 * If c is not null, then it will be used to fill in missing data like the title and icon.
2560 */
Sunny Goyald09c3702016-04-06 16:18:20 -07002561 public ShortcutInfo getAppShortcutInfo(Intent intent,
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002562 UserHandleCompat user, Cursor c, CursorIconInfo iconInfo,
Sunny Goyal34b65272015-03-11 16:56:52 -07002563 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01002564 if (user == null) {
2565 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002566 return null;
2567 }
2568
Kenny Guyed131872014-04-30 03:02:21 +01002569 ComponentName componentName = intent.getComponent();
2570 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08002571 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01002572 return null;
2573 }
2574
2575 Intent newIntent = new Intent(intent.getAction(), null);
2576 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2577 newIntent.setComponent(componentName);
2578 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002579 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01002580 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
2581 return null;
2582 }
2583
2584 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07002585 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002586 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002587 Bitmap icon = iconInfo.loadIcon(c);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002588 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01002589 }
2590
Sunny Goyald09c3702016-04-06 16:18:20 -07002591 if (lai != null && PackageManagerHelper.isAppSuspended(lai.getApplicationInfo())) {
2592 info.isDisabled = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
2593 }
2594
Joe Onorato56d82912010-03-07 14:32:10 -05002595 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002596 if (TextUtils.isEmpty(info.title) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002597 info.title = iconInfo.getTitle(c);
Joe Onorato56d82912010-03-07 14:32:10 -05002598 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002599
Joe Onorato56d82912010-03-07 14:32:10 -05002600 // fall back to the class name of the activity
2601 if (info.title == null) {
2602 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002603 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002604
Joe Onorato9c1289c2009-08-17 11:03:03 -04002605 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01002606 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07002607 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002608 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002609 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07002610
Sunny Goyal1a745e82014-10-02 15:58:31 -07002611 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08002612 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002613 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002614 @Thunk ShortcutInfo getShortcutInfo(Cursor c, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002615 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01002616 // Non-app shortcuts are only supported for current user.
2617 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002618 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002619
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002620 // TODO: If there's an explicit component and we can't install that, delete it.
2621
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002622 loadInfoFromCursor(info, c, iconInfo);
2623 return info;
2624 }
Joe Onorato56d82912010-03-07 14:32:10 -05002625
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002626 /**
2627 * Make an ShortcutInfo object for a shortcut that isn't an application.
2628 */
2629 public void loadInfoFromCursor(ShortcutInfo info, Cursor c, CursorIconInfo iconInfo) {
2630 info.title = iconInfo.getTitle(c);
2631 Bitmap icon = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002632 // the fallback icon
2633 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01002634 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05002635 info.usingFallbackIcon = true;
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);
2672 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05002673 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08002674 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05002675
Winson Chung82b016c2015-05-08 17:00:10 -07002676 info.title = Utilities.trim(name);
2677 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002678 info.intent = intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -08002679 info.iconResource = iconResource;
2680
2681 return info;
2682 }
2683
Sunny Goyal651077b2014-06-30 14:15:31 -07002684 static boolean isValidProvider(AppWidgetProviderInfo provider) {
2685 return (provider != null) && (provider.provider != null)
2686 && (provider.provider.getPackageName() != null);
2687 }
2688
Joe Onoratobe386092009-11-17 17:32:16 -08002689 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08002690 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002691 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
2692 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
2693 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
2694 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04002695 if (mLoaderTask != null) {
2696 mLoaderTask.dumpState();
2697 } else {
2698 Log.d(TAG, "mLoaderTask=null");
2699 }
Joe Onoratobe386092009-11-17 17:32:16 -08002700 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002701
2702 public Callbacks getCallback() {
2703 return mCallbacks != null ? mCallbacks.get() : null;
2704 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002705
2706 /**
2707 * @return {@link FolderInfo} if its already loaded.
2708 */
2709 public FolderInfo findFolderById(Long folderId) {
Sunny Goyale9956a72016-09-01 17:24:47 -07002710 synchronized (sBgDataModel) {
2711 return sBgDataModel.folders.get(folderId);
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002712 }
2713 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002714
Sunny Goyal527c7d32015-08-28 15:19:36 -07002715 @Thunk class DeferredMainThreadExecutor implements Executor {
2716
2717 @Override
2718 public void execute(Runnable command) {
2719 runOnMainThread(command);
2720 }
2721 }
2722
Sunny Goyal756adbc2015-04-16 15:20:51 -07002723 /**
2724 * @return the looper for the worker thread which can be used to start background tasks.
2725 */
2726 public static Looper getWorkerLooper() {
2727 return sWorkerThread.getLooper();
2728 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002729}