blob: b5ca301d0c3f2c32609de728a3878d0234d40e03 [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;
Mario Bertschler817afa32017-03-15 11:56:47 -070029import android.content.pm.PackageInstaller;
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;
Tony Wickham86222d22017-03-29 15:30:43 -070038import android.support.annotation.Nullable;
Winson Chunga90303b2013-11-15 13:05:06 -080039import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070040import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080041import android.util.LongSparseArray;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070042import android.util.MutableInt;
Sunny Goyala474a9b2017-05-04 16:47:11 -070043import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010044
Sunny Goyalffe83f12014-08-14 17:39:34 -070045import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010046import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070047import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070048import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010049import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyal3d706ad2017-03-06 16:56:39 -080050import com.android.launcher3.config.FeatureFlags;
Tony Wickham827cef22016-03-17 15:39:39 -070051import com.android.launcher3.dynamicui.ExtractionUtils;
Sunny Goyal26119432016-02-18 22:09:23 +000052import com.android.launcher3.folder.Folder;
53import com.android.launcher3.folder.FolderIcon;
Jon Miranda655ec422017-02-07 11:40:22 -080054import com.android.launcher3.folder.FolderIconPreviewVerifier;
Sunny Goyal1b072632017-01-18 11:30:23 -080055import com.android.launcher3.graphics.LauncherIcons;
Sunny Goyal1acb9e92016-05-16 12:41:09 -070056import com.android.launcher3.logging.FileLog;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070057import com.android.launcher3.model.AddWorkspaceItemsTask;
Sunny Goyale9956a72016-09-01 17:24:47 -070058import com.android.launcher3.model.BgDataModel;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070059import com.android.launcher3.model.CacheDataUpdatedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080060import com.android.launcher3.model.ExtendedModelTask;
Sunny Goyalf862a262015-12-14 14:27:38 -080061import com.android.launcher3.model.GridSizeMigrationTask;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -080062import com.android.launcher3.model.LoaderCursor;
Sunny Goyal43bf11d2017-02-02 13:52:53 -080063import com.android.launcher3.model.ModelWriter;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070064import com.android.launcher3.model.PackageInstallStateChangedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080065import com.android.launcher3.model.PackageItemInfo;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070066import com.android.launcher3.model.PackageUpdatedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080067import com.android.launcher3.model.SdCardAvailableReceiver;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070068import com.android.launcher3.model.ShortcutsChangedTask;
69import com.android.launcher3.model.UserLockStateChangedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080070import com.android.launcher3.model.WidgetItem;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070071import com.android.launcher3.model.WidgetsModel;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -070072import com.android.launcher3.provider.ImportDataTask;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070073import com.android.launcher3.provider.LauncherDbUtils;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070074import com.android.launcher3.shortcuts.DeepShortcutManager;
75import com.android.launcher3.shortcuts.ShortcutInfoCompat;
76import com.android.launcher3.shortcuts.ShortcutKey;
Robin Lee26ace122015-03-16 19:41:43 +000077import com.android.launcher3.util.ComponentKey;
Sunny Goyalb265ba72017-02-14 15:03:45 -080078import com.android.launcher3.util.LooperIdleLock;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070079import com.android.launcher3.util.ManagedProfileHeuristic;
Tony Wickhamd82a39d2016-07-01 15:44:13 -070080import com.android.launcher3.util.MultiHashMap;
Sunny Goyald09c3702016-04-06 16:18:20 -070081import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham86222d22017-03-29 15:30:43 -070082import com.android.launcher3.util.PackageUserKey;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070083import com.android.launcher3.util.Preconditions;
Sunny Goyal2bcbe132016-11-16 09:23:42 -080084import com.android.launcher3.util.Provider;
Adam Cohen091440a2015-03-18 14:16:05 -070085import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070086import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080087
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -080088import java.io.FileDescriptor;
89import java.io.PrintWriter;
Michael Jurkac2f801e2011-07-12 14:19:46 -070090import java.lang.ref.WeakReference;
Michael Jurkac2f801e2011-07-12 14:19:46 -070091import java.util.ArrayList;
92import java.util.Collections;
93import java.util.Comparator;
94import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070095import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070096import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070097import java.util.List;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070098import java.util.Map;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070099import java.util.Set;
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800100import java.util.concurrent.CancellationException;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700101import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -0700102
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800103/**
104 * Maintains in-memory state of the Launcher. It is expected that there should be only one
105 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700106 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107 */
Kenny Guyed131872014-04-30 03:02:21 +0100108public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +0100109 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800110 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -0400111 private static final boolean DEBUG_RECEIVER = false;
Chris Wrenb358f812014-04-16 13:37:00 -0400112
Joe Onorato9c1289c2009-08-17 11:03:03 -0400113 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700114
Joe Onorato36115782010-06-17 13:28:48 -0400115 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500116 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800117
Sunny Goyalb265ba72017-02-14 15:03:45 -0800118 private final MainThreadExecutor mUiExecutor = new MainThreadExecutor();
Adam Cohen091440a2015-03-18 14:16:05 -0700119 @Thunk final LauncherAppState mApp;
120 @Thunk final Object mLock = new Object();
Adam Cohen091440a2015-03-18 14:16:05 -0700121 @Thunk LoaderTask mLoaderTask;
122 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700123 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124
Adam Cohen091440a2015-03-18 14:16:05 -0700125 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700126 static {
127 sWorkerThread.start();
128 }
Adam Cohen091440a2015-03-18 14:16:05 -0700129 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700130
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800131 // Indicates whether the current model data is valid or not.
132 // We start off with everything not loaded. After that, we assume that
Joe Onoratocc67f472010-06-08 10:54:30 -0700133 // our monitoring of the package manager provides all updates and we never
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800134 // need to do a requery. This is only ever touched from the loader thread.
135 private boolean mModelLoaded;
Hyunyoung Song6aa37292017-02-06 10:46:24 -0800136 public boolean isModelLoaded() {
137 synchronized (mLock) {
138 return mModelLoaded && mLoaderTask == null;
139 }
140 }
Joe Onoratocc67f472010-06-08 10:54:30 -0700141
Adam Cohen091440a2015-03-18 14:16:05 -0700142 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700144 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800145 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700146 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800147 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800148
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700149 private boolean mHasShortcutHostPermission;
150 // Runnable to check if the shortcuts permission has changed.
151 private final Runnable mShortcutPermissionCheckRunnable = new Runnable() {
152 @Override
153 public void run() {
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800154 if (mModelLoaded) {
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530155 boolean hasShortcutHostPermission =
156 DeepShortcutManager.getInstance(mApp.getContext()).hasHostPermission();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700157 if (hasShortcutHostPermission != mHasShortcutHostPermission) {
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800158 forceReload();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700159 }
160 }
161 }
162 };
163
Sunny Goyale9956a72016-09-01 17:24:47 -0700164 /**
165 * All the static data should be accessed on the background thread, A lock should be acquired
166 * on this object when accessing any data from this model.
167 */
168 static final BgDataModel sBgDataModel = new BgDataModel();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700169
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700170 // </ only access in worker thread >
171
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700172 private final IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700174 private final LauncherAppsCompat mLauncherApps;
175 private final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100176
Joe Onorato9c1289c2009-08-17 11:03:03 -0400177 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700178 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400179 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700180 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400181 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700182 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
183 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700184 public void bindScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700185 public void finishFirstPageBind(ViewOnDrawExecutor executor);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800186 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400187 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200188 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700189 public void bindAppsAdded(ArrayList<Long> newScreens,
190 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700191 ArrayList<ItemInfo> addAnimated,
192 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200193 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700194 public void bindPromiseAppProgressUpdated(PromiseAppInfo app);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700195 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800196 ArrayList<ShortcutInfo> removed, UserHandle user);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700197 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700198 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700199 public void bindWorkspaceComponentsRemoved(
200 HashSet<String> packageNames, HashSet<ComponentName> components,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800201 UserHandle user);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700202 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800203 public void notifyWidgetProvidersChanged();
Sunny Goyald164b7f2016-10-12 20:49:31 -0700204 public void bindAllWidgets(MultiHashMap<PackageItemInfo, WidgetItem> widgets);
Adam Cohen1462de32012-07-24 22:34:36 -0700205 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700206 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700207 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMap);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400208 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530210 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800211 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400212 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100213 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Sunny Goyald164b7f2016-10-12 20:49:31 -0700214 mBgWidgetsModel = new WidgetsModel(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800215 mIconCache = iconCache;
216
Kenny Guyed131872014-04-30 03:02:21 +0100217 mLauncherApps = LauncherAppsCompat.getInstance(context);
218 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800219 }
220
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700221 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
222 * posted on the worker thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700223 private static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700224 if (sWorkerThread.getThreadId() == Process.myTid()) {
225 r.run();
226 } else {
227 // If we are not on the worker thread, then post to the worker handler
228 sWorker.post(r);
229 }
230 }
231
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700232 public void setPackageState(PackageInstallInfo installInfo) {
233 enqueueModelUpdateTask(new PackageInstallStateChangedTask(installInfo));
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500234 }
235
Sunny Goyal756adbc2015-04-16 15:20:51 -0700236 /**
237 * Updates the icons and label of all pending icons for the provided package name.
238 */
239 public void updateSessionDisplayInfo(final String packageName) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700240 HashSet<String> packages = new HashSet<>();
241 packages.add(packageName);
242 enqueueModelUpdateTask(new CacheDataUpdatedTask(
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800243 CacheDataUpdatedTask.OP_SESSION_UPDATE, Process.myUserHandle(), packages));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800244 }
245
246 /**
247 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800248 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700249 public void addAndBindAddedWorkspaceItems(
Sunny Goyala474a9b2017-05-04 16:47:11 -0700250 Provider<List<Pair<ItemInfo, Object>>> appsProvider) {
Sunny Goyal2bcbe132016-11-16 09:23:42 -0800251 enqueueModelUpdateTask(new AddWorkspaceItemsTask(appsProvider));
Winson Chung64359a52013-07-08 17:17:08 -0700252 }
253
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800254 public ModelWriter getWriter(boolean hasVerticalHotseat) {
255 return new ModelWriter(mApp.getContext(), sBgDataModel, hasVerticalHotseat);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800256 }
257
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700258 static void checkItemInfoLocked(
259 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
Sunny Goyale9956a72016-09-01 17:24:47 -0700260 ItemInfo modelItem = sBgDataModel.itemsIdMap.get(itemId);
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700261 if (modelItem != null && item != modelItem) {
262 // check all the data is consistent
263 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
264 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
265 ShortcutInfo shortcut = (ShortcutInfo) item;
266 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
267 modelShortcut.intent.filterEquals(shortcut.intent) &&
268 modelShortcut.id == shortcut.id &&
269 modelShortcut.itemType == shortcut.itemType &&
270 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700271 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700272 modelShortcut.cellX == shortcut.cellX &&
273 modelShortcut.cellY == shortcut.cellY &&
274 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700275 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700276 // For all intents and purposes, this is the same object
277 return;
278 }
279 }
280
281 // the modelItem needs to match up perfectly with item if our model is
282 // to be consistent with the database-- for now, just require
283 // modelItem == item or the equality check above
284 String msg = "item: " + ((item != null) ? item.toString() : "null") +
285 "modelItem: " +
286 ((modelItem != null) ? modelItem.toString() : "null") +
287 "Error: ItemInfo passed to checkItemInfo doesn't match original";
288 RuntimeException e = new RuntimeException(msg);
289 if (stackTrace != null) {
290 e.setStackTrace(stackTrace);
291 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800292 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700293 }
294 }
295
Michael Jurka816474f2012-06-25 14:49:02 -0700296 static void checkItemInfo(final ItemInfo item) {
297 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
298 final long itemId = item.id;
299 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700300 public void run() {
Sunny Goyale9956a72016-09-01 17:24:47 -0700301 synchronized (sBgDataModel) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700302 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700303 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700304 }
305 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700306 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700307 }
308
Joe Onorato9c1289c2009-08-17 11:03:03 -0400309 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700310 * Update the order of the workspace screens in the database. The array list contains
311 * a list of screen ids in the order that they should appear.
312 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700313 public static void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -0700314 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700315 final ContentResolver cr = context.getContentResolver();
316 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
317
318 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700319 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700320 while (iter.hasNext()) {
321 long id = iter.next();
322 if (id < 0) {
323 iter.remove();
324 }
325 }
326
327 Runnable r = new Runnable() {
328 @Override
329 public void run() {
Yura085c8532014-02-11 15:15:29 +0000330 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700331 // Clear the table
Yura085c8532014-02-11 15:15:29 +0000332 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -0700333 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700334 for (int i = 0; i < count; i++) {
335 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700336 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700337 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
338 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +0000339 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -0700340 }
Yura085c8532014-02-11 15:15:29 +0000341
342 try {
343 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
344 } catch (Exception ex) {
345 throw new RuntimeException(ex);
346 }
Winson Chung9e6a0a22013-08-27 11:58:12 -0700347
Sunny Goyale9956a72016-09-01 17:24:47 -0700348 synchronized (sBgDataModel) {
349 sBgDataModel.workspaceScreens.clear();
350 sBgDataModel.workspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -0700351 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700352 }
353 };
354 runOnWorkerThread(r);
355 }
356
357 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400358 * Set this as the current Launcher activity object for the loader.
359 */
360 public void initialize(Callbacks callbacks) {
361 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -0700362 Preconditions.assertUIThread();
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -0700363 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400364 }
365 }
366
Kenny Guyed131872014-04-30 03:02:21 +0100367 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800368 public void onPackageChanged(String packageName, UserHandle user) {
Kenny Guyed131872014-04-30 03:02:21 +0100369 int op = PackageUpdatedTask.OP_UPDATE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700370 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100371 }
372
373 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800374 public void onPackageRemoved(String packageName, UserHandle user) {
Sunny Goyalc2936bc2016-09-01 15:50:36 -0700375 onPackagesRemoved(user, packageName);
376 }
377
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800378 public void onPackagesRemoved(UserHandle user, String... packages) {
Kenny Guyed131872014-04-30 03:02:21 +0100379 int op = PackageUpdatedTask.OP_REMOVE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700380 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packages));
Kenny Guyed131872014-04-30 03:02:21 +0100381 }
382
383 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800384 public void onPackageAdded(String packageName, UserHandle user) {
Kenny Guyed131872014-04-30 03:02:21 +0100385 int op = PackageUpdatedTask.OP_ADD;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700386 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100387 }
388
389 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800390 public void onPackagesAvailable(String[] packageNames, UserHandle user,
Kenny Guyed131872014-04-30 03:02:21 +0100391 boolean replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700392 enqueueModelUpdateTask(
393 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100394 }
395
396 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800397 public void onPackagesUnavailable(String[] packageNames, UserHandle user,
Kenny Guyed131872014-04-30 03:02:21 +0100398 boolean replacing) {
399 if (!replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700400 enqueueModelUpdateTask(new PackageUpdatedTask(
401 PackageUpdatedTask.OP_UNAVAILABLE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100402 }
Kenny Guyed131872014-04-30 03:02:21 +0100403 }
404
Kenny Guy44cba692016-01-21 19:50:02 +0000405 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800406 public void onPackagesSuspended(String[] packageNames, UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700407 enqueueModelUpdateTask(new PackageUpdatedTask(
408 PackageUpdatedTask.OP_SUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000409 }
410
411 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800412 public void onPackagesUnsuspended(String[] packageNames, UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700413 enqueueModelUpdateTask(new PackageUpdatedTask(
414 PackageUpdatedTask.OP_UNSUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000415 }
416
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700417 @Override
418 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800419 UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700420 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, true));
Sunny Goyal50941fb2016-08-04 12:03:52 -0700421 }
422
423 public void updatePinnedShortcuts(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800424 UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700425 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, false));
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700426 }
427
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700428 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400429 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
430 * ACTION_PACKAGE_CHANGED.
431 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100432 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400433 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -0400434 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700435
Joe Onorato36115782010-06-17 13:28:48 -0400436 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +0100437 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -0700438 // If we have changed locale we need to clear out the labels in all apps/workspace.
439 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700440 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
441 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -0700442 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -0700443 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700444 } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
445 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
446 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800447 UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER);
Sunny Goyalda891c12016-03-18 18:29:24 -0700448 if (user != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700449 if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
450 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700451 enqueueModelUpdateTask(new PackageUpdatedTask(
452 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE, user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700453 }
454
455 // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so
456 // we need to run the state change task again.
457 if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
458 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700459 enqueueModelUpdateTask(new UserLockStateChangedTask(user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700460 }
Sunny Goyalda891c12016-03-18 18:29:24 -0700461 }
Tony Wickham827cef22016-03-17 15:39:39 -0700462 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
463 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700464 }
465 }
466
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800467 /**
468 * Reloads the workspace items from the DB and re-binds the workspace. This should generally
469 * not be called as DB updates are automatically followed by UI update
470 */
471 public void forceReload() {
472 synchronized (mLock) {
473 // Stop any existing loaders first, so they don't set mModelLoaded to true later
474 stopLoaderLocked();
475 mModelLoaded = false;
476 }
Winson Chungf0c6ae02012-03-21 16:10:31 -0700477
Reena Lee93f824a2011-09-23 17:20:28 -0700478 // Do this here because if the launcher activity is running it will be restarted.
479 // If it's not running startLoaderFromBackground will merely tell it that it needs
480 // to reload.
481 startLoaderFromBackground();
482 }
483
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700484 /**
485 * When the launcher is in the background, it's possible for it to miss paired
486 * configuration changes. So whenever we trigger the loader from the background
487 * tell the launcher that it needs to re-run the loader when it comes back instead
488 * of doing it now.
489 */
490 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800491 Callbacks callbacks = getCallback();
492 if (callbacks != null) {
493 // Only actually run the loader if they're not paused.
494 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -0700495 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700496 }
497 }
Joe Onorato36115782010-06-17 13:28:48 -0400498 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400499
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700500 /**
501 * If there is already a loader task running, tell it to stop.
502 */
503 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -0700504 LoaderTask oldTask = mLoaderTask;
505 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -0700506 oldTask.stopLocked();
507 }
Reena Lee93f824a2011-09-23 17:20:28 -0700508 }
509
Adam Cohen1a85c582014-09-30 09:48:49 -0700510 public boolean isCurrentCallbacks(Callbacks callbacks) {
511 return (mCallbacks != null && mCallbacks.get() == callbacks);
512 }
513
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700514 /**
515 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
516 * @return true if the page could be bound synchronously.
517 */
518 public boolean startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700519 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
Sunny Goyala474a9b2017-05-04 16:47:11 -0700520 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_LOADER_RUNNING);
Joe Onorato36115782010-06-17 13:28:48 -0400521 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -0400522 // Don't bother to start the thread if we know it's not going to do anything
523 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -0700524 final Callbacks oldCallbacks = mCallbacks.get();
525 // Clear any pending bind-runnables from the synchronized load process.
Sunny Goyalb265ba72017-02-14 15:03:45 -0800526 mUiExecutor.execute(new Runnable() {
527 public void run() {
528 oldCallbacks.clearPendingBinds();
529 }
530 });
Sunny Goyal527c7d32015-08-28 15:19:36 -0700531
Joe Onorato36115782010-06-17 13:28:48 -0400532 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700533 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700534 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800535 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
536 && mModelLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700537 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700538 return true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700539 } else {
540 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
541 sWorker.post(mLoaderTask);
542 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400543 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400544 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700545 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400546 }
547
Joe Onorato36115782010-06-17 13:28:48 -0400548 public void stopLoader() {
549 synchronized (mLock) {
550 if (mLoaderTask != null) {
551 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400552 }
553 }
Joe Onorato36115782010-06-17 13:28:48 -0400554 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400555
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800556 /**
557 * Loads the workspace screen ids in an ordered list.
558 */
Sunny Goyale5bb7052015-07-27 14:36:07 -0700559 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -0700560 final ContentResolver contentResolver = context.getContentResolver();
561 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -0700562
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800563 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -0700564 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
565 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -0700566 }
567
Mario Bertschler817afa32017-03-15 11:56:47 -0700568 public void onInstallSessionCreated(final PackageInstallInfo sessionInfo) {
569 enqueueModelUpdateTask(new ExtendedModelTask() {
570 @Override
571 public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
572 apps.addPromiseApp(app.getContext(), sessionInfo);
573 if (!apps.added.isEmpty()) {
574 final ArrayList<AppInfo> arrayList = new ArrayList<>(apps.added);
575 apps.added.clear();
576 scheduleCallbackTask(new CallbackTask() {
577 @Override
578 public void execute(Callbacks callbacks) {
579 callbacks.bindAppsAdded(null, null, null, arrayList);
580 }
581 });
582 }
583 }
584 });
585 }
586
Joe Onorato36115782010-06-17 13:28:48 -0400587 /**
588 * Runnable for the thread that loads the contents of the launcher:
589 * - workspace icons
590 * - widgets
591 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700592 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -0400593 */
594 private class LoaderTask implements Runnable {
595 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -0700596 private int mPageToBindFirst;
597
Joe Onorato36115782010-06-17 13:28:48 -0400598 private boolean mStopped;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700599
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700600 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -0400601 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -0700602 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400603 }
604
Joe Onorato36115782010-06-17 13:28:48 -0400605 private void waitForIdle() {
606 // Wait until the either we're stopped or the other threads are done.
607 // This way we don't start loading all apps until the workspace has settled
608 // down.
609 synchronized (LoaderTask.this) {
Sunny Goyalb265ba72017-02-14 15:03:45 -0800610 LooperIdleLock idleLock = new LooperIdleLock(this, Looper.getMainLooper());
611 // Just in case mFlushingWorkerThread changes but we aren't woken up,
612 // wait no longer than 1sec at a time
613 while (!mStopped && idleLock.awaitLocked(1000));
Daniel Sandler843e8602010-06-07 14:59:01 -0400614 }
Joe Onorato36115782010-06-17 13:28:48 -0400615 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400616
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700617 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -0500618 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700619 // Ensure that we have a valid page index to load synchronously
620 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
621 "valid page index");
622 }
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800623 if (!mModelLoaded) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700624 // Ensure that we don't try and bind a specified page when the pages have not been
625 // loaded already (we should load everything asynchronously in that case)
626 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
627 }
628 synchronized (mLock) {
629 if (mIsLoaderTaskRunning) {
630 // Ensure that we are never running the background loading at this point since
631 // we also touch the background collections
632 throw new RuntimeException("Error! Background loading is already running");
633 }
634 }
635
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700636 // Divide the set of loaded items into those that we are binding synchronously, and
637 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800638 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700639 // XXX: For now, continue posting the binding of AllApps as there are other issues that
640 // arise from that.
641 onlyBindAllApps();
Tony Wickham80f57872016-06-29 18:12:15 -0700642
643 bindDeepShortcuts();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700644 }
645
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800646 private void verifyNotStopped() throws CancellationException {
647 synchronized (LoaderTask.this) {
648 if (mStopped) {
649 throw new CancellationException("Loader stopped");
650 }
651 }
652 }
653
Joe Onorato36115782010-06-17 13:28:48 -0400654 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700655 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -0700656 if (mStopped) {
657 return;
658 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700659 mIsLoaderTaskRunning = true;
660 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400661
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800662 try {
Sunny Goyalb265ba72017-02-14 15:03:45 -0800663 long now = 0;
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800664 if (DEBUG_LOADERS) Log.d(TAG, "step 1.1: loading workspace");
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800665 loadWorkspace();
Joe Onorato36115782010-06-17 13:28:48 -0400666
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800667 verifyNotStopped();
668 if (DEBUG_LOADERS) Log.d(TAG, "step 1.2: bind workspace workspace");
669 bindWorkspace(mPageToBindFirst);
670
671 // Take a break
Sunny Goyalb265ba72017-02-14 15:03:45 -0800672 if (DEBUG_LOADERS) {
673 Log.d(TAG, "step 1 completed, wait for idle");
674 now = SystemClock.uptimeMillis();
675 }
Joe Onorato36115782010-06-17 13:28:48 -0400676 waitForIdle();
Sunny Goyalb265ba72017-02-14 15:03:45 -0800677 if (DEBUG_LOADERS) Log.d(TAG, "Waited " + (SystemClock.uptimeMillis() - now) + "ms");
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800678 verifyNotStopped();
Daniel Sandler843e8602010-06-07 14:59:01 -0400679
680 // second step
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800681 if (DEBUG_LOADERS) Log.d(TAG, "step 2.1: loading all apps");
682 loadAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700683
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800684 verifyNotStopped();
685 if (DEBUG_LOADERS) Log.d(TAG, "step 2.2: Update icon cache");
686 updateIconCache();
687
688 // Take a break
Sunny Goyalb265ba72017-02-14 15:03:45 -0800689 if (DEBUG_LOADERS) {
690 Log.d(TAG, "step 2 completed, wait for idle");
691 now = SystemClock.uptimeMillis();
692 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700693 waitForIdle();
Sunny Goyalb265ba72017-02-14 15:03:45 -0800694 if (DEBUG_LOADERS) Log.d(TAG, "Waited " + (SystemClock.uptimeMillis() - now) + "ms");
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800695 verifyNotStopped();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700696
697 // third step
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800698 if (DEBUG_LOADERS) Log.d(TAG, "step 3.1: loading deep shortcuts");
699 loadDeepShortcuts();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400700
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800701 verifyNotStopped();
702 if (DEBUG_LOADERS) Log.d(TAG, "step 3.2: bind deep shortcuts");
703 bindDeepShortcuts();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400704
Tony Wickhama7e1c1c2017-04-13 16:24:38 -0700705 // Take a break
706 if (DEBUG_LOADERS) Log.d(TAG, "step 3 completed, wait for idle");
707 waitForIdle();
708 verifyNotStopped();
709
710 // fourth step
711 if (DEBUG_LOADERS) Log.d(TAG, "step 4.1: loading widgets");
712 refreshAndBindWidgetsAndShortcuts(getCallback(), false /* bindFirst */,
713 null /* packageUser */);
714
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800715 synchronized (mLock) {
716 // Everything loaded bind the data.
717 mModelLoaded = true;
718 mHasLoaderCompletedOnce = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400719 }
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800720 } catch (CancellationException e) {
721 // Loader stopped, ignore
722 } finally {
723 // Clear out this reference, otherwise we end up holding it until all of the
724 // callback runnables are done.
725 mContext = null;
726
727 synchronized (mLock) {
728 // If we are still the last one to be scheduled, remove ourselves.
729 if (mLoaderTask == this) {
730 mLoaderTask = null;
731 }
732 mIsLoaderTaskRunning = false;
733 }
Joe Onorato36115782010-06-17 13:28:48 -0400734 }
Joe Onorato36115782010-06-17 13:28:48 -0400735 }
736
737 public void stopLocked() {
738 synchronized (LoaderTask.this) {
739 mStopped = true;
740 this.notify();
741 }
742 }
743
744 /**
745 * Gets the callbacks object. If we've been stopped, or if the launcher object
746 * has somehow been garbage collected, return null instead. Pass in the Callbacks
747 * object that was around when the deferred message was scheduled, and if there's
748 * a new Callbacks object around then also return null. This will save us from
749 * calling onto it with data that will be ignored.
750 */
751 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
752 synchronized (mLock) {
753 if (mStopped) {
754 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400755 }
Joe Onorato36115782010-06-17 13:28:48 -0400756
757 if (mCallbacks == null) {
758 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -0400759 }
Joe Onorato36115782010-06-17 13:28:48 -0400760
761 final Callbacks callbacks = mCallbacks.get();
762 if (callbacks != oldCallbacks) {
763 return null;
764 }
765 if (callbacks == null) {
766 Log.w(TAG, "no mCallbacks");
767 return null;
768 }
769
770 return callbacks;
771 }
772 }
773
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800774 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -0700775 if (LauncherAppState.PROFILE_STARTUP) {
776 Trace.beginSection("Loading Workspace");
777 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400778
Joe Onorato36115782010-06-17 13:28:48 -0400779 final Context context = mContext;
780 final ContentResolver contentResolver = context.getContentResolver();
Sunny Goyal342e4662017-02-02 15:21:08 -0800781 final PackageManagerHelper pmHelper = new PackageManagerHelper(context);
782 final boolean isSafeMode = pmHelper.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700783 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530784 final DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -0700785 final boolean isSdCardReady = Utilities.isBootCompleted();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800786 final MultiHashMap<UserHandle, String> pendingPackages = new MultiHashMap<>();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400787
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700788 boolean clearDb = false;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -0700789 try {
790 ImportDataTask.performImportIfPossible(context);
791 } catch (Exception e) {
792 // Migration failed. Clear workspace.
793 clearDb = true;
794 }
795
796 if (!clearDb && GridSizeMigrationTask.ENABLED &&
Sunny Goyalf076eae2016-01-11 12:25:10 -0800797 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
798 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700799 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -0700800 }
801
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700802 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -0700803 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -0700804 LauncherSettings.Settings.call(contentResolver,
Sunny Goyale05b08f2017-02-23 18:30:22 -0800805 LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -0500806 }
807
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700808 Log.d(TAG, "loadWorkspace: loading default favorites");
809 LauncherSettings.Settings.call(contentResolver,
810 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -0700811
Sunny Goyale9956a72016-09-01 17:24:47 -0700812 synchronized (sBgDataModel) {
813 sBgDataModel.clear();
814
Sunny Goyal756adbc2015-04-16 15:20:51 -0700815 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -0700816 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyale9956a72016-09-01 17:24:47 -0700817 sBgDataModel.workspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -0800818
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700819 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800820 final LoaderCursor c = new LoaderCursor(contentResolver.query(
821 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null), mApp);
Daniel Sandler8802e962010-05-26 16:28:16 -0400822
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800823 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400824
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700825 try {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700826 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
827 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -0400828 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
829 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700830 final int spanXIndex = c.getColumnIndexOrThrow
831 (LauncherSettings.Favorites.SPANX);
832 final int spanYIndex = c.getColumnIndexOrThrow(
833 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800834 final int rankIndex = c.getColumnIndexOrThrow(
835 LauncherSettings.Favorites.RANK);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700836 final int optionsIndex = c.getColumnIndexOrThrow(
837 LauncherSettings.Favorites.OPTIONS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400838
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800839 final LongSparseArray<UserHandle> allUsers = c.allUsers;
Kenny Guyff05f432016-01-22 17:48:29 +0000840 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700841 final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800842 for (UserHandle user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +0000843 long serialNo = mUserManager.getSerialNumberForUser(user);
844 allUsers.put(serialNo, user);
845 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700846
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700847 boolean userUnlocked = mUserManager.isUserUnlocked(user);
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700848
849 // We can only query for shortcuts when the user is unlocked.
850 if (userUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -0700851 List<ShortcutInfoCompat> pinnedShortcuts =
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530852 shortcutManager.queryForPinnedShortcuts(null, user);
853 if (shortcutManager.wasLastCallSuccess()) {
Sunny Goyal49f4f032016-08-01 15:45:49 -0700854 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
855 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
856 shortcut);
857 }
858 } else {
859 // Shortcut manager can fail due to some race condition when the
860 // lock state changes too frequently. For the purpose of the loading
861 // shortcuts, consider the user is still locked.
862 userUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700863 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700864 }
Sunny Goyal49f4f032016-08-01 15:45:49 -0700865 unlockedUsers.put(serialNo, userUnlocked);
Sunny Goyal7f834d22015-04-21 10:10:23 -0700866 }
867
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700868 ShortcutInfo info;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700869 LauncherAppWidgetInfo appWidgetInfo;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700870 Intent intent;
Sunny Goyal81e44912017-01-14 15:05:14 -0800871 String targetPkg;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400872
Jon Miranda529af302017-02-28 14:18:54 -0800873 FolderIconPreviewVerifier verifier =
874 new FolderIconPreviewVerifier(mApp.getInvariantDeviceProfile());
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700875 while (!mStopped && c.moveToNext()) {
876 try {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800877 if (c.user == null) {
878 // User has been deleted, remove the item.
879 c.markDeleted("User has been deleted");
880 continue;
881 }
882
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700883 boolean allowMissingTarget = false;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800884 switch (c.itemType) {
Sunny Goyal81e44912017-01-14 15:05:14 -0800885 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700886 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700887 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Sunny Goyal81e44912017-01-14 15:05:14 -0800888 intent = c.parseIntent();
889 if (intent == null) {
890 c.markDeleted("Invalid or null intent");
891 continue;
892 }
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800893
Sunny Goyal81e44912017-01-14 15:05:14 -0800894 int disabledState = quietMode.get(c.serialNumber) ?
895 ShortcutInfo.FLAG_DISABLED_QUIET_USER : 0;
896 ComponentName cn = intent.getComponent();
897 targetPkg = cn == null ? intent.getPackage() : cn.getPackageName();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700898
Sunny Goyal81e44912017-01-14 15:05:14 -0800899 if (!Process.myUserHandle().equals(c.user)) {
900 if (c.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
901 c.markDeleted("Legacy shortcuts are only allowed for default user");
902 continue;
903 } else if (c.restoreFlag != 0) {
904 // Don't restore items for other profiles.
905 c.markDeleted("Restore from managed profile not supported");
906 continue;
907 }
908 }
909 if (TextUtils.isEmpty(targetPkg) &&
910 c.itemType != LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
911 c.markDeleted("Only legacy shortcuts can have null package");
912 continue;
913 }
Sunny Goyal34942622014-08-29 17:20:55 -0700914
Sunny Goyal81e44912017-01-14 15:05:14 -0800915 // If there is no target package, its an implicit intent
916 // (legacy shortcut) which is always valid
917 boolean validTarget = TextUtils.isEmpty(targetPkg) ||
918 launcherApps.isPackageEnabledForProfile(targetPkg, c.user);
Sunny Goyal94485362014-09-18 16:13:58 -0700919
Sunny Goyal81e44912017-01-14 15:05:14 -0800920 if (cn != null && validTarget) {
921 // If the apk is present and the shortcut points to a specific
922 // component.
923
924 // If the component is already present
925 if (launcherApps.isActivityEnabledForProfile(cn, c.user)) {
926 // no special handling necessary for this item
927 c.markRestored();
928 } else {
Mario Bertschlerc06af332017-03-28 12:23:22 -0700929 if (c.hasRestoreFlag(ShortcutInfo.FLAG_AUTOINSTALL_ICON)) {
Sunny Goyal81e44912017-01-14 15:05:14 -0800930 // We allow auto install apps to have their intent
931 // updated after an install.
Sunny Goyal342e4662017-02-02 15:21:08 -0800932 intent = pmHelper.getAppLaunchIntent(targetPkg, c.user);
Sunny Goyal81e44912017-01-14 15:05:14 -0800933 if (intent != null) {
934 c.restoreFlag = 0;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800935 c.updater().put(
Sunny Goyal81e44912017-01-14 15:05:14 -0800936 LauncherSettings.Favorites.INTENT,
937 intent.toUri(0)).commit();
938 cn = intent.getComponent();
Sunny Goyalb05a00a2016-08-29 10:06:57 -0700939 } else {
Sunny Goyal81e44912017-01-14 15:05:14 -0800940 c.markDeleted("Unable to find a launch target");
Sunny Goyal94485362014-09-18 16:13:58 -0700941 continue;
942 }
Sunny Goyal1a745e82014-10-02 15:58:31 -0700943 } else {
Sunny Goyal81e44912017-01-14 15:05:14 -0800944 // The app is installed but the component is no
945 // longer available.
946 c.markDeleted("Invalid component removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -0700947 continue;
Winson Chungee055712013-07-30 14:46:24 -0700948 }
Winson Chungee055712013-07-30 14:46:24 -0700949 }
Sunny Goyal81e44912017-01-14 15:05:14 -0800950 }
951 // else if cn == null => can't infer much, leave it
952 // else if !validPkg => could be restored icon or missing sd-card
953
954 if (!TextUtils.isEmpty(targetPkg) && !validTarget) {
955 // Points to a valid app (superset of cn != null) but the apk
956 // is not available.
957
958 if (c.restoreFlag != 0) {
959 // Package is not yet available but might be
960 // installed later.
961 FileLog.d(TAG, "package not yet restored: " + targetPkg);
962
963 if (c.hasRestoreFlag(ShortcutInfo.FLAG_RESTORE_STARTED)) {
964 // Restore has started once.
965 } else if (installingPkgs.containsKey(targetPkg)) {
966 // App restore has started. Update the flag
967 c.restoreFlag |= ShortcutInfo.FLAG_RESTORE_STARTED;
968 c.updater().commit();
969 } else {
970 c.markDeleted("Unrestored app removed: " + targetPkg);
971 continue;
972 }
Sunny Goyalad2e91a2017-02-28 15:14:58 -0800973 } else if (pmHelper.isAppOnSdcard(targetPkg, c.user)) {
Sunny Goyal81e44912017-01-14 15:05:14 -0800974 // Package is present but not available.
975 disabledState |= ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
976 // Add the icon on the workspace anyway.
977 allowMissingTarget = true;
978 } else if (!isSdCardReady) {
979 // SdCard is not ready yet. Package might get available,
980 // once it is ready.
981 Log.d(TAG, "Missing pkg, will check later: " + targetPkg);
982 pendingPackages.addToList(c.user, targetPkg);
983 // Add the icon on the workspace anyway.
984 allowMissingTarget = true;
985 } else {
986 // Do not wait for external media load anymore.
987 c.markDeleted("Invalid package removed: " + targetPkg);
988 continue;
989 }
990 }
991
992 if (validTarget) {
993 // The shortcut points to a valid target (either no target
994 // or something which is ready to be used)
995 c.markRestored();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700996 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400997
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800998 boolean useLowResIcon = !c.isOnWorkspaceOrHotseat() &&
Jon Miranda529af302017-02-28 14:18:54 -0800999 !verifier.isItemInPreview(c.getInt(rankIndex));
Sunny Goyal34b65272015-03-11 16:56:52 -07001000
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001001 if (c.restoreFlag != 0) {
Sunny Goyal81e44912017-01-14 15:05:14 -08001002 // Already verified above that user is same as default user
1003 info = c.getRestoredItemInfo(intent);
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001004 } else if (c.itemType ==
Chris Wrenf4d08112014-01-16 18:13:56 -05001005 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001006 info = c.getAppShortcutInfo(
1007 intent, allowMissingTarget, useLowResIcon);
1008 } else if (c.itemType ==
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001009 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001010
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001011 ShortcutKey key = ShortcutKey.fromIntent(intent, c.user);
1012 if (unlockedUsers.get(c.serialNumber)) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001013 ShortcutInfoCompat pinnedShortcut =
1014 shortcutKeyToPinnedShortcuts.get(key);
1015 if (pinnedShortcut == null) {
1016 // The shortcut is no longer valid.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001017 c.markDeleted("Pinned shortcut not found");
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001018 continue;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001019 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001020 info = new ShortcutInfo(pinnedShortcut, context);
Sunny Goyal1b072632017-01-18 11:30:23 -08001021 info.iconBitmap = LauncherIcons
1022 .createShortcutIcon(pinnedShortcut, context);
Charles He3ff90472017-02-15 12:22:56 +00001023 if (pmHelper.isAppSuspended(
Sunny Goyalad2e91a2017-02-28 15:14:58 -08001024 pinnedShortcut.getPackage(), info.user)) {
Charles He3ff90472017-02-15 12:22:56 +00001025 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1026 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001027 intent = info.intent;
1028 } else {
1029 // Create a shortcut info in disabled mode for now.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001030 info = c.loadSimpleShortcut();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001031 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001032 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001033 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001034 info = c.loadSimpleShortcut();
Michael Jurka96879562012-03-22 05:54:33 -07001035
Sunny Goyald09c3702016-04-06 16:18:20 -07001036 // Shortcuts are only available on the primary profile
Tony Wickham431a7132017-05-04 15:39:43 -07001037 if (!TextUtils.isEmpty(targetPkg)
1038 && pmHelper.isAppSuspended(targetPkg, c.user)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001039 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1040 }
1041
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001042 // App shortcuts that used to be automatically added to Launcher
1043 // didn't always have the correct intent flags set, so do that
1044 // here
1045 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001046 intent.getCategories() != null &&
1047 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001048 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001049 intent.addFlags(
1050 Intent.FLAG_ACTIVITY_NEW_TASK |
1051 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1052 }
Michael Jurka96879562012-03-22 05:54:33 -07001053 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001054
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001055 if (info != null) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001056 c.applyCommonProperties(info);
1057
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001058 info.intent = intent;
Sunny Goyal08f72612015-01-05 13:41:43 -08001059 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001060 info.spanX = 1;
1061 info.spanY = 1;
Sunny Goyald09c3702016-04-06 16:18:20 -07001062 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001063 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1064 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1065 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001066
Sunny Goyal81e44912017-01-14 15:05:14 -08001067 if (c.restoreFlag != 0 && !TextUtils.isEmpty(targetPkg)) {
1068 Integer progress = installingPkgs.get(targetPkg);
1069 if (progress != null) {
1070 info.setInstallProgress(progress);
1071 } else {
1072 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001073 }
1074 }
1075
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001076 c.checkAndAddItem(info, sBgDataModel);
Winson Chung1323b482013-08-05 12:41:55 -07001077 } else {
1078 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001079 }
1080 break;
1081
1082 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001083 FolderInfo folderInfo = sBgDataModel.findOrMakeFolder(c.id);
1084 c.applyCommonProperties(folderInfo);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001085
Sunny Goyala508e4f2015-05-21 09:33:57 -07001086 // Do not trim the folder label, as is was set by the user.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001087 folderInfo.title = c.getString(c.titleIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001088 folderInfo.spanX = 1;
1089 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001090 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001091
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001092 // no special handling required for restored folders
1093 c.markRestored();
Chris Wrenf4d08112014-01-16 18:13:56 -05001094
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001095 c.checkAndAddItem(folderInfo, sBgDataModel);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001096 break;
1097
1098 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08001099 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001100 // Read all Launcher-specific widget details
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001101 boolean customWidget = c.itemType ==
Adam Cohen59400422014-03-05 18:07:04 -08001102 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
1103
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001104 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001105 String savedProvider = c.getString(appWidgetProviderIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001106
Sunny Goyalff572272014-07-23 13:58:07 -07001107 final ComponentName component =
1108 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04001109
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001110 final boolean isIdValid = !c.hasRestoreFlag(
1111 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID);
1112 final boolean wasProviderReady = !c.hasRestoreFlag(
1113 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY);
Sunny Goyal651077b2014-06-30 14:15:31 -07001114
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001115 if (widgetProvidersMap == null) {
1116 widgetProvidersMap = AppWidgetManagerCompat
1117 .getInstance(mContext).getAllProvidersMap();
1118 }
1119 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
1120 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00001121 ComponentName.unflattenFromString(savedProvider),
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001122 c.user));
Sunny Goyalff572272014-07-23 13:58:07 -07001123
1124 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08001125 if (!isSafeMode && !customWidget &&
1126 wasProviderReady && !isProviderReady) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001127 c.markDeleted(
1128 "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07001129 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001130 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07001131 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07001132 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1133 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08001134
Sunny Goyal53f96722015-07-13 19:54:53 -07001135 // The provider is available. So the widget is either
1136 // available or not available. We do not need to track
1137 // any future restore updates.
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001138 int status = c.restoreFlag &
Sunny Goyal53f96722015-07-13 19:54:53 -07001139 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07001140 if (!wasProviderReady) {
1141 // If provider was not previously ready, update the
1142 // status and UI flag.
1143
1144 // Id would be valid only if the widget restore broadcast was received.
1145 if (isIdValid) {
Sunny Goyal86df1382016-08-10 15:03:22 -07001146 status |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07001147 } else {
1148 status &= ~LauncherAppWidgetInfo
1149 .FLAG_PROVIDER_NOT_READY;
1150 }
1151 }
1152 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07001153 } else {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001154 Log.v(TAG, "Widget restore pending id=" + c.id
Sunny Goyal651077b2014-06-30 14:15:31 -07001155 + " appWidgetId=" + appWidgetId
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001156 + " status =" + c.restoreFlag);
Sunny Goyal651077b2014-06-30 14:15:31 -07001157 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07001158 component);
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001159 appWidgetInfo.restoreStatus = c.restoreFlag;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001160 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07001161
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001162 if (c.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_RESTORE_STARTED)) {
Sunny Goyal94485362014-09-18 16:13:58 -07001163 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001164 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07001165 // App restore has started. Update the flag
1166 appWidgetInfo.restoreStatus |=
1167 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001168 } else if (!isSafeMode) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001169 c.markDeleted("Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07001170 continue;
1171 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07001172
1173 appWidgetInfo.installProgress =
1174 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07001175 }
Sunny Goyal86df1382016-08-10 15:03:22 -07001176 if (appWidgetInfo.hasRestoreFlag(
1177 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG)) {
Sunny Goyal81e44912017-01-14 15:05:14 -08001178 appWidgetInfo.bindOptions = c.parseIntent();
Sunny Goyal86df1382016-08-10 15:03:22 -07001179 }
Sunny Goyalff572272014-07-23 13:58:07 -07001180
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001181 c.applyCommonProperties(appWidgetInfo);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001182 appWidgetInfo.spanX = c.getInt(spanXIndex);
1183 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001184 appWidgetInfo.user = c.user;
Joe Onorato36115782010-06-17 13:28:48 -04001185
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001186 if (!c.isOnWorkspaceOrHotseat()) {
1187 c.markDeleted("Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001188 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001189 continue;
1190 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001191
Adam Cohen59400422014-03-05 18:07:04 -08001192 if (!customWidget) {
1193 String providerName =
1194 appWidgetInfo.providerName.flattenToString();
1195 if (!providerName.equals(savedProvider) ||
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001196 (appWidgetInfo.restoreStatus != c.restoreFlag)) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001197 c.updater()
1198 .put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1199 providerName)
1200 .put(LauncherSettings.Favorites.RESTORED,
1201 appWidgetInfo.restoreStatus)
1202 .commit();
Adam Cohen59400422014-03-05 18:07:04 -08001203 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001204 }
Jon Miranda2b823f42017-05-02 18:36:18 -07001205
1206 if (appWidgetInfo.restoreStatus !=
1207 LauncherAppWidgetInfo.RESTORE_COMPLETED) {
1208 String pkg = appWidgetInfo.providerName.getPackageName();
1209 appWidgetInfo.pendingItemInfo = new PackageItemInfo(pkg);
1210 appWidgetInfo.pendingItemInfo.user = appWidgetInfo.user;
1211 mIconCache.getTitleAndIconForApp(
1212 appWidgetInfo.pendingItemInfo, false);
1213 }
1214
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001215 c.checkAndAddItem(appWidgetInfo, sBgDataModel);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001216 }
Joe Onorato36115782010-06-17 13:28:48 -04001217 break;
1218 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001219 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001220 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001221 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001222 }
1223 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001224 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001225 }
1226
Winson Chungba9c37f2013-08-30 14:11:37 -07001227 // Break early if we've stopped loading
1228 if (mStopped) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001229 sBgDataModel.clear();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001230 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07001231 }
1232
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001233 // Remove dead items
1234 if (c.commitDeleted()) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001235 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07001236 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
1237 .call(contentResolver,
1238 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
1239 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
1240 for (long folderId : deletedFolderIds) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001241 sBgDataModel.workspaceItems.remove(sBgDataModel.folders.get(folderId));
1242 sBgDataModel.folders.remove(folderId);
1243 sBgDataModel.itemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001244 }
Sunny Goyal55fddc82017-04-06 15:02:52 -07001245
1246 // Remove any ghost widgets
1247 LauncherSettings.Settings.call(contentResolver,
1248 LauncherSettings.Settings.METHOD_REMOVE_GHOST_WIDGETS);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001249 }
1250
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001251 // Unpin shortcuts that don't exist on the workspace.
Sunny Goyalf75baa92016-11-22 03:23:51 +05301252 HashSet<ShortcutKey> pendingShortcuts =
1253 InstallShortcutReceiver.getPendingShortcuts(context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001254 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001255 MutableInt numTimesPinned = sBgDataModel.pinnedShortcutCounts.get(key);
Sunny Goyalf75baa92016-11-22 03:23:51 +05301256 if ((numTimesPinned == null || numTimesPinned.value == 0)
1257 && !pendingShortcuts.contains(key)) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001258 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301259 shortcutManager.unpinShortcut(key);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001260 }
1261 }
1262
Jon Miranda655ec422017-02-07 11:40:22 -08001263 FolderIconPreviewVerifier verifier =
1264 new FolderIconPreviewVerifier(mApp.getInvariantDeviceProfile());
1265 // Sort the folder items and make sure all items in the preview are high resolution.
Sunny Goyale9956a72016-09-01 17:24:47 -07001266 for (FolderInfo folder : sBgDataModel.folders) {
Sunny Goyal317698b2015-07-29 11:45:41 -07001267 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
Jon Miranda655ec422017-02-07 11:40:22 -08001268 verifier.setFolderInfo(folder);
1269
1270 int numItemsInPreview = 0;
Sunny Goyal317698b2015-07-29 11:45:41 -07001271 for (ShortcutInfo info : folder.contents) {
Jon Miranda655ec422017-02-07 11:40:22 -08001272 if (info.usingLowResIcon
1273 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION
1274 && verifier.isItemInPreview(info.rank)) {
Sunny Goyal3fe4a142016-12-15 17:40:07 -08001275 mIconCache.getTitleAndIcon(info, false);
Jon Miranda655ec422017-02-07 11:40:22 -08001276 numItemsInPreview++;
Sunny Goyal317698b2015-07-29 11:45:41 -07001277 }
Jon Miranda655ec422017-02-07 11:40:22 -08001278
1279 if (numItemsInPreview >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
Sunny Goyal317698b2015-07-29 11:45:41 -07001280 break;
1281 }
1282 }
1283 }
1284
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001285 c.commitRestoredItems();
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001286 if (!isSdCardReady && !pendingPackages.isEmpty()) {
1287 context.registerReceiver(
1288 new SdCardAvailableReceiver(
1289 LauncherModel.this, mContext, pendingPackages),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001290 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001291 null,
1292 sWorker);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001293 }
1294
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001295 // Remove any empty screens
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001296 ArrayList<Long> unusedScreens = new ArrayList<>(sBgDataModel.workspaceScreens);
Sunny Goyale9956a72016-09-01 17:24:47 -07001297 for (ItemInfo item: sBgDataModel.itemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001298 long screenId = item.screenId;
1299 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1300 unusedScreens.contains(screenId)) {
1301 unusedScreens.remove(screenId);
1302 }
1303 }
1304
1305 // If there are any empty screens remove them, and update.
1306 if (unusedScreens.size() != 0) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001307 sBgDataModel.workspaceScreens.removeAll(unusedScreens);
1308 updateWorkspaceScreenOrder(context, sBgDataModel.workspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001309 }
Joe Onorato36115782010-06-17 13:28:48 -04001310 }
Sunny Goyale26d1002016-06-20 14:52:14 -07001311 if (LauncherAppState.PROFILE_STARTUP) {
1312 Trace.endSection();
1313 }
Adam Cohene25af792013-06-06 23:08:25 -07001314 }
1315
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001316 /** Filters the set of items who are directly or indirectly (via another container) on the
1317 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08001318 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001319 ArrayList<ItemInfo> allWorkspaceItems,
1320 ArrayList<ItemInfo> currentScreenItems,
1321 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001322 // Purge any null ItemInfos
1323 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
1324 while (iter.hasNext()) {
1325 ItemInfo i = iter.next();
1326 if (i == null) {
1327 iter.remove();
1328 }
1329 }
1330
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001331 // Order the set of items by their containers first, this allows use to walk through the
1332 // list sequentially, build up a list of containers that are in the specified screen,
1333 // as well as all items in those containers.
1334 Set<Long> itemsOnScreen = new HashSet<Long>();
1335 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
1336 @Override
1337 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07001338 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001339 }
1340 });
1341 for (ItemInfo info : allWorkspaceItems) {
1342 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001343 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001344 currentScreenItems.add(info);
1345 itemsOnScreen.add(info.id);
1346 } else {
1347 otherScreenItems.add(info);
1348 }
1349 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1350 currentScreenItems.add(info);
1351 itemsOnScreen.add(info.id);
1352 } else {
1353 if (itemsOnScreen.contains(info.container)) {
1354 currentScreenItems.add(info);
1355 itemsOnScreen.add(info.id);
1356 } else {
1357 otherScreenItems.add(info);
1358 }
1359 }
1360 }
1361 }
1362
1363 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08001364 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001365 ArrayList<LauncherAppWidgetInfo> appWidgets,
1366 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
1367 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001368
1369 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001370 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001371 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08001372 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001373 currentScreenWidgets.add(widget);
1374 } else {
1375 otherScreenWidgets.add(widget);
1376 }
1377 }
1378 }
1379
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001380 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
1381 * right) */
1382 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Sunny Goyal87f784c2017-01-11 10:48:34 -08001383 final InvariantDeviceProfile profile = mApp.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07001384 final int screenCols = profile.numColumns;
1385 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001386 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07001387 @Override
1388 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07001389 if (lhs.container == rhs.container) {
1390 // Within containers, order by their spatial position in that container
1391 switch ((int) lhs.container) {
1392 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
1393 long lr = (lhs.screenId * screenCellCount +
1394 lhs.cellY * screenCols + lhs.cellX);
1395 long rr = (rhs.screenId * screenCellCount +
1396 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07001397 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07001398 }
1399 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
1400 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07001401 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07001402 }
1403 default:
Sunny Goyal3d706ad2017-03-06 16:56:39 -08001404 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07001405 throw new RuntimeException("Unexpected container type when " +
1406 "sorting workspace items.");
1407 }
1408 return 0;
1409 }
1410 } else {
1411 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07001412 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07001413 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001414 }
1415 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001416 }
Winson Chungdb8a8942012-04-03 14:08:41 -07001417
Adam Cohendcd297f2013-06-18 13:13:40 -07001418 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
1419 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001420 final Runnable r = new Runnable() {
1421 @Override
1422 public void run() {
1423 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1424 if (callbacks != null) {
1425 callbacks.bindScreens(orderedScreens);
1426 }
1427 }
1428 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001429 mUiExecutor.execute(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07001430 }
1431
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001432 private void bindWorkspaceItems(final Callbacks oldCallbacks,
1433 final ArrayList<ItemInfo> workspaceItems,
1434 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07001435 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001436
1437 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07001438 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001439 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04001440 final int start = i;
1441 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001442 final Runnable r = new Runnable() {
1443 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04001444 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08001445 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001446 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07001447 callbacks.bindItems(workspaceItems, start, start+chunkSize,
1448 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001449 }
1450 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001451 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001452 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04001453 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001454
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001455 // Bind the widgets, one at a time
1456 N = appWidgets.size();
1457 for (int i = 0; i < N; i++) {
1458 final LauncherAppWidgetInfo widget = appWidgets.get(i);
1459 final Runnable r = new Runnable() {
1460 public void run() {
1461 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1462 if (callbacks != null) {
1463 callbacks.bindAppWidget(widget);
1464 }
1465 }
1466 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001467 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001468 }
1469 }
1470
1471 /**
1472 * Binds all loaded data to actual views on the main thread.
1473 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001474 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001475 final long t = SystemClock.uptimeMillis();
1476 Runnable r;
1477
1478 // Don't use these two variables in any of the callback runnables.
1479 // Otherwise we hold a reference to them.
1480 final Callbacks oldCallbacks = mCallbacks.get();
1481 if (oldCallbacks == null) {
1482 // This launcher has exited and nobody bothered to tell us. Just bail.
1483 Log.w(TAG, "LoaderTask running with no launcher");
1484 return;
1485 }
1486
Winson Chung9b9fb962013-11-15 15:39:34 -08001487 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07001488 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
1489 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
1490 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07001491
Sunny Goyale9956a72016-09-01 17:24:47 -07001492 synchronized (sBgDataModel) {
1493 workspaceItems.addAll(sBgDataModel.workspaceItems);
1494 appWidgets.addAll(sBgDataModel.appWidgets);
1495 orderedScreenIds.addAll(sBgDataModel.workspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001496 }
1497
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001498 final int currentScreen;
1499 {
1500 int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE
1501 ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen();
1502 if (currScreen >= orderedScreenIds.size()) {
1503 // There may be no workspace screens (just hotseat items and an empty page).
1504 currScreen = PagedView.INVALID_RESTORE_PAGE;
1505 }
1506 currentScreen = currScreen;
Winson Chung9b9fb962013-11-15 15:39:34 -08001507 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001508 final boolean validFirstPage = currentScreen >= 0;
1509 final long currentScreenId =
1510 validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID;
Winson Chung9b9fb962013-11-15 15:39:34 -08001511
Winson Chung9b9fb962013-11-15 15:39:34 -08001512 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07001513 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
1514 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
1515 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
1516 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001517
Winson Chung9b9fb962013-11-15 15:39:34 -08001518 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001519 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08001520 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001521 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001522 sortWorkspaceItemsSpatially(currentWorkspaceItems);
1523 sortWorkspaceItemsSpatially(otherWorkspaceItems);
1524
1525 // Tell the workspace that we're about to start binding items
1526 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001527 public void run() {
1528 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1529 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001530 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001531 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04001532 }
1533 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001534 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001535 mUiExecutor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001536
Adam Cohendcd297f2013-06-18 13:13:40 -07001537 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
1538
Sunny Goyalb265ba72017-02-14 15:03:45 -08001539 Executor mainExecutor = mUiExecutor;
Sunny Goyal527c7d32015-08-28 15:19:36 -07001540 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07001541 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001542
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001543 // In case of validFirstPage, only bind the first screen, and defer binding the
1544 // remaining screens after first onDraw (and an optional the fade animation whichever
1545 // happens later).
1546 // This ensures that the first screen is immediately visible (eg. during rotation)
1547 // In case of !validFirstPage, bind all pages one after other.
1548 final Executor deferredExecutor =
Sunny Goyalb265ba72017-02-14 15:03:45 -08001549 validFirstPage ? new ViewOnDrawExecutor(mUiExecutor) : mainExecutor;
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001550
1551 mainExecutor.execute(new Runnable() {
1552 @Override
1553 public void run() {
1554 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1555 if (callbacks != null) {
1556 callbacks.finishFirstPageBind(
1557 validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null);
1558 }
1559 }
1560 });
Sunny Goyal527c7d32015-08-28 15:19:36 -07001561
Sunny Goyal44c06432016-04-02 10:56:02 -07001562 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001563
1564 // Tell the workspace that we're done binding items
1565 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001566 public void run() {
1567 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1568 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001569 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04001570 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001571
Winson Chung98e030b2012-05-07 16:01:11 -07001572 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04001573 if (DEBUG_LOADERS) {
1574 Log.d(TAG, "bound workspace in "
1575 + (SystemClock.uptimeMillis()-t) + "ms");
1576 }
Winson Chung36a62fe2012-05-06 18:04:42 -07001577
Joe Onorato36115782010-06-17 13:28:48 -04001578 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001579 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07001580 deferredExecutor.execute(r);
1581
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001582 if (validFirstPage) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001583 r = new Runnable() {
1584 public void run() {
1585 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1586 if (callbacks != null) {
1587 // We are loading synchronously, which means, some of the pages will be
1588 // bound after first draw. Inform the callbacks that page binding is
1589 // not complete, and schedule the remaining pages.
1590 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
1591 callbacks.onPageBoundSynchronously(currentScreen);
1592 }
1593 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
1594 }
1595 }
1596 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001597 mUiExecutor.execute(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07001598 }
Joe Onorato36115782010-06-17 13:28:48 -04001599 }
Joe Onoratocc67f472010-06-08 10:54:30 -07001600
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001601 private void updateIconCache() {
1602 // Ignore packages which have a promise icon.
1603 HashSet<String> packagesToIgnore = new HashSet<>();
Sunny Goyale9956a72016-09-01 17:24:47 -07001604 synchronized (sBgDataModel) {
1605 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001606 if (info instanceof ShortcutInfo) {
1607 ShortcutInfo si = (ShortcutInfo) info;
1608 if (si.isPromise() && si.getTargetComponent() != null) {
1609 packagesToIgnore.add(si.getTargetComponent().getPackageName());
1610 }
1611 } else if (info instanceof LauncherAppWidgetInfo) {
1612 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
1613 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
1614 packagesToIgnore.add(lawi.providerName.getPackageName());
1615 }
1616 }
1617 }
1618 }
1619 mIconCache.updateDbIcons(packagesToIgnore);
1620 }
1621
Joe Onorato36115782010-06-17 13:28:48 -04001622 private void onlyBindAllApps() {
1623 final Callbacks oldCallbacks = mCallbacks.get();
1624 if (oldCallbacks == null) {
1625 // This launcher has exited and nobody bothered to tell us. Just bail.
1626 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
1627 return;
1628 }
1629
1630 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07001631 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001632 final ArrayList<AppInfo> list
1633 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07001634 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04001635 public void run() {
1636 final long t = SystemClock.uptimeMillis();
1637 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
1638 if (callbacks != null) {
1639 callbacks.bindAllApplications(list);
1640 }
1641 if (DEBUG_LOADERS) {
1642 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08001643 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04001644 }
1645 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07001646 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001647 mUiExecutor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04001648 }
1649
Winson Chung64359a52013-07-08 17:17:08 -07001650 private void loadAllApps() {
1651 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04001652
Joe Onorato36115782010-06-17 13:28:48 -04001653 final Callbacks oldCallbacks = mCallbacks.get();
1654 if (oldCallbacks == null) {
1655 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07001656 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04001657 return;
1658 }
1659
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001660 final List<UserHandle> profiles = mUserManager.getUserProfiles();
Kenny Guyed131872014-04-30 03:02:21 +01001661
Winson Chung64359a52013-07-08 17:17:08 -07001662 // Clear the list of apps
1663 mBgAllAppsList.clear();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001664 for (UserHandle user : profiles) {
Kenny Guyed131872014-04-30 03:02:21 +01001665 // Query for the set of apps
1666 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal3e9be432017-01-05 15:22:41 -08001667 final List<LauncherActivityInfo> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01001668 if (DEBUG_LOADERS) {
1669 Log.d(TAG, "getActivityList took "
1670 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
1671 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
1672 }
1673 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08001674 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01001675 if (apps == null || apps.isEmpty()) {
1676 return;
1677 }
Kenny Guyff05f432016-01-22 17:48:29 +00001678 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01001679 // Create the ApplicationInfos
1680 for (int i = 0; i < apps.size(); i++) {
Sunny Goyal3e9be432017-01-05 15:22:41 -08001681 LauncherActivityInfo app = apps.get(i);
Kenny Guyed131872014-04-30 03:02:21 +01001682 // This builds the icon bitmaps.
Sunny Goyal24bb66a2017-03-21 15:12:01 -07001683 mBgAllAppsList.add(new AppInfo(app, user, quietMode), app);
Kenny Guyed131872014-04-30 03:02:21 +01001684 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08001685
Sunny Goyala474a9b2017-05-04 16:47:11 -07001686 ManagedProfileHeuristic.onAllAppsLoaded(mContext, apps, user);
Winson Chung64359a52013-07-08 17:17:08 -07001687 }
Mario Bertschler817afa32017-03-15 11:56:47 -07001688
1689 if (FeatureFlags.LAUNCHER3_PROMISE_APPS_IN_ALL_APPS) {
1690 // get all active sessions and add them to the all apps list
1691 PackageInstallerCompat installer = PackageInstallerCompat.getInstance(mContext);
1692 for (PackageInstaller.SessionInfo info : installer.getAllVerifiedSessions()) {
1693 mBgAllAppsList.addPromiseApp(mContext,
1694 PackageInstallInfo.fromInstallingState(info));
1695 }
1696 }
1697
Bjorn Bringert85f418d2013-09-06 12:50:05 +01001698 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001699 final ArrayList<AppInfo> added = mBgAllAppsList.added;
1700 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07001701
Mario Bertschler817afa32017-03-15 11:56:47 -07001702
Winson Chung64359a52013-07-08 17:17:08 -07001703 // Post callback on main thread
Sunny Goyalb265ba72017-02-14 15:03:45 -08001704 mUiExecutor.execute(new Runnable() {
Winson Chung64359a52013-07-08 17:17:08 -07001705 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07001706
Winson Chung64359a52013-07-08 17:17:08 -07001707 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07001708 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07001709 if (callbacks != null) {
1710 callbacks.bindAllApplications(added);
1711 if (DEBUG_LOADERS) {
1712 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001713 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07001714 }
1715 } else {
1716 Log.i(TAG, "not binding apps: no Launcher activity");
1717 }
1718 }
1719 });
Joe Onorato36115782010-06-17 13:28:48 -04001720 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07001721 Log.d(TAG, "Icons processed in "
1722 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08001723 }
1724 }
1725
Sunny Goyaldd96a5e2017-02-17 11:22:34 -08001726 private void loadDeepShortcuts() {
Sunny Goyal4869bf32017-05-17 12:05:46 -07001727 sBgDataModel.deepShortcutMap.clear();
1728 DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(mContext);
1729 mHasShortcutHostPermission = shortcutManager.hasHostPermission();
1730 if (mHasShortcutHostPermission) {
1731 for (UserHandle user : mUserManager.getUserProfiles()) {
1732 if (mUserManager.isUserUnlocked(user)) {
1733 List<ShortcutInfoCompat> shortcuts =
1734 shortcutManager.queryForAllShortcuts(user);
1735 sBgDataModel.updateDeepShortcutMap(null, user, shortcuts);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001736 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001737 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001738 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001739 }
Joe Onorato36115782010-06-17 13:28:48 -04001740 }
1741
Tony Wickham80f57872016-06-29 18:12:15 -07001742 public void bindDeepShortcuts() {
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07001743 final MultiHashMap<ComponentKey, String> shortcutMapCopy =
1744 sBgDataModel.deepShortcutMap.clone();
Tony Wickham80f57872016-06-29 18:12:15 -07001745 Runnable r = new Runnable() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001746 @Override
1747 public void run() {
1748 Callbacks callbacks = getCallback();
1749 if (callbacks != null) {
1750 callbacks.bindDeepShortcutMap(shortcutMapCopy);
1751 }
1752 }
Tony Wickham80f57872016-06-29 18:12:15 -07001753 };
Sunny Goyalb265ba72017-02-14 15:03:45 -08001754 mUiExecutor.execute(r);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001755 }
1756
Sunny Goyal75b0f552015-05-20 21:57:06 -07001757 /**
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001758 * Refreshes the cached shortcuts if the shortcut permission has changed.
1759 * Current implementation simply reloads the workspace, but it can be optimized to
1760 * use partial updates similar to {@link UserManagerCompat}
1761 */
1762 public void refreshShortcutsIfRequired() {
Sunny Goyalf5e37442016-11-02 10:31:24 -07001763 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001764 sWorker.removeCallbacks(mShortcutPermissionCheckRunnable);
1765 sWorker.post(mShortcutPermissionCheckRunnable);
1766 }
1767 }
1768
1769 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -07001770 * Called when the icons for packages have been updated in the icon cache.
1771 */
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001772 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandle user) {
Sunny Goyal75b0f552015-05-20 21:57:06 -07001773 // If any package icon has changed (app was updated while launcher was dead),
1774 // update the corresponding shortcuts.
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001775 enqueueModelUpdateTask(new CacheDataUpdatedTask(
1776 CacheDataUpdatedTask.OP_CACHE_UPDATE, user, updatedPackages));
Sunny Goyal75b0f552015-05-20 21:57:06 -07001777 }
1778
Sunny Goyal0e947fb2017-05-19 12:21:50 -07001779 public void enqueueModelUpdateTask(BaseModelUpdateTask task) {
Jon Mirandaf28629a2017-02-23 15:12:42 -08001780 if (!mModelLoaded && mLoaderTask == null) {
1781 if (DEBUG_LOADERS) {
1782 Log.d(TAG, "enqueueModelUpdateTask Ignoring task since loader is pending=" + task);
1783 }
1784 return;
1785 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001786 task.init(this);
1787 runOnWorkerThread(task);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001788 }
1789
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001790 /**
1791 * A task to be executed on the current callbacks on the UI thread.
1792 * If there is no current callbacks, the task is ignored.
1793 */
1794 public interface CallbackTask {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001795
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001796 void execute(Callbacks callbacks);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001797 }
1798
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001799 /**
1800 * A runnable which changes/updates the data model of the launcher based on certain events.
1801 */
1802 public static abstract class BaseModelUpdateTask implements Runnable {
Joe Onorato36115782010-06-17 13:28:48 -04001803
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001804 private LauncherModel mModel;
Sunny Goyalb265ba72017-02-14 15:03:45 -08001805 private Executor mUiExecutor;
Joe Onorato36115782010-06-17 13:28:48 -04001806
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001807 /* package private */
1808 void init(LauncherModel model) {
1809 mModel = model;
Sunny Goyalb265ba72017-02-14 15:03:45 -08001810 mUiExecutor = mModel.mUiExecutor;
Joe Onorato36115782010-06-17 13:28:48 -04001811 }
1812
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001813 @Override
Joe Onorato36115782010-06-17 13:28:48 -04001814 public void run() {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001815 if (!mModel.mHasLoaderCompletedOnce) {
Sunny Goyalc905efc2015-05-06 09:54:53 -07001816 // Loader has not yet run.
1817 return;
1818 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001819 execute(mModel.mApp, sBgDataModel, mModel.mBgAllAppsList);
1820 }
Joe Onorato36115782010-06-17 13:28:48 -04001821
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001822 /**
1823 * Execute the actual task. Called on the worker thread.
1824 */
1825 public abstract void execute(
1826 LauncherAppState app, BgDataModel dataModel, AllAppsList apps);
Sunny Goyale0f58d72014-11-10 18:05:31 -08001827
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001828 /**
1829 * Schedules a {@param task} to be executed on the current callbacks.
1830 */
1831 public final void scheduleCallbackTask(final CallbackTask task) {
1832 final Callbacks callbacks = mModel.getCallback();
Sunny Goyalb265ba72017-02-14 15:03:45 -08001833 mUiExecutor.execute(new Runnable() {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001834 public void run() {
1835 Callbacks cb = mModel.getCallback();
1836 if (callbacks == cb && cb != null) {
1837 task.execute(callbacks);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07001838 }
1839 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001840 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001841 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001842
1843 public ModelWriter getModelWriter() {
1844 // Updates from model task, do not deal with icon position in hotseat.
1845 return mModel.getWriter(false /* hasVerticalHotseat */);
1846 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001847 }
1848
Sunny Goyal1b072632017-01-18 11:30:23 -08001849 public void updateAndBindShortcutInfo(final ShortcutInfo si, final ShortcutInfoCompat info) {
1850 updateAndBindShortcutInfo(new Provider<ShortcutInfo>() {
1851 @Override
1852 public ShortcutInfo get() {
1853 si.updateFromDeepShortcutInfo(info, mApp.getContext());
1854 si.iconBitmap = LauncherIcons.createShortcutIcon(info, mApp.getContext());
1855 return si;
1856 }
1857 });
1858 }
1859
Sunny Goyal10923b32016-07-20 15:42:44 -07001860 /**
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001861 * Utility method to update a shortcut on the background thread.
Sunny Goyal10923b32016-07-20 15:42:44 -07001862 */
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001863 public void updateAndBindShortcutInfo(final Provider<ShortcutInfo> shortcutProvider) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001864 enqueueModelUpdateTask(new ExtendedModelTask() {
Sunny Goyal10923b32016-07-20 15:42:44 -07001865 @Override
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001866 public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001867 ShortcutInfo info = shortcutProvider.get();
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001868 ArrayList<ShortcutInfo> update = new ArrayList<>();
Sunny Goyal10923b32016-07-20 15:42:44 -07001869 update.add(info);
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001870 bindUpdatedShortcuts(update, info.user);
Sunny Goyal10923b32016-07-20 15:42:44 -07001871 }
1872 });
1873 }
1874
Sunny Goyald164b7f2016-10-12 20:49:31 -07001875 private void bindWidgetsModel(final Callbacks callbacks) {
1876 final MultiHashMap<PackageItemInfo, WidgetItem> widgets
1877 = mBgWidgetsModel.getWidgetsMap().clone();
Sunny Goyalb265ba72017-02-14 15:03:45 -08001878 mUiExecutor.execute(new Runnable() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001879 @Override
1880 public void run() {
1881 Callbacks cb = getCallback();
1882 if (callbacks == cb && cb != null) {
Sunny Goyald164b7f2016-10-12 20:49:31 -07001883 callbacks.bindAllWidgets(widgets);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001884 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07001885 }
1886 });
1887 }
1888
Tony Wickham86222d22017-03-29 15:30:43 -07001889 public void refreshAndBindWidgetsAndShortcuts(final Callbacks callbacks,
1890 final boolean bindFirst, @Nullable final PackageUserKey packageUser) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001891 runOnWorkerThread(new Runnable() {
1892 @Override
1893 public void run() {
1894 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
Sunny Goyald164b7f2016-10-12 20:49:31 -07001895 bindWidgetsModel(callbacks);
Sunny Goyal31860582015-09-18 08:38:57 -07001896 }
Tony Wickham86222d22017-03-29 15:30:43 -07001897 ArrayList<WidgetItem> widgets = mBgWidgetsModel.update(
1898 mApp.getContext(), packageUser);
Sunny Goyald164b7f2016-10-12 20:49:31 -07001899 bindWidgetsModel(callbacks);
1900
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001901 // update the Widget entries inside DB on the worker thread.
Tony Wickham86222d22017-03-29 15:30:43 -07001902 mApp.getWidgetCache().removeObsoletePreviews(widgets, packageUser);
Sunny Goyal31860582015-09-18 08:38:57 -07001903 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001904 });
Michael Jurkac402cd92013-05-20 15:49:32 +02001905 }
1906
Sunny Goyal651077b2014-06-30 14:15:31 -07001907 static boolean isValidProvider(AppWidgetProviderInfo provider) {
1908 return (provider != null) && (provider.provider != null)
1909 && (provider.provider.getPackageName() != null);
1910 }
1911
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001912 public void dumpState(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
1913 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
1914 writer.println(prefix + "All apps list: size=" + mBgAllAppsList.data.size());
1915 for (AppInfo info : mBgAllAppsList.data) {
1916 writer.println(prefix + " title=\"" + info.title + "\" iconBitmap=" + info.iconBitmap
1917 + " componentName=" + info.componentName.getPackageName());
1918 }
Joe Onorato36115782010-06-17 13:28:48 -04001919 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001920 sBgDataModel.dump(prefix, fd, writer, args);
Joe Onoratobe386092009-11-17 17:32:16 -08001921 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08001922
1923 public Callbacks getCallback() {
1924 return mCallbacks != null ? mCallbacks.get() : null;
1925 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07001926
1927 /**
1928 * @return {@link FolderInfo} if its already loaded.
1929 */
1930 public FolderInfo findFolderById(Long folderId) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001931 synchronized (sBgDataModel) {
1932 return sBgDataModel.folders.get(folderId);
Sunny Goyal18bf8e22015-04-08 18:13:46 -07001933 }
1934 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07001935
1936 /**
1937 * @return the looper for the worker thread which can be used to start background tasks.
1938 */
1939 public static Looper getWorkerLooper() {
1940 return sWorkerThread.getLooper();
1941 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001942}