blob: 2389d8433048c0a5f844ede763fde1a9beb994a5 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Romain Guy629de3e2010-01-13 12:20:59 -080019import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070020import android.content.BroadcastReceiver;
21import android.content.ComponentName;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070022import android.content.ContentProviderOperation;
23import android.content.ContentResolver;
24import android.content.ContentValues;
25import android.content.Context;
26import android.content.Intent;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070027import android.content.IntentFilter;
Sunny Goyal3e9be432017-01-05 15:22:41 -080028import android.content.pm.LauncherActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.content.pm.PackageManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.net.Uri;
Joe Onorato36115782010-06-17 13:28:48 -040031import android.os.Handler;
32import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070033import android.os.Looper;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040035import android.os.SystemClock;
Sunny Goyale26d1002016-06-20 14:52:14 -070036import android.os.Trace;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080037import android.os.UserHandle;
Winson Chunga90303b2013-11-15 13:05:06 -080038import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070039import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080040import android.util.LongSparseArray;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070041import android.util.MutableInt;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010042
Sunny Goyalffe83f12014-08-14 17:39:34 -070043import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010044import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070045import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070046import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010047import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyal6c56c682015-07-16 14:09:05 -070048import com.android.launcher3.config.ProviderConfig;
Tony Wickham827cef22016-03-17 15:39:39 -070049import com.android.launcher3.dynamicui.ExtractionUtils;
Sunny Goyal26119432016-02-18 22:09:23 +000050import com.android.launcher3.folder.Folder;
51import com.android.launcher3.folder.FolderIcon;
Sunny Goyal1b072632017-01-18 11:30:23 -080052import com.android.launcher3.graphics.LauncherIcons;
Sunny Goyal1acb9e92016-05-16 12:41:09 -070053import com.android.launcher3.logging.FileLog;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070054import com.android.launcher3.model.AddWorkspaceItemsTask;
Sunny Goyale9956a72016-09-01 17:24:47 -070055import com.android.launcher3.model.BgDataModel;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070056import com.android.launcher3.model.CacheDataUpdatedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080057import com.android.launcher3.model.ExtendedModelTask;
Sunny Goyalf862a262015-12-14 14:27:38 -080058import com.android.launcher3.model.GridSizeMigrationTask;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -080059import com.android.launcher3.model.LoaderCursor;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070060import com.android.launcher3.model.PackageInstallStateChangedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080061import com.android.launcher3.model.PackageItemInfo;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070062import com.android.launcher3.model.PackageUpdatedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080063import com.android.launcher3.model.SdCardAvailableReceiver;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070064import com.android.launcher3.model.ShortcutsChangedTask;
65import com.android.launcher3.model.UserLockStateChangedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080066import com.android.launcher3.model.WidgetItem;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070067import com.android.launcher3.model.WidgetsModel;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -070068import com.android.launcher3.provider.ImportDataTask;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070069import com.android.launcher3.provider.LauncherDbUtils;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070070import com.android.launcher3.shortcuts.DeepShortcutManager;
71import com.android.launcher3.shortcuts.ShortcutInfoCompat;
72import com.android.launcher3.shortcuts.ShortcutKey;
Robin Lee26ace122015-03-16 19:41:43 +000073import com.android.launcher3.util.ComponentKey;
Sunny Goyal32f3dda2016-11-11 11:45:00 -080074import com.android.launcher3.util.ContentWriter;
Sunny Goyal40452cf2016-09-01 15:17:46 -070075import com.android.launcher3.util.ItemInfoMatcher;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070076import com.android.launcher3.util.ManagedProfileHeuristic;
Tony Wickhamd82a39d2016-07-01 15:44:13 -070077import com.android.launcher3.util.MultiHashMap;
Sunny Goyald09c3702016-04-06 16:18:20 -070078import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070079import com.android.launcher3.util.Preconditions;
Sunny Goyal2bcbe132016-11-16 09:23:42 -080080import com.android.launcher3.util.Provider;
Adam Cohen091440a2015-03-18 14:16:05 -070081import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070082import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080083
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -080084import java.io.FileDescriptor;
85import java.io.PrintWriter;
Michael Jurkac2f801e2011-07-12 14:19:46 -070086import java.lang.ref.WeakReference;
87import java.net.URISyntaxException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070088import java.util.ArrayList;
89import java.util.Collections;
90import java.util.Comparator;
91import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070092import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070093import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070094import java.util.List;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070095import java.util.Map;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070096import java.util.Set;
Sunny Goyal527c7d32015-08-28 15:19:36 -070097import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -070098
The Android Open Source Project31dd5032009-03-03 19:32:27 -080099/**
100 * Maintains in-memory state of the Launcher. It is expected that there should be only one
101 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700102 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800103 */
Kenny Guyed131872014-04-30 03:02:21 +0100104public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +0100105 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800106 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -0400107 private static final boolean DEBUG_RECEIVER = false;
Chris Wrenb358f812014-04-16 13:37:00 -0400108
Joe Onorato9c1289c2009-08-17 11:03:03 -0400109 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700110
Joe Onorato36115782010-06-17 13:28:48 -0400111 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500112 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800113
Adam Cohen091440a2015-03-18 14:16:05 -0700114 @Thunk final LauncherAppState mApp;
115 @Thunk final Object mLock = new Object();
116 @Thunk DeferredHandler mHandler = new DeferredHandler();
117 @Thunk LoaderTask mLoaderTask;
118 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700119 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120
Adam Cohen091440a2015-03-18 14:16:05 -0700121 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700122 static {
123 sWorkerThread.start();
124 }
Adam Cohen091440a2015-03-18 14:16:05 -0700125 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700126
Joe Onoratocc67f472010-06-08 10:54:30 -0700127 // We start off with everything not loaded. After that, we assume that
128 // our monitoring of the package manager provides all updates and we never
129 // need to do a requery. These are only ever touched from the loader thread.
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700130 private boolean mWorkspaceLoaded;
131 private boolean mAllAppsLoaded;
132 private boolean mDeepShortcutsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700133
Sunny Goyal756a28a2015-04-23 17:07:55 -0700134 /**
135 * Set of runnables to be called on the background thread after the workspace binding
136 * is complete.
137 */
138 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
139
Adam Cohen091440a2015-03-18 14:16:05 -0700140 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700142 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800143 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700144 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800145 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800146
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700147 private boolean mHasShortcutHostPermission;
148 // Runnable to check if the shortcuts permission has changed.
149 private final Runnable mShortcutPermissionCheckRunnable = new Runnable() {
150 @Override
151 public void run() {
152 if (mDeepShortcutsLoaded) {
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530153 boolean hasShortcutHostPermission =
154 DeepShortcutManager.getInstance(mApp.getContext()).hasHostPermission();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700155 if (hasShortcutHostPermission != mHasShortcutHostPermission) {
156 mApp.reloadWorkspace();
157 }
158 }
159 }
160 };
161
Sunny Goyale9956a72016-09-01 17:24:47 -0700162 /**
163 * All the static data should be accessed on the background thread, A lock should be acquired
164 * on this object when accessing any data from this model.
165 */
166 static final BgDataModel sBgDataModel = new BgDataModel();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700167
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700168 // </ only access in worker thread >
169
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700170 private final IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700172 private final LauncherAppsCompat mLauncherApps;
173 private final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100174
Joe Onorato9c1289c2009-08-17 11:03:03 -0400175 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700176 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400177 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700178 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400179 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700180 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
181 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700182 public void bindScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700183 public void finishFirstPageBind(ViewOnDrawExecutor executor);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800184 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400185 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200186 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700187 public void bindAppsAdded(ArrayList<Long> newScreens,
188 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700189 ArrayList<ItemInfo> addAnimated,
190 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200191 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700192 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800193 ArrayList<ShortcutInfo> removed, UserHandle user);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700194 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700195 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700196 public void bindWorkspaceComponentsRemoved(
197 HashSet<String> packageNames, HashSet<ComponentName> components,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800198 UserHandle user);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700199 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800200 public void notifyWidgetProvidersChanged();
Sunny Goyald164b7f2016-10-12 20:49:31 -0700201 public void bindAllWidgets(MultiHashMap<PackageItemInfo, WidgetItem> widgets);
Adam Cohen1462de32012-07-24 22:34:36 -0700202 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700203 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700204 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMap);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400205 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530207 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800208 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400209 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100210 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Sunny Goyald164b7f2016-10-12 20:49:31 -0700211 mBgWidgetsModel = new WidgetsModel(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800212 mIconCache = iconCache;
213
Kenny Guyed131872014-04-30 03:02:21 +0100214 mLauncherApps = LauncherAppsCompat.getInstance(context);
215 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800216 }
217
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700218 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
219 * posted on the main thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700220 private void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700221 if (sWorkerThread.getThreadId() == Process.myTid()) {
222 // If we are on the worker thread, post onto the main handler
223 mHandler.post(r);
224 } else {
225 r.run();
226 }
227 }
228
229 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
230 * posted on the worker thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700231 private static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700232 if (sWorkerThread.getThreadId() == Process.myTid()) {
233 r.run();
234 } else {
235 // If we are not on the worker thread, then post to the worker handler
236 sWorker.post(r);
237 }
238 }
239
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700240 public void setPackageState(PackageInstallInfo installInfo) {
241 enqueueModelUpdateTask(new PackageInstallStateChangedTask(installInfo));
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500242 }
243
Sunny Goyal756adbc2015-04-16 15:20:51 -0700244 /**
245 * Updates the icons and label of all pending icons for the provided package name.
246 */
247 public void updateSessionDisplayInfo(final String packageName) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700248 HashSet<String> packages = new HashSet<>();
249 packages.add(packageName);
250 enqueueModelUpdateTask(new CacheDataUpdatedTask(
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800251 CacheDataUpdatedTask.OP_SESSION_UPDATE, Process.myUserHandle(), packages));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800252 }
253
254 /**
255 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800256 */
Sunny Goyal2bcbe132016-11-16 09:23:42 -0800257 public void addAndBindAddedWorkspaceItems(List<ItemInfo> workspaceApps) {
258 addAndBindAddedWorkspaceItems(Provider.of(workspaceApps));
259 }
260
261 /**
262 * Adds the provided items to the workspace.
263 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700264 public void addAndBindAddedWorkspaceItems(
Sunny Goyal2bcbe132016-11-16 09:23:42 -0800265 Provider<List<ItemInfo>> appsProvider) {
266 enqueueModelUpdateTask(new AddWorkspaceItemsTask(appsProvider));
Winson Chung64359a52013-07-08 17:17:08 -0700267 }
268
Joe Onorato9c1289c2009-08-17 11:03:03 -0400269 /**
270 * Adds an item to the DB if it was not created previously, or move it to a new
271 * <container, screen, cellX, cellY>
272 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800273 public static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700274 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400275 if (item.container == ItemInfo.NO_ID) {
276 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700277 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400278 } else {
279 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700280 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281 }
282 }
283
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700284 static void checkItemInfoLocked(
285 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
Sunny Goyale9956a72016-09-01 17:24:47 -0700286 ItemInfo modelItem = sBgDataModel.itemsIdMap.get(itemId);
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700287 if (modelItem != null && item != modelItem) {
288 // check all the data is consistent
289 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
290 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
291 ShortcutInfo shortcut = (ShortcutInfo) item;
292 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
293 modelShortcut.intent.filterEquals(shortcut.intent) &&
294 modelShortcut.id == shortcut.id &&
295 modelShortcut.itemType == shortcut.itemType &&
296 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700297 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700298 modelShortcut.cellX == shortcut.cellX &&
299 modelShortcut.cellY == shortcut.cellY &&
300 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700301 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700302 // For all intents and purposes, this is the same object
303 return;
304 }
305 }
306
307 // the modelItem needs to match up perfectly with item if our model is
308 // to be consistent with the database-- for now, just require
309 // modelItem == item or the equality check above
310 String msg = "item: " + ((item != null) ? item.toString() : "null") +
311 "modelItem: " +
312 ((modelItem != null) ? modelItem.toString() : "null") +
313 "Error: ItemInfo passed to checkItemInfo doesn't match original";
314 RuntimeException e = new RuntimeException(msg);
315 if (stackTrace != null) {
316 e.setStackTrace(stackTrace);
317 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800318 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700319 }
320 }
321
Michael Jurka816474f2012-06-25 14:49:02 -0700322 static void checkItemInfo(final ItemInfo item) {
323 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
324 final long itemId = item.id;
325 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700326 public void run() {
Sunny Goyale9956a72016-09-01 17:24:47 -0700327 synchronized (sBgDataModel) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700328 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700329 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700330 }
331 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700332 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700333 }
334
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800335 static void updateItemInDatabaseHelper(Context context, final ContentWriter writer,
Michael Jurkac9d95c52011-08-29 14:03:34 -0700336 final ItemInfo item, final String callingFunction) {
337 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700338 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700339 final ContentResolver cr = context.getContentResolver();
340
Adam Cohen487f7dd2012-06-28 18:12:10 -0700341 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Sunny Goyal87f784c2017-01-11 10:48:34 -0800342 final Context appContext = context.getApplicationContext();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700343 Runnable r = new Runnable() {
344 public void run() {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800345 cr.update(uri, writer.getValues(appContext), null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700346 updateItemArrays(item, itemId, stackTrace);
347 }
348 };
349 runOnWorkerThread(r);
350 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700351
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700352 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
353 final ArrayList<ItemInfo> items, final String callingFunction) {
354 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700355
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700356 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
357 Runnable r = new Runnable() {
358 public void run() {
359 ArrayList<ContentProviderOperation> ops =
360 new ArrayList<ContentProviderOperation>();
361 int count = items.size();
362 for (int i = 0; i < count; i++) {
363 ItemInfo item = items.get(i);
364 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700365 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700366 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700367
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700368 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
369 updateItemArrays(item, itemId, stackTrace);
370
371 }
372 try {
373 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
374 } catch (Exception e) {
375 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700376 }
377 }
378 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700379 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700380 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700381
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700382 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
383 // Lock on mBgLock *after* the db operation
Sunny Goyale9956a72016-09-01 17:24:47 -0700384 synchronized (sBgDataModel) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700385 checkItemInfoLocked(itemId, item, stackTrace);
386
387 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
388 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
389 // Item is in a folder, make sure this folder exists
Sunny Goyale9956a72016-09-01 17:24:47 -0700390 if (!sBgDataModel.folders.containsKey(item.container)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700391 // An items container is being set to a that of an item which is not in
392 // the list of Folders.
393 String msg = "item: " + item + " container being set to: " +
394 item.container + ", not in the list of folders";
395 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700396 }
397 }
398
399 // Items are added/removed from the corresponding FolderInfo elsewhere, such
400 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
401 // that are on the desktop, as appropriate
Sunny Goyale9956a72016-09-01 17:24:47 -0700402 ItemInfo modelItem = sBgDataModel.itemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800403 if (modelItem != null &&
404 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
405 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700406 switch (modelItem.itemType) {
407 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
408 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700409 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700410 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyale9956a72016-09-01 17:24:47 -0700411 if (!sBgDataModel.workspaceItems.contains(modelItem)) {
412 sBgDataModel.workspaceItems.add(modelItem);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700413 }
414 break;
415 default:
416 break;
417 }
418 } else {
Sunny Goyale9956a72016-09-01 17:24:47 -0700419 sBgDataModel.workspaceItems.remove(modelItem);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700420 }
421 }
422 }
423
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800424 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400425 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700426 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700427 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700428 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400429 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400430 item.cellX = cellX;
431 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700432
Winson Chung3d503fb2011-07-13 17:25:49 -0700433 // We store hotseat items in canonical form which is this orientation invariant position
434 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700435 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700436 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700437 item.screenId = Launcher.getLauncher(context).getHotseat()
438 .getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700439 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700440 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700441 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400442
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800443 final ContentWriter writer = new ContentWriter(context)
444 .put(LauncherSettings.Favorites.CONTAINER, item.container)
445 .put(LauncherSettings.Favorites.CELLX, item.cellX)
446 .put(LauncherSettings.Favorites.CELLY, item.cellY)
447 .put(LauncherSettings.Favorites.RANK, item.rank)
448 .put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400449
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800450 updateItemInDatabaseHelper(context, writer, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700451 }
452
453 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700454 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
455 * cellX, cellY have already been updated on the ItemInfos.
456 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800457 public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700458 final long container, final int screen) {
459
460 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
461 int count = items.size();
462
463 for (int i = 0; i < count; i++) {
464 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700465 item.container = container;
466
467 // We store hotseat items in canonical form which is this orientation invariant position
468 // in the hotseat
469 if (context instanceof Launcher && screen < 0 &&
470 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700471 item.screenId = Launcher.getLauncher(context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700472 item.cellY);
473 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700474 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700475 }
476
477 final ContentValues values = new ContentValues();
478 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
479 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
480 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800481 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700482 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700483
484 contentValues.add(values);
485 }
486 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
487 }
488
489 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700490 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800491 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700492 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700493 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700494 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800495 item.cellX = cellX;
496 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700497 item.spanX = spanX;
498 item.spanY = spanY;
499
500 // We store hotseat items in canonical form which is this orientation invariant position
501 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700502 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700503 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700504 item.screenId = Launcher.getLauncher(context).getHotseat()
505 .getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700506 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700507 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700508 }
Adam Cohend4844c32011-02-18 19:25:06 -0800509
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800510 final ContentWriter writer = new ContentWriter(context)
511 .put(LauncherSettings.Favorites.CONTAINER, item.container)
512 .put(LauncherSettings.Favorites.CELLX, item.cellX)
513 .put(LauncherSettings.Favorites.CELLY, item.cellY)
514 .put(LauncherSettings.Favorites.RANK, item.rank)
515 .put(LauncherSettings.Favorites.SPANX, item.spanX)
516 .put(LauncherSettings.Favorites.SPANY, item.spanY)
517 .put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800518
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800519 updateItemInDatabaseHelper(context, writer, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700520 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700521
522 /**
523 * Update an item to the database in a specified container.
524 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700525 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800526 ContentWriter writer = new ContentWriter(context);
527 item.onAddToDatabase(writer);
528 updateItemInDatabaseHelper(context, writer, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800529 }
530
Joe Onorato9c1289c2009-08-17 11:03:03 -0400531 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400532 * Add an item to the database in a specified container. Sets the container, screen, cellX and
533 * cellY fields of the item. Also assigns an ID to the item.
534 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700535 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700536 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400537 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400538 item.cellX = cellX;
539 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700540 // We store hotseat items in canonical form which is this orientation invariant position
541 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700542 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700543 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700544 item.screenId = Launcher.getLauncher(context).getHotseat()
545 .getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700546 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700547 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700548 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400549
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800550 final ContentWriter writer = new ContentWriter(context);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400551 final ContentResolver cr = context.getContentResolver();
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800552 item.onAddToDatabase(writer);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400553
Sunny Goyald2497482015-09-22 18:24:19 -0700554 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
555 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
556
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800557 writer.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700558
Jason Monk8e19cf22014-03-20 15:06:57 -0400559 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Sunny Goyal87f784c2017-01-11 10:48:34 -0800560 final Context appContext = context.getApplicationContext();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700561 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700562 public void run() {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800563 cr.insert(LauncherSettings.Favorites.CONTENT_URI, writer.getValues(appContext));
Joe Onorato9c1289c2009-08-17 11:03:03 -0400564
Sunny Goyale9956a72016-09-01 17:24:47 -0700565 synchronized (sBgDataModel) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400566 checkItemInfoLocked(item.id, item, stackTrace);
Sunny Goyal87f784c2017-01-11 10:48:34 -0800567 sBgDataModel.addItem(appContext, item, true);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700568 }
569 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700570 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700571 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700572 }
573
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700574 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700575 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -0400576 */
Sunny Goyalfa401a12015-04-10 13:45:42 -0700577 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyal40452cf2016-09-01 15:17:46 -0700578 ArrayList<ItemInfo> items = new ArrayList<>();
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700579 items.add(item);
580 deleteItemsFromDatabase(context, items);
581 }
582
583 /**
Sunny Goyal40452cf2016-09-01 15:17:46 -0700584 * Removes all the items from the database matching {@param matcher}.
585 */
586 public static void deleteItemsFromDatabase(Context context, ItemInfoMatcher matcher) {
Sunny Goyale9956a72016-09-01 17:24:47 -0700587 deleteItemsFromDatabase(context, matcher.filterItemInfos(sBgDataModel.itemsIdMap));
Sunny Goyal40452cf2016-09-01 15:17:46 -0700588 }
589
590 /**
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700591 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700592 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700593 public static void deleteItemsFromDatabase(Context context,
594 final Iterable<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400595 final ContentResolver cr = context.getContentResolver();
Sunny Goyal87f784c2017-01-11 10:48:34 -0800596 final Context appContext = context.getApplicationContext();
Michael Jurka83df1882011-08-31 20:59:26 -0700597 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700598 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700599 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700600 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700601 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700602
Sunny Goyal87f784c2017-01-11 10:48:34 -0800603 sBgDataModel.removeItem(appContext, item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700604 }
605 }
Michael Jurka83df1882011-08-31 20:59:26 -0700606 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700607 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400608 }
609
610 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700611 * Update the order of the workspace screens in the database. The array list contains
612 * a list of screen ids in the order that they should appear.
613 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700614 public static void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -0700615 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700616 final ContentResolver cr = context.getContentResolver();
617 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
618
619 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700620 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700621 while (iter.hasNext()) {
622 long id = iter.next();
623 if (id < 0) {
624 iter.remove();
625 }
626 }
627
628 Runnable r = new Runnable() {
629 @Override
630 public void run() {
Yura085c8532014-02-11 15:15:29 +0000631 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700632 // Clear the table
Yura085c8532014-02-11 15:15:29 +0000633 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -0700634 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700635 for (int i = 0; i < count; i++) {
636 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700637 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700638 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
639 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +0000640 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -0700641 }
Yura085c8532014-02-11 15:15:29 +0000642
643 try {
644 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
645 } catch (Exception ex) {
646 throw new RuntimeException(ex);
647 }
Winson Chung9e6a0a22013-08-27 11:58:12 -0700648
Sunny Goyale9956a72016-09-01 17:24:47 -0700649 synchronized (sBgDataModel) {
650 sBgDataModel.workspaceScreens.clear();
651 sBgDataModel.workspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -0700652 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700653 }
654 };
655 runOnWorkerThread(r);
656 }
657
658 /**
Winsonc0b52fe2015-09-09 16:38:15 -0700659 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -0400660 */
Winsonc0b52fe2015-09-09 16:38:15 -0700661 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400662 final ContentResolver cr = context.getContentResolver();
Sunny Goyal87f784c2017-01-11 10:48:34 -0800663 final Context appContext = context.getApplicationContext();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400664
Michael Jurkac9d95c52011-08-29 14:03:34 -0700665 Runnable r = new Runnable() {
666 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700667 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -0700668 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Sunny Goyal87f784c2017-01-11 10:48:34 -0800669 sBgDataModel.removeItem(appContext, info.contents);
Sunny Goyale9956a72016-09-01 17:24:47 -0700670 info.contents.clear();
671
672 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Sunny Goyal87f784c2017-01-11 10:48:34 -0800673 sBgDataModel.removeItem(appContext, info);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700674 }
675 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700676 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400677 }
678
679 /**
680 * Set this as the current Launcher activity object for the loader.
681 */
682 public void initialize(Callbacks callbacks) {
683 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -0700684 Preconditions.assertUIThread();
685 // Remove any queued UI runnables
686 mHandler.cancelAll();
687 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400688 }
689 }
690
Kenny Guyed131872014-04-30 03:02:21 +0100691 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800692 public void onPackageChanged(String packageName, UserHandle user) {
Kenny Guyed131872014-04-30 03:02:21 +0100693 int op = PackageUpdatedTask.OP_UPDATE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700694 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100695 }
696
697 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800698 public void onPackageRemoved(String packageName, UserHandle user) {
Sunny Goyalc2936bc2016-09-01 15:50:36 -0700699 onPackagesRemoved(user, packageName);
700 }
701
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800702 public void onPackagesRemoved(UserHandle user, String... packages) {
Kenny Guyed131872014-04-30 03:02:21 +0100703 int op = PackageUpdatedTask.OP_REMOVE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700704 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packages));
Kenny Guyed131872014-04-30 03:02:21 +0100705 }
706
707 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800708 public void onPackageAdded(String packageName, UserHandle user) {
Kenny Guyed131872014-04-30 03:02:21 +0100709 int op = PackageUpdatedTask.OP_ADD;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700710 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100711 }
712
713 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800714 public void onPackagesAvailable(String[] packageNames, UserHandle user,
Kenny Guyed131872014-04-30 03:02:21 +0100715 boolean replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700716 enqueueModelUpdateTask(
717 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100718 }
719
720 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800721 public void onPackagesUnavailable(String[] packageNames, UserHandle user,
Kenny Guyed131872014-04-30 03:02:21 +0100722 boolean replacing) {
723 if (!replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700724 enqueueModelUpdateTask(new PackageUpdatedTask(
725 PackageUpdatedTask.OP_UNAVAILABLE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100726 }
Kenny Guyed131872014-04-30 03:02:21 +0100727 }
728
Kenny Guy44cba692016-01-21 19:50:02 +0000729 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800730 public void onPackagesSuspended(String[] packageNames, UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700731 enqueueModelUpdateTask(new PackageUpdatedTask(
732 PackageUpdatedTask.OP_SUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000733 }
734
735 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800736 public void onPackagesUnsuspended(String[] packageNames, UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700737 enqueueModelUpdateTask(new PackageUpdatedTask(
738 PackageUpdatedTask.OP_UNSUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000739 }
740
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700741 @Override
742 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800743 UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700744 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, true));
Sunny Goyal50941fb2016-08-04 12:03:52 -0700745 }
746
747 public void updatePinnedShortcuts(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800748 UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700749 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, false));
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700750 }
751
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700752 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400753 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
754 * ACTION_PACKAGE_CHANGED.
755 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100756 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400757 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -0400758 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700759
Joe Onorato36115782010-06-17 13:28:48 -0400760 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +0100761 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -0700762 // If we have changed locale we need to clear out the labels in all apps/workspace.
763 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700764 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
765 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -0700766 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -0700767 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700768 } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
769 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
770 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800771 UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER);
Sunny Goyalda891c12016-03-18 18:29:24 -0700772 if (user != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700773 if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
774 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700775 enqueueModelUpdateTask(new PackageUpdatedTask(
776 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE, user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700777 }
778
779 // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so
780 // we need to run the state change task again.
781 if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
782 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700783 enqueueModelUpdateTask(new UserLockStateChangedTask(user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700784 }
Sunny Goyalda891c12016-03-18 18:29:24 -0700785 }
Tony Wickham827cef22016-03-17 15:39:39 -0700786 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
787 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700788 }
789 }
790
Amith Yamasani6cc806d2014-05-02 13:47:11 -0700791 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700792 resetLoadedState(true, true);
793
Reena Lee93f824a2011-09-23 17:20:28 -0700794 // Do this here because if the launcher activity is running it will be restarted.
795 // If it's not running startLoaderFromBackground will merely tell it that it needs
796 // to reload.
797 startLoaderFromBackground();
798 }
799
Winson Chungf0c6ae02012-03-21 16:10:31 -0700800 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
801 synchronized (mLock) {
802 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
803 // mWorkspaceLoaded to true later
804 stopLoaderLocked();
805 if (resetAllAppsLoaded) mAllAppsLoaded = false;
806 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700807 // Always reset deep shortcuts loaded.
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700808 // TODO: why?
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700809 mDeepShortcutsLoaded = false;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700810 }
811 }
812
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700813 /**
814 * When the launcher is in the background, it's possible for it to miss paired
815 * configuration changes. So whenever we trigger the loader from the background
816 * tell the launcher that it needs to re-run the loader when it comes back instead
817 * of doing it now.
818 */
819 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800820 Callbacks callbacks = getCallback();
821 if (callbacks != null) {
822 // Only actually run the loader if they're not paused.
823 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -0700824 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700825 }
826 }
Joe Onorato36115782010-06-17 13:28:48 -0400827 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400828
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700829 /**
830 * If there is already a loader task running, tell it to stop.
831 */
832 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -0700833 LoaderTask oldTask = mLoaderTask;
834 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -0700835 oldTask.stopLocked();
836 }
Reena Lee93f824a2011-09-23 17:20:28 -0700837 }
838
Adam Cohen1a85c582014-09-30 09:48:49 -0700839 public boolean isCurrentCallbacks(Callbacks callbacks) {
840 return (mCallbacks != null && mCallbacks.get() == callbacks);
841 }
842
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700843 /**
844 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
845 * @return true if the page could be bound synchronously.
846 */
847 public boolean startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700848 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
849 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -0400850 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -0400851 // Don't bother to start the thread if we know it's not going to do anything
852 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -0700853 final Callbacks oldCallbacks = mCallbacks.get();
854 // Clear any pending bind-runnables from the synchronized load process.
855 runOnMainThread(new Runnable() {
856 public void run() {
857 oldCallbacks.clearPendingBinds();
858 }
859 });
860
Joe Onorato36115782010-06-17 13:28:48 -0400861 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700862 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700863 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700864 // TODO: mDeepShortcutsLoaded does not need to be true for synchronous bind.
Tony Wickham80f57872016-06-29 18:12:15 -0700865 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE && mAllAppsLoaded
866 && mWorkspaceLoaded && mDeepShortcutsLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700867 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700868 return true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700869 } else {
870 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
871 sWorker.post(mLoaderTask);
872 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400873 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400874 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700875 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400876 }
877
Joe Onorato36115782010-06-17 13:28:48 -0400878 public void stopLoader() {
879 synchronized (mLock) {
880 if (mLoaderTask != null) {
881 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400882 }
883 }
Joe Onorato36115782010-06-17 13:28:48 -0400884 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400885
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800886 /**
887 * Loads the workspace screen ids in an ordered list.
888 */
Sunny Goyale5bb7052015-07-27 14:36:07 -0700889 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -0700890 final ContentResolver contentResolver = context.getContentResolver();
891 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -0700892
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800893 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -0700894 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
895 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -0700896 }
897
Joe Onorato36115782010-06-17 13:28:48 -0400898 /**
899 * Runnable for the thread that loads the contents of the launcher:
900 * - workspace icons
901 * - widgets
902 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700903 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -0400904 */
905 private class LoaderTask implements Runnable {
906 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -0700907 private int mPageToBindFirst;
908
Adam Cohen091440a2015-03-18 14:16:05 -0700909 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -0400910 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -0700911 @Thunk boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700912
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700913 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -0400914 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -0700915 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400916 }
917
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800918 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700919 mIsLoadingAndBindingWorkspace = true;
920
Joe Onorato36115782010-06-17 13:28:48 -0400921 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -0400922 if (DEBUG_LOADERS) {
923 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400924 }
Michael Jurka288a36b2011-07-12 16:53:48 -0700925
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700926 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800927 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -0700928 synchronized (LoaderTask.this) {
929 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800930 return;
Reena Lee93f824a2011-09-23 17:20:28 -0700931 }
932 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400933 }
934 }
935
Joe Onorato36115782010-06-17 13:28:48 -0400936 // Bind the workspace
Sunny Goyal93f878c2016-03-30 17:31:24 -0700937 bindWorkspace(mPageToBindFirst);
Joe Onorato36115782010-06-17 13:28:48 -0400938 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400939
Joe Onorato36115782010-06-17 13:28:48 -0400940 private void waitForIdle() {
941 // Wait until the either we're stopped or the other threads are done.
942 // This way we don't start loading all apps until the workspace has settled
943 // down.
944 synchronized (LoaderTask.this) {
945 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -0700946
Joe Onorato36115782010-06-17 13:28:48 -0400947 mHandler.postIdle(new Runnable() {
948 public void run() {
949 synchronized (LoaderTask.this) {
950 mLoadAndBindStepFinished = true;
951 if (DEBUG_LOADERS) {
952 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -0400953 }
Joe Onorato36115782010-06-17 13:28:48 -0400954 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -0400955 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400956 }
Joe Onorato36115782010-06-17 13:28:48 -0400957 });
958
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800959 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -0400960 try {
Michael Jurkac7700af2013-05-14 20:17:58 +0200961 // Just in case mFlushingWorkerThread changes but we aren't woken up,
962 // wait no longer than 1sec at a time
963 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -0400964 } catch (InterruptedException ex) {
965 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -0400966 }
967 }
Joe Onorato36115782010-06-17 13:28:48 -0400968 if (DEBUG_LOADERS) {
969 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -0700970 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -0400971 + "ms for previous step to finish binding");
972 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400973 }
Joe Onorato36115782010-06-17 13:28:48 -0400974 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400975
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700976 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -0500977 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700978 // Ensure that we have a valid page index to load synchronously
979 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
980 "valid page index");
981 }
982 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
983 // Ensure that we don't try and bind a specified page when the pages have not been
984 // loaded already (we should load everything asynchronously in that case)
985 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
986 }
987 synchronized (mLock) {
988 if (mIsLoaderTaskRunning) {
989 // Ensure that we are never running the background loading at this point since
990 // we also touch the background collections
991 throw new RuntimeException("Error! Background loading is already running");
992 }
993 }
994
995 // XXX: Throw an exception if we are already loading (since we touch the worker thread
996 // data structures, we can't allow any other thread to touch that data, but because
997 // this call is synchronous, we can get away with not locking).
998
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400999 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001000 // operations from the previous activity. We need to ensure that all queued operations
1001 // are executed before any synchronous binding work is done.
1002 mHandler.flush();
1003
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001004 // Divide the set of loaded items into those that we are binding synchronously, and
1005 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001006 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001007 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1008 // arise from that.
1009 onlyBindAllApps();
Tony Wickham80f57872016-06-29 18:12:15 -07001010
1011 bindDeepShortcuts();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001012 }
1013
Joe Onorato36115782010-06-17 13:28:48 -04001014 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001015 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001016 if (mStopped) {
1017 return;
1018 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001019 mIsLoaderTaskRunning = true;
1020 }
Joe Onorato36115782010-06-17 13:28:48 -04001021 // Optimize for end-user experience: if the Launcher is up and // running with the
1022 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1023 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001024 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001025 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001026 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001027
Joe Onorato36115782010-06-17 13:28:48 -04001028 if (mStopped) {
1029 break keep_running;
1030 }
1031
Joe Onorato36115782010-06-17 13:28:48 -04001032 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001033
1034 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001035 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1036 loadAndBindAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001037
1038 waitForIdle();
1039
1040 // third step
1041 if (DEBUG_LOADERS) Log.d(TAG, "step 3: loading deep shortcuts");
1042 loadAndBindDeepShortcuts();
Joe Onorato36115782010-06-17 13:28:48 -04001043 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001044
Joe Onorato36115782010-06-17 13:28:48 -04001045 // Clear out this reference, otherwise we end up holding it until all of the
1046 // callback runnables are done.
1047 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001048
Joe Onorato36115782010-06-17 13:28:48 -04001049 synchronized (mLock) {
1050 // If we are still the last one to be scheduled, remove ourselves.
1051 if (mLoaderTask == this) {
1052 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001053 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001054 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001055 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001056 }
Joe Onorato36115782010-06-17 13:28:48 -04001057 }
1058
1059 public void stopLocked() {
1060 synchronized (LoaderTask.this) {
1061 mStopped = true;
1062 this.notify();
1063 }
1064 }
1065
1066 /**
1067 * Gets the callbacks object. If we've been stopped, or if the launcher object
1068 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1069 * object that was around when the deferred message was scheduled, and if there's
1070 * a new Callbacks object around then also return null. This will save us from
1071 * calling onto it with data that will be ignored.
1072 */
1073 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1074 synchronized (mLock) {
1075 if (mStopped) {
1076 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001077 }
Joe Onorato36115782010-06-17 13:28:48 -04001078
1079 if (mCallbacks == null) {
1080 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001081 }
Joe Onorato36115782010-06-17 13:28:48 -04001082
1083 final Callbacks callbacks = mCallbacks.get();
1084 if (callbacks != oldCallbacks) {
1085 return null;
1086 }
1087 if (callbacks == null) {
1088 Log.w(TAG, "no mCallbacks");
1089 return null;
1090 }
1091
1092 return callbacks;
1093 }
1094 }
1095
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001096 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -07001097 if (LauncherAppState.PROFILE_STARTUP) {
1098 Trace.beginSection("Loading Workspace");
1099 }
Joe Onorato36115782010-06-17 13:28:48 -04001100 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001101
Joe Onorato36115782010-06-17 13:28:48 -04001102 final Context context = mContext;
1103 final ContentResolver contentResolver = context.getContentResolver();
1104 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001105 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001106 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301107 final DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001108 final boolean isSdCardReady = Utilities.isBootCompleted();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001109 final MultiHashMap<UserHandle, String> pendingPackages = new MultiHashMap<>();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001110
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001111 boolean clearDb = false;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -07001112 try {
1113 ImportDataTask.performImportIfPossible(context);
1114 } catch (Exception e) {
1115 // Migration failed. Clear workspace.
1116 clearDb = true;
1117 }
1118
1119 if (!clearDb && GridSizeMigrationTask.ENABLED &&
Sunny Goyalf076eae2016-01-11 12:25:10 -08001120 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1121 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001122 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001123 }
1124
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001125 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -07001126 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001127 LauncherSettings.Settings.call(contentResolver,
1128 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001129 }
1130
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001131 Log.d(TAG, "loadWorkspace: loading default favorites");
1132 LauncherSettings.Settings.call(contentResolver,
1133 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -07001134
Sunny Goyale9956a72016-09-01 17:24:47 -07001135 synchronized (sBgDataModel) {
1136 sBgDataModel.clear();
1137
Sunny Goyal756adbc2015-04-16 15:20:51 -07001138 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001139 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyale9956a72016-09-01 17:24:47 -07001140 sBgDataModel.workspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001141
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001142 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001143 final LoaderCursor c = new LoaderCursor(contentResolver.query(
1144 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null), mApp);
Daniel Sandler8802e962010-05-26 16:28:16 -04001145
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001146 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001147
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001148 try {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001149 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1150 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001151 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1152 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001153 final int spanXIndex = c.getColumnIndexOrThrow
1154 (LauncherSettings.Favorites.SPANX);
1155 final int spanYIndex = c.getColumnIndexOrThrow(
1156 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001157 final int rankIndex = c.getColumnIndexOrThrow(
1158 LauncherSettings.Favorites.RANK);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001159 final int optionsIndex = c.getColumnIndexOrThrow(
1160 LauncherSettings.Favorites.OPTIONS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001161
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001162 final LongSparseArray<UserHandle> allUsers = c.allUsers;
Kenny Guyff05f432016-01-22 17:48:29 +00001163 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001164 final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001165 for (UserHandle user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001166 long serialNo = mUserManager.getSerialNumberForUser(user);
1167 allUsers.put(serialNo, user);
1168 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001169
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001170 boolean userUnlocked = mUserManager.isUserUnlocked(user);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001171
1172 // We can only query for shortcuts when the user is unlocked.
1173 if (userUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07001174 List<ShortcutInfoCompat> pinnedShortcuts =
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301175 shortcutManager.queryForPinnedShortcuts(null, user);
1176 if (shortcutManager.wasLastCallSuccess()) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07001177 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
1178 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
1179 shortcut);
1180 }
1181 } else {
1182 // Shortcut manager can fail due to some race condition when the
1183 // lock state changes too frequently. For the purpose of the loading
1184 // shortcuts, consider the user is still locked.
1185 userUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001186 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001187 }
Sunny Goyal49f4f032016-08-01 15:45:49 -07001188 unlockedUsers.put(serialNo, userUnlocked);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001189 }
1190
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001191 ShortcutInfo info;
1192 String intentDescription;
1193 LauncherAppWidgetInfo appWidgetInfo;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001194 Intent intent;
Sunny Goyal81e44912017-01-14 15:05:14 -08001195 String targetPkg;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001196
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001197 while (!mStopped && c.moveToNext()) {
1198 try {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001199 if (c.user == null) {
1200 // User has been deleted, remove the item.
1201 c.markDeleted("User has been deleted");
1202 continue;
1203 }
1204
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001205 boolean allowMissingTarget = false;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001206 switch (c.itemType) {
Sunny Goyal81e44912017-01-14 15:05:14 -08001207 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001208 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001209 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Sunny Goyal81e44912017-01-14 15:05:14 -08001210 intent = c.parseIntent();
1211 if (intent == null) {
1212 c.markDeleted("Invalid or null intent");
1213 continue;
1214 }
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001215
Sunny Goyal81e44912017-01-14 15:05:14 -08001216 int disabledState = quietMode.get(c.serialNumber) ?
1217 ShortcutInfo.FLAG_DISABLED_QUIET_USER : 0;
1218 ComponentName cn = intent.getComponent();
1219 targetPkg = cn == null ? intent.getPackage() : cn.getPackageName();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001220
Sunny Goyal81e44912017-01-14 15:05:14 -08001221 if (!Process.myUserHandle().equals(c.user)) {
1222 if (c.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
1223 c.markDeleted("Legacy shortcuts are only allowed for default user");
1224 continue;
1225 } else if (c.restoreFlag != 0) {
1226 // Don't restore items for other profiles.
1227 c.markDeleted("Restore from managed profile not supported");
1228 continue;
1229 }
1230 }
1231 if (TextUtils.isEmpty(targetPkg) &&
1232 c.itemType != LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
1233 c.markDeleted("Only legacy shortcuts can have null package");
1234 continue;
1235 }
Sunny Goyal34942622014-08-29 17:20:55 -07001236
Sunny Goyal81e44912017-01-14 15:05:14 -08001237 // If there is no target package, its an implicit intent
1238 // (legacy shortcut) which is always valid
1239 boolean validTarget = TextUtils.isEmpty(targetPkg) ||
1240 launcherApps.isPackageEnabledForProfile(targetPkg, c.user);
Sunny Goyal94485362014-09-18 16:13:58 -07001241
Sunny Goyal81e44912017-01-14 15:05:14 -08001242 if (cn != null && validTarget) {
1243 // If the apk is present and the shortcut points to a specific
1244 // component.
1245
1246 // If the component is already present
1247 if (launcherApps.isActivityEnabledForProfile(cn, c.user)) {
1248 // no special handling necessary for this item
1249 c.markRestored();
1250 } else {
1251 if (c.hasRestoreFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
1252 // We allow auto install apps to have their intent
1253 // updated after an install.
1254 intent = manager.getLaunchIntentForPackage(targetPkg);
1255 if (intent != null) {
1256 c.restoreFlag = 0;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001257 c.updater().put(
Sunny Goyal81e44912017-01-14 15:05:14 -08001258 LauncherSettings.Favorites.INTENT,
1259 intent.toUri(0)).commit();
1260 cn = intent.getComponent();
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001261 } else {
Sunny Goyal81e44912017-01-14 15:05:14 -08001262 c.markDeleted("Unable to find a launch target");
Sunny Goyal94485362014-09-18 16:13:58 -07001263 continue;
1264 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07001265 } else {
Sunny Goyal81e44912017-01-14 15:05:14 -08001266 // The app is installed but the component is no
1267 // longer available.
1268 c.markDeleted("Invalid component removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001269 continue;
Winson Chungee055712013-07-30 14:46:24 -07001270 }
Winson Chungee055712013-07-30 14:46:24 -07001271 }
Sunny Goyal81e44912017-01-14 15:05:14 -08001272 }
1273 // else if cn == null => can't infer much, leave it
1274 // else if !validPkg => could be restored icon or missing sd-card
1275
1276 if (!TextUtils.isEmpty(targetPkg) && !validTarget) {
1277 // Points to a valid app (superset of cn != null) but the apk
1278 // is not available.
1279
1280 if (c.restoreFlag != 0) {
1281 // Package is not yet available but might be
1282 // installed later.
1283 FileLog.d(TAG, "package not yet restored: " + targetPkg);
1284
1285 if (c.hasRestoreFlag(ShortcutInfo.FLAG_RESTORE_STARTED)) {
1286 // Restore has started once.
1287 } else if (installingPkgs.containsKey(targetPkg)) {
1288 // App restore has started. Update the flag
1289 c.restoreFlag |= ShortcutInfo.FLAG_RESTORE_STARTED;
1290 c.updater().commit();
1291 } else {
1292 c.markDeleted("Unrestored app removed: " + targetPkg);
1293 continue;
1294 }
1295 } else if (PackageManagerHelper.isAppOnSdcard(
1296 manager, targetPkg)) {
1297 // Package is present but not available.
1298 disabledState |= ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1299 // Add the icon on the workspace anyway.
1300 allowMissingTarget = true;
1301 } else if (!isSdCardReady) {
1302 // SdCard is not ready yet. Package might get available,
1303 // once it is ready.
1304 Log.d(TAG, "Missing pkg, will check later: " + targetPkg);
1305 pendingPackages.addToList(c.user, targetPkg);
1306 // Add the icon on the workspace anyway.
1307 allowMissingTarget = true;
1308 } else {
1309 // Do not wait for external media load anymore.
1310 c.markDeleted("Invalid package removed: " + targetPkg);
1311 continue;
1312 }
1313 }
1314
1315 if (validTarget) {
1316 // The shortcut points to a valid target (either no target
1317 // or something which is ready to be used)
1318 c.markRestored();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001319 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001320
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001321 boolean useLowResIcon = !c.isOnWorkspaceOrHotseat() &&
Sunny Goyal34b65272015-03-11 16:56:52 -07001322 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1323
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001324 if (c.restoreFlag != 0) {
Sunny Goyal81e44912017-01-14 15:05:14 -08001325 // Already verified above that user is same as default user
1326 info = c.getRestoredItemInfo(intent);
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001327 } else if (c.itemType ==
Chris Wrenf4d08112014-01-16 18:13:56 -05001328 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001329 info = c.getAppShortcutInfo(
1330 intent, allowMissingTarget, useLowResIcon);
1331 } else if (c.itemType ==
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001332 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001333
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001334 ShortcutKey key = ShortcutKey.fromIntent(intent, c.user);
1335 if (unlockedUsers.get(c.serialNumber)) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001336 ShortcutInfoCompat pinnedShortcut =
1337 shortcutKeyToPinnedShortcuts.get(key);
1338 if (pinnedShortcut == null) {
1339 // The shortcut is no longer valid.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001340 c.markDeleted("Pinned shortcut not found");
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001341 continue;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001342 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001343 info = new ShortcutInfo(pinnedShortcut, context);
Sunny Goyal1b072632017-01-18 11:30:23 -08001344 info.iconBitmap = LauncherIcons
1345 .createShortcutIcon(pinnedShortcut, context);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001346 intent = info.intent;
1347 } else {
1348 // Create a shortcut info in disabled mode for now.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001349 info = c.loadSimpleShortcut();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001350 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001351 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001352 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001353 info = c.loadSimpleShortcut();
Michael Jurka96879562012-03-22 05:54:33 -07001354
Sunny Goyald09c3702016-04-06 16:18:20 -07001355 // Shortcuts are only available on the primary profile
Sunny Goyal81e44912017-01-14 15:05:14 -08001356 if (PackageManagerHelper.isAppSuspended(manager, targetPkg)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001357 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1358 }
1359
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001360 // App shortcuts that used to be automatically added to Launcher
1361 // didn't always have the correct intent flags set, so do that
1362 // here
1363 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001364 intent.getCategories() != null &&
1365 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001366 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001367 intent.addFlags(
1368 Intent.FLAG_ACTIVITY_NEW_TASK |
1369 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1370 }
Michael Jurka96879562012-03-22 05:54:33 -07001371 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001372
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001373 if (info != null) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001374 c.applyCommonProperties(info);
1375
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001376 info.intent = intent;
Sunny Goyal08f72612015-01-05 13:41:43 -08001377 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001378 info.spanX = 1;
1379 info.spanY = 1;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001380 // TODO: Remove this extra. Instead we should be using
1381 // itemInfo#user.
1382 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, c.serialNumber);
Sunny Goyald09c3702016-04-06 16:18:20 -07001383 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001384 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1385 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1386 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001387
Sunny Goyal81e44912017-01-14 15:05:14 -08001388 if (c.restoreFlag != 0 && !TextUtils.isEmpty(targetPkg)) {
1389 Integer progress = installingPkgs.get(targetPkg);
1390 if (progress != null) {
1391 info.setInstallProgress(progress);
1392 } else {
1393 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001394 }
1395 }
1396
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001397 c.checkAndAddItem(info, sBgDataModel);
Winson Chung1323b482013-08-05 12:41:55 -07001398 } else {
1399 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001400 }
1401 break;
1402
1403 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001404 FolderInfo folderInfo = sBgDataModel.findOrMakeFolder(c.id);
1405 c.applyCommonProperties(folderInfo);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001406
Sunny Goyala508e4f2015-05-21 09:33:57 -07001407 // Do not trim the folder label, as is was set by the user.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001408 folderInfo.title = c.getString(c.titleIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001409 folderInfo.spanX = 1;
1410 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001411 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001412
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001413 // no special handling required for restored folders
1414 c.markRestored();
Chris Wrenf4d08112014-01-16 18:13:56 -05001415
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001416 c.checkAndAddItem(folderInfo, sBgDataModel);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001417 break;
1418
1419 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08001420 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001421 // Read all Launcher-specific widget details
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001422 boolean customWidget = c.itemType ==
Adam Cohen59400422014-03-05 18:07:04 -08001423 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
1424
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001425 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001426 String savedProvider = c.getString(appWidgetProviderIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001427
Sunny Goyalff572272014-07-23 13:58:07 -07001428 final ComponentName component =
1429 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04001430
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001431 final boolean isIdValid = !c.hasRestoreFlag(
1432 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID);
1433 final boolean wasProviderReady = !c.hasRestoreFlag(
1434 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY);
Sunny Goyal651077b2014-06-30 14:15:31 -07001435
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001436 if (widgetProvidersMap == null) {
1437 widgetProvidersMap = AppWidgetManagerCompat
1438 .getInstance(mContext).getAllProvidersMap();
1439 }
1440 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
1441 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00001442 ComponentName.unflattenFromString(savedProvider),
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001443 c.user));
Sunny Goyalff572272014-07-23 13:58:07 -07001444
1445 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08001446 if (!isSafeMode && !customWidget &&
1447 wasProviderReady && !isProviderReady) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001448 c.markDeleted(
1449 "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07001450 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001451 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07001452 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07001453 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1454 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08001455
Sunny Goyal53f96722015-07-13 19:54:53 -07001456 // The provider is available. So the widget is either
1457 // available or not available. We do not need to track
1458 // any future restore updates.
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001459 int status = c.restoreFlag &
Sunny Goyal53f96722015-07-13 19:54:53 -07001460 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07001461 if (!wasProviderReady) {
1462 // If provider was not previously ready, update the
1463 // status and UI flag.
1464
1465 // Id would be valid only if the widget restore broadcast was received.
1466 if (isIdValid) {
Sunny Goyal86df1382016-08-10 15:03:22 -07001467 status |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07001468 } else {
1469 status &= ~LauncherAppWidgetInfo
1470 .FLAG_PROVIDER_NOT_READY;
1471 }
1472 }
1473 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07001474 } else {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001475 Log.v(TAG, "Widget restore pending id=" + c.id
Sunny Goyal651077b2014-06-30 14:15:31 -07001476 + " appWidgetId=" + appWidgetId
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001477 + " status =" + c.restoreFlag);
Sunny Goyal651077b2014-06-30 14:15:31 -07001478 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07001479 component);
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001480 appWidgetInfo.restoreStatus = c.restoreFlag;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001481 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07001482
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001483 if (c.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_RESTORE_STARTED)) {
Sunny Goyal94485362014-09-18 16:13:58 -07001484 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001485 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07001486 // App restore has started. Update the flag
1487 appWidgetInfo.restoreStatus |=
1488 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001489 } else if (!isSafeMode) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001490 c.markDeleted("Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07001491 continue;
1492 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07001493
1494 appWidgetInfo.installProgress =
1495 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07001496 }
Sunny Goyal86df1382016-08-10 15:03:22 -07001497 if (appWidgetInfo.hasRestoreFlag(
1498 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG)) {
Sunny Goyal81e44912017-01-14 15:05:14 -08001499 appWidgetInfo.bindOptions = c.parseIntent();
Sunny Goyal86df1382016-08-10 15:03:22 -07001500 }
Sunny Goyalff572272014-07-23 13:58:07 -07001501
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001502 c.applyCommonProperties(appWidgetInfo);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001503 appWidgetInfo.spanX = c.getInt(spanXIndex);
1504 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001505 appWidgetInfo.user = c.user;
Joe Onorato36115782010-06-17 13:28:48 -04001506
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001507 if (!c.isOnWorkspaceOrHotseat()) {
1508 c.markDeleted("Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001509 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001510 continue;
1511 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001512
Adam Cohen59400422014-03-05 18:07:04 -08001513 if (!customWidget) {
1514 String providerName =
1515 appWidgetInfo.providerName.flattenToString();
1516 if (!providerName.equals(savedProvider) ||
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001517 (appWidgetInfo.restoreStatus != c.restoreFlag)) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001518 c.updater()
1519 .put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1520 providerName)
1521 .put(LauncherSettings.Favorites.RESTORED,
1522 appWidgetInfo.restoreStatus)
1523 .commit();
Adam Cohen59400422014-03-05 18:07:04 -08001524 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001525 }
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001526 c.checkAndAddItem(appWidgetInfo, sBgDataModel);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001527 }
Joe Onorato36115782010-06-17 13:28:48 -04001528 break;
1529 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001530 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001531 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001532 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001533 }
1534 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001535 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001536 }
1537
Winson Chungba9c37f2013-08-30 14:11:37 -07001538 // Break early if we've stopped loading
1539 if (mStopped) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001540 sBgDataModel.clear();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001541 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07001542 }
1543
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001544 // Remove dead items
1545 if (c.commitDeleted()) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001546 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07001547 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
1548 .call(contentResolver,
1549 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
1550 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
1551 for (long folderId : deletedFolderIds) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001552 sBgDataModel.workspaceItems.remove(sBgDataModel.folders.get(folderId));
1553 sBgDataModel.folders.remove(folderId);
1554 sBgDataModel.itemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001555 }
1556 }
1557
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001558 // Unpin shortcuts that don't exist on the workspace.
Sunny Goyalf75baa92016-11-22 03:23:51 +05301559 HashSet<ShortcutKey> pendingShortcuts =
1560 InstallShortcutReceiver.getPendingShortcuts(context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001561 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001562 MutableInt numTimesPinned = sBgDataModel.pinnedShortcutCounts.get(key);
Sunny Goyalf75baa92016-11-22 03:23:51 +05301563 if ((numTimesPinned == null || numTimesPinned.value == 0)
1564 && !pendingShortcuts.contains(key)) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001565 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301566 shortcutManager.unpinShortcut(key);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001567 }
1568 }
1569
Sunny Goyal317698b2015-07-29 11:45:41 -07001570 // Sort all the folder items and make sure the first 3 items are high resolution.
Sunny Goyale9956a72016-09-01 17:24:47 -07001571 for (FolderInfo folder : sBgDataModel.folders) {
Sunny Goyal317698b2015-07-29 11:45:41 -07001572 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
1573 int pos = 0;
1574 for (ShortcutInfo info : folder.contents) {
Sunny Goyal1cd01b02016-11-09 10:43:58 -08001575 if (info.usingLowResIcon &&
1576 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyal3fe4a142016-12-15 17:40:07 -08001577 mIconCache.getTitleAndIcon(info, false);
Sunny Goyal317698b2015-07-29 11:45:41 -07001578 }
1579 pos ++;
1580 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
1581 break;
1582 }
1583 }
1584 }
1585
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001586 c.commitRestoredItems();
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001587 if (!isSdCardReady && !pendingPackages.isEmpty()) {
1588 context.registerReceiver(
1589 new SdCardAvailableReceiver(
1590 LauncherModel.this, mContext, pendingPackages),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001591 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001592 null,
1593 sWorker);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001594 }
1595
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001596 // Remove any empty screens
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001597 ArrayList<Long> unusedScreens = new ArrayList<>(sBgDataModel.workspaceScreens);
Sunny Goyale9956a72016-09-01 17:24:47 -07001598 for (ItemInfo item: sBgDataModel.itemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001599 long screenId = item.screenId;
1600 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1601 unusedScreens.contains(screenId)) {
1602 unusedScreens.remove(screenId);
1603 }
1604 }
1605
1606 // If there are any empty screens remove them, and update.
1607 if (unusedScreens.size() != 0) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001608 sBgDataModel.workspaceScreens.removeAll(unusedScreens);
1609 updateWorkspaceScreenOrder(context, sBgDataModel.workspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001610 }
Joe Onorato36115782010-06-17 13:28:48 -04001611 }
Sunny Goyale26d1002016-06-20 14:52:14 -07001612 if (LauncherAppState.PROFILE_STARTUP) {
1613 Trace.endSection();
1614 }
Adam Cohene25af792013-06-06 23:08:25 -07001615 }
1616
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001617 /** Filters the set of items who are directly or indirectly (via another container) on the
1618 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08001619 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001620 ArrayList<ItemInfo> allWorkspaceItems,
1621 ArrayList<ItemInfo> currentScreenItems,
1622 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001623 // Purge any null ItemInfos
1624 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
1625 while (iter.hasNext()) {
1626 ItemInfo i = iter.next();
1627 if (i == null) {
1628 iter.remove();
1629 }
1630 }
1631
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001632 // Order the set of items by their containers first, this allows use to walk through the
1633 // list sequentially, build up a list of containers that are in the specified screen,
1634 // as well as all items in those containers.
1635 Set<Long> itemsOnScreen = new HashSet<Long>();
1636 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
1637 @Override
1638 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07001639 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001640 }
1641 });
1642 for (ItemInfo info : allWorkspaceItems) {
1643 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001644 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001645 currentScreenItems.add(info);
1646 itemsOnScreen.add(info.id);
1647 } else {
1648 otherScreenItems.add(info);
1649 }
1650 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1651 currentScreenItems.add(info);
1652 itemsOnScreen.add(info.id);
1653 } else {
1654 if (itemsOnScreen.contains(info.container)) {
1655 currentScreenItems.add(info);
1656 itemsOnScreen.add(info.id);
1657 } else {
1658 otherScreenItems.add(info);
1659 }
1660 }
1661 }
1662 }
1663
1664 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08001665 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001666 ArrayList<LauncherAppWidgetInfo> appWidgets,
1667 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
1668 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001669
1670 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001671 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001672 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08001673 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001674 currentScreenWidgets.add(widget);
1675 } else {
1676 otherScreenWidgets.add(widget);
1677 }
1678 }
1679 }
1680
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001681 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
1682 * right) */
1683 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Sunny Goyal87f784c2017-01-11 10:48:34 -08001684 final InvariantDeviceProfile profile = mApp.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07001685 final int screenCols = profile.numColumns;
1686 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001687 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07001688 @Override
1689 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07001690 if (lhs.container == rhs.container) {
1691 // Within containers, order by their spatial position in that container
1692 switch ((int) lhs.container) {
1693 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
1694 long lr = (lhs.screenId * screenCellCount +
1695 lhs.cellY * screenCols + lhs.cellX);
1696 long rr = (rhs.screenId * screenCellCount +
1697 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07001698 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07001699 }
1700 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
1701 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07001702 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07001703 }
1704 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07001705 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07001706 throw new RuntimeException("Unexpected container type when " +
1707 "sorting workspace items.");
1708 }
1709 return 0;
1710 }
1711 } else {
1712 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07001713 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07001714 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001715 }
1716 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001717 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001718
Adam Cohendcd297f2013-06-18 13:13:40 -07001719 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
1720 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001721 final Runnable r = new Runnable() {
1722 @Override
1723 public void run() {
1724 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1725 if (callbacks != null) {
1726 callbacks.bindScreens(orderedScreens);
1727 }
1728 }
1729 };
Sunny Goyald33860f2015-04-23 16:02:20 -07001730 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07001731 }
1732
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001733 private void bindWorkspaceItems(final Callbacks oldCallbacks,
1734 final ArrayList<ItemInfo> workspaceItems,
1735 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07001736 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001737
1738 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07001739 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001740 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04001741 final int start = i;
1742 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001743 final Runnable r = new Runnable() {
1744 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001745 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001746 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001747 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07001748 callbacks.bindItems(workspaceItems, start, start+chunkSize,
1749 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001750 }
1751 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001752 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001753 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04001754 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001755
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001756 // Bind the widgets, one at a time
1757 N = appWidgets.size();
1758 for (int i = 0; i < N; i++) {
1759 final LauncherAppWidgetInfo widget = appWidgets.get(i);
1760 final Runnable r = new Runnable() {
1761 public void run() {
1762 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1763 if (callbacks != null) {
1764 callbacks.bindAppWidget(widget);
1765 }
1766 }
1767 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001768 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001769 }
1770 }
1771
1772 /**
1773 * Binds all loaded data to actual views on the main thread.
1774 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001775 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001776 final long t = SystemClock.uptimeMillis();
1777 Runnable r;
1778
1779 // Don't use these two variables in any of the callback runnables.
1780 // Otherwise we hold a reference to them.
1781 final Callbacks oldCallbacks = mCallbacks.get();
1782 if (oldCallbacks == null) {
1783 // This launcher has exited and nobody bothered to tell us. Just bail.
1784 Log.w(TAG, "LoaderTask running with no launcher");
1785 return;
1786 }
1787
Winson Chung9b9fb962013-11-15 15:39:34 -08001788 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07001789 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
1790 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
1791 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07001792
Sunny Goyale9956a72016-09-01 17:24:47 -07001793 synchronized (sBgDataModel) {
1794 workspaceItems.addAll(sBgDataModel.workspaceItems);
1795 appWidgets.addAll(sBgDataModel.appWidgets);
1796 orderedScreenIds.addAll(sBgDataModel.workspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001797 }
1798
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001799 final int currentScreen;
1800 {
1801 int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE
1802 ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen();
1803 if (currScreen >= orderedScreenIds.size()) {
1804 // There may be no workspace screens (just hotseat items and an empty page).
1805 currScreen = PagedView.INVALID_RESTORE_PAGE;
1806 }
1807 currentScreen = currScreen;
Winson Chung9b9fb962013-11-15 15:39:34 -08001808 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001809 final boolean validFirstPage = currentScreen >= 0;
1810 final long currentScreenId =
1811 validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID;
Winson Chung9b9fb962013-11-15 15:39:34 -08001812
Winson Chung9b9fb962013-11-15 15:39:34 -08001813 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07001814 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
1815 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
1816 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
1817 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001818
Winson Chung9b9fb962013-11-15 15:39:34 -08001819 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001820 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08001821 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001822 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001823 sortWorkspaceItemsSpatially(currentWorkspaceItems);
1824 sortWorkspaceItemsSpatially(otherWorkspaceItems);
1825
1826 // Tell the workspace that we're about to start binding items
1827 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001828 public void run() {
1829 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1830 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001831 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001832 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04001833 }
1834 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001835 };
Sunny Goyald33860f2015-04-23 16:02:20 -07001836 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001837
Adam Cohendcd297f2013-06-18 13:13:40 -07001838 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
1839
Sunny Goyal527c7d32015-08-28 15:19:36 -07001840 Executor mainExecutor = new DeferredMainThreadExecutor();
1841 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07001842 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001843
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001844 // In case of validFirstPage, only bind the first screen, and defer binding the
1845 // remaining screens after first onDraw (and an optional the fade animation whichever
1846 // happens later).
1847 // This ensures that the first screen is immediately visible (eg. during rotation)
1848 // In case of !validFirstPage, bind all pages one after other.
1849 final Executor deferredExecutor =
1850 validFirstPage ? new ViewOnDrawExecutor(mHandler) : mainExecutor;
1851
1852 mainExecutor.execute(new Runnable() {
1853 @Override
1854 public void run() {
1855 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1856 if (callbacks != null) {
1857 callbacks.finishFirstPageBind(
1858 validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null);
1859 }
1860 }
1861 });
Sunny Goyal527c7d32015-08-28 15:19:36 -07001862
Sunny Goyal44c06432016-04-02 10:56:02 -07001863 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001864
1865 // Tell the workspace that we're done binding items
1866 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001867 public void run() {
1868 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1869 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001870 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04001871 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001872
Sunny Goyal639e9062015-08-19 19:17:06 -07001873 mIsLoadingAndBindingWorkspace = false;
1874
1875 // Run all the bind complete runnables after workspace is bound.
1876 if (!mBindCompleteRunnables.isEmpty()) {
1877 synchronized (mBindCompleteRunnables) {
1878 for (final Runnable r : mBindCompleteRunnables) {
1879 runOnWorkerThread(r);
1880 }
1881 mBindCompleteRunnables.clear();
1882 }
1883 }
1884
Winson Chung98e030b2012-05-07 16:01:11 -07001885 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04001886 if (DEBUG_LOADERS) {
1887 Log.d(TAG, "bound workspace in "
1888 + (SystemClock.uptimeMillis()-t) + "ms");
1889 }
Winson Chung36a62fe2012-05-06 18:04:42 -07001890
Joe Onorato36115782010-06-17 13:28:48 -04001891 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001892 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001893 deferredExecutor.execute(r);
1894
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001895 if (validFirstPage) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001896 r = new Runnable() {
1897 public void run() {
1898 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1899 if (callbacks != null) {
1900 // We are loading synchronously, which means, some of the pages will be
1901 // bound after first draw. Inform the callbacks that page binding is
1902 // not complete, and schedule the remaining pages.
1903 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
1904 callbacks.onPageBoundSynchronously(currentScreen);
1905 }
1906 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
1907 }
1908 }
1909 };
Sunny Goyald33860f2015-04-23 16:02:20 -07001910 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07001911 }
Joe Onorato36115782010-06-17 13:28:48 -04001912 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001913
Joe Onorato36115782010-06-17 13:28:48 -04001914 private void loadAndBindAllApps() {
1915 if (DEBUG_LOADERS) {
1916 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
1917 }
1918 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07001919 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001920 synchronized (LoaderTask.this) {
1921 if (mStopped) {
1922 return;
1923 }
1924 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001925 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07001926 synchronized (LoaderTask.this) {
1927 if (mStopped) {
1928 return;
1929 }
1930 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07001931 }
Joe Onorato36115782010-06-17 13:28:48 -04001932 } else {
1933 onlyBindAllApps();
1934 }
1935 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001936
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001937 private void updateIconCache() {
1938 // Ignore packages which have a promise icon.
1939 HashSet<String> packagesToIgnore = new HashSet<>();
Sunny Goyale9956a72016-09-01 17:24:47 -07001940 synchronized (sBgDataModel) {
1941 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001942 if (info instanceof ShortcutInfo) {
1943 ShortcutInfo si = (ShortcutInfo) info;
1944 if (si.isPromise() && si.getTargetComponent() != null) {
1945 packagesToIgnore.add(si.getTargetComponent().getPackageName());
1946 }
1947 } else if (info instanceof LauncherAppWidgetInfo) {
1948 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
1949 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
1950 packagesToIgnore.add(lawi.providerName.getPackageName());
1951 }
1952 }
1953 }
1954 }
1955 mIconCache.updateDbIcons(packagesToIgnore);
1956 }
1957
Joe Onorato36115782010-06-17 13:28:48 -04001958 private void onlyBindAllApps() {
1959 final Callbacks oldCallbacks = mCallbacks.get();
1960 if (oldCallbacks == null) {
1961 // This launcher has exited and nobody bothered to tell us. Just bail.
1962 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1963 return;
1964 }
1965
1966 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07001967 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001968 final ArrayList<AppInfo> list
1969 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07001970 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001971 public void run() {
1972 final long t = SystemClock.uptimeMillis();
1973 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1974 if (callbacks != null) {
1975 callbacks.bindAllApplications(list);
1976 }
1977 if (DEBUG_LOADERS) {
1978 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08001979 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04001980 }
1981 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07001982 };
Tony Wickham80f57872016-06-29 18:12:15 -07001983 runOnMainThread(r);
Joe Onorato36115782010-06-17 13:28:48 -04001984 }
1985
Winson Chung64359a52013-07-08 17:17:08 -07001986 private void loadAllApps() {
1987 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04001988
Joe Onorato36115782010-06-17 13:28:48 -04001989 final Callbacks oldCallbacks = mCallbacks.get();
1990 if (oldCallbacks == null) {
1991 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07001992 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04001993 return;
1994 }
1995
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001996 final List<UserHandle> profiles = mUserManager.getUserProfiles();
Kenny Guyed131872014-04-30 03:02:21 +01001997
Winson Chung64359a52013-07-08 17:17:08 -07001998 // Clear the list of apps
1999 mBgAllAppsList.clear();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002000 for (UserHandle user : profiles) {
Kenny Guyed131872014-04-30 03:02:21 +01002001 // Query for the set of apps
2002 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal3e9be432017-01-05 15:22:41 -08002003 final List<LauncherActivityInfo> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002004 if (DEBUG_LOADERS) {
2005 Log.d(TAG, "getActivityList took "
2006 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2007 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2008 }
2009 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002010 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002011 if (apps == null || apps.isEmpty()) {
2012 return;
2013 }
Kenny Guyff05f432016-01-22 17:48:29 +00002014 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002015 // Create the ApplicationInfos
2016 for (int i = 0; i < apps.size(); i++) {
Sunny Goyal3e9be432017-01-05 15:22:41 -08002017 LauncherActivityInfo app = apps.get(i);
Kenny Guyed131872014-04-30 03:02:21 +01002018 // This builds the icon bitmaps.
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08002019 mBgAllAppsList.add(new AppInfo(mContext, app, user, quietMode), app);
Kenny Guyed131872014-04-30 03:02:21 +01002020 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002021
Sunny Goyal756a28a2015-04-23 17:07:55 -07002022 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2023 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002024 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002025
2026 @Override
2027 public void run() {
2028 heuristic.processUserApps(apps);
2029 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002030 };
2031 runOnMainThread(new Runnable() {
2032
2033 @Override
2034 public void run() {
2035 // Check isLoadingWorkspace on the UI thread, as it is updated on
2036 // the UI thread.
2037 if (mIsLoadingAndBindingWorkspace) {
2038 synchronized (mBindCompleteRunnables) {
2039 mBindCompleteRunnables.add(r);
2040 }
2041 } else {
2042 runOnWorkerThread(r);
2043 }
2044 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002045 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002046 }
Winson Chung64359a52013-07-08 17:17:08 -07002047 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002048 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002049 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2050 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002051
2052 // Post callback on main thread
2053 mHandler.post(new Runnable() {
2054 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002055
Winson Chung64359a52013-07-08 17:17:08 -07002056 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002057 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002058 if (callbacks != null) {
2059 callbacks.bindAllApplications(added);
2060 if (DEBUG_LOADERS) {
2061 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002062 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07002063 }
2064 } else {
2065 Log.i(TAG, "not binding apps: no Launcher activity");
2066 }
2067 }
2068 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002069 // Cleanup any data stored for a deleted user.
2070 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04002071 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002072 Log.d(TAG, "Icons processed in "
2073 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002074 }
2075 }
2076
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002077 private void loadAndBindDeepShortcuts() {
2078 if (DEBUG_LOADERS) {
2079 Log.d(TAG, "loadAndBindDeepShortcuts mDeepShortcutsLoaded=" + mDeepShortcutsLoaded);
2080 }
2081 if (!mDeepShortcutsLoaded) {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07002082 sBgDataModel.deepShortcutMap.clear();
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302083 DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(mContext);
2084 mHasShortcutHostPermission = shortcutManager.hasHostPermission();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002085 if (mHasShortcutHostPermission) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002086 for (UserHandle user : mUserManager.getUserProfiles()) {
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002087 if (mUserManager.isUserUnlocked(user)) {
Sunny Goyaldde4fd92016-11-21 16:02:39 +05302088 List<ShortcutInfoCompat> shortcuts =
2089 shortcutManager.queryForAllShortcuts(user);
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07002090 sBgDataModel.updateDeepShortcutMap(null, user, shortcuts);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002091 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002092 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002093 }
2094 synchronized (LoaderTask.this) {
2095 if (mStopped) {
2096 return;
2097 }
2098 mDeepShortcutsLoaded = true;
2099 }
2100 }
Tony Wickham80f57872016-06-29 18:12:15 -07002101 bindDeepShortcuts();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002102 }
Joe Onorato36115782010-06-17 13:28:48 -04002103 }
2104
Tony Wickham80f57872016-06-29 18:12:15 -07002105 public void bindDeepShortcuts() {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07002106 final MultiHashMap<ComponentKey, String> shortcutMapCopy =
2107 sBgDataModel.deepShortcutMap.clone();
Tony Wickham80f57872016-06-29 18:12:15 -07002108 Runnable r = new Runnable() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002109 @Override
2110 public void run() {
2111 Callbacks callbacks = getCallback();
2112 if (callbacks != null) {
2113 callbacks.bindDeepShortcutMap(shortcutMapCopy);
2114 }
2115 }
Tony Wickham80f57872016-06-29 18:12:15 -07002116 };
2117 runOnMainThread(r);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002118 }
2119
Sunny Goyal75b0f552015-05-20 21:57:06 -07002120 /**
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002121 * Refreshes the cached shortcuts if the shortcut permission has changed.
2122 * Current implementation simply reloads the workspace, but it can be optimized to
2123 * use partial updates similar to {@link UserManagerCompat}
2124 */
2125 public void refreshShortcutsIfRequired() {
Sunny Goyalf5e37442016-11-02 10:31:24 -07002126 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002127 sWorker.removeCallbacks(mShortcutPermissionCheckRunnable);
2128 sWorker.post(mShortcutPermissionCheckRunnable);
2129 }
2130 }
2131
2132 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -07002133 * Called when the icons for packages have been updated in the icon cache.
2134 */
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08002135 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandle user) {
Sunny Goyal75b0f552015-05-20 21:57:06 -07002136 // If any package icon has changed (app was updated while launcher was dead),
2137 // update the corresponding shortcuts.
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002138 enqueueModelUpdateTask(new CacheDataUpdatedTask(
2139 CacheDataUpdatedTask.OP_CACHE_UPDATE, user, updatedPackages));
Sunny Goyal75b0f552015-05-20 21:57:06 -07002140 }
2141
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002142 void enqueueModelUpdateTask(BaseModelUpdateTask task) {
2143 task.init(this);
2144 runOnWorkerThread(task);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002145 }
2146
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002147 /**
2148 * A task to be executed on the current callbacks on the UI thread.
2149 * If there is no current callbacks, the task is ignored.
2150 */
2151 public interface CallbackTask {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002152
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002153 void execute(Callbacks callbacks);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002154 }
2155
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002156 /**
2157 * A runnable which changes/updates the data model of the launcher based on certain events.
2158 */
2159 public static abstract class BaseModelUpdateTask implements Runnable {
Joe Onorato36115782010-06-17 13:28:48 -04002160
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002161 private LauncherModel mModel;
2162 private DeferredHandler mUiHandler;
Joe Onorato36115782010-06-17 13:28:48 -04002163
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002164 /* package private */
2165 void init(LauncherModel model) {
2166 mModel = model;
2167 mUiHandler = mModel.mHandler;
Joe Onorato36115782010-06-17 13:28:48 -04002168 }
2169
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002170 @Override
Joe Onorato36115782010-06-17 13:28:48 -04002171 public void run() {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002172 if (!mModel.mHasLoaderCompletedOnce) {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002173 // Loader has not yet run.
2174 return;
2175 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002176 execute(mModel.mApp, sBgDataModel, mModel.mBgAllAppsList);
2177 }
Joe Onorato36115782010-06-17 13:28:48 -04002178
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002179 /**
2180 * Execute the actual task. Called on the worker thread.
2181 */
2182 public abstract void execute(
2183 LauncherAppState app, BgDataModel dataModel, AllAppsList apps);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002184
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002185 /**
2186 * Schedules a {@param task} to be executed on the current callbacks.
2187 */
2188 public final void scheduleCallbackTask(final CallbackTask task) {
2189 final Callbacks callbacks = mModel.getCallback();
2190 mUiHandler.post(new Runnable() {
2191 public void run() {
2192 Callbacks cb = mModel.getCallback();
2193 if (callbacks == cb && cb != null) {
2194 task.execute(callbacks);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07002195 }
2196 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002197 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002198 }
2199 }
2200
Sunny Goyal1b072632017-01-18 11:30:23 -08002201 public void updateAndBindShortcutInfo(final ShortcutInfo si, final ShortcutInfoCompat info) {
2202 updateAndBindShortcutInfo(new Provider<ShortcutInfo>() {
2203 @Override
2204 public ShortcutInfo get() {
2205 si.updateFromDeepShortcutInfo(info, mApp.getContext());
2206 si.iconBitmap = LauncherIcons.createShortcutIcon(info, mApp.getContext());
2207 return si;
2208 }
2209 });
2210 }
2211
Sunny Goyal10923b32016-07-20 15:42:44 -07002212 /**
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08002213 * Utility method to update a shortcut on the background thread.
Sunny Goyal10923b32016-07-20 15:42:44 -07002214 */
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08002215 public void updateAndBindShortcutInfo(final Provider<ShortcutInfo> shortcutProvider) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002216 enqueueModelUpdateTask(new ExtendedModelTask() {
Sunny Goyal10923b32016-07-20 15:42:44 -07002217 @Override
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002218 public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08002219 ShortcutInfo info = shortcutProvider.get();
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07002220 ArrayList<ShortcutInfo> update = new ArrayList<>();
Sunny Goyal10923b32016-07-20 15:42:44 -07002221 update.add(info);
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08002222 bindUpdatedShortcuts(update, info.user);
Sunny Goyal10923b32016-07-20 15:42:44 -07002223 }
2224 });
2225 }
2226
Sunny Goyald164b7f2016-10-12 20:49:31 -07002227 private void bindWidgetsModel(final Callbacks callbacks) {
2228 final MultiHashMap<PackageItemInfo, WidgetItem> widgets
2229 = mBgWidgetsModel.getWidgetsMap().clone();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002230 mHandler.post(new Runnable() {
2231 @Override
2232 public void run() {
2233 Callbacks cb = getCallback();
2234 if (callbacks == cb && cb != null) {
Sunny Goyald164b7f2016-10-12 20:49:31 -07002235 callbacks.bindAllWidgets(widgets);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002236 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07002237 }
2238 });
2239 }
2240
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002241 public void refreshAndBindWidgetsAndShortcuts(
2242 final Callbacks callbacks, final boolean bindFirst) {
2243 runOnWorkerThread(new Runnable() {
2244 @Override
2245 public void run() {
2246 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
Sunny Goyald164b7f2016-10-12 20:49:31 -07002247 bindWidgetsModel(callbacks);
Sunny Goyal31860582015-09-18 08:38:57 -07002248 }
Sunny Goyald164b7f2016-10-12 20:49:31 -07002249 ArrayList<WidgetItem> allWidgets = mBgWidgetsModel.update(mApp.getContext());
2250 bindWidgetsModel(callbacks);
2251
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002252 // update the Widget entries inside DB on the worker thread.
Sunny Goyal87f784c2017-01-11 10:48:34 -08002253 mApp.getWidgetCache().removeObsoletePreviews(allWidgets);
Sunny Goyal31860582015-09-18 08:38:57 -07002254 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002255 });
Michael Jurkac402cd92013-05-20 15:49:32 +02002256 }
2257
Sunny Goyal651077b2014-06-30 14:15:31 -07002258 static boolean isValidProvider(AppWidgetProviderInfo provider) {
2259 return (provider != null) && (provider.provider != null)
2260 && (provider.provider.getPackageName() != null);
2261 }
2262
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002263 public void dumpState(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
2264 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
2265 writer.println(prefix + "All apps list: size=" + mBgAllAppsList.data.size());
2266 for (AppInfo info : mBgAllAppsList.data) {
2267 writer.println(prefix + " title=\"" + info.title + "\" iconBitmap=" + info.iconBitmap
2268 + " componentName=" + info.componentName.getPackageName());
2269 }
Joe Onorato36115782010-06-17 13:28:48 -04002270 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08002271 sBgDataModel.dump(prefix, fd, writer, args);
Joe Onoratobe386092009-11-17 17:32:16 -08002272 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002273
2274 public Callbacks getCallback() {
2275 return mCallbacks != null ? mCallbacks.get() : null;
2276 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002277
2278 /**
2279 * @return {@link FolderInfo} if its already loaded.
2280 */
2281 public FolderInfo findFolderById(Long folderId) {
Sunny Goyale9956a72016-09-01 17:24:47 -07002282 synchronized (sBgDataModel) {
2283 return sBgDataModel.folders.get(folderId);
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002284 }
2285 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002286
Sunny Goyal527c7d32015-08-28 15:19:36 -07002287 @Thunk class DeferredMainThreadExecutor implements Executor {
2288
2289 @Override
2290 public void execute(Runnable command) {
2291 runOnMainThread(command);
2292 }
2293 }
2294
Sunny Goyal756adbc2015-04-16 15:20:51 -07002295 /**
2296 * @return the looper for the worker thread which can be used to start background tasks.
2297 */
2298 public static Looper getWorkerLooper() {
2299 return sWorkerThread.getLooper();
2300 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002301}