blob: daa910fc8f22fcc21d104ddd48f9b0dc1c1141c9 [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;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080040import android.os.UserHandle;
Chris Wrenc3919c02013-09-18 09:48:33 -040041import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080042import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070043import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080044import android.util.LongSparseArray;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070045import android.util.MutableInt;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010046
Sunny Goyalffe83f12014-08-14 17:39:34 -070047import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010048import com.android.launcher3.compat.LauncherActivityInfoCompat;
49import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070050import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070051import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010052import 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;
Sunny Goyale9956a72016-09-01 17:24:47 -070061import com.android.launcher3.model.BgDataModel;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070062import com.android.launcher3.model.CacheDataUpdatedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080063import com.android.launcher3.model.ExtendedModelTask;
Sunny Goyalf862a262015-12-14 14:27:38 -080064import com.android.launcher3.model.GridSizeMigrationTask;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070065import com.android.launcher3.model.PackageInstallStateChangedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080066import com.android.launcher3.model.PackageItemInfo;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070067import com.android.launcher3.model.PackageUpdatedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080068import com.android.launcher3.model.SdCardAvailableReceiver;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070069import com.android.launcher3.model.ShortcutsChangedTask;
70import com.android.launcher3.model.UserLockStateChangedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080071import com.android.launcher3.model.WidgetItem;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070072import com.android.launcher3.model.WidgetsModel;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -070073import com.android.launcher3.provider.ImportDataTask;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070074import com.android.launcher3.provider.LauncherDbUtils;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070075import com.android.launcher3.shortcuts.DeepShortcutManager;
76import com.android.launcher3.shortcuts.ShortcutInfoCompat;
77import com.android.launcher3.shortcuts.ShortcutKey;
Robin Lee26ace122015-03-16 19:41:43 +000078import com.android.launcher3.util.ComponentKey;
Sunny Goyal32f3dda2016-11-11 11:45:00 -080079import com.android.launcher3.util.ContentWriter;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070080import com.android.launcher3.util.CursorIconInfo;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070081import com.android.launcher3.util.GridOccupancy;
Sunny Goyal40452cf2016-09-01 15:17:46 -070082import com.android.launcher3.util.ItemInfoMatcher;
Sunny Goyale2df0622015-04-24 11:27:00 -070083import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070084import com.android.launcher3.util.ManagedProfileHeuristic;
Tony Wickhamd82a39d2016-07-01 15:44:13 -070085import com.android.launcher3.util.MultiHashMap;
Sunny Goyald09c3702016-04-06 16:18:20 -070086import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070087import com.android.launcher3.util.Preconditions;
Sunny Goyal2bcbe132016-11-16 09:23:42 -080088import com.android.launcher3.util.Provider;
Adam Cohen091440a2015-03-18 14:16:05 -070089import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070090import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080091
Michael Jurkac2f801e2011-07-12 14:19:46 -070092import java.lang.ref.WeakReference;
93import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070094import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070095import java.util.ArrayList;
96import java.util.Collections;
97import java.util.Comparator;
98import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070099import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -0700100import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -0700101import java.util.List;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700102import java.util.Map;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700103import java.util.Set;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700104import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -0700105
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106/**
107 * Maintains in-memory state of the Launcher. It is expected that there should be only one
108 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700109 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110 */
Kenny Guyed131872014-04-30 03:02:21 +0100111public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +0100112 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800113 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -0400114 private static final boolean DEBUG_RECEIVER = false;
Chris Wrenb358f812014-04-16 13:37:00 -0400115
Joe Onorato9c1289c2009-08-17 11:03:03 -0400116 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700117
Joe Onorato36115782010-06-17 13:28:48 -0400118 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500119 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800120
Adam Cohen091440a2015-03-18 14:16:05 -0700121 @Thunk final LauncherAppState mApp;
122 @Thunk final Object mLock = new Object();
123 @Thunk DeferredHandler mHandler = new DeferredHandler();
124 @Thunk LoaderTask mLoaderTask;
125 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700126 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127
Adam Cohen091440a2015-03-18 14:16:05 -0700128 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700129 static {
130 sWorkerThread.start();
131 }
Adam Cohen091440a2015-03-18 14:16:05 -0700132 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700133
Joe Onoratocc67f472010-06-08 10:54:30 -0700134 // We start off with everything not loaded. After that, we assume that
135 // our monitoring of the package manager provides all updates and we never
136 // need to do a requery. These are only ever touched from the loader thread.
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700137 private boolean mWorkspaceLoaded;
138 private boolean mAllAppsLoaded;
139 private boolean mDeepShortcutsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700140
Sunny Goyal756a28a2015-04-23 17:07:55 -0700141 /**
142 * Set of runnables to be called on the background thread after the workspace binding
143 * is complete.
144 */
145 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
146
Adam Cohen091440a2015-03-18 14:16:05 -0700147 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700149 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800150 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700151 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800152 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800153
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700154 private boolean mHasShortcutHostPermission;
155 // Runnable to check if the shortcuts permission has changed.
156 private final Runnable mShortcutPermissionCheckRunnable = new Runnable() {
157 @Override
158 public void run() {
159 if (mDeepShortcutsLoaded) {
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530160 boolean hasShortcutHostPermission =
161 DeepShortcutManager.getInstance(mApp.getContext()).hasHostPermission();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700162 if (hasShortcutHostPermission != mHasShortcutHostPermission) {
163 mApp.reloadWorkspace();
164 }
165 }
166 }
167 };
168
Sunny Goyale9956a72016-09-01 17:24:47 -0700169 /**
170 * All the static data should be accessed on the background thread, A lock should be acquired
171 * on this object when accessing any data from this model.
172 */
173 static final BgDataModel sBgDataModel = new BgDataModel();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700174
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700175 // </ only access in worker thread >
176
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700177 private final IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700179 private final LauncherAppsCompat mLauncherApps;
180 private final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100181
Joe Onorato9c1289c2009-08-17 11:03:03 -0400182 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700183 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400184 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700185 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400186 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700187 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
188 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700189 public void bindScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700190 public void finishFirstPageBind(ViewOnDrawExecutor executor);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800191 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400192 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200193 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700194 public void bindAppsAdded(ArrayList<Long> newScreens,
195 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700196 ArrayList<ItemInfo> addAnimated,
197 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200198 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700199 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800200 ArrayList<ShortcutInfo> removed, UserHandle user);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700201 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700202 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700203 public void bindWorkspaceComponentsRemoved(
204 HashSet<String> packageNames, HashSet<ComponentName> components,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800205 UserHandle user);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700206 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800207 public void notifyWidgetProvidersChanged();
Sunny Goyald164b7f2016-10-12 20:49:31 -0700208 public void bindAllWidgets(MultiHashMap<PackageItemInfo, WidgetItem> widgets);
Adam Cohen1462de32012-07-24 22:34:36 -0700209 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700210 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700211 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMap);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400212 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530214 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800215 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400216 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100217 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Sunny Goyald164b7f2016-10-12 20:49:31 -0700218 mBgWidgetsModel = new WidgetsModel(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800219 mIconCache = iconCache;
220
Kenny Guyed131872014-04-30 03:02:21 +0100221 mLauncherApps = LauncherAppsCompat.getInstance(context);
222 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800223 }
224
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700225 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
226 * posted on the main thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700227 private void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700228 if (sWorkerThread.getThreadId() == Process.myTid()) {
229 // If we are on the worker thread, post onto the main handler
230 mHandler.post(r);
231 } else {
232 r.run();
233 }
234 }
235
236 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
237 * posted on the worker thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700238 private static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700239 if (sWorkerThread.getThreadId() == Process.myTid()) {
240 r.run();
241 } else {
242 // If we are not on the worker thread, then post to the worker handler
243 sWorker.post(r);
244 }
245 }
246
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700247 public void setPackageState(PackageInstallInfo installInfo) {
248 enqueueModelUpdateTask(new PackageInstallStateChangedTask(installInfo));
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500249 }
250
Sunny Goyal756adbc2015-04-16 15:20:51 -0700251 /**
252 * Updates the icons and label of all pending icons for the provided package name.
253 */
254 public void updateSessionDisplayInfo(final String packageName) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700255 HashSet<String> packages = new HashSet<>();
256 packages.add(packageName);
257 enqueueModelUpdateTask(new CacheDataUpdatedTask(
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800258 CacheDataUpdatedTask.OP_SESSION_UPDATE, Process.myUserHandle(), packages));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800259 }
260
261 /**
262 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800263 */
Sunny Goyal2bcbe132016-11-16 09:23:42 -0800264 public void addAndBindAddedWorkspaceItems(List<ItemInfo> workspaceApps) {
265 addAndBindAddedWorkspaceItems(Provider.of(workspaceApps));
266 }
267
268 /**
269 * Adds the provided items to the workspace.
270 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700271 public void addAndBindAddedWorkspaceItems(
Sunny Goyal2bcbe132016-11-16 09:23:42 -0800272 Provider<List<ItemInfo>> appsProvider) {
273 enqueueModelUpdateTask(new AddWorkspaceItemsTask(appsProvider));
Winson Chung64359a52013-07-08 17:17:08 -0700274 }
275
Joe Onorato9c1289c2009-08-17 11:03:03 -0400276 /**
277 * Adds an item to the DB if it was not created previously, or move it to a new
278 * <container, screen, cellX, cellY>
279 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800280 public static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700281 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400282 if (item.container == ItemInfo.NO_ID) {
283 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700284 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400285 } else {
286 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700287 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800288 }
289 }
290
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700291 static void checkItemInfoLocked(
292 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
Sunny Goyale9956a72016-09-01 17:24:47 -0700293 ItemInfo modelItem = sBgDataModel.itemsIdMap.get(itemId);
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700294 if (modelItem != null && item != modelItem) {
295 // check all the data is consistent
296 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
297 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
298 ShortcutInfo shortcut = (ShortcutInfo) item;
299 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
300 modelShortcut.intent.filterEquals(shortcut.intent) &&
301 modelShortcut.id == shortcut.id &&
302 modelShortcut.itemType == shortcut.itemType &&
303 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700304 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700305 modelShortcut.cellX == shortcut.cellX &&
306 modelShortcut.cellY == shortcut.cellY &&
307 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700308 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700309 // For all intents and purposes, this is the same object
310 return;
311 }
312 }
313
314 // the modelItem needs to match up perfectly with item if our model is
315 // to be consistent with the database-- for now, just require
316 // modelItem == item or the equality check above
317 String msg = "item: " + ((item != null) ? item.toString() : "null") +
318 "modelItem: " +
319 ((modelItem != null) ? modelItem.toString() : "null") +
320 "Error: ItemInfo passed to checkItemInfo doesn't match original";
321 RuntimeException e = new RuntimeException(msg);
322 if (stackTrace != null) {
323 e.setStackTrace(stackTrace);
324 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800325 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700326 }
327 }
328
Michael Jurka816474f2012-06-25 14:49:02 -0700329 static void checkItemInfo(final ItemInfo item) {
330 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
331 final long itemId = item.id;
332 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700333 public void run() {
Sunny Goyale9956a72016-09-01 17:24:47 -0700334 synchronized (sBgDataModel) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700335 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700336 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700337 }
338 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700339 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700340 }
341
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800342 static void updateItemInDatabaseHelper(Context context, final ContentWriter writer,
Michael Jurkac9d95c52011-08-29 14:03:34 -0700343 final ItemInfo item, final String callingFunction) {
344 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700345 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700346 final ContentResolver cr = context.getContentResolver();
347
Adam Cohen487f7dd2012-06-28 18:12:10 -0700348 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700349 Runnable r = new Runnable() {
350 public void run() {
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800351 cr.update(uri, writer.getValues(), null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700352 updateItemArrays(item, itemId, stackTrace);
353 }
354 };
355 runOnWorkerThread(r);
356 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700357
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700358 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
359 final ArrayList<ItemInfo> items, final String callingFunction) {
360 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700361
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700362 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
363 Runnable r = new Runnable() {
364 public void run() {
365 ArrayList<ContentProviderOperation> ops =
366 new ArrayList<ContentProviderOperation>();
367 int count = items.size();
368 for (int i = 0; i < count; i++) {
369 ItemInfo item = items.get(i);
370 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700371 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700372 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700373
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700374 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
375 updateItemArrays(item, itemId, stackTrace);
376
377 }
378 try {
379 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
380 } catch (Exception e) {
381 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700382 }
383 }
384 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700385 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700386 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700387
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700388 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
389 // Lock on mBgLock *after* the db operation
Sunny Goyale9956a72016-09-01 17:24:47 -0700390 synchronized (sBgDataModel) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700391 checkItemInfoLocked(itemId, item, stackTrace);
392
393 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
394 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
395 // Item is in a folder, make sure this folder exists
Sunny Goyale9956a72016-09-01 17:24:47 -0700396 if (!sBgDataModel.folders.containsKey(item.container)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700397 // An items container is being set to a that of an item which is not in
398 // the list of Folders.
399 String msg = "item: " + item + " container being set to: " +
400 item.container + ", not in the list of folders";
401 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700402 }
403 }
404
405 // Items are added/removed from the corresponding FolderInfo elsewhere, such
406 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
407 // that are on the desktop, as appropriate
Sunny Goyale9956a72016-09-01 17:24:47 -0700408 ItemInfo modelItem = sBgDataModel.itemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800409 if (modelItem != null &&
410 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
411 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700412 switch (modelItem.itemType) {
413 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
414 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700415 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700416 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyale9956a72016-09-01 17:24:47 -0700417 if (!sBgDataModel.workspaceItems.contains(modelItem)) {
418 sBgDataModel.workspaceItems.add(modelItem);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700419 }
420 break;
421 default:
422 break;
423 }
424 } else {
Sunny Goyale9956a72016-09-01 17:24:47 -0700425 sBgDataModel.workspaceItems.remove(modelItem);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700426 }
427 }
428 }
429
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800430 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400431 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700432 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700433 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700434 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400435 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400436 item.cellX = cellX;
437 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700438
Winson Chung3d503fb2011-07-13 17:25:49 -0700439 // We store hotseat items in canonical form which is this orientation invariant position
440 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700441 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700442 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700443 item.screenId = Launcher.getLauncher(context).getHotseat()
444 .getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700445 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700446 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700447 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400448
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800449 final ContentWriter writer = new ContentWriter(context)
450 .put(LauncherSettings.Favorites.CONTAINER, item.container)
451 .put(LauncherSettings.Favorites.CELLX, item.cellX)
452 .put(LauncherSettings.Favorites.CELLY, item.cellY)
453 .put(LauncherSettings.Favorites.RANK, item.rank)
454 .put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400455
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800456 updateItemInDatabaseHelper(context, writer, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700457 }
458
459 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700460 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
461 * cellX, cellY have already been updated on the ItemInfos.
462 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800463 public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700464 final long container, final int screen) {
465
466 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
467 int count = items.size();
468
469 for (int i = 0; i < count; i++) {
470 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700471 item.container = container;
472
473 // We store hotseat items in canonical form which is this orientation invariant position
474 // in the hotseat
475 if (context instanceof Launcher && screen < 0 &&
476 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700477 item.screenId = Launcher.getLauncher(context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700478 item.cellY);
479 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700480 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700481 }
482
483 final ContentValues values = new ContentValues();
484 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
485 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
486 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800487 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700488 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700489
490 contentValues.add(values);
491 }
492 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
493 }
494
495 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700496 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800497 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700498 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700499 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700500 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800501 item.cellX = cellX;
502 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700503 item.spanX = spanX;
504 item.spanY = spanY;
505
506 // We store hotseat items in canonical form which is this orientation invariant position
507 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700508 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700509 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700510 item.screenId = Launcher.getLauncher(context).getHotseat()
511 .getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700512 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700513 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700514 }
Adam Cohend4844c32011-02-18 19:25:06 -0800515
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800516 final ContentWriter writer = new ContentWriter(context)
517 .put(LauncherSettings.Favorites.CONTAINER, item.container)
518 .put(LauncherSettings.Favorites.CELLX, item.cellX)
519 .put(LauncherSettings.Favorites.CELLY, item.cellY)
520 .put(LauncherSettings.Favorites.RANK, item.rank)
521 .put(LauncherSettings.Favorites.SPANX, item.spanX)
522 .put(LauncherSettings.Favorites.SPANY, item.spanY)
523 .put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800524
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800525 updateItemInDatabaseHelper(context, writer, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700526 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700527
528 /**
529 * Update an item to the database in a specified container.
530 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700531 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800532 ContentWriter writer = new ContentWriter(context);
533 item.onAddToDatabase(writer);
534 updateItemInDatabaseHelper(context, writer, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800535 }
536
Joe Onorato9c1289c2009-08-17 11:03:03 -0400537 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400538 * Add an item to the database in a specified container. Sets the container, screen, cellX and
539 * cellY fields of the item. Also assigns an ID to the item.
540 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700541 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700542 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400543 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400544 item.cellX = cellX;
545 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700546 // We store hotseat items in canonical form which is this orientation invariant position
547 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700548 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700549 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700550 item.screenId = Launcher.getLauncher(context).getHotseat()
551 .getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700552 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700553 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700554 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400555
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800556 final ContentWriter writer = new ContentWriter(context);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400557 final ContentResolver cr = context.getContentResolver();
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800558 item.onAddToDatabase(writer);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400559
Sunny Goyald2497482015-09-22 18:24:19 -0700560 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
561 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
562
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800563 writer.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700564
Jason Monk8e19cf22014-03-20 15:06:57 -0400565 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700566 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700567 public void run() {
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800568 cr.insert(LauncherSettings.Favorites.CONTENT_URI, writer.getValues());
Joe Onorato9c1289c2009-08-17 11:03:03 -0400569
Sunny Goyale9956a72016-09-01 17:24:47 -0700570 synchronized (sBgDataModel) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400571 checkItemInfoLocked(item.id, item, stackTrace);
Sunny Goyale9956a72016-09-01 17:24:47 -0700572 sBgDataModel.addItem(item, true);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700573 }
574 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700575 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700576 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700577 }
578
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700579 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700580 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -0400581 */
Sunny Goyalfa401a12015-04-10 13:45:42 -0700582 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyal40452cf2016-09-01 15:17:46 -0700583 ArrayList<ItemInfo> items = new ArrayList<>();
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700584 items.add(item);
585 deleteItemsFromDatabase(context, items);
586 }
587
588 /**
Sunny Goyal40452cf2016-09-01 15:17:46 -0700589 * Removes all the items from the database matching {@param matcher}.
590 */
591 public static void deleteItemsFromDatabase(Context context, ItemInfoMatcher matcher) {
Sunny Goyale9956a72016-09-01 17:24:47 -0700592 deleteItemsFromDatabase(context, matcher.filterItemInfos(sBgDataModel.itemsIdMap));
Sunny Goyal40452cf2016-09-01 15:17:46 -0700593 }
594
595 /**
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700596 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700597 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700598 public static void deleteItemsFromDatabase(Context context,
599 final Iterable<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400600 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -0700601 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700602 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700603 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700604 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700605 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700606
Sunny Goyale9956a72016-09-01 17:24:47 -0700607 sBgDataModel.removeItem(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700608 }
609 }
Michael Jurka83df1882011-08-31 20:59:26 -0700610 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700611 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400612 }
613
614 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700615 * Update the order of the workspace screens in the database. The array list contains
616 * a list of screen ids in the order that they should appear.
617 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700618 public static void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -0700619 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700620 final ContentResolver cr = context.getContentResolver();
621 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
622
623 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700624 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700625 while (iter.hasNext()) {
626 long id = iter.next();
627 if (id < 0) {
628 iter.remove();
629 }
630 }
631
632 Runnable r = new Runnable() {
633 @Override
634 public void run() {
Yura085c8532014-02-11 15:15:29 +0000635 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700636 // Clear the table
Yura085c8532014-02-11 15:15:29 +0000637 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -0700638 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700639 for (int i = 0; i < count; i++) {
640 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700641 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700642 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
643 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +0000644 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -0700645 }
Yura085c8532014-02-11 15:15:29 +0000646
647 try {
648 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
649 } catch (Exception ex) {
650 throw new RuntimeException(ex);
651 }
Winson Chung9e6a0a22013-08-27 11:58:12 -0700652
Sunny Goyale9956a72016-09-01 17:24:47 -0700653 synchronized (sBgDataModel) {
654 sBgDataModel.workspaceScreens.clear();
655 sBgDataModel.workspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -0700656 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700657 }
658 };
659 runOnWorkerThread(r);
660 }
661
662 /**
Winsonc0b52fe2015-09-09 16:38:15 -0700663 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -0400664 */
Winsonc0b52fe2015-09-09 16:38:15 -0700665 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400666 final ContentResolver cr = context.getContentResolver();
667
Michael Jurkac9d95c52011-08-29 14:03:34 -0700668 Runnable r = new Runnable() {
669 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700670 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -0700671 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Sunny Goyale9956a72016-09-01 17:24:47 -0700672 sBgDataModel.removeItem(info.contents);
673 info.contents.clear();
674
675 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
676 sBgDataModel.removeItem(info);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700677 }
678 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700679 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400680 }
681
682 /**
683 * Set this as the current Launcher activity object for the loader.
684 */
685 public void initialize(Callbacks callbacks) {
686 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -0700687 Preconditions.assertUIThread();
688 // Remove any queued UI runnables
689 mHandler.cancelAll();
690 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400691 }
692 }
693
Kenny Guyed131872014-04-30 03:02:21 +0100694 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800695 public void onPackageChanged(String packageName, UserHandle user) {
Kenny Guyed131872014-04-30 03:02:21 +0100696 int op = PackageUpdatedTask.OP_UPDATE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700697 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100698 }
699
700 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800701 public void onPackageRemoved(String packageName, UserHandle user) {
Sunny Goyalc2936bc2016-09-01 15:50:36 -0700702 onPackagesRemoved(user, packageName);
703 }
704
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800705 public void onPackagesRemoved(UserHandle user, String... packages) {
Kenny Guyed131872014-04-30 03:02:21 +0100706 int op = PackageUpdatedTask.OP_REMOVE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700707 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packages));
Kenny Guyed131872014-04-30 03:02:21 +0100708 }
709
710 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800711 public void onPackageAdded(String packageName, UserHandle user) {
Kenny Guyed131872014-04-30 03:02:21 +0100712 int op = PackageUpdatedTask.OP_ADD;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700713 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100714 }
715
716 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800717 public void onPackagesAvailable(String[] packageNames, UserHandle user,
Kenny Guyed131872014-04-30 03:02:21 +0100718 boolean replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700719 enqueueModelUpdateTask(
720 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100721 }
722
723 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800724 public void onPackagesUnavailable(String[] packageNames, UserHandle user,
Kenny Guyed131872014-04-30 03:02:21 +0100725 boolean replacing) {
726 if (!replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700727 enqueueModelUpdateTask(new PackageUpdatedTask(
728 PackageUpdatedTask.OP_UNAVAILABLE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100729 }
Kenny Guyed131872014-04-30 03:02:21 +0100730 }
731
Kenny Guy44cba692016-01-21 19:50:02 +0000732 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800733 public void onPackagesSuspended(String[] packageNames, UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700734 enqueueModelUpdateTask(new PackageUpdatedTask(
735 PackageUpdatedTask.OP_SUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000736 }
737
738 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800739 public void onPackagesUnsuspended(String[] packageNames, UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700740 enqueueModelUpdateTask(new PackageUpdatedTask(
741 PackageUpdatedTask.OP_UNSUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000742 }
743
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700744 @Override
745 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800746 UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700747 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, true));
Sunny Goyal50941fb2016-08-04 12:03:52 -0700748 }
749
750 public void updatePinnedShortcuts(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800751 UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700752 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, false));
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700753 }
754
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700755 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400756 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
757 * ACTION_PACKAGE_CHANGED.
758 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100759 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400760 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -0400761 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700762
Joe Onorato36115782010-06-17 13:28:48 -0400763 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +0100764 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -0700765 // If we have changed locale we need to clear out the labels in all apps/workspace.
766 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700767 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
768 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -0700769 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -0700770 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700771 } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
772 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
773 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800774 UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER);
Sunny Goyalda891c12016-03-18 18:29:24 -0700775 if (user != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700776 if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
777 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700778 enqueueModelUpdateTask(new PackageUpdatedTask(
779 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE, user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700780 }
781
782 // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so
783 // we need to run the state change task again.
784 if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
785 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700786 enqueueModelUpdateTask(new UserLockStateChangedTask(user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700787 }
Sunny Goyalda891c12016-03-18 18:29:24 -0700788 }
Tony Wickham827cef22016-03-17 15:39:39 -0700789 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
790 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700791 }
792 }
793
Amith Yamasani6cc806d2014-05-02 13:47:11 -0700794 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700795 resetLoadedState(true, true);
796
Reena Lee93f824a2011-09-23 17:20:28 -0700797 // Do this here because if the launcher activity is running it will be restarted.
798 // If it's not running startLoaderFromBackground will merely tell it that it needs
799 // to reload.
800 startLoaderFromBackground();
801 }
802
Winson Chungf0c6ae02012-03-21 16:10:31 -0700803 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
804 synchronized (mLock) {
805 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
806 // mWorkspaceLoaded to true later
807 stopLoaderLocked();
808 if (resetAllAppsLoaded) mAllAppsLoaded = false;
809 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700810 // Always reset deep shortcuts loaded.
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700811 // TODO: why?
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700812 mDeepShortcutsLoaded = false;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700813 }
814 }
815
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700816 /**
817 * When the launcher is in the background, it's possible for it to miss paired
818 * configuration changes. So whenever we trigger the loader from the background
819 * tell the launcher that it needs to re-run the loader when it comes back instead
820 * of doing it now.
821 */
822 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800823 Callbacks callbacks = getCallback();
824 if (callbacks != null) {
825 // Only actually run the loader if they're not paused.
826 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -0700827 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700828 }
829 }
Joe Onorato36115782010-06-17 13:28:48 -0400830 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400831
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700832 /**
833 * If there is already a loader task running, tell it to stop.
834 */
835 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -0700836 LoaderTask oldTask = mLoaderTask;
837 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -0700838 oldTask.stopLocked();
839 }
Reena Lee93f824a2011-09-23 17:20:28 -0700840 }
841
Adam Cohen1a85c582014-09-30 09:48:49 -0700842 public boolean isCurrentCallbacks(Callbacks callbacks) {
843 return (mCallbacks != null && mCallbacks.get() == callbacks);
844 }
845
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700846 /**
847 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
848 * @return true if the page could be bound synchronously.
849 */
850 public boolean startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700851 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
852 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -0400853 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -0400854 // Don't bother to start the thread if we know it's not going to do anything
855 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -0700856 final Callbacks oldCallbacks = mCallbacks.get();
857 // Clear any pending bind-runnables from the synchronized load process.
858 runOnMainThread(new Runnable() {
859 public void run() {
860 oldCallbacks.clearPendingBinds();
861 }
862 });
863
Joe Onorato36115782010-06-17 13:28:48 -0400864 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700865 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700866 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700867 // TODO: mDeepShortcutsLoaded does not need to be true for synchronous bind.
Tony Wickham80f57872016-06-29 18:12:15 -0700868 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE && mAllAppsLoaded
869 && mWorkspaceLoaded && mDeepShortcutsLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700870 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700871 return true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700872 } else {
873 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
874 sWorker.post(mLoaderTask);
875 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400876 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400877 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700878 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400879 }
880
Joe Onorato36115782010-06-17 13:28:48 -0400881 public void stopLoader() {
882 synchronized (mLock) {
883 if (mLoaderTask != null) {
884 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400885 }
886 }
Joe Onorato36115782010-06-17 13:28:48 -0400887 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400888
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800889 /**
890 * Loads the workspace screen ids in an ordered list.
891 */
Sunny Goyale5bb7052015-07-27 14:36:07 -0700892 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -0700893 final ContentResolver contentResolver = context.getContentResolver();
894 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -0700895
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800896 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -0700897 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
898 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -0700899 }
900
Joe Onorato36115782010-06-17 13:28:48 -0400901 /**
902 * Runnable for the thread that loads the contents of the launcher:
903 * - workspace icons
904 * - widgets
905 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700906 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -0400907 */
908 private class LoaderTask implements Runnable {
909 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -0700910 private int mPageToBindFirst;
911
Adam Cohen091440a2015-03-18 14:16:05 -0700912 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -0400913 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -0700914 @Thunk boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700915
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700916 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -0400917 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -0700918 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400919 }
920
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800921 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700922 mIsLoadingAndBindingWorkspace = true;
923
Joe Onorato36115782010-06-17 13:28:48 -0400924 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -0400925 if (DEBUG_LOADERS) {
926 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400927 }
Michael Jurka288a36b2011-07-12 16:53:48 -0700928
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700929 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800930 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -0700931 synchronized (LoaderTask.this) {
932 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800933 return;
Reena Lee93f824a2011-09-23 17:20:28 -0700934 }
935 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400936 }
937 }
938
Joe Onorato36115782010-06-17 13:28:48 -0400939 // Bind the workspace
Sunny Goyal93f878c2016-03-30 17:31:24 -0700940 bindWorkspace(mPageToBindFirst);
Joe Onorato36115782010-06-17 13:28:48 -0400941 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400942
Joe Onorato36115782010-06-17 13:28:48 -0400943 private void waitForIdle() {
944 // Wait until the either we're stopped or the other threads are done.
945 // This way we don't start loading all apps until the workspace has settled
946 // down.
947 synchronized (LoaderTask.this) {
948 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700949
Joe Onorato36115782010-06-17 13:28:48 -0400950 mHandler.postIdle(new Runnable() {
951 public void run() {
952 synchronized (LoaderTask.this) {
953 mLoadAndBindStepFinished = true;
954 if (DEBUG_LOADERS) {
955 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400956 }
Joe Onorato36115782010-06-17 13:28:48 -0400957 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400958 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400959 }
Joe Onorato36115782010-06-17 13:28:48 -0400960 });
961
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800962 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -0400963 try {
Michael Jurkac7700af2013-05-14 20:17:58 +0200964 // Just in case mFlushingWorkerThread changes but we aren't woken up,
965 // wait no longer than 1sec at a time
966 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -0400967 } catch (InterruptedException ex) {
968 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400969 }
970 }
Joe Onorato36115782010-06-17 13:28:48 -0400971 if (DEBUG_LOADERS) {
972 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700973 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400974 + "ms for previous step to finish binding");
975 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400976 }
Joe Onorato36115782010-06-17 13:28:48 -0400977 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400978
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700979 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -0500980 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700981 // Ensure that we have a valid page index to load synchronously
982 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
983 "valid page index");
984 }
985 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
986 // Ensure that we don't try and bind a specified page when the pages have not been
987 // loaded already (we should load everything asynchronously in that case)
988 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
989 }
990 synchronized (mLock) {
991 if (mIsLoaderTaskRunning) {
992 // Ensure that we are never running the background loading at this point since
993 // we also touch the background collections
994 throw new RuntimeException("Error! Background loading is already running");
995 }
996 }
997
998 // XXX: Throw an exception if we are already loading (since we touch the worker thread
999 // data structures, we can't allow any other thread to touch that data, but because
1000 // this call is synchronous, we can get away with not locking).
1001
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001002 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001003 // operations from the previous activity. We need to ensure that all queued operations
1004 // are executed before any synchronous binding work is done.
1005 mHandler.flush();
1006
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001007 // Divide the set of loaded items into those that we are binding synchronously, and
1008 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001009 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001010 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1011 // arise from that.
1012 onlyBindAllApps();
Tony Wickham80f57872016-06-29 18:12:15 -07001013
1014 bindDeepShortcuts();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001015 }
1016
Joe Onorato36115782010-06-17 13:28:48 -04001017 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001018 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001019 if (mStopped) {
1020 return;
1021 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001022 mIsLoaderTaskRunning = true;
1023 }
Joe Onorato36115782010-06-17 13:28:48 -04001024 // Optimize for end-user experience: if the Launcher is up and // running with the
1025 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1026 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001027 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001028 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001029 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001030
Joe Onorato36115782010-06-17 13:28:48 -04001031 if (mStopped) {
1032 break keep_running;
1033 }
1034
Joe Onorato36115782010-06-17 13:28:48 -04001035 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001036
1037 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001038 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1039 loadAndBindAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001040
1041 waitForIdle();
1042
1043 // third step
1044 if (DEBUG_LOADERS) Log.d(TAG, "step 3: loading deep shortcuts");
1045 loadAndBindDeepShortcuts();
Joe Onorato36115782010-06-17 13:28:48 -04001046 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001047
Joe Onorato36115782010-06-17 13:28:48 -04001048 // Clear out this reference, otherwise we end up holding it until all of the
1049 // callback runnables are done.
1050 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001051
Joe Onorato36115782010-06-17 13:28:48 -04001052 synchronized (mLock) {
1053 // If we are still the last one to be scheduled, remove ourselves.
1054 if (mLoaderTask == this) {
1055 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001056 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001057 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001058 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001059 }
Joe Onorato36115782010-06-17 13:28:48 -04001060 }
1061
1062 public void stopLocked() {
1063 synchronized (LoaderTask.this) {
1064 mStopped = true;
1065 this.notify();
1066 }
1067 }
1068
1069 /**
1070 * Gets the callbacks object. If we've been stopped, or if the launcher object
1071 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1072 * object that was around when the deferred message was scheduled, and if there's
1073 * a new Callbacks object around then also return null. This will save us from
1074 * calling onto it with data that will be ignored.
1075 */
1076 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1077 synchronized (mLock) {
1078 if (mStopped) {
1079 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001080 }
Joe Onorato36115782010-06-17 13:28:48 -04001081
1082 if (mCallbacks == null) {
1083 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001084 }
Joe Onorato36115782010-06-17 13:28:48 -04001085
1086 final Callbacks callbacks = mCallbacks.get();
1087 if (callbacks != oldCallbacks) {
1088 return null;
1089 }
1090 if (callbacks == null) {
1091 Log.w(TAG, "no mCallbacks");
1092 return null;
1093 }
1094
1095 return callbacks;
1096 }
1097 }
1098
1099 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001100 private boolean checkItemPlacement(LongArrayMap<GridOccupancy> occupied, ItemInfo item,
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001101 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001102 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001103 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung892c74d2013-08-22 16:15:50 -07001104
Adam Cohendcd297f2013-06-18 13:13:40 -07001105 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001106 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001107 // Return early if we detect that an item is under the hotseat button
Sunny Goyalbb011da2016-06-15 15:42:29 -07001108 if (!FeatureFlags.NO_ALL_APPS_ICON &&
1109 profile.isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001110 Log.e(TAG, "Error loading shortcut into hotseat " + item
1111 + " into position (" + item.screenId + ":" + item.cellX + ","
1112 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001113 return false;
1114 }
1115
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001116 final GridOccupancy hotseatOccupancy =
Dan Sandler295ae182013-12-10 16:05:47 -05001117 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1118
Adam Cohen2e6da152015-05-06 11:42:25 -07001119 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001120 Log.e(TAG, "Error loading shortcut " + item
1121 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001122 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001123 + ")");
1124 return false;
1125 }
1126
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001127 if (hotseatOccupancy != null) {
1128 if (hotseatOccupancy.cells[(int) item.screenId][0]) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001129 Log.e(TAG, "Error loading shortcut into hotseat " + item
1130 + " into position (" + item.screenId + ":" + item.cellX + ","
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001131 + item.cellY + ") already occupied");
Adam Cohendcd297f2013-06-18 13:13:40 -07001132 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001133 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001134 hotseatOccupancy.cells[(int) item.screenId][0] = true;
Dan Sandler295ae182013-12-10 16:05:47 -05001135 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001136 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001137 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001138 final GridOccupancy occupancy = new GridOccupancy(profile.numHotseatIcons, 1);
1139 occupancy.cells[(int) item.screenId][0] = true;
1140 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, occupancy);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001141 return true;
1142 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001143 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1144 if (!workspaceScreens.contains((Long) item.screenId)) {
1145 // The item has an invalid screen id.
1146 return false;
1147 }
1148 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001149 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001150 return true;
1151 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001152
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001153 final int countX = profile.numColumns;
1154 final int countY = profile.numRows;
Adam Cohenae4409d2013-11-26 10:34:59 -08001155 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1156 item.cellX < 0 || item.cellY < 0 ||
1157 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1158 Log.e(TAG, "Error loading shortcut " + item
1159 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1160 + item.cellX + "," + item.cellY
1161 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1162 return false;
1163 }
1164
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001165 if (!occupied.containsKey(item.screenId)) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001166 GridOccupancy screen = new GridOccupancy(countX + 1, countY + 1);
1167 if (item.screenId == Workspace.FIRST_SCREEN_ID) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001168 // Mark the first row as occupied (if the feature is enabled)
1169 // in order to account for the QSB.
1170 screen.markCells(0, 0, countX + 1, 1, FeatureFlags.QSB_ON_FIRST_SCREEN);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001171 }
1172 occupied.put(item.screenId, screen);
Joe Onorato36115782010-06-17 13:28:48 -04001173 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001174 final GridOccupancy occupancy = occupied.get(item.screenId);
Winson Chungf30ad5f2011-08-08 10:55:42 -07001175
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001176 // Check if any workspace icons overlap with each other
1177 if (occupancy.isRegionVacant(item.cellX, item.cellY, item.spanX, item.spanY)) {
1178 occupancy.markCells(item, true);
1179 return true;
1180 } else {
1181 Log.e(TAG, "Error loading shortcut " + item
1182 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1183 + item.cellX + "," + item.cellX + "," + item.spanX + "," + item.spanY
1184 + ") already occupied");
1185 return false;
1186 }
Joe Onorato36115782010-06-17 13:28:48 -04001187 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001188
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001189 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -07001190 if (LauncherAppState.PROFILE_STARTUP) {
1191 Trace.beginSection("Loading Workspace");
1192 }
Joe Onorato36115782010-06-17 13:28:48 -04001193 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001194
Joe Onorato36115782010-06-17 13:28:48 -04001195 final Context context = mContext;
1196 final ContentResolver contentResolver = context.getContentResolver();
1197 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001198 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001199 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301200 final DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001201 final boolean isSdCardReady = Utilities.isBootCompleted();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001202 final MultiHashMap<UserHandle, String> pendingPackages = new MultiHashMap<>();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001203
Winson Chung892c74d2013-08-22 16:15:50 -07001204 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001205 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001206 int countX = profile.numColumns;
1207 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001208
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001209 boolean clearDb = false;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -07001210 try {
1211 ImportDataTask.performImportIfPossible(context);
1212 } catch (Exception e) {
1213 // Migration failed. Clear workspace.
1214 clearDb = true;
1215 }
1216
1217 if (!clearDb && GridSizeMigrationTask.ENABLED &&
Sunny Goyalf076eae2016-01-11 12:25:10 -08001218 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1219 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001220 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001221 }
1222
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001223 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -07001224 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001225 LauncherSettings.Settings.call(contentResolver,
1226 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001227 }
1228
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001229 Log.d(TAG, "loadWorkspace: loading default favorites");
1230 LauncherSettings.Settings.call(contentResolver,
1231 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -07001232
Sunny Goyale9956a72016-09-01 17:24:47 -07001233 synchronized (sBgDataModel) {
1234 sBgDataModel.clear();
1235
Sunny Goyal756adbc2015-04-16 15:20:51 -07001236 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001237 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyale9956a72016-09-01 17:24:47 -07001238 sBgDataModel.workspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001239
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001240 final ArrayList<Long> itemsToRemove = new ArrayList<>();
1241 final ArrayList<Long> restoredRows = new ArrayList<>();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001242 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001243 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001244 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001245 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001246
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001247 // +1 for the hotseat (it can be larger than the workspace)
1248 // Load workspace in reverse order to ensure that latest items are loaded first (and
1249 // before any earlier duplicates)
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001250 final LongArrayMap<GridOccupancy> occupied = new LongArrayMap<>();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001251 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001252
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001253 try {
1254 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1255 final int intentIndex = c.getColumnIndexOrThrow
1256 (LauncherSettings.Favorites.INTENT);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001257 final int containerIndex = c.getColumnIndexOrThrow(
1258 LauncherSettings.Favorites.CONTAINER);
1259 final int itemTypeIndex = c.getColumnIndexOrThrow(
1260 LauncherSettings.Favorites.ITEM_TYPE);
1261 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1262 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001263 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1264 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001265 final int screenIndex = c.getColumnIndexOrThrow(
1266 LauncherSettings.Favorites.SCREEN);
1267 final int cellXIndex = c.getColumnIndexOrThrow
1268 (LauncherSettings.Favorites.CELLX);
1269 final int cellYIndex = c.getColumnIndexOrThrow
1270 (LauncherSettings.Favorites.CELLY);
1271 final int spanXIndex = c.getColumnIndexOrThrow
1272 (LauncherSettings.Favorites.SPANX);
1273 final int spanYIndex = c.getColumnIndexOrThrow(
1274 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001275 final int rankIndex = c.getColumnIndexOrThrow(
1276 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001277 final int restoredIndex = c.getColumnIndexOrThrow(
1278 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001279 final int profileIdIndex = c.getColumnIndexOrThrow(
1280 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001281 final int optionsIndex = c.getColumnIndexOrThrow(
1282 LauncherSettings.Favorites.OPTIONS);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001283 final CursorIconInfo cursorIconInfo = new CursorIconInfo(mContext, c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001284
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001285 final LongSparseArray<UserHandle> allUsers = new LongSparseArray<>();
Kenny Guyff05f432016-01-22 17:48:29 +00001286 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001287 final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001288 for (UserHandle user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001289 long serialNo = mUserManager.getSerialNumberForUser(user);
1290 allUsers.put(serialNo, user);
1291 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001292
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001293 boolean userUnlocked = mUserManager.isUserUnlocked(user);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001294
1295 // We can only query for shortcuts when the user is unlocked.
1296 if (userUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07001297 List<ShortcutInfoCompat> pinnedShortcuts =
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301298 shortcutManager.queryForPinnedShortcuts(null, user);
1299 if (shortcutManager.wasLastCallSuccess()) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07001300 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
1301 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
1302 shortcut);
1303 }
1304 } else {
1305 // Shortcut manager can fail due to some race condition when the
1306 // lock state changes too frequently. For the purpose of the loading
1307 // shortcuts, consider the user is still locked.
1308 userUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001309 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001310 }
Sunny Goyal49f4f032016-08-01 15:45:49 -07001311 unlockedUsers.put(serialNo, userUnlocked);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001312 }
1313
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001314 ShortcutInfo info;
1315 String intentDescription;
1316 LauncherAppWidgetInfo appWidgetInfo;
1317 int container;
1318 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001319 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001320 Intent intent;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001321 UserHandle user;
Sunny Goyald09c3702016-04-06 16:18:20 -07001322 String targetPackage;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001323
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001324 while (!mStopped && c.moveToNext()) {
1325 try {
1326 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001327 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001328 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001329 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001330
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001331 switch (itemType) {
1332 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1333 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001334 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001335 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001336 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001337 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001338 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001339 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001340 int disabledState = 0;
Sunny Goyald09c3702016-04-06 16:18:20 -07001341 targetPackage = null;
Kenny Guyed131872014-04-30 03:02:21 +01001342 if (user == null) {
1343 // User has been deleted remove the item.
1344 itemsToRemove.add(id);
1345 continue;
1346 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001347 try {
1348 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001349 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001350 if (cn != null && cn.getPackageName() != null) {
1351 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1352 cn.getPackageName(), user);
1353 boolean validComponent = validPkg &&
1354 launcherApps.isActivityEnabledForProfile(cn, user);
Sunny Goyald09c3702016-04-06 16:18:20 -07001355 if (validPkg) {
1356 targetPackage = cn.getPackageName();
1357 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001358
1359 if (validComponent) {
1360 if (restored) {
1361 // no special handling necessary for this item
1362 restoredRows.add(id);
1363 restored = false;
1364 }
Kenny Guyff05f432016-01-22 17:48:29 +00001365 if (quietMode.get(serialNumber)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001366 disabledState = ShortcutInfo.FLAG_DISABLED_QUIET_USER;
Kenny Guyff05f432016-01-22 17:48:29 +00001367 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001368 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001369 intent = null;
1370 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1371 // We allow auto install apps to have their intent
1372 // updated after an install.
1373 intent = manager.getLaunchIntentForPackage(
1374 cn.getPackageName());
1375 if (intent != null) {
1376 ContentValues values = new ContentValues();
1377 values.put(LauncherSettings.Favorites.INTENT,
1378 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001379 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001380 }
1381 }
1382
1383 if (intent == null) {
1384 // The app is installed but the component is no
1385 // longer available.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001386 FileLog.d(TAG, "Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001387 itemsToRemove.add(id);
1388 continue;
1389 } else {
1390 // no special handling necessary for this item
1391 restoredRows.add(id);
1392 restored = false;
1393 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001394 } else if (restored) {
1395 // Package is not yet available but might be
1396 // installed later.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001397 FileLog.d(TAG, "package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001398
1399 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1400 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001401 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001402 // App restore has started. Update the flag
1403 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1404 ContentValues values = new ContentValues();
1405 values.put(LauncherSettings.Favorites.RESTORED,
1406 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001407 updateItem(id, values);
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001408 } else {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001409 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001410 itemsToRemove.add(id);
1411 continue;
1412 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001413 } else if (PackageManagerHelper.isAppOnSdcard(
1414 manager, cn.getPackageName())) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07001415 // Package is present but not available.
1416 allowMissingTarget = true;
1417 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1418 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001419 // SdCard is not ready yet. Package might get available,
1420 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001421 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001422 pendingPackages.addToList(user, cn.getPackageName());
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001423 allowMissingTarget = true;
1424 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001425
1426 } else {
1427 // Do not wait for external media load anymore.
1428 // Log the invalid package, and remove it
Sunny Goyal713edfc2016-05-06 09:58:34 -07001429 FileLog.d(TAG, "Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001430 itemsToRemove.add(id);
1431 continue;
Winson Chungee055712013-07-30 14:46:24 -07001432 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001433 } else if (cn == null) {
1434 // For shortcuts with no component, keep them as they are
1435 restoredRows.add(id);
1436 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001437 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001438 } catch (URISyntaxException e) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001439 FileLog.d(TAG, "Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001440 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001441 continue;
1442 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001443
Sunny Goyal34b65272015-03-11 16:56:52 -07001444 boolean useLowResIcon = container >= 0 &&
1445 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1446
Sunny Goyala32bf9b2017-01-05 14:03:59 -08001447 if (restored) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001448 if (user.equals(Process.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001449 info = getRestoredItemInfo(c, intent,
1450 promiseType, itemType, cursorIconInfo);
Kenny Guyed131872014-04-30 03:02:21 +01001451 intent = getRestoredItemIntent(c, context, intent);
1452 } else {
1453 // Don't restore items for other profiles.
1454 itemsToRemove.add(id);
1455 continue;
1456 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001457 } else if (itemType ==
1458 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001459 info = getAppShortcutInfo(intent, user, c,
1460 cursorIconInfo, allowMissingTarget, useLowResIcon);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001461 } else if (itemType ==
1462 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001463
1464 ShortcutKey key = ShortcutKey.fromIntent(intent, user);
1465 if (unlockedUsers.get(serialNumber)) {
1466 ShortcutInfoCompat pinnedShortcut =
1467 shortcutKeyToPinnedShortcuts.get(key);
1468 if (pinnedShortcut == null) {
1469 // The shortcut is no longer valid.
1470 itemsToRemove.add(id);
1471 continue;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001472 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001473 info = new ShortcutInfo(pinnedShortcut, context);
1474 intent = info.intent;
1475 } else {
1476 // Create a shortcut info in disabled mode for now.
1477 info = new ShortcutInfo();
1478 info.user = user;
1479 info.itemType = itemType;
1480 loadInfoFromCursor(info, c, cursorIconInfo);
1481
1482 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001483 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001484 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001485 info = getShortcutInfo(c, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001486
Sunny Goyald09c3702016-04-06 16:18:20 -07001487 // Shortcuts are only available on the primary profile
1488 if (PackageManagerHelper.isAppSuspended(manager, targetPackage)) {
1489 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1490 }
1491
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001492 // App shortcuts that used to be automatically added to Launcher
1493 // didn't always have the correct intent flags set, so do that
1494 // here
1495 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001496 intent.getCategories() != null &&
1497 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001498 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001499 intent.addFlags(
1500 Intent.FLAG_ACTIVITY_NEW_TASK |
1501 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1502 }
Michael Jurka96879562012-03-22 05:54:33 -07001503 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001504
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001505 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001506 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001507 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001508 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001509 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001510 info.cellX = c.getInt(cellXIndex);
1511 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08001512 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001513 info.spanX = 1;
1514 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01001515 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001516 if (info.promisedIntent != null) {
1517 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
1518 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001519 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001520 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1521 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1522 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001523
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001524 // check & update map of what's occupied
Sunny Goyale9956a72016-09-01 17:24:47 -07001525 if (!checkItemPlacement(occupied, info, sBgDataModel.workspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001526 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001527 break;
1528 }
1529
Sunny Goyal756adbc2015-04-16 15:20:51 -07001530 if (restored) {
1531 ComponentName cn = info.getTargetComponent();
1532 if (cn != null) {
1533 Integer progress = installingPkgs.get(cn.getPackageName());
1534 if (progress != null) {
1535 info.setInstallProgress(progress);
1536 } else {
1537 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
1538 }
1539 }
1540 }
1541
Sunny Goyale9956a72016-09-01 17:24:47 -07001542 sBgDataModel.addItem(info, false);
Winson Chung1323b482013-08-05 12:41:55 -07001543 } else {
1544 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001545 }
1546 break;
1547
1548 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1549 id = c.getLong(idIndex);
Sunny Goyale9956a72016-09-01 17:24:47 -07001550 FolderInfo folderInfo = sBgDataModel.findOrMakeFolder(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001551
Sunny Goyala508e4f2015-05-21 09:33:57 -07001552 // Do not trim the folder label, as is was set by the user.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001553 folderInfo.title = c.getString(cursorIconInfo.titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001554 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001555 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001556 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001557 folderInfo.cellX = c.getInt(cellXIndex);
1558 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001559 folderInfo.spanX = 1;
1560 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001561 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001562
Daniel Sandler8802e962010-05-26 16:28:16 -04001563 // check & update map of what's occupied
Sunny Goyale9956a72016-09-01 17:24:47 -07001564 if (!checkItemPlacement(occupied, folderInfo, sBgDataModel.workspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001565 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04001566 break;
1567 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001568 if (restored) {
1569 // no special handling required for restored folders
1570 restoredRows.add(id);
1571 }
1572
Sunny Goyale9956a72016-09-01 17:24:47 -07001573 sBgDataModel.addItem(folderInfo, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001574 break;
1575
1576 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08001577 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001578 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08001579 boolean customWidget = itemType ==
1580 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
1581
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001582 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001583 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001584 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001585 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001586 user = allUsers.get(serialNumber);
1587 if (user == null) {
1588 itemsToRemove.add(id);
1589 continue;
1590 }
1591
Sunny Goyalff572272014-07-23 13:58:07 -07001592 final ComponentName component =
1593 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04001594
Sunny Goyal651077b2014-06-30 14:15:31 -07001595 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07001596 final boolean isIdValid = (restoreStatus &
1597 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07001598 final boolean wasProviderReady = (restoreStatus &
1599 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07001600
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001601 if (widgetProvidersMap == null) {
1602 widgetProvidersMap = AppWidgetManagerCompat
1603 .getInstance(mContext).getAllProvidersMap();
1604 }
1605 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
1606 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00001607 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001608 user));
Sunny Goyalff572272014-07-23 13:58:07 -07001609
1610 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08001611 if (!isSafeMode && !customWidget &&
1612 wasProviderReady && !isProviderReady) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001613 FileLog.d(TAG, "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07001614 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001615 itemsToRemove.add(id);
1616 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07001617 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07001618 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1619 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08001620
Sunny Goyal53f96722015-07-13 19:54:53 -07001621 // The provider is available. So the widget is either
1622 // available or not available. We do not need to track
1623 // any future restore updates.
1624 int status = restoreStatus &
1625 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07001626 if (!wasProviderReady) {
1627 // If provider was not previously ready, update the
1628 // status and UI flag.
1629
1630 // Id would be valid only if the widget restore broadcast was received.
1631 if (isIdValid) {
Sunny Goyal86df1382016-08-10 15:03:22 -07001632 status |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07001633 } else {
1634 status &= ~LauncherAppWidgetInfo
1635 .FLAG_PROVIDER_NOT_READY;
1636 }
1637 }
1638 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07001639 } else {
1640 Log.v(TAG, "Widget restore pending id=" + id
1641 + " appWidgetId=" + appWidgetId
1642 + " status =" + restoreStatus);
1643 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07001644 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07001645 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001646 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07001647
1648 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
1649 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001650 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07001651 // App restore has started. Update the flag
1652 appWidgetInfo.restoreStatus |=
1653 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001654 } else if (!isSafeMode) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001655 FileLog.d(TAG, "Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07001656 itemsToRemove.add(id);
1657 continue;
1658 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07001659
1660 appWidgetInfo.installProgress =
1661 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07001662 }
Sunny Goyal86df1382016-08-10 15:03:22 -07001663 if (appWidgetInfo.hasRestoreFlag(
1664 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG)) {
1665 intentDescription = c.getString(intentIndex);
1666 if (!TextUtils.isEmpty(intentDescription)) {
1667 appWidgetInfo.bindOptions =
1668 Intent.parseUri(intentDescription, 0);
1669 }
1670 }
Sunny Goyalff572272014-07-23 13:58:07 -07001671
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001672 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07001673 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001674 appWidgetInfo.cellX = c.getInt(cellXIndex);
1675 appWidgetInfo.cellY = c.getInt(cellYIndex);
1676 appWidgetInfo.spanX = c.getInt(spanXIndex);
1677 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07001678 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04001679
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001680 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1681 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1682 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001683 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1684 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001685 continue;
1686 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001687
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001688 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001689 // check & update map of what's occupied
Sunny Goyale9956a72016-09-01 17:24:47 -07001690 if (!checkItemPlacement(occupied, appWidgetInfo, sBgDataModel.workspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001691 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001692 break;
1693 }
Sunny Goyal651077b2014-06-30 14:15:31 -07001694
Adam Cohen59400422014-03-05 18:07:04 -08001695 if (!customWidget) {
1696 String providerName =
1697 appWidgetInfo.providerName.flattenToString();
1698 if (!providerName.equals(savedProvider) ||
1699 (appWidgetInfo.restoreStatus != restoreStatus)) {
1700 ContentValues values = new ContentValues();
1701 values.put(
1702 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1703 providerName);
1704 values.put(LauncherSettings.Favorites.RESTORED,
1705 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001706 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08001707 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001708 }
Sunny Goyale9956a72016-09-01 17:24:47 -07001709 sBgDataModel.addItem(appWidgetInfo, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001710 }
Joe Onorato36115782010-06-17 13:28:48 -04001711 break;
1712 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001713 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001714 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001715 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001716 }
1717 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001718 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001719 }
1720
Winson Chungba9c37f2013-08-30 14:11:37 -07001721 // Break early if we've stopped loading
1722 if (mStopped) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001723 sBgDataModel.clear();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001724 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07001725 }
1726
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001727 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001728 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001729 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
1730 Utilities.createDbSelectionQuery(
1731 LauncherSettings.Favorites._ID, itemsToRemove), null);
1732 if (DEBUG_LOADERS) {
1733 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
1734 LauncherSettings.Favorites._ID, itemsToRemove));
1735 }
1736
1737 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07001738 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
1739 .call(contentResolver,
1740 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
1741 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
1742 for (long folderId : deletedFolderIds) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001743 sBgDataModel.workspaceItems.remove(sBgDataModel.folders.get(folderId));
1744 sBgDataModel.folders.remove(folderId);
1745 sBgDataModel.itemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001746 }
1747 }
1748
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001749 // Unpin shortcuts that don't exist on the workspace.
Sunny Goyalf75baa92016-11-22 03:23:51 +05301750 HashSet<ShortcutKey> pendingShortcuts =
1751 InstallShortcutReceiver.getPendingShortcuts(context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001752 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001753 MutableInt numTimesPinned = sBgDataModel.pinnedShortcutCounts.get(key);
Sunny Goyalf75baa92016-11-22 03:23:51 +05301754 if ((numTimesPinned == null || numTimesPinned.value == 0)
1755 && !pendingShortcuts.contains(key)) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001756 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301757 shortcutManager.unpinShortcut(key);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001758 }
1759 }
1760
Sunny Goyal317698b2015-07-29 11:45:41 -07001761 // Sort all the folder items and make sure the first 3 items are high resolution.
Sunny Goyale9956a72016-09-01 17:24:47 -07001762 for (FolderInfo folder : sBgDataModel.folders) {
Sunny Goyal317698b2015-07-29 11:45:41 -07001763 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
1764 int pos = 0;
1765 for (ShortcutInfo info : folder.contents) {
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001766 if (info.usingLowResIcon &&
1767 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal3fe4a142016-12-15 17:40:07 -08001768 mIconCache.getTitleAndIcon(info, false);
Sunny Goyal317698b2015-07-29 11:45:41 -07001769 }
1770 pos ++;
1771 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
1772 break;
1773 }
1774 }
1775 }
1776
Chris Wrenf4d08112014-01-16 18:13:56 -05001777 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001778 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001779 ContentValues values = new ContentValues();
1780 values.put(LauncherSettings.Favorites.RESTORED, 0);
1781 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
1782 Utilities.createDbSelectionQuery(
1783 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05001784 }
1785
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001786 if (!isSdCardReady && !pendingPackages.isEmpty()) {
1787 context.registerReceiver(
1788 new SdCardAvailableReceiver(
1789 LauncherModel.this, mContext, pendingPackages),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001790 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001791 null,
1792 sWorker);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001793 }
1794
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001795 // Remove any empty screens
Sunny Goyale9956a72016-09-01 17:24:47 -07001796 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgDataModel.workspaceScreens);
1797 for (ItemInfo item: sBgDataModel.itemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001798 long screenId = item.screenId;
1799 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1800 unusedScreens.contains(screenId)) {
1801 unusedScreens.remove(screenId);
1802 }
1803 }
1804
1805 // If there are any empty screens remove them, and update.
1806 if (unusedScreens.size() != 0) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001807 sBgDataModel.workspaceScreens.removeAll(unusedScreens);
1808 updateWorkspaceScreenOrder(context, sBgDataModel.workspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001809 }
1810
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001811 if (DEBUG_LOADERS) {
1812 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1813 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07001814 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07001815 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001816 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07001817
Sunny Goyale2df0622015-04-24 11:27:00 -07001818 for (int i = 0; i < nScreens; i++) {
1819 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07001820 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001821 line += " | ";
1822 }
Joe Onorato36115782010-06-17 13:28:48 -04001823 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001824 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04001825 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001826 }
Joe Onorato36115782010-06-17 13:28:48 -04001827 }
Sunny Goyale26d1002016-06-20 14:52:14 -07001828 if (LauncherAppState.PROFILE_STARTUP) {
1829 Trace.endSection();
1830 }
Adam Cohene25af792013-06-06 23:08:25 -07001831 }
1832
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001833 /**
1834 * Partially updates the item without any notification. Must be called on the worker thread.
1835 */
1836 private void updateItem(long itemId, ContentValues update) {
1837 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001838 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001839 update,
1840 BaseColumns._ID + "= ?",
1841 new String[]{Long.toString(itemId)});
1842 }
1843
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001844 /** Filters the set of items who are directly or indirectly (via another container) on the
1845 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08001846 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001847 ArrayList<ItemInfo> allWorkspaceItems,
1848 ArrayList<ItemInfo> currentScreenItems,
1849 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001850 // Purge any null ItemInfos
1851 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
1852 while (iter.hasNext()) {
1853 ItemInfo i = iter.next();
1854 if (i == null) {
1855 iter.remove();
1856 }
1857 }
1858
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001859 // Order the set of items by their containers first, this allows use to walk through the
1860 // list sequentially, build up a list of containers that are in the specified screen,
1861 // as well as all items in those containers.
1862 Set<Long> itemsOnScreen = new HashSet<Long>();
1863 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
1864 @Override
1865 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07001866 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001867 }
1868 });
1869 for (ItemInfo info : allWorkspaceItems) {
1870 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001871 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001872 currentScreenItems.add(info);
1873 itemsOnScreen.add(info.id);
1874 } else {
1875 otherScreenItems.add(info);
1876 }
1877 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1878 currentScreenItems.add(info);
1879 itemsOnScreen.add(info.id);
1880 } else {
1881 if (itemsOnScreen.contains(info.container)) {
1882 currentScreenItems.add(info);
1883 itemsOnScreen.add(info.id);
1884 } else {
1885 otherScreenItems.add(info);
1886 }
1887 }
1888 }
1889 }
1890
1891 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08001892 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001893 ArrayList<LauncherAppWidgetInfo> appWidgets,
1894 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
1895 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001896
1897 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001898 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001899 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08001900 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001901 currentScreenWidgets.add(widget);
1902 } else {
1903 otherScreenWidgets.add(widget);
1904 }
1905 }
1906 }
1907
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001908 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
1909 * right) */
1910 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07001911 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001912 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07001913 final int screenCols = profile.numColumns;
1914 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001915 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07001916 @Override
1917 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07001918 if (lhs.container == rhs.container) {
1919 // Within containers, order by their spatial position in that container
1920 switch ((int) lhs.container) {
1921 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
1922 long lr = (lhs.screenId * screenCellCount +
1923 lhs.cellY * screenCols + lhs.cellX);
1924 long rr = (rhs.screenId * screenCellCount +
1925 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07001926 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07001927 }
1928 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
1929 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07001930 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07001931 }
1932 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07001933 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07001934 throw new RuntimeException("Unexpected container type when " +
1935 "sorting workspace items.");
1936 }
1937 return 0;
1938 }
1939 } else {
1940 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07001941 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07001942 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001943 }
1944 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001945 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001946
Adam Cohendcd297f2013-06-18 13:13:40 -07001947 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
1948 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001949 final Runnable r = new Runnable() {
1950 @Override
1951 public void run() {
1952 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1953 if (callbacks != null) {
1954 callbacks.bindScreens(orderedScreens);
1955 }
1956 }
1957 };
Sunny Goyald33860f2015-04-23 16:02:20 -07001958 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07001959 }
1960
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001961 private void bindWorkspaceItems(final Callbacks oldCallbacks,
1962 final ArrayList<ItemInfo> workspaceItems,
1963 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07001964 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001965
1966 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07001967 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001968 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04001969 final int start = i;
1970 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001971 final Runnable r = new Runnable() {
1972 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001973 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001974 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001975 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07001976 callbacks.bindItems(workspaceItems, start, start+chunkSize,
1977 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001978 }
1979 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001980 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001981 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04001982 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001983
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001984 // Bind the widgets, one at a time
1985 N = appWidgets.size();
1986 for (int i = 0; i < N; i++) {
1987 final LauncherAppWidgetInfo widget = appWidgets.get(i);
1988 final Runnable r = new Runnable() {
1989 public void run() {
1990 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1991 if (callbacks != null) {
1992 callbacks.bindAppWidget(widget);
1993 }
1994 }
1995 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001996 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001997 }
1998 }
1999
2000 /**
2001 * Binds all loaded data to actual views on the main thread.
2002 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002003 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002004 final long t = SystemClock.uptimeMillis();
2005 Runnable r;
2006
2007 // Don't use these two variables in any of the callback runnables.
2008 // Otherwise we hold a reference to them.
2009 final Callbacks oldCallbacks = mCallbacks.get();
2010 if (oldCallbacks == null) {
2011 // This launcher has exited and nobody bothered to tell us. Just bail.
2012 Log.w(TAG, "LoaderTask running with no launcher");
2013 return;
2014 }
2015
Winson Chung9b9fb962013-11-15 15:39:34 -08002016 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07002017 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
2018 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
2019 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002020
Sunny Goyale9956a72016-09-01 17:24:47 -07002021 synchronized (sBgDataModel) {
2022 workspaceItems.addAll(sBgDataModel.workspaceItems);
2023 appWidgets.addAll(sBgDataModel.appWidgets);
2024 orderedScreenIds.addAll(sBgDataModel.workspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002025 }
2026
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002027 final int currentScreen;
2028 {
2029 int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE
2030 ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen();
2031 if (currScreen >= orderedScreenIds.size()) {
2032 // There may be no workspace screens (just hotseat items and an empty page).
2033 currScreen = PagedView.INVALID_RESTORE_PAGE;
2034 }
2035 currentScreen = currScreen;
Winson Chung9b9fb962013-11-15 15:39:34 -08002036 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002037 final boolean validFirstPage = currentScreen >= 0;
2038 final long currentScreenId =
2039 validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID;
Winson Chung9b9fb962013-11-15 15:39:34 -08002040
Winson Chung9b9fb962013-11-15 15:39:34 -08002041 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07002042 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
2043 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
2044 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
2045 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002046
Winson Chung9b9fb962013-11-15 15:39:34 -08002047 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002048 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002049 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002050 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002051 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2052 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2053
2054 // Tell the workspace that we're about to start binding items
2055 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002056 public void run() {
2057 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2058 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002059 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002060 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002061 }
2062 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002063 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002064 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002065
Adam Cohendcd297f2013-06-18 13:13:40 -07002066 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2067
Sunny Goyal527c7d32015-08-28 15:19:36 -07002068 Executor mainExecutor = new DeferredMainThreadExecutor();
2069 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07002070 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002071
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002072 // In case of validFirstPage, only bind the first screen, and defer binding the
2073 // remaining screens after first onDraw (and an optional the fade animation whichever
2074 // happens later).
2075 // This ensures that the first screen is immediately visible (eg. during rotation)
2076 // In case of !validFirstPage, bind all pages one after other.
2077 final Executor deferredExecutor =
2078 validFirstPage ? new ViewOnDrawExecutor(mHandler) : mainExecutor;
2079
2080 mainExecutor.execute(new Runnable() {
2081 @Override
2082 public void run() {
2083 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2084 if (callbacks != null) {
2085 callbacks.finishFirstPageBind(
2086 validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null);
2087 }
2088 }
2089 });
Sunny Goyal527c7d32015-08-28 15:19:36 -07002090
Sunny Goyal44c06432016-04-02 10:56:02 -07002091 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002092
2093 // Tell the workspace that we're done binding items
2094 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002095 public void run() {
2096 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2097 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002098 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002099 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002100
Sunny Goyal639e9062015-08-19 19:17:06 -07002101 mIsLoadingAndBindingWorkspace = false;
2102
2103 // Run all the bind complete runnables after workspace is bound.
2104 if (!mBindCompleteRunnables.isEmpty()) {
2105 synchronized (mBindCompleteRunnables) {
2106 for (final Runnable r : mBindCompleteRunnables) {
2107 runOnWorkerThread(r);
2108 }
2109 mBindCompleteRunnables.clear();
2110 }
2111 }
2112
Winson Chung98e030b2012-05-07 16:01:11 -07002113 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002114 if (DEBUG_LOADERS) {
2115 Log.d(TAG, "bound workspace in "
2116 + (SystemClock.uptimeMillis()-t) + "ms");
2117 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002118
Joe Onorato36115782010-06-17 13:28:48 -04002119 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002120 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002121 deferredExecutor.execute(r);
2122
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002123 if (validFirstPage) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002124 r = new Runnable() {
2125 public void run() {
2126 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2127 if (callbacks != null) {
2128 // We are loading synchronously, which means, some of the pages will be
2129 // bound after first draw. Inform the callbacks that page binding is
2130 // not complete, and schedule the remaining pages.
2131 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2132 callbacks.onPageBoundSynchronously(currentScreen);
2133 }
2134 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2135 }
2136 }
2137 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002138 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002139 }
Joe Onorato36115782010-06-17 13:28:48 -04002140 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002141
Joe Onorato36115782010-06-17 13:28:48 -04002142 private void loadAndBindAllApps() {
2143 if (DEBUG_LOADERS) {
2144 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2145 }
2146 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002147 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002148 synchronized (LoaderTask.this) {
2149 if (mStopped) {
2150 return;
2151 }
2152 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002153 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002154 synchronized (LoaderTask.this) {
2155 if (mStopped) {
2156 return;
2157 }
2158 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002159 }
Joe Onorato36115782010-06-17 13:28:48 -04002160 } else {
2161 onlyBindAllApps();
2162 }
2163 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002164
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002165 private void updateIconCache() {
2166 // Ignore packages which have a promise icon.
2167 HashSet<String> packagesToIgnore = new HashSet<>();
Sunny Goyale9956a72016-09-01 17:24:47 -07002168 synchronized (sBgDataModel) {
2169 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002170 if (info instanceof ShortcutInfo) {
2171 ShortcutInfo si = (ShortcutInfo) info;
2172 if (si.isPromise() && si.getTargetComponent() != null) {
2173 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2174 }
2175 } else if (info instanceof LauncherAppWidgetInfo) {
2176 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2177 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2178 packagesToIgnore.add(lawi.providerName.getPackageName());
2179 }
2180 }
2181 }
2182 }
2183 mIconCache.updateDbIcons(packagesToIgnore);
2184 }
2185
Joe Onorato36115782010-06-17 13:28:48 -04002186 private void onlyBindAllApps() {
2187 final Callbacks oldCallbacks = mCallbacks.get();
2188 if (oldCallbacks == null) {
2189 // This launcher has exited and nobody bothered to tell us. Just bail.
2190 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2191 return;
2192 }
2193
2194 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002195 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002196 final ArrayList<AppInfo> list
2197 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002198 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002199 public void run() {
2200 final long t = SystemClock.uptimeMillis();
2201 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2202 if (callbacks != null) {
2203 callbacks.bindAllApplications(list);
2204 }
2205 if (DEBUG_LOADERS) {
2206 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002207 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04002208 }
2209 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002210 };
Tony Wickham80f57872016-06-29 18:12:15 -07002211 runOnMainThread(r);
Joe Onorato36115782010-06-17 13:28:48 -04002212 }
2213
Winson Chung64359a52013-07-08 17:17:08 -07002214 private void loadAllApps() {
2215 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002216
Joe Onorato36115782010-06-17 13:28:48 -04002217 final Callbacks oldCallbacks = mCallbacks.get();
2218 if (oldCallbacks == null) {
2219 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002220 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002221 return;
2222 }
2223
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002224 final List<UserHandle> profiles = mUserManager.getUserProfiles();
Kenny Guyed131872014-04-30 03:02:21 +01002225
Winson Chung64359a52013-07-08 17:17:08 -07002226 // Clear the list of apps
2227 mBgAllAppsList.clear();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002228 for (UserHandle user : profiles) {
Kenny Guyed131872014-04-30 03:02:21 +01002229 // Query for the set of apps
2230 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002231 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002232 if (DEBUG_LOADERS) {
2233 Log.d(TAG, "getActivityList took "
2234 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2235 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2236 }
2237 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002238 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002239 if (apps == null || apps.isEmpty()) {
2240 return;
2241 }
Kenny Guyff05f432016-01-22 17:48:29 +00002242 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002243 // Create the ApplicationInfos
2244 for (int i = 0; i < apps.size(); i++) {
2245 LauncherActivityInfoCompat app = apps.get(i);
2246 // This builds the icon bitmaps.
Kenny Guyff05f432016-01-22 17:48:29 +00002247 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode));
Kenny Guyed131872014-04-30 03:02:21 +01002248 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002249
Sunny Goyal756a28a2015-04-23 17:07:55 -07002250 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2251 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002252 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002253
2254 @Override
2255 public void run() {
2256 heuristic.processUserApps(apps);
2257 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002258 };
2259 runOnMainThread(new Runnable() {
2260
2261 @Override
2262 public void run() {
2263 // Check isLoadingWorkspace on the UI thread, as it is updated on
2264 // the UI thread.
2265 if (mIsLoadingAndBindingWorkspace) {
2266 synchronized (mBindCompleteRunnables) {
2267 mBindCompleteRunnables.add(r);
2268 }
2269 } else {
2270 runOnWorkerThread(r);
2271 }
2272 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002273 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002274 }
Winson Chung64359a52013-07-08 17:17:08 -07002275 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002276 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002277 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2278 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002279
2280 // Post callback on main thread
2281 mHandler.post(new Runnable() {
2282 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002283
Winson Chung64359a52013-07-08 17:17:08 -07002284 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002285 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002286 if (callbacks != null) {
2287 callbacks.bindAllApplications(added);
2288 if (DEBUG_LOADERS) {
2289 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002290 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07002291 }
2292 } else {
2293 Log.i(TAG, "not binding apps: no Launcher activity");
2294 }
2295 }
2296 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002297 // Cleanup any data stored for a deleted user.
2298 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04002299 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002300 Log.d(TAG, "Icons processed in "
2301 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002302 }
2303 }
2304
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002305 private void loadAndBindDeepShortcuts() {
2306 if (DEBUG_LOADERS) {
2307 Log.d(TAG, "loadAndBindDeepShortcuts mDeepShortcutsLoaded=" + mDeepShortcutsLoaded);
2308 }
2309 if (!mDeepShortcutsLoaded) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07002310 sBgDataModel.deepShortcutMap.clear();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302311 DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(mContext);
2312 mHasShortcutHostPermission = shortcutManager.hasHostPermission();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002313 if (mHasShortcutHostPermission) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002314 for (UserHandle user : mUserManager.getUserProfiles()) {
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002315 if (mUserManager.isUserUnlocked(user)) {
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302316 List<ShortcutInfoCompat> shortcuts =
2317 shortcutManager.queryForAllShortcuts(user);
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07002318 sBgDataModel.updateDeepShortcutMap(null, user, shortcuts);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002319 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002320 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002321 }
2322 synchronized (LoaderTask.this) {
2323 if (mStopped) {
2324 return;
2325 }
2326 mDeepShortcutsLoaded = true;
2327 }
2328 }
Tony Wickham80f57872016-06-29 18:12:15 -07002329 bindDeepShortcuts();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002330 }
2331
Joe Onoratobe386092009-11-17 17:32:16 -08002332 public void dumpState() {
Sunny Goyale9956a72016-09-01 17:24:47 -07002333 synchronized (sBgDataModel) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002334 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002335 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2336 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
Sunny Goyale9956a72016-09-01 17:24:47 -07002337 Log.d(TAG, "mItems size=" + sBgDataModel.workspaceItems.size());
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002338 }
Joe Onorato36115782010-06-17 13:28:48 -04002339 }
2340 }
2341
Tony Wickham80f57872016-06-29 18:12:15 -07002342 public void bindDeepShortcuts() {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07002343 final MultiHashMap<ComponentKey, String> shortcutMapCopy =
2344 sBgDataModel.deepShortcutMap.clone();
Tony Wickham80f57872016-06-29 18:12:15 -07002345 Runnable r = new Runnable() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002346 @Override
2347 public void run() {
2348 Callbacks callbacks = getCallback();
2349 if (callbacks != null) {
2350 callbacks.bindDeepShortcutMap(shortcutMapCopy);
2351 }
2352 }
Tony Wickham80f57872016-06-29 18:12:15 -07002353 };
2354 runOnMainThread(r);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002355 }
2356
Sunny Goyal75b0f552015-05-20 21:57:06 -07002357 /**
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002358 * Refreshes the cached shortcuts if the shortcut permission has changed.
2359 * Current implementation simply reloads the workspace, but it can be optimized to
2360 * use partial updates similar to {@link UserManagerCompat}
2361 */
2362 public void refreshShortcutsIfRequired() {
Sunny Goyalf5e37442016-11-02 10:31:24 -07002363 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002364 sWorker.removeCallbacks(mShortcutPermissionCheckRunnable);
2365 sWorker.post(mShortcutPermissionCheckRunnable);
2366 }
2367 }
2368
2369 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -07002370 * Called when the icons for packages have been updated in the icon cache.
2371 */
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002372 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandle user) {
Sunny Goyal75b0f552015-05-20 21:57:06 -07002373 // If any package icon has changed (app was updated while launcher was dead),
2374 // update the corresponding shortcuts.
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002375 enqueueModelUpdateTask(new CacheDataUpdatedTask(
2376 CacheDataUpdatedTask.OP_CACHE_UPDATE, user, updatedPackages));
Sunny Goyal75b0f552015-05-20 21:57:06 -07002377 }
2378
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002379 void enqueueModelUpdateTask(BaseModelUpdateTask task) {
2380 task.init(this);
2381 runOnWorkerThread(task);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002382 }
2383
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002384 /**
2385 * A task to be executed on the current callbacks on the UI thread.
2386 * If there is no current callbacks, the task is ignored.
2387 */
2388 public interface CallbackTask {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002389
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002390 void execute(Callbacks callbacks);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002391 }
2392
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002393 /**
2394 * A runnable which changes/updates the data model of the launcher based on certain events.
2395 */
2396 public static abstract class BaseModelUpdateTask implements Runnable {
Joe Onorato36115782010-06-17 13:28:48 -04002397
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002398 private LauncherModel mModel;
2399 private DeferredHandler mUiHandler;
Joe Onorato36115782010-06-17 13:28:48 -04002400
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002401 /* package private */
2402 void init(LauncherModel model) {
2403 mModel = model;
2404 mUiHandler = mModel.mHandler;
Joe Onorato36115782010-06-17 13:28:48 -04002405 }
2406
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002407 @Override
Joe Onorato36115782010-06-17 13:28:48 -04002408 public void run() {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002409 if (!mModel.mHasLoaderCompletedOnce) {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002410 // Loader has not yet run.
2411 return;
2412 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002413 execute(mModel.mApp, sBgDataModel, mModel.mBgAllAppsList);
2414 }
Joe Onorato36115782010-06-17 13:28:48 -04002415
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002416 /**
2417 * Execute the actual task. Called on the worker thread.
2418 */
2419 public abstract void execute(
2420 LauncherAppState app, BgDataModel dataModel, AllAppsList apps);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002421
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002422 /**
2423 * Schedules a {@param task} to be executed on the current callbacks.
2424 */
2425 public final void scheduleCallbackTask(final CallbackTask task) {
2426 final Callbacks callbacks = mModel.getCallback();
2427 mUiHandler.post(new Runnable() {
2428 public void run() {
2429 Callbacks cb = mModel.getCallback();
2430 if (callbacks == cb && cb != null) {
2431 task.execute(callbacks);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002432 }
2433 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002434 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002435 }
2436 }
2437
Sunny Goyal10923b32016-07-20 15:42:44 -07002438 /**
2439 * Repopulates the shortcut info, possibly updating any icon already on the workspace.
2440 */
2441 public void updateShortcutInfo(final ShortcutInfoCompat fullDetail, final ShortcutInfo info) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002442 enqueueModelUpdateTask(new ExtendedModelTask() {
Sunny Goyal10923b32016-07-20 15:42:44 -07002443 @Override
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002444 public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
2445 info.updateFromDeepShortcutInfo(fullDetail, app.getContext());
2446
2447 ArrayList<ShortcutInfo> update = new ArrayList<>();
Sunny Goyal10923b32016-07-20 15:42:44 -07002448 update.add(info);
2449 bindUpdatedShortcuts(update, fullDetail.getUserHandle());
2450 }
2451 });
2452 }
2453
Sunny Goyald164b7f2016-10-12 20:49:31 -07002454 private void bindWidgetsModel(final Callbacks callbacks) {
2455 final MultiHashMap<PackageItemInfo, WidgetItem> widgets
2456 = mBgWidgetsModel.getWidgetsMap().clone();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002457 mHandler.post(new Runnable() {
2458 @Override
2459 public void run() {
2460 Callbacks cb = getCallback();
2461 if (callbacks == cb && cb != null) {
Sunny Goyald164b7f2016-10-12 20:49:31 -07002462 callbacks.bindAllWidgets(widgets);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002463 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07002464 }
2465 });
2466 }
2467
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002468 public void refreshAndBindWidgetsAndShortcuts(
2469 final Callbacks callbacks, final boolean bindFirst) {
2470 runOnWorkerThread(new Runnable() {
2471 @Override
2472 public void run() {
2473 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
Sunny Goyald164b7f2016-10-12 20:49:31 -07002474 bindWidgetsModel(callbacks);
Sunny Goyal31860582015-09-18 08:38:57 -07002475 }
Sunny Goyald164b7f2016-10-12 20:49:31 -07002476 ArrayList<WidgetItem> allWidgets = mBgWidgetsModel.update(mApp.getContext());
2477 bindWidgetsModel(callbacks);
2478
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002479 // update the Widget entries inside DB on the worker thread.
Sunny Goyald164b7f2016-10-12 20:49:31 -07002480 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(allWidgets);
Sunny Goyal31860582015-09-18 08:38:57 -07002481 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002482 });
Michael Jurkac402cd92013-05-20 15:49:32 +02002483 }
2484
Joe Onorato9c1289c2009-08-17 11:03:03 -04002485 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05002486 * Make an ShortcutInfo object for a restored application or shortcut item that points
2487 * to a package that is not yet installed on the system.
2488 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002489 public ShortcutInfo getRestoredItemInfo(Cursor c, Intent intent,
2490 int promiseType, int itemType, CursorIconInfo iconInfo) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002491 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002492 info.user = Process.myUserHandle();
Sunny Goyal3fe4a142016-12-15 17:40:07 -08002493 info.promisedIntent = intent;
2494
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002495 info.iconBitmap = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002496 // the fallback icon
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002497 if (info.iconBitmap == null) {
Sunny Goyal3fe4a142016-12-15 17:40:07 -08002498 mIconCache.getTitleAndIcon(info, false /* useLowResIcon */);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002499 }
Sunny Goyal34942622014-08-29 17:20:55 -07002500
2501 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002502 String title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07002503 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07002504 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07002505 }
Sunny Goyal34942622014-08-29 17:20:55 -07002506 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
2507 if (TextUtils.isEmpty(info.title)) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002508 info.title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07002509 }
Sunny Goyal34942622014-08-29 17:20:55 -07002510 } else {
2511 throw new InvalidParameterException("Invalid restoreType " + promiseType);
2512 }
2513
Winson Chung82b016c2015-05-08 17:00:10 -07002514 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07002515 info.itemType = itemType;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002516 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05002517 return info;
2518 }
2519
2520 /**
2521 * Make an Intent object for a restored application or shortcut item that points
2522 * to the market page for the item.
2523 */
Adam Cohen091440a2015-03-18 14:16:05 -07002524 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002525 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002526 return getMarketIntent(componentName.getPackageName());
2527 }
2528
2529 static Intent getMarketIntent(String packageName) {
2530 return new Intent(Intent.ACTION_VIEW)
2531 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05002532 .scheme("market")
2533 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002534 .appendQueryParameter("id", packageName)
2535 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05002536 }
2537
2538 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002539 * Make an ShortcutInfo object for a shortcut that is an application.
2540 *
2541 * If c is not null, then it will be used to fill in missing data like the title and icon.
2542 */
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002543 public ShortcutInfo getAppShortcutInfo(Intent intent, UserHandle user, Cursor c,
2544 CursorIconInfo iconInfo, boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01002545 if (user == null) {
2546 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002547 return null;
2548 }
2549
Kenny Guyed131872014-04-30 03:02:21 +01002550 ComponentName componentName = intent.getComponent();
2551 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08002552 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01002553 return null;
2554 }
2555
2556 Intent newIntent = new Intent(intent.getAction(), null);
2557 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2558 newIntent.setComponent(componentName);
2559 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002560 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01002561 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
2562 return null;
2563 }
2564
2565 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal3fe4a142016-12-15 17:40:07 -08002566 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
2567 info.user = user;
2568 info.intent = newIntent;
2569
2570 mIconCache.getTitleAndIcon(info, lai, useLowResIcon);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002571 if (mIconCache.isDefaultIcon(info.iconBitmap, user) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002572 Bitmap icon = iconInfo.loadIcon(c);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002573 info.iconBitmap = icon != null ? icon : mIconCache.getDefaultIcon(user);
Kenny Guyed131872014-04-30 03:02:21 +01002574 }
2575
Sunny Goyald09c3702016-04-06 16:18:20 -07002576 if (lai != null && PackageManagerHelper.isAppSuspended(lai.getApplicationInfo())) {
2577 info.isDisabled = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
2578 }
2579
Joe Onorato56d82912010-03-07 14:32:10 -05002580 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002581 if (TextUtils.isEmpty(info.title) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002582 info.title = iconInfo.getTitle(c);
Joe Onorato56d82912010-03-07 14:32:10 -05002583 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002584
Joe Onorato56d82912010-03-07 14:32:10 -05002585 // fall back to the class name of the activity
2586 if (info.title == null) {
2587 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002588 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002589
Winson Chung82b016c2015-05-08 17:00:10 -07002590 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002591 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002592 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07002593
Sunny Goyal1a745e82014-10-02 15:58:31 -07002594 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08002595 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002596 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002597 @Thunk ShortcutInfo getShortcutInfo(Cursor c, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002598 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01002599 // Non-app shortcuts are only supported for current user.
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002600 info.user = Process.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002601 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002602
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002603 // TODO: If there's an explicit component and we can't install that, delete it.
2604
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002605 loadInfoFromCursor(info, c, iconInfo);
2606 return info;
2607 }
Joe Onorato56d82912010-03-07 14:32:10 -05002608
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002609 /**
2610 * Make an ShortcutInfo object for a shortcut that isn't an application.
2611 */
2612 public void loadInfoFromCursor(ShortcutInfo info, Cursor c, CursorIconInfo iconInfo) {
2613 info.title = iconInfo.getTitle(c);
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002614 info.iconBitmap = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002615 // the fallback icon
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002616 if (info.iconBitmap == null) {
2617 info.iconBitmap = mIconCache.getDefaultIcon(info.user);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002618 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002619 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002620
Sunny Goyal2350bc92014-10-14 16:42:54 -07002621 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08002622 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
2623 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
2624 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
2625
Adam Cohend9198822011-11-22 16:42:47 -08002626 if (intent == null) {
2627 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
2628 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
2629 return null;
2630 }
2631
Michael Jurkac9d95c52011-08-29 14:03:34 -07002632 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05002633
Kenny Guyed131872014-04-30 03:02:21 +01002634 // Only support intents for current user for now. Intents sent from other
2635 // users wouldn't get here without intent forwarding anyway.
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002636 info.user = Process.myUserHandle();
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002637
2638 if (bitmap instanceof Bitmap) {
2639 info.iconBitmap = LauncherIcons.createIconBitmap((Bitmap) bitmap, context);
2640 } else {
2641 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
2642 if (extra instanceof ShortcutIconResource) {
2643 info.iconResource = (ShortcutIconResource) extra;
2644 info.iconBitmap = LauncherIcons.createIconBitmap(info.iconResource, context);
2645 }
Joe Onorato56d82912010-03-07 14:32:10 -05002646 }
Sunny Goyal1cd01b02016-11-09 10:43:58 -08002647 if (info.iconBitmap == null) {
2648 info.iconBitmap = mIconCache.getDefaultIcon(info.user);
2649 }
Joe Onorato56d82912010-03-07 14:32:10 -05002650
Winson Chung82b016c2015-05-08 17:00:10 -07002651 info.title = Utilities.trim(name);
2652 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002653 info.intent = intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -08002654
2655 return info;
2656 }
2657
Sunny Goyal651077b2014-06-30 14:15:31 -07002658 static boolean isValidProvider(AppWidgetProviderInfo provider) {
2659 return (provider != null) && (provider.provider != null)
2660 && (provider.provider.getPackageName() != null);
2661 }
2662
Joe Onoratobe386092009-11-17 17:32:16 -08002663 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08002664 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002665 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
2666 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
2667 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
2668 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04002669 if (mLoaderTask != null) {
2670 mLoaderTask.dumpState();
2671 } else {
2672 Log.d(TAG, "mLoaderTask=null");
2673 }
Joe Onoratobe386092009-11-17 17:32:16 -08002674 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002675
2676 public Callbacks getCallback() {
2677 return mCallbacks != null ? mCallbacks.get() : null;
2678 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002679
2680 /**
2681 * @return {@link FolderInfo} if its already loaded.
2682 */
2683 public FolderInfo findFolderById(Long folderId) {
Sunny Goyale9956a72016-09-01 17:24:47 -07002684 synchronized (sBgDataModel) {
2685 return sBgDataModel.folders.get(folderId);
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002686 }
2687 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002688
Sunny Goyal527c7d32015-08-28 15:19:36 -07002689 @Thunk class DeferredMainThreadExecutor implements Executor {
2690
2691 @Override
2692 public void execute(Runnable command) {
2693 runOnMainThread(command);
2694 }
2695 }
2696
Sunny Goyal756adbc2015-04-16 15:20:51 -07002697 /**
2698 * @return the looper for the worker thread which can be used to start background tasks.
2699 */
2700 public static Looper getWorkerLooper() {
2701 return sWorkerThread.getLooper();
2702 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002703}