blob: 144330a98da71400afb45856d8e76969ba83073c [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) {
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530158 boolean hasShortcutHostPermission =
159 DeepShortcutManager.getInstance(mApp.getContext()).hasHostPermission();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700160 if (hasShortcutHostPermission != mHasShortcutHostPermission) {
161 mApp.reloadWorkspace();
162 }
163 }
164 }
165 };
166
Sunny Goyale9956a72016-09-01 17:24:47 -0700167 /**
168 * All the static data should be accessed on the background thread, A lock should be acquired
169 * on this object when accessing any data from this model.
170 */
171 static final BgDataModel sBgDataModel = new BgDataModel();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700172
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700173 // </ only access in worker thread >
174
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700175 private final IconCache mIconCache;
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
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530212 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800213 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400214 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100215 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Sunny Goyald164b7f2016-10-12 20:49:31 -0700216 mBgWidgetsModel = new WidgetsModel(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800217 mIconCache = iconCache;
218
Kenny Guyed131872014-04-30 03:02:21 +0100219 mLauncherApps = LauncherAppsCompat.getInstance(context);
220 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800221 }
222
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700223 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
224 * posted on the main thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700225 private void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700226 if (sWorkerThread.getThreadId() == Process.myTid()) {
227 // If we are on the worker thread, post onto the main handler
228 mHandler.post(r);
229 } else {
230 r.run();
231 }
232 }
233
234 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
235 * posted on the worker thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700236 private static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700237 if (sWorkerThread.getThreadId() == Process.myTid()) {
238 r.run();
239 } else {
240 // If we are not on the worker thread, then post to the worker handler
241 sWorker.post(r);
242 }
243 }
244
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700245 public void setPackageState(PackageInstallInfo installInfo) {
246 enqueueModelUpdateTask(new PackageInstallStateChangedTask(installInfo));
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500247 }
248
Sunny Goyal756adbc2015-04-16 15:20:51 -0700249 /**
250 * Updates the icons and label of all pending icons for the provided package name.
251 */
252 public void updateSessionDisplayInfo(final String packageName) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700253 HashSet<String> packages = new HashSet<>();
254 packages.add(packageName);
255 enqueueModelUpdateTask(new CacheDataUpdatedTask(
256 CacheDataUpdatedTask.OP_SESSION_UPDATE, UserHandleCompat.myUserHandle(), packages));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800257 }
258
259 /**
260 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800261 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700262 public void addAndBindAddedWorkspaceItems(
Sunny Goyala214a632015-05-06 12:23:34 -0700263 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700264 enqueueModelUpdateTask(new AddWorkspaceItemsTask(workspaceApps));
Winson Chung64359a52013-07-08 17:17:08 -0700265 }
266
Joe Onorato9c1289c2009-08-17 11:03:03 -0400267 /**
268 * Adds an item to the DB if it was not created previously, or move it to a new
269 * <container, screen, cellX, cellY>
270 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800271 public static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700272 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400273 if (item.container == ItemInfo.NO_ID) {
274 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700275 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400276 } else {
277 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700278 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800279 }
280 }
281
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700282 static void checkItemInfoLocked(
283 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
Sunny Goyale9956a72016-09-01 17:24:47 -0700284 ItemInfo modelItem = sBgDataModel.itemsIdMap.get(itemId);
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700285 if (modelItem != null && item != modelItem) {
286 // check all the data is consistent
287 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
288 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
289 ShortcutInfo shortcut = (ShortcutInfo) item;
290 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
291 modelShortcut.intent.filterEquals(shortcut.intent) &&
292 modelShortcut.id == shortcut.id &&
293 modelShortcut.itemType == shortcut.itemType &&
294 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700295 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700296 modelShortcut.cellX == shortcut.cellX &&
297 modelShortcut.cellY == shortcut.cellY &&
298 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700299 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700300 // For all intents and purposes, this is the same object
301 return;
302 }
303 }
304
305 // the modelItem needs to match up perfectly with item if our model is
306 // to be consistent with the database-- for now, just require
307 // modelItem == item or the equality check above
308 String msg = "item: " + ((item != null) ? item.toString() : "null") +
309 "modelItem: " +
310 ((modelItem != null) ? modelItem.toString() : "null") +
311 "Error: ItemInfo passed to checkItemInfo doesn't match original";
312 RuntimeException e = new RuntimeException(msg);
313 if (stackTrace != null) {
314 e.setStackTrace(stackTrace);
315 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800316 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700317 }
318 }
319
Michael Jurka816474f2012-06-25 14:49:02 -0700320 static void checkItemInfo(final ItemInfo item) {
321 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
322 final long itemId = item.id;
323 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700324 public void run() {
Sunny Goyale9956a72016-09-01 17:24:47 -0700325 synchronized (sBgDataModel) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700326 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700327 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700328 }
329 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700330 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700331 }
332
Michael Jurkac9d95c52011-08-29 14:03:34 -0700333 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
334 final ItemInfo item, final String callingFunction) {
335 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700336 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700337 final ContentResolver cr = context.getContentResolver();
338
Adam Cohen487f7dd2012-06-28 18:12:10 -0700339 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700340 Runnable r = new Runnable() {
341 public void run() {
342 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700343 updateItemArrays(item, itemId, stackTrace);
344 }
345 };
346 runOnWorkerThread(r);
347 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700348
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700349 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
350 final ArrayList<ItemInfo> items, final String callingFunction) {
351 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700352
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700353 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
354 Runnable r = new Runnable() {
355 public void run() {
356 ArrayList<ContentProviderOperation> ops =
357 new ArrayList<ContentProviderOperation>();
358 int count = items.size();
359 for (int i = 0; i < count; i++) {
360 ItemInfo item = items.get(i);
361 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700362 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700363 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700364
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700365 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
366 updateItemArrays(item, itemId, stackTrace);
367
368 }
369 try {
370 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
371 } catch (Exception e) {
372 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700373 }
374 }
375 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700376 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700377 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700378
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700379 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
380 // Lock on mBgLock *after* the db operation
Sunny Goyale9956a72016-09-01 17:24:47 -0700381 synchronized (sBgDataModel) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700382 checkItemInfoLocked(itemId, item, stackTrace);
383
384 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
385 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
386 // Item is in a folder, make sure this folder exists
Sunny Goyale9956a72016-09-01 17:24:47 -0700387 if (!sBgDataModel.folders.containsKey(item.container)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700388 // An items container is being set to a that of an item which is not in
389 // the list of Folders.
390 String msg = "item: " + item + " container being set to: " +
391 item.container + ", not in the list of folders";
392 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700393 }
394 }
395
396 // Items are added/removed from the corresponding FolderInfo elsewhere, such
397 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
398 // that are on the desktop, as appropriate
Sunny Goyale9956a72016-09-01 17:24:47 -0700399 ItemInfo modelItem = sBgDataModel.itemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800400 if (modelItem != null &&
401 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
402 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700403 switch (modelItem.itemType) {
404 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
405 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700406 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700407 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyale9956a72016-09-01 17:24:47 -0700408 if (!sBgDataModel.workspaceItems.contains(modelItem)) {
409 sBgDataModel.workspaceItems.add(modelItem);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700410 }
411 break;
412 default:
413 break;
414 }
415 } else {
Sunny Goyale9956a72016-09-01 17:24:47 -0700416 sBgDataModel.workspaceItems.remove(modelItem);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700417 }
418 }
419 }
420
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800421 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400422 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700423 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700424 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700425 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400426 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400427 item.cellX = cellX;
428 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700429
Winson Chung3d503fb2011-07-13 17:25:49 -0700430 // We store hotseat items in canonical form which is this orientation invariant position
431 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700432 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700433 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700434 item.screenId = Launcher.getLauncher(context).getHotseat()
435 .getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700436 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700437 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700438 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400439
440 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400441 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700442 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
443 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800444 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700445 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400446
Michael Jurkac9d95c52011-08-29 14:03:34 -0700447 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700448 }
449
450 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700451 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
452 * cellX, cellY have already been updated on the ItemInfos.
453 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800454 public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700455 final long container, final int screen) {
456
457 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
458 int count = items.size();
459
460 for (int i = 0; i < count; i++) {
461 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700462 item.container = container;
463
464 // We store hotseat items in canonical form which is this orientation invariant position
465 // in the hotseat
466 if (context instanceof Launcher && screen < 0 &&
467 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700468 item.screenId = Launcher.getLauncher(context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700469 item.cellY);
470 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700471 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700472 }
473
474 final ContentValues values = new ContentValues();
475 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
476 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
477 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800478 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700479 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700480
481 contentValues.add(values);
482 }
483 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
484 }
485
486 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700487 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800488 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700489 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700490 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700491 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800492 item.cellX = cellX;
493 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700494 item.spanX = spanX;
495 item.spanY = spanY;
496
497 // We store hotseat items in canonical form which is this orientation invariant position
498 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700499 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700500 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700501 item.screenId = Launcher.getLauncher(context).getHotseat()
502 .getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700503 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700504 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700505 }
Adam Cohend4844c32011-02-18 19:25:06 -0800506
Adam Cohend4844c32011-02-18 19:25:06 -0800507 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800508 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700509 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
510 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800511 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700512 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
513 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700514 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800515
Michael Jurka816474f2012-06-25 14:49:02 -0700516 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700517 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700518
519 /**
520 * Update an item to the database in a specified container.
521 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700522 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700523 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100524 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700525 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800526 }
527
Joe Onorato9c1289c2009-08-17 11:03:03 -0400528 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400529 * Add an item to the database in a specified container. Sets the container, screen, cellX and
530 * cellY fields of the item. Also assigns an ID to the item.
531 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700532 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700533 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400534 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400535 item.cellX = cellX;
536 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700537 // We store hotseat items in canonical form which is this orientation invariant position
538 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700539 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700540 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700541 item.screenId = Launcher.getLauncher(context).getHotseat()
542 .getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700543 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700544 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700545 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400546
547 final ContentValues values = new ContentValues();
548 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100549 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400550
Sunny Goyald2497482015-09-22 18:24:19 -0700551 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
552 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
553
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700554 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700555
Jason Monk8e19cf22014-03-20 15:06:57 -0400556 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700557 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700558 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700559 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400560
Sunny Goyale9956a72016-09-01 17:24:47 -0700561 synchronized (sBgDataModel) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400562 checkItemInfoLocked(item.id, item, stackTrace);
Sunny Goyale9956a72016-09-01 17:24:47 -0700563 sBgDataModel.addItem(item, true);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700564 }
565 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700566 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700567 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700568 }
569
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700570 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700571 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -0400572 */
Sunny Goyalfa401a12015-04-10 13:45:42 -0700573 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyal40452cf2016-09-01 15:17:46 -0700574 ArrayList<ItemInfo> items = new ArrayList<>();
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700575 items.add(item);
576 deleteItemsFromDatabase(context, items);
577 }
578
579 /**
Sunny Goyal40452cf2016-09-01 15:17:46 -0700580 * Removes all the items from the database matching {@param matcher}.
581 */
582 public static void deleteItemsFromDatabase(Context context, ItemInfoMatcher matcher) {
Sunny Goyale9956a72016-09-01 17:24:47 -0700583 deleteItemsFromDatabase(context, matcher.filterItemInfos(sBgDataModel.itemsIdMap));
Sunny Goyal40452cf2016-09-01 15:17:46 -0700584 }
585
586 /**
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700587 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700588 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700589 public static void deleteItemsFromDatabase(Context context,
590 final Iterable<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400591 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -0700592 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700593 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700594 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700595 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700596 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700597
Sunny Goyale9956a72016-09-01 17:24:47 -0700598 sBgDataModel.removeItem(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700599 }
600 }
Michael Jurka83df1882011-08-31 20:59:26 -0700601 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700602 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400603 }
604
605 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700606 * Update the order of the workspace screens in the database. The array list contains
607 * a list of screen ids in the order that they should appear.
608 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700609 public static void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -0700610 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700611 final ContentResolver cr = context.getContentResolver();
612 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
613
614 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700615 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700616 while (iter.hasNext()) {
617 long id = iter.next();
618 if (id < 0) {
619 iter.remove();
620 }
621 }
622
623 Runnable r = new Runnable() {
624 @Override
625 public void run() {
Yura085c8532014-02-11 15:15:29 +0000626 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700627 // Clear the table
Yura085c8532014-02-11 15:15:29 +0000628 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -0700629 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700630 for (int i = 0; i < count; i++) {
631 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700632 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700633 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
634 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +0000635 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -0700636 }
Yura085c8532014-02-11 15:15:29 +0000637
638 try {
639 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
640 } catch (Exception ex) {
641 throw new RuntimeException(ex);
642 }
Winson Chung9e6a0a22013-08-27 11:58:12 -0700643
Sunny Goyale9956a72016-09-01 17:24:47 -0700644 synchronized (sBgDataModel) {
645 sBgDataModel.workspaceScreens.clear();
646 sBgDataModel.workspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -0700647 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700648 }
649 };
650 runOnWorkerThread(r);
651 }
652
653 /**
Winsonc0b52fe2015-09-09 16:38:15 -0700654 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -0400655 */
Winsonc0b52fe2015-09-09 16:38:15 -0700656 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400657 final ContentResolver cr = context.getContentResolver();
658
Michael Jurkac9d95c52011-08-29 14:03:34 -0700659 Runnable r = new Runnable() {
660 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700661 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -0700662 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Sunny Goyale9956a72016-09-01 17:24:47 -0700663 sBgDataModel.removeItem(info.contents);
664 info.contents.clear();
665
666 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
667 sBgDataModel.removeItem(info);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700668 }
669 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700670 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400671 }
672
673 /**
674 * Set this as the current Launcher activity object for the loader.
675 */
676 public void initialize(Callbacks callbacks) {
677 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -0700678 Preconditions.assertUIThread();
679 // Remove any queued UI runnables
680 mHandler.cancelAll();
681 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400682 }
683 }
684
Kenny Guyed131872014-04-30 03:02:21 +0100685 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +0100686 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +0100687 int op = PackageUpdatedTask.OP_UPDATE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700688 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100689 }
690
691 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +0100692 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Sunny Goyalc2936bc2016-09-01 15:50:36 -0700693 onPackagesRemoved(user, packageName);
694 }
695
696 public void onPackagesRemoved(UserHandleCompat user, String... packages) {
Kenny Guyed131872014-04-30 03:02:21 +0100697 int op = PackageUpdatedTask.OP_REMOVE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700698 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packages));
Kenny Guyed131872014-04-30 03:02:21 +0100699 }
700
701 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +0100702 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +0100703 int op = PackageUpdatedTask.OP_ADD;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700704 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100705 }
706
707 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +0100708 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +0100709 boolean replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700710 enqueueModelUpdateTask(
711 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100712 }
713
714 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +0100715 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +0100716 boolean replacing) {
717 if (!replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700718 enqueueModelUpdateTask(new PackageUpdatedTask(
719 PackageUpdatedTask.OP_UNAVAILABLE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100720 }
Kenny Guyed131872014-04-30 03:02:21 +0100721 }
722
Kenny Guy44cba692016-01-21 19:50:02 +0000723 @Override
724 public void onPackagesSuspended(String[] packageNames, UserHandleCompat user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700725 enqueueModelUpdateTask(new PackageUpdatedTask(
726 PackageUpdatedTask.OP_SUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000727 }
728
729 @Override
730 public void onPackagesUnsuspended(String[] packageNames, UserHandleCompat user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700731 enqueueModelUpdateTask(new PackageUpdatedTask(
732 PackageUpdatedTask.OP_UNSUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000733 }
734
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700735 @Override
736 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
737 UserHandleCompat user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700738 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, true));
Sunny Goyal50941fb2016-08-04 12:03:52 -0700739 }
740
741 public void updatePinnedShortcuts(String packageName, List<ShortcutInfoCompat> shortcuts,
742 UserHandleCompat user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700743 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, false));
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700744 }
745
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700746 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400747 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
748 * ACTION_PACKAGE_CHANGED.
749 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100750 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400751 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -0400752 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700753
Joe Onorato36115782010-06-17 13:28:48 -0400754 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +0100755 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -0700756 // If we have changed locale we need to clear out the labels in all apps/workspace.
757 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700758 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
759 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -0700760 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -0700761 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700762 } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
763 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
764 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyalda891c12016-03-18 18:29:24 -0700765 UserHandleCompat user = UserHandleCompat.fromIntent(intent);
766 if (user != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700767 if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
768 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700769 enqueueModelUpdateTask(new PackageUpdatedTask(
770 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE, user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700771 }
772
773 // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so
774 // we need to run the state change task again.
775 if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
776 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700777 enqueueModelUpdateTask(new UserLockStateChangedTask(user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700778 }
Sunny Goyalda891c12016-03-18 18:29:24 -0700779 }
Tony Wickham827cef22016-03-17 15:39:39 -0700780 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
781 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700782 }
783 }
784
Amith Yamasani6cc806d2014-05-02 13:47:11 -0700785 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700786 resetLoadedState(true, true);
787
Reena Lee93f824a2011-09-23 17:20:28 -0700788 // Do this here because if the launcher activity is running it will be restarted.
789 // If it's not running startLoaderFromBackground will merely tell it that it needs
790 // to reload.
791 startLoaderFromBackground();
792 }
793
Winson Chungf0c6ae02012-03-21 16:10:31 -0700794 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
795 synchronized (mLock) {
796 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
797 // mWorkspaceLoaded to true later
798 stopLoaderLocked();
799 if (resetAllAppsLoaded) mAllAppsLoaded = false;
800 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700801 // Always reset deep shortcuts loaded.
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700802 // TODO: why?
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700803 mDeepShortcutsLoaded = false;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700804 }
805 }
806
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700807 /**
808 * When the launcher is in the background, it's possible for it to miss paired
809 * configuration changes. So whenever we trigger the loader from the background
810 * tell the launcher that it needs to re-run the loader when it comes back instead
811 * of doing it now.
812 */
813 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800814 Callbacks callbacks = getCallback();
815 if (callbacks != null) {
816 // Only actually run the loader if they're not paused.
817 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -0700818 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700819 }
820 }
Joe Onorato36115782010-06-17 13:28:48 -0400821 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400822
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700823 /**
824 * If there is already a loader task running, tell it to stop.
825 */
826 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -0700827 LoaderTask oldTask = mLoaderTask;
828 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -0700829 oldTask.stopLocked();
830 }
Reena Lee93f824a2011-09-23 17:20:28 -0700831 }
832
Adam Cohen1a85c582014-09-30 09:48:49 -0700833 public boolean isCurrentCallbacks(Callbacks callbacks) {
834 return (mCallbacks != null && mCallbacks.get() == callbacks);
835 }
836
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700837 /**
838 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
839 * @return true if the page could be bound synchronously.
840 */
841 public boolean startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700842 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
843 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -0400844 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -0400845 // Don't bother to start the thread if we know it's not going to do anything
846 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -0700847 final Callbacks oldCallbacks = mCallbacks.get();
848 // Clear any pending bind-runnables from the synchronized load process.
849 runOnMainThread(new Runnable() {
850 public void run() {
851 oldCallbacks.clearPendingBinds();
852 }
853 });
854
Joe Onorato36115782010-06-17 13:28:48 -0400855 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700856 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700857 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700858 // TODO: mDeepShortcutsLoaded does not need to be true for synchronous bind.
Tony Wickham80f57872016-06-29 18:12:15 -0700859 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE && mAllAppsLoaded
860 && mWorkspaceLoaded && mDeepShortcutsLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700861 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700862 return true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700863 } else {
864 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
865 sWorker.post(mLoaderTask);
866 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400867 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400868 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700869 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400870 }
871
Joe Onorato36115782010-06-17 13:28:48 -0400872 public void stopLoader() {
873 synchronized (mLock) {
874 if (mLoaderTask != null) {
875 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400876 }
877 }
Joe Onorato36115782010-06-17 13:28:48 -0400878 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400879
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800880 /**
881 * Loads the workspace screen ids in an ordered list.
882 */
Sunny Goyale5bb7052015-07-27 14:36:07 -0700883 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -0700884 final ContentResolver contentResolver = context.getContentResolver();
885 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -0700886
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800887 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -0700888 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
889 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -0700890 }
891
Joe Onorato36115782010-06-17 13:28:48 -0400892 /**
893 * Runnable for the thread that loads the contents of the launcher:
894 * - workspace icons
895 * - widgets
896 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700897 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -0400898 */
899 private class LoaderTask implements Runnable {
900 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -0700901 private int mPageToBindFirst;
902
Adam Cohen091440a2015-03-18 14:16:05 -0700903 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -0400904 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -0700905 @Thunk boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700906
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700907 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -0400908 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -0700909 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400910 }
911
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800912 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700913 mIsLoadingAndBindingWorkspace = true;
914
Joe Onorato36115782010-06-17 13:28:48 -0400915 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -0400916 if (DEBUG_LOADERS) {
917 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400918 }
Michael Jurka288a36b2011-07-12 16:53:48 -0700919
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700920 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800921 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -0700922 synchronized (LoaderTask.this) {
923 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800924 return;
Reena Lee93f824a2011-09-23 17:20:28 -0700925 }
926 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400927 }
928 }
929
Joe Onorato36115782010-06-17 13:28:48 -0400930 // Bind the workspace
Sunny Goyal93f878c2016-03-30 17:31:24 -0700931 bindWorkspace(mPageToBindFirst);
Joe Onorato36115782010-06-17 13:28:48 -0400932 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400933
Joe Onorato36115782010-06-17 13:28:48 -0400934 private void waitForIdle() {
935 // Wait until the either we're stopped or the other threads are done.
936 // This way we don't start loading all apps until the workspace has settled
937 // down.
938 synchronized (LoaderTask.this) {
939 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700940
Joe Onorato36115782010-06-17 13:28:48 -0400941 mHandler.postIdle(new Runnable() {
942 public void run() {
943 synchronized (LoaderTask.this) {
944 mLoadAndBindStepFinished = true;
945 if (DEBUG_LOADERS) {
946 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400947 }
Joe Onorato36115782010-06-17 13:28:48 -0400948 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400949 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400950 }
Joe Onorato36115782010-06-17 13:28:48 -0400951 });
952
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800953 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -0400954 try {
Michael Jurkac7700af2013-05-14 20:17:58 +0200955 // Just in case mFlushingWorkerThread changes but we aren't woken up,
956 // wait no longer than 1sec at a time
957 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -0400958 } catch (InterruptedException ex) {
959 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400960 }
961 }
Joe Onorato36115782010-06-17 13:28:48 -0400962 if (DEBUG_LOADERS) {
963 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700964 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400965 + "ms for previous step to finish binding");
966 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400967 }
Joe Onorato36115782010-06-17 13:28:48 -0400968 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400969
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700970 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -0500971 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700972 // Ensure that we have a valid page index to load synchronously
973 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
974 "valid page index");
975 }
976 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
977 // Ensure that we don't try and bind a specified page when the pages have not been
978 // loaded already (we should load everything asynchronously in that case)
979 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
980 }
981 synchronized (mLock) {
982 if (mIsLoaderTaskRunning) {
983 // Ensure that we are never running the background loading at this point since
984 // we also touch the background collections
985 throw new RuntimeException("Error! Background loading is already running");
986 }
987 }
988
989 // XXX: Throw an exception if we are already loading (since we touch the worker thread
990 // data structures, we can't allow any other thread to touch that data, but because
991 // this call is synchronous, we can get away with not locking).
992
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400993 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -0700994 // operations from the previous activity. We need to ensure that all queued operations
995 // are executed before any synchronous binding work is done.
996 mHandler.flush();
997
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700998 // Divide the set of loaded items into those that we are binding synchronously, and
999 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001000 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001001 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1002 // arise from that.
1003 onlyBindAllApps();
Tony Wickham80f57872016-06-29 18:12:15 -07001004
1005 bindDeepShortcuts();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001006 }
1007
Joe Onorato36115782010-06-17 13:28:48 -04001008 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001009 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001010 if (mStopped) {
1011 return;
1012 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001013 mIsLoaderTaskRunning = true;
1014 }
Joe Onorato36115782010-06-17 13:28:48 -04001015 // Optimize for end-user experience: if the Launcher is up and // running with the
1016 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1017 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001018 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001019 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001020 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001021
Joe Onorato36115782010-06-17 13:28:48 -04001022 if (mStopped) {
1023 break keep_running;
1024 }
1025
Joe Onorato36115782010-06-17 13:28:48 -04001026 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001027
1028 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001029 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1030 loadAndBindAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001031
1032 waitForIdle();
1033
1034 // third step
1035 if (DEBUG_LOADERS) Log.d(TAG, "step 3: loading deep shortcuts");
1036 loadAndBindDeepShortcuts();
Joe Onorato36115782010-06-17 13:28:48 -04001037 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001038
Joe Onorato36115782010-06-17 13:28:48 -04001039 // Clear out this reference, otherwise we end up holding it until all of the
1040 // callback runnables are done.
1041 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001042
Joe Onorato36115782010-06-17 13:28:48 -04001043 synchronized (mLock) {
1044 // If we are still the last one to be scheduled, remove ourselves.
1045 if (mLoaderTask == this) {
1046 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001047 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001048 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001049 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001050 }
Joe Onorato36115782010-06-17 13:28:48 -04001051 }
1052
1053 public void stopLocked() {
1054 synchronized (LoaderTask.this) {
1055 mStopped = true;
1056 this.notify();
1057 }
1058 }
1059
1060 /**
1061 * Gets the callbacks object. If we've been stopped, or if the launcher object
1062 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1063 * object that was around when the deferred message was scheduled, and if there's
1064 * a new Callbacks object around then also return null. This will save us from
1065 * calling onto it with data that will be ignored.
1066 */
1067 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1068 synchronized (mLock) {
1069 if (mStopped) {
1070 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001071 }
Joe Onorato36115782010-06-17 13:28:48 -04001072
1073 if (mCallbacks == null) {
1074 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001075 }
Joe Onorato36115782010-06-17 13:28:48 -04001076
1077 final Callbacks callbacks = mCallbacks.get();
1078 if (callbacks != oldCallbacks) {
1079 return null;
1080 }
1081 if (callbacks == null) {
1082 Log.w(TAG, "no mCallbacks");
1083 return null;
1084 }
1085
1086 return callbacks;
1087 }
1088 }
1089
1090 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001091 private boolean checkItemPlacement(LongArrayMap<GridOccupancy> occupied, ItemInfo item,
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001092 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001093 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001094 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung892c74d2013-08-22 16:15:50 -07001095
Adam Cohendcd297f2013-06-18 13:13:40 -07001096 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001097 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001098 // Return early if we detect that an item is under the hotseat button
Sunny Goyalbb011da2016-06-15 15:42:29 -07001099 if (!FeatureFlags.NO_ALL_APPS_ICON &&
1100 profile.isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001101 Log.e(TAG, "Error loading shortcut into hotseat " + item
1102 + " into position (" + item.screenId + ":" + item.cellX + ","
1103 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001104 return false;
1105 }
1106
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001107 final GridOccupancy hotseatOccupancy =
Dan Sandler295ae182013-12-10 16:05:47 -05001108 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1109
Adam Cohen2e6da152015-05-06 11:42:25 -07001110 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001111 Log.e(TAG, "Error loading shortcut " + item
1112 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001113 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001114 + ")");
1115 return false;
1116 }
1117
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001118 if (hotseatOccupancy != null) {
1119 if (hotseatOccupancy.cells[(int) item.screenId][0]) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001120 Log.e(TAG, "Error loading shortcut into hotseat " + item
1121 + " into position (" + item.screenId + ":" + item.cellX + ","
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001122 + item.cellY + ") already occupied");
Adam Cohendcd297f2013-06-18 13:13:40 -07001123 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001124 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001125 hotseatOccupancy.cells[(int) item.screenId][0] = true;
Dan Sandler295ae182013-12-10 16:05:47 -05001126 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001127 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001128 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001129 final GridOccupancy occupancy = new GridOccupancy(profile.numHotseatIcons, 1);
1130 occupancy.cells[(int) item.screenId][0] = true;
1131 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, occupancy);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001132 return true;
1133 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001134 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1135 if (!workspaceScreens.contains((Long) item.screenId)) {
1136 // The item has an invalid screen id.
1137 return false;
1138 }
1139 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001140 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001141 return true;
1142 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001143
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001144 final int countX = profile.numColumns;
1145 final int countY = profile.numRows;
Adam Cohenae4409d2013-11-26 10:34:59 -08001146 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1147 item.cellX < 0 || item.cellY < 0 ||
1148 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1149 Log.e(TAG, "Error loading shortcut " + item
1150 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1151 + item.cellX + "," + item.cellY
1152 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1153 return false;
1154 }
1155
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001156 if (!occupied.containsKey(item.screenId)) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001157 GridOccupancy screen = new GridOccupancy(countX + 1, countY + 1);
1158 if (item.screenId == Workspace.FIRST_SCREEN_ID) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001159 // Mark the first row as occupied (if the feature is enabled)
1160 // in order to account for the QSB.
1161 screen.markCells(0, 0, countX + 1, 1, FeatureFlags.QSB_ON_FIRST_SCREEN);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001162 }
1163 occupied.put(item.screenId, screen);
Joe Onorato36115782010-06-17 13:28:48 -04001164 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001165 final GridOccupancy occupancy = occupied.get(item.screenId);
Winson Chungf30ad5f2011-08-08 10:55:42 -07001166
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001167 // Check if any workspace icons overlap with each other
1168 if (occupancy.isRegionVacant(item.cellX, item.cellY, item.spanX, item.spanY)) {
1169 occupancy.markCells(item, true);
1170 return true;
1171 } else {
1172 Log.e(TAG, "Error loading shortcut " + item
1173 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1174 + item.cellX + "," + item.cellX + "," + item.spanX + "," + item.spanY
1175 + ") already occupied");
1176 return false;
1177 }
Joe Onorato36115782010-06-17 13:28:48 -04001178 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001179
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001180 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -07001181 if (LauncherAppState.PROFILE_STARTUP) {
1182 Trace.beginSection("Loading Workspace");
1183 }
Joe Onorato36115782010-06-17 13:28:48 -04001184 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001185
Joe Onorato36115782010-06-17 13:28:48 -04001186 final Context context = mContext;
1187 final ContentResolver contentResolver = context.getContentResolver();
1188 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001189 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001190 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301191 final DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001192 final boolean isSdCardReady = Utilities.isBootCompleted();
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001193 final MultiHashMap<UserHandleCompat, String> pendingPackages = new MultiHashMap<>();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001194
Winson Chung892c74d2013-08-22 16:15:50 -07001195 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001196 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001197 int countX = profile.numColumns;
1198 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001199
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001200 boolean clearDb = false;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -07001201 try {
1202 ImportDataTask.performImportIfPossible(context);
1203 } catch (Exception e) {
1204 // Migration failed. Clear workspace.
1205 clearDb = true;
1206 }
1207
1208 if (!clearDb && GridSizeMigrationTask.ENABLED &&
Sunny Goyalf076eae2016-01-11 12:25:10 -08001209 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1210 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001211 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001212 }
1213
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001214 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -07001215 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001216 LauncherSettings.Settings.call(contentResolver,
1217 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001218 }
1219
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001220 Log.d(TAG, "loadWorkspace: loading default favorites");
1221 LauncherSettings.Settings.call(contentResolver,
1222 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -07001223
Sunny Goyale9956a72016-09-01 17:24:47 -07001224 synchronized (sBgDataModel) {
1225 sBgDataModel.clear();
1226
Sunny Goyal756adbc2015-04-16 15:20:51 -07001227 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001228 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyale9956a72016-09-01 17:24:47 -07001229 sBgDataModel.workspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001230
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001231 final ArrayList<Long> itemsToRemove = new ArrayList<>();
1232 final ArrayList<Long> restoredRows = new ArrayList<>();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001233 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001234 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001235 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001236 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001237
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001238 // +1 for the hotseat (it can be larger than the workspace)
1239 // Load workspace in reverse order to ensure that latest items are loaded first (and
1240 // before any earlier duplicates)
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001241 final LongArrayMap<GridOccupancy> occupied = new LongArrayMap<>();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001242 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001243
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001244 try {
1245 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1246 final int intentIndex = c.getColumnIndexOrThrow
1247 (LauncherSettings.Favorites.INTENT);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001248 final int containerIndex = c.getColumnIndexOrThrow(
1249 LauncherSettings.Favorites.CONTAINER);
1250 final int itemTypeIndex = c.getColumnIndexOrThrow(
1251 LauncherSettings.Favorites.ITEM_TYPE);
1252 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1253 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001254 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1255 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001256 final int screenIndex = c.getColumnIndexOrThrow(
1257 LauncherSettings.Favorites.SCREEN);
1258 final int cellXIndex = c.getColumnIndexOrThrow
1259 (LauncherSettings.Favorites.CELLX);
1260 final int cellYIndex = c.getColumnIndexOrThrow
1261 (LauncherSettings.Favorites.CELLY);
1262 final int spanXIndex = c.getColumnIndexOrThrow
1263 (LauncherSettings.Favorites.SPANX);
1264 final int spanYIndex = c.getColumnIndexOrThrow(
1265 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001266 final int rankIndex = c.getColumnIndexOrThrow(
1267 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001268 final int restoredIndex = c.getColumnIndexOrThrow(
1269 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001270 final int profileIdIndex = c.getColumnIndexOrThrow(
1271 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001272 final int optionsIndex = c.getColumnIndexOrThrow(
1273 LauncherSettings.Favorites.OPTIONS);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001274 final CursorIconInfo cursorIconInfo = new CursorIconInfo(mContext, c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001275
Sunny Goyal7f834d22015-04-21 10:10:23 -07001276 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
Kenny Guyff05f432016-01-22 17:48:29 +00001277 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001278 final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>();
Sunny Goyal7f834d22015-04-21 10:10:23 -07001279 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001280 long serialNo = mUserManager.getSerialNumberForUser(user);
1281 allUsers.put(serialNo, user);
1282 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001283
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001284 boolean userUnlocked = mUserManager.isUserUnlocked(user);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001285
1286 // We can only query for shortcuts when the user is unlocked.
1287 if (userUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07001288 List<ShortcutInfoCompat> pinnedShortcuts =
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301289 shortcutManager.queryForPinnedShortcuts(null, user);
1290 if (shortcutManager.wasLastCallSuccess()) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07001291 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
1292 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
1293 shortcut);
1294 }
1295 } else {
1296 // Shortcut manager can fail due to some race condition when the
1297 // lock state changes too frequently. For the purpose of the loading
1298 // shortcuts, consider the user is still locked.
1299 userUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001300 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001301 }
Sunny Goyal49f4f032016-08-01 15:45:49 -07001302 unlockedUsers.put(serialNo, userUnlocked);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001303 }
1304
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001305 ShortcutInfo info;
1306 String intentDescription;
1307 LauncherAppWidgetInfo appWidgetInfo;
1308 int container;
1309 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001310 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001311 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001312 UserHandleCompat user;
Sunny Goyald09c3702016-04-06 16:18:20 -07001313 String targetPackage;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001314
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001315 while (!mStopped && c.moveToNext()) {
1316 try {
1317 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001318 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001319 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001320 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001321
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001322 switch (itemType) {
1323 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1324 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001325 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001326 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001327 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001328 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001329 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001330 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001331 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001332 boolean itemReplaced = false;
Sunny Goyald09c3702016-04-06 16:18:20 -07001333 targetPackage = null;
Kenny Guyed131872014-04-30 03:02:21 +01001334 if (user == null) {
1335 // User has been deleted remove the item.
1336 itemsToRemove.add(id);
1337 continue;
1338 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001339 try {
1340 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001341 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001342 if (cn != null && cn.getPackageName() != null) {
1343 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1344 cn.getPackageName(), user);
1345 boolean validComponent = validPkg &&
1346 launcherApps.isActivityEnabledForProfile(cn, user);
Sunny Goyald09c3702016-04-06 16:18:20 -07001347 if (validPkg) {
1348 targetPackage = cn.getPackageName();
1349 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001350
1351 if (validComponent) {
1352 if (restored) {
1353 // no special handling necessary for this item
1354 restoredRows.add(id);
1355 restored = false;
1356 }
Kenny Guyff05f432016-01-22 17:48:29 +00001357 if (quietMode.get(serialNumber)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001358 disabledState = ShortcutInfo.FLAG_DISABLED_QUIET_USER;
Kenny Guyff05f432016-01-22 17:48:29 +00001359 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001360 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001361 intent = null;
1362 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1363 // We allow auto install apps to have their intent
1364 // updated after an install.
1365 intent = manager.getLaunchIntentForPackage(
1366 cn.getPackageName());
1367 if (intent != null) {
1368 ContentValues values = new ContentValues();
1369 values.put(LauncherSettings.Favorites.INTENT,
1370 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001371 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001372 }
1373 }
1374
1375 if (intent == null) {
1376 // The app is installed but the component is no
1377 // longer available.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001378 FileLog.d(TAG, "Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001379 itemsToRemove.add(id);
1380 continue;
1381 } else {
1382 // no special handling necessary for this item
1383 restoredRows.add(id);
1384 restored = false;
1385 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001386 } else if (restored) {
1387 // Package is not yet available but might be
1388 // installed later.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001389 FileLog.d(TAG, "package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001390
1391 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1392 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001393 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001394 // App restore has started. Update the flag
1395 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1396 ContentValues values = new ContentValues();
1397 values.put(LauncherSettings.Favorites.RESTORED,
1398 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001399 updateItem(id, values);
1400 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1401 // This is a common app. Try to replace this.
1402 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1403 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1404 if (parser.findDefaultApp()) {
1405 // Default app found. Replace it.
1406 intent = parser.parsedIntent;
1407 cn = intent.getComponent();
1408 ContentValues values = parser.parsedValues;
1409 values.put(LauncherSettings.Favorites.RESTORED, 0);
1410 updateItem(id, values);
1411 restored = false;
1412 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001413
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001414 } else {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001415 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001416 itemsToRemove.add(id);
1417 continue;
1418 }
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001419 } else {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001420 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001421 itemsToRemove.add(id);
1422 continue;
1423 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001424 } else if (PackageManagerHelper.isAppOnSdcard(
1425 manager, cn.getPackageName())) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07001426 // Package is present but not available.
1427 allowMissingTarget = true;
1428 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1429 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001430 // SdCard is not ready yet. Package might get available,
1431 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001432 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001433 pendingPackages.addToList(user, cn.getPackageName());
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001434 allowMissingTarget = true;
1435 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001436
1437 } else {
1438 // Do not wait for external media load anymore.
1439 // Log the invalid package, and remove it
Sunny Goyal713edfc2016-05-06 09:58:34 -07001440 FileLog.d(TAG, "Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001441 itemsToRemove.add(id);
1442 continue;
Winson Chungee055712013-07-30 14:46:24 -07001443 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001444 } else if (cn == null) {
1445 // For shortcuts with no component, keep them as they are
1446 restoredRows.add(id);
1447 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001448 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001449 } catch (URISyntaxException e) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001450 FileLog.d(TAG, "Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001451 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001452 continue;
1453 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001454
Sunny Goyal34b65272015-03-11 16:56:52 -07001455 boolean useLowResIcon = container >= 0 &&
1456 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1457
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001458 if (itemReplaced) {
1459 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001460 info = getAppShortcutInfo(intent, user, null,
1461 cursorIconInfo, false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001462 } else {
1463 // Don't replace items for other profiles.
1464 itemsToRemove.add(id);
1465 continue;
1466 }
1467 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001468 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001469 info = getRestoredItemInfo(c, intent,
1470 promiseType, itemType, cursorIconInfo);
Kenny Guyed131872014-04-30 03:02:21 +01001471 intent = getRestoredItemIntent(c, context, intent);
1472 } else {
1473 // Don't restore items for other profiles.
1474 itemsToRemove.add(id);
1475 continue;
1476 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001477 } else if (itemType ==
1478 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001479 info = getAppShortcutInfo(intent, user, c,
1480 cursorIconInfo, allowMissingTarget, useLowResIcon);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001481 } else if (itemType ==
1482 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001483
1484 ShortcutKey key = ShortcutKey.fromIntent(intent, user);
1485 if (unlockedUsers.get(serialNumber)) {
1486 ShortcutInfoCompat pinnedShortcut =
1487 shortcutKeyToPinnedShortcuts.get(key);
1488 if (pinnedShortcut == null) {
1489 // The shortcut is no longer valid.
1490 itemsToRemove.add(id);
1491 continue;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001492 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001493 info = new ShortcutInfo(pinnedShortcut, context);
1494 intent = info.intent;
1495 } else {
1496 // Create a shortcut info in disabled mode for now.
1497 info = new ShortcutInfo();
1498 info.user = user;
1499 info.itemType = itemType;
1500 loadInfoFromCursor(info, c, cursorIconInfo);
1501
1502 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001503 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001504 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001505 info = getShortcutInfo(c, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001506
Sunny Goyald09c3702016-04-06 16:18:20 -07001507 // Shortcuts are only available on the primary profile
1508 if (PackageManagerHelper.isAppSuspended(manager, targetPackage)) {
1509 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1510 }
1511
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001512 // App shortcuts that used to be automatically added to Launcher
1513 // didn't always have the correct intent flags set, so do that
1514 // here
1515 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001516 intent.getCategories() != null &&
1517 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001518 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001519 intent.addFlags(
1520 Intent.FLAG_ACTIVITY_NEW_TASK |
1521 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1522 }
Michael Jurka96879562012-03-22 05:54:33 -07001523 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001524
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001525 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001526 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001527 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001528 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001529 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001530 info.cellX = c.getInt(cellXIndex);
1531 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08001532 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001533 info.spanX = 1;
1534 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01001535 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001536 if (info.promisedIntent != null) {
1537 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
1538 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001539 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001540 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1541 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1542 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001543
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001544 // check & update map of what's occupied
Sunny Goyale9956a72016-09-01 17:24:47 -07001545 if (!checkItemPlacement(occupied, info, sBgDataModel.workspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001546 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001547 break;
1548 }
1549
Sunny Goyal756adbc2015-04-16 15:20:51 -07001550 if (restored) {
1551 ComponentName cn = info.getTargetComponent();
1552 if (cn != null) {
1553 Integer progress = installingPkgs.get(cn.getPackageName());
1554 if (progress != null) {
1555 info.setInstallProgress(progress);
1556 } else {
1557 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
1558 }
1559 }
1560 }
1561
Sunny Goyale9956a72016-09-01 17:24:47 -07001562 sBgDataModel.addItem(info, false);
Winson Chung1323b482013-08-05 12:41:55 -07001563 } else {
1564 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001565 }
1566 break;
1567
1568 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1569 id = c.getLong(idIndex);
Sunny Goyale9956a72016-09-01 17:24:47 -07001570 FolderInfo folderInfo = sBgDataModel.findOrMakeFolder(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001571
Sunny Goyala508e4f2015-05-21 09:33:57 -07001572 // Do not trim the folder label, as is was set by the user.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001573 folderInfo.title = c.getString(cursorIconInfo.titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001574 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001575 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001576 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001577 folderInfo.cellX = c.getInt(cellXIndex);
1578 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001579 folderInfo.spanX = 1;
1580 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001581 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001582
Daniel Sandler8802e962010-05-26 16:28:16 -04001583 // check & update map of what's occupied
Sunny Goyale9956a72016-09-01 17:24:47 -07001584 if (!checkItemPlacement(occupied, folderInfo, sBgDataModel.workspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001585 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001586 break;
1587 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001588 if (restored) {
1589 // no special handling required for restored folders
1590 restoredRows.add(id);
1591 }
1592
Sunny Goyale9956a72016-09-01 17:24:47 -07001593 sBgDataModel.addItem(folderInfo, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001594 break;
1595
1596 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08001597 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001598 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08001599 boolean customWidget = itemType ==
1600 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
1601
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001602 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001603 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001604 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001605 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001606 user = allUsers.get(serialNumber);
1607 if (user == null) {
1608 itemsToRemove.add(id);
1609 continue;
1610 }
1611
Sunny Goyalff572272014-07-23 13:58:07 -07001612 final ComponentName component =
1613 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04001614
Sunny Goyal651077b2014-06-30 14:15:31 -07001615 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07001616 final boolean isIdValid = (restoreStatus &
1617 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07001618 final boolean wasProviderReady = (restoreStatus &
1619 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07001620
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001621 if (widgetProvidersMap == null) {
1622 widgetProvidersMap = AppWidgetManagerCompat
1623 .getInstance(mContext).getAllProvidersMap();
1624 }
1625 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
1626 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00001627 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001628 user));
Sunny Goyalff572272014-07-23 13:58:07 -07001629
1630 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08001631 if (!isSafeMode && !customWidget &&
1632 wasProviderReady && !isProviderReady) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001633 FileLog.d(TAG, "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07001634 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001635 itemsToRemove.add(id);
1636 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07001637 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07001638 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1639 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08001640
Sunny Goyal53f96722015-07-13 19:54:53 -07001641 // The provider is available. So the widget is either
1642 // available or not available. We do not need to track
1643 // any future restore updates.
1644 int status = restoreStatus &
1645 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07001646 if (!wasProviderReady) {
1647 // If provider was not previously ready, update the
1648 // status and UI flag.
1649
1650 // Id would be valid only if the widget restore broadcast was received.
1651 if (isIdValid) {
Sunny Goyal86df1382016-08-10 15:03:22 -07001652 status |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07001653 } else {
1654 status &= ~LauncherAppWidgetInfo
1655 .FLAG_PROVIDER_NOT_READY;
1656 }
1657 }
1658 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07001659 } else {
1660 Log.v(TAG, "Widget restore pending id=" + id
1661 + " appWidgetId=" + appWidgetId
1662 + " status =" + restoreStatus);
1663 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07001664 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07001665 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001666 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07001667
1668 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
1669 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001670 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07001671 // App restore has started. Update the flag
1672 appWidgetInfo.restoreStatus |=
1673 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001674 } else if (!isSafeMode) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001675 FileLog.d(TAG, "Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07001676 itemsToRemove.add(id);
1677 continue;
1678 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07001679
1680 appWidgetInfo.installProgress =
1681 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07001682 }
Sunny Goyal86df1382016-08-10 15:03:22 -07001683 if (appWidgetInfo.hasRestoreFlag(
1684 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG)) {
1685 intentDescription = c.getString(intentIndex);
1686 if (!TextUtils.isEmpty(intentDescription)) {
1687 appWidgetInfo.bindOptions =
1688 Intent.parseUri(intentDescription, 0);
1689 }
1690 }
Sunny Goyalff572272014-07-23 13:58:07 -07001691
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001692 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07001693 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001694 appWidgetInfo.cellX = c.getInt(cellXIndex);
1695 appWidgetInfo.cellY = c.getInt(cellYIndex);
1696 appWidgetInfo.spanX = c.getInt(spanXIndex);
1697 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07001698 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04001699
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001700 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1701 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1702 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001703 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1704 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001705 continue;
1706 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001707
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001708 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001709 // check & update map of what's occupied
Sunny Goyale9956a72016-09-01 17:24:47 -07001710 if (!checkItemPlacement(occupied, appWidgetInfo, sBgDataModel.workspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001711 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001712 break;
1713 }
Sunny Goyal651077b2014-06-30 14:15:31 -07001714
Adam Cohen59400422014-03-05 18:07:04 -08001715 if (!customWidget) {
1716 String providerName =
1717 appWidgetInfo.providerName.flattenToString();
1718 if (!providerName.equals(savedProvider) ||
1719 (appWidgetInfo.restoreStatus != restoreStatus)) {
1720 ContentValues values = new ContentValues();
1721 values.put(
1722 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1723 providerName);
1724 values.put(LauncherSettings.Favorites.RESTORED,
1725 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001726 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08001727 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001728 }
Sunny Goyale9956a72016-09-01 17:24:47 -07001729 sBgDataModel.addItem(appWidgetInfo, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001730 }
Joe Onorato36115782010-06-17 13:28:48 -04001731 break;
1732 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001733 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001734 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001735 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001736 }
1737 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001738 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001739 }
1740
Winson Chungba9c37f2013-08-30 14:11:37 -07001741 // Break early if we've stopped loading
1742 if (mStopped) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001743 sBgDataModel.clear();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001744 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07001745 }
1746
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001747 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001748 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001749 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
1750 Utilities.createDbSelectionQuery(
1751 LauncherSettings.Favorites._ID, itemsToRemove), null);
1752 if (DEBUG_LOADERS) {
1753 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
1754 LauncherSettings.Favorites._ID, itemsToRemove));
1755 }
1756
1757 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07001758 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
1759 .call(contentResolver,
1760 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
1761 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
1762 for (long folderId : deletedFolderIds) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001763 sBgDataModel.workspaceItems.remove(sBgDataModel.folders.get(folderId));
1764 sBgDataModel.folders.remove(folderId);
1765 sBgDataModel.itemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001766 }
1767 }
1768
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001769 // Unpin shortcuts that don't exist on the workspace.
1770 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001771 MutableInt numTimesPinned = sBgDataModel.pinnedShortcutCounts.get(key);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001772 if (numTimesPinned == null || numTimesPinned.value == 0) {
1773 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301774 shortcutManager.unpinShortcut(key);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001775 }
1776 }
1777
Sunny Goyal317698b2015-07-29 11:45:41 -07001778 // Sort all the folder items and make sure the first 3 items are high resolution.
Sunny Goyale9956a72016-09-01 17:24:47 -07001779 for (FolderInfo folder : sBgDataModel.folders) {
Sunny Goyal317698b2015-07-29 11:45:41 -07001780 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
1781 int pos = 0;
1782 for (ShortcutInfo info : folder.contents) {
1783 if (info.usingLowResIcon) {
1784 info.updateIcon(mIconCache, false);
1785 }
1786 pos ++;
1787 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
1788 break;
1789 }
1790 }
1791 }
1792
Chris Wrenf4d08112014-01-16 18:13:56 -05001793 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001794 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001795 ContentValues values = new ContentValues();
1796 values.put(LauncherSettings.Favorites.RESTORED, 0);
1797 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
1798 Utilities.createDbSelectionQuery(
1799 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05001800 }
1801
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001802 if (!isSdCardReady && !pendingPackages.isEmpty()) {
1803 context.registerReceiver(
1804 new SdCardAvailableReceiver(
1805 LauncherModel.this, mContext, pendingPackages),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001806 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001807 null,
1808 sWorker);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001809 }
1810
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001811 // Remove any empty screens
Sunny Goyale9956a72016-09-01 17:24:47 -07001812 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgDataModel.workspaceScreens);
1813 for (ItemInfo item: sBgDataModel.itemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001814 long screenId = item.screenId;
1815 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1816 unusedScreens.contains(screenId)) {
1817 unusedScreens.remove(screenId);
1818 }
1819 }
1820
1821 // If there are any empty screens remove them, and update.
1822 if (unusedScreens.size() != 0) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001823 sBgDataModel.workspaceScreens.removeAll(unusedScreens);
1824 updateWorkspaceScreenOrder(context, sBgDataModel.workspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001825 }
1826
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001827 if (DEBUG_LOADERS) {
1828 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1829 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07001830 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07001831 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001832 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07001833
Sunny Goyale2df0622015-04-24 11:27:00 -07001834 for (int i = 0; i < nScreens; i++) {
1835 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07001836 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001837 line += " | ";
1838 }
Joe Onorato36115782010-06-17 13:28:48 -04001839 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001840 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04001841 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001842 }
Joe Onorato36115782010-06-17 13:28:48 -04001843 }
Sunny Goyale26d1002016-06-20 14:52:14 -07001844 if (LauncherAppState.PROFILE_STARTUP) {
1845 Trace.endSection();
1846 }
Adam Cohene25af792013-06-06 23:08:25 -07001847 }
1848
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001849 /**
1850 * Partially updates the item without any notification. Must be called on the worker thread.
1851 */
1852 private void updateItem(long itemId, ContentValues update) {
1853 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001854 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001855 update,
1856 BaseColumns._ID + "= ?",
1857 new String[]{Long.toString(itemId)});
1858 }
1859
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001860 /** Filters the set of items who are directly or indirectly (via another container) on the
1861 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08001862 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001863 ArrayList<ItemInfo> allWorkspaceItems,
1864 ArrayList<ItemInfo> currentScreenItems,
1865 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001866 // Purge any null ItemInfos
1867 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
1868 while (iter.hasNext()) {
1869 ItemInfo i = iter.next();
1870 if (i == null) {
1871 iter.remove();
1872 }
1873 }
1874
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001875 // Order the set of items by their containers first, this allows use to walk through the
1876 // list sequentially, build up a list of containers that are in the specified screen,
1877 // as well as all items in those containers.
1878 Set<Long> itemsOnScreen = new HashSet<Long>();
1879 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
1880 @Override
1881 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07001882 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001883 }
1884 });
1885 for (ItemInfo info : allWorkspaceItems) {
1886 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001887 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001888 currentScreenItems.add(info);
1889 itemsOnScreen.add(info.id);
1890 } else {
1891 otherScreenItems.add(info);
1892 }
1893 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1894 currentScreenItems.add(info);
1895 itemsOnScreen.add(info.id);
1896 } else {
1897 if (itemsOnScreen.contains(info.container)) {
1898 currentScreenItems.add(info);
1899 itemsOnScreen.add(info.id);
1900 } else {
1901 otherScreenItems.add(info);
1902 }
1903 }
1904 }
1905 }
1906
1907 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08001908 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001909 ArrayList<LauncherAppWidgetInfo> appWidgets,
1910 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
1911 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001912
1913 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001914 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001915 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08001916 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001917 currentScreenWidgets.add(widget);
1918 } else {
1919 otherScreenWidgets.add(widget);
1920 }
1921 }
1922 }
1923
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001924 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
1925 * right) */
1926 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07001927 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001928 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07001929 final int screenCols = profile.numColumns;
1930 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001931 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07001932 @Override
1933 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07001934 if (lhs.container == rhs.container) {
1935 // Within containers, order by their spatial position in that container
1936 switch ((int) lhs.container) {
1937 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
1938 long lr = (lhs.screenId * screenCellCount +
1939 lhs.cellY * screenCols + lhs.cellX);
1940 long rr = (rhs.screenId * screenCellCount +
1941 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07001942 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07001943 }
1944 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
1945 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07001946 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07001947 }
1948 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07001949 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07001950 throw new RuntimeException("Unexpected container type when " +
1951 "sorting workspace items.");
1952 }
1953 return 0;
1954 }
1955 } else {
1956 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07001957 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07001958 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001959 }
1960 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001961 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001962
Adam Cohendcd297f2013-06-18 13:13:40 -07001963 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
1964 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001965 final Runnable r = new Runnable() {
1966 @Override
1967 public void run() {
1968 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1969 if (callbacks != null) {
1970 callbacks.bindScreens(orderedScreens);
1971 }
1972 }
1973 };
Sunny Goyald33860f2015-04-23 16:02:20 -07001974 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07001975 }
1976
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001977 private void bindWorkspaceItems(final Callbacks oldCallbacks,
1978 final ArrayList<ItemInfo> workspaceItems,
1979 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07001980 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001981
1982 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07001983 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001984 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04001985 final int start = i;
1986 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001987 final Runnable r = new Runnable() {
1988 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001989 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001990 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001991 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07001992 callbacks.bindItems(workspaceItems, start, start+chunkSize,
1993 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001994 }
1995 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001996 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001997 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04001998 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001999
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002000 // Bind the widgets, one at a time
2001 N = appWidgets.size();
2002 for (int i = 0; i < N; i++) {
2003 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2004 final Runnable r = new Runnable() {
2005 public void run() {
2006 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2007 if (callbacks != null) {
2008 callbacks.bindAppWidget(widget);
2009 }
2010 }
2011 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002012 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002013 }
2014 }
2015
2016 /**
2017 * Binds all loaded data to actual views on the main thread.
2018 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002019 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002020 final long t = SystemClock.uptimeMillis();
2021 Runnable r;
2022
2023 // Don't use these two variables in any of the callback runnables.
2024 // Otherwise we hold a reference to them.
2025 final Callbacks oldCallbacks = mCallbacks.get();
2026 if (oldCallbacks == null) {
2027 // This launcher has exited and nobody bothered to tell us. Just bail.
2028 Log.w(TAG, "LoaderTask running with no launcher");
2029 return;
2030 }
2031
Winson Chung9b9fb962013-11-15 15:39:34 -08002032 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07002033 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
2034 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
2035 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002036
Sunny Goyale9956a72016-09-01 17:24:47 -07002037 synchronized (sBgDataModel) {
2038 workspaceItems.addAll(sBgDataModel.workspaceItems);
2039 appWidgets.addAll(sBgDataModel.appWidgets);
2040 orderedScreenIds.addAll(sBgDataModel.workspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002041 }
2042
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002043 final int currentScreen;
2044 {
2045 int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE
2046 ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen();
2047 if (currScreen >= orderedScreenIds.size()) {
2048 // There may be no workspace screens (just hotseat items and an empty page).
2049 currScreen = PagedView.INVALID_RESTORE_PAGE;
2050 }
2051 currentScreen = currScreen;
Winson Chung9b9fb962013-11-15 15:39:34 -08002052 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002053 final boolean validFirstPage = currentScreen >= 0;
2054 final long currentScreenId =
2055 validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID;
Winson Chung9b9fb962013-11-15 15:39:34 -08002056
Winson Chung9b9fb962013-11-15 15:39:34 -08002057 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07002058 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
2059 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
2060 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
2061 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002062
Winson Chung9b9fb962013-11-15 15:39:34 -08002063 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002064 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002065 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002066 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002067 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2068 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2069
2070 // Tell the workspace that we're about to start binding items
2071 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002072 public void run() {
2073 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2074 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002075 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002076 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002077 }
2078 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002079 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002080 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002081
Adam Cohendcd297f2013-06-18 13:13:40 -07002082 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2083
Sunny Goyal527c7d32015-08-28 15:19:36 -07002084 Executor mainExecutor = new DeferredMainThreadExecutor();
2085 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07002086 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002087
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002088 // In case of validFirstPage, only bind the first screen, and defer binding the
2089 // remaining screens after first onDraw (and an optional the fade animation whichever
2090 // happens later).
2091 // This ensures that the first screen is immediately visible (eg. during rotation)
2092 // In case of !validFirstPage, bind all pages one after other.
2093 final Executor deferredExecutor =
2094 validFirstPage ? new ViewOnDrawExecutor(mHandler) : mainExecutor;
2095
2096 mainExecutor.execute(new Runnable() {
2097 @Override
2098 public void run() {
2099 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2100 if (callbacks != null) {
2101 callbacks.finishFirstPageBind(
2102 validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null);
2103 }
2104 }
2105 });
Sunny Goyal527c7d32015-08-28 15:19:36 -07002106
Sunny Goyal44c06432016-04-02 10:56:02 -07002107 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002108
2109 // Tell the workspace that we're done binding items
2110 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002111 public void run() {
2112 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2113 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002114 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002115 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002116
Sunny Goyal639e9062015-08-19 19:17:06 -07002117 mIsLoadingAndBindingWorkspace = false;
2118
2119 // Run all the bind complete runnables after workspace is bound.
2120 if (!mBindCompleteRunnables.isEmpty()) {
2121 synchronized (mBindCompleteRunnables) {
2122 for (final Runnable r : mBindCompleteRunnables) {
2123 runOnWorkerThread(r);
2124 }
2125 mBindCompleteRunnables.clear();
2126 }
2127 }
2128
Winson Chung98e030b2012-05-07 16:01:11 -07002129 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002130 if (DEBUG_LOADERS) {
2131 Log.d(TAG, "bound workspace in "
2132 + (SystemClock.uptimeMillis()-t) + "ms");
2133 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002134
Joe Onorato36115782010-06-17 13:28:48 -04002135 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002136 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002137 deferredExecutor.execute(r);
2138
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002139 if (validFirstPage) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002140 r = new Runnable() {
2141 public void run() {
2142 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2143 if (callbacks != null) {
2144 // We are loading synchronously, which means, some of the pages will be
2145 // bound after first draw. Inform the callbacks that page binding is
2146 // not complete, and schedule the remaining pages.
2147 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2148 callbacks.onPageBoundSynchronously(currentScreen);
2149 }
2150 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2151 }
2152 }
2153 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002154 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002155 }
Joe Onorato36115782010-06-17 13:28:48 -04002156 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002157
Joe Onorato36115782010-06-17 13:28:48 -04002158 private void loadAndBindAllApps() {
2159 if (DEBUG_LOADERS) {
2160 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2161 }
2162 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002163 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002164 synchronized (LoaderTask.this) {
2165 if (mStopped) {
2166 return;
2167 }
2168 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002169 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002170 synchronized (LoaderTask.this) {
2171 if (mStopped) {
2172 return;
2173 }
2174 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002175 }
Joe Onorato36115782010-06-17 13:28:48 -04002176 } else {
2177 onlyBindAllApps();
2178 }
2179 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002180
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002181 private void updateIconCache() {
2182 // Ignore packages which have a promise icon.
2183 HashSet<String> packagesToIgnore = new HashSet<>();
Sunny Goyale9956a72016-09-01 17:24:47 -07002184 synchronized (sBgDataModel) {
2185 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002186 if (info instanceof ShortcutInfo) {
2187 ShortcutInfo si = (ShortcutInfo) info;
2188 if (si.isPromise() && si.getTargetComponent() != null) {
2189 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2190 }
2191 } else if (info instanceof LauncherAppWidgetInfo) {
2192 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2193 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2194 packagesToIgnore.add(lawi.providerName.getPackageName());
2195 }
2196 }
2197 }
2198 }
2199 mIconCache.updateDbIcons(packagesToIgnore);
2200 }
2201
Joe Onorato36115782010-06-17 13:28:48 -04002202 private void onlyBindAllApps() {
2203 final Callbacks oldCallbacks = mCallbacks.get();
2204 if (oldCallbacks == null) {
2205 // This launcher has exited and nobody bothered to tell us. Just bail.
2206 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2207 return;
2208 }
2209
2210 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002211 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002212 final ArrayList<AppInfo> list
2213 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002214 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002215 public void run() {
2216 final long t = SystemClock.uptimeMillis();
2217 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2218 if (callbacks != null) {
2219 callbacks.bindAllApplications(list);
2220 }
2221 if (DEBUG_LOADERS) {
2222 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002223 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04002224 }
2225 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002226 };
Tony Wickham80f57872016-06-29 18:12:15 -07002227 runOnMainThread(r);
Joe Onorato36115782010-06-17 13:28:48 -04002228 }
2229
Winson Chung64359a52013-07-08 17:17:08 -07002230 private void loadAllApps() {
2231 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002232
Joe Onorato36115782010-06-17 13:28:48 -04002233 final Callbacks oldCallbacks = mCallbacks.get();
2234 if (oldCallbacks == null) {
2235 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002236 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002237 return;
2238 }
2239
Kenny Guyed131872014-04-30 03:02:21 +01002240 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2241
Winson Chung64359a52013-07-08 17:17:08 -07002242 // Clear the list of apps
2243 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002244 for (UserHandleCompat user : profiles) {
2245 // Query for the set of apps
2246 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002247 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002248 if (DEBUG_LOADERS) {
2249 Log.d(TAG, "getActivityList took "
2250 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2251 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2252 }
2253 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002254 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002255 if (apps == null || apps.isEmpty()) {
2256 return;
2257 }
Kenny Guyff05f432016-01-22 17:48:29 +00002258 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002259 // Create the ApplicationInfos
2260 for (int i = 0; i < apps.size(); i++) {
2261 LauncherActivityInfoCompat app = apps.get(i);
2262 // This builds the icon bitmaps.
Kenny Guyff05f432016-01-22 17:48:29 +00002263 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode));
Kenny Guyed131872014-04-30 03:02:21 +01002264 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002265
Sunny Goyal756a28a2015-04-23 17:07:55 -07002266 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2267 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002268 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002269
2270 @Override
2271 public void run() {
2272 heuristic.processUserApps(apps);
2273 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002274 };
2275 runOnMainThread(new Runnable() {
2276
2277 @Override
2278 public void run() {
2279 // Check isLoadingWorkspace on the UI thread, as it is updated on
2280 // the UI thread.
2281 if (mIsLoadingAndBindingWorkspace) {
2282 synchronized (mBindCompleteRunnables) {
2283 mBindCompleteRunnables.add(r);
2284 }
2285 } else {
2286 runOnWorkerThread(r);
2287 }
2288 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002289 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002290 }
Winson Chung64359a52013-07-08 17:17:08 -07002291 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002292 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002293 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2294 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002295
2296 // Post callback on main thread
2297 mHandler.post(new Runnable() {
2298 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002299
Winson Chung64359a52013-07-08 17:17:08 -07002300 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002301 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002302 if (callbacks != null) {
2303 callbacks.bindAllApplications(added);
2304 if (DEBUG_LOADERS) {
2305 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002306 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07002307 }
2308 } else {
2309 Log.i(TAG, "not binding apps: no Launcher activity");
2310 }
2311 }
2312 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002313 // Cleanup any data stored for a deleted user.
2314 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04002315 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002316 Log.d(TAG, "Icons processed in "
2317 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002318 }
2319 }
2320
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002321 private void loadAndBindDeepShortcuts() {
2322 if (DEBUG_LOADERS) {
2323 Log.d(TAG, "loadAndBindDeepShortcuts mDeepShortcutsLoaded=" + mDeepShortcutsLoaded);
2324 }
2325 if (!mDeepShortcutsLoaded) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07002326 sBgDataModel.deepShortcutMap.clear();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302327 DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(mContext);
2328 mHasShortcutHostPermission = shortcutManager.hasHostPermission();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002329 if (mHasShortcutHostPermission) {
2330 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
2331 if (mUserManager.isUserUnlocked(user)) {
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302332 List<ShortcutInfoCompat> shortcuts =
2333 shortcutManager.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}