blob: ee06d9e5877b81ae634d3e1c80504597f3747bfe [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 Goyalf8d6ed22017-06-01 14:26:38 -070063import com.android.launcher3.model.LoaderResults;
Sunny Goyal43bf11d2017-02-02 13:52:53 -080064import com.android.launcher3.model.ModelWriter;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070065import com.android.launcher3.model.PackageInstallStateChangedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080066import com.android.launcher3.model.PackageItemInfo;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070067import com.android.launcher3.model.PackageUpdatedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080068import com.android.launcher3.model.SdCardAvailableReceiver;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070069import com.android.launcher3.model.ShortcutsChangedTask;
70import com.android.launcher3.model.UserLockStateChangedTask;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080071import com.android.launcher3.model.WidgetItem;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070072import com.android.launcher3.model.WidgetsModel;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -070073import com.android.launcher3.provider.ImportDataTask;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070074import com.android.launcher3.provider.LauncherDbUtils;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070075import com.android.launcher3.shortcuts.DeepShortcutManager;
76import com.android.launcher3.shortcuts.ShortcutInfoCompat;
77import com.android.launcher3.shortcuts.ShortcutKey;
Robin Lee26ace122015-03-16 19:41:43 +000078import com.android.launcher3.util.ComponentKey;
Sunny Goyalb265ba72017-02-14 15:03:45 -080079import com.android.launcher3.util.LooperIdleLock;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070080import com.android.launcher3.util.ManagedProfileHeuristic;
Tony Wickhamd82a39d2016-07-01 15:44:13 -070081import com.android.launcher3.util.MultiHashMap;
Sunny Goyald09c3702016-04-06 16:18:20 -070082import com.android.launcher3.util.PackageManagerHelper;
Tony Wickham86222d22017-03-29 15:30:43 -070083import com.android.launcher3.util.PackageUserKey;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070084import com.android.launcher3.util.Preconditions;
Sunny Goyal2bcbe132016-11-16 09:23:42 -080085import com.android.launcher3.util.Provider;
Adam Cohen091440a2015-03-18 14:16:05 -070086import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070087import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080088
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -080089import java.io.FileDescriptor;
90import java.io.PrintWriter;
Michael Jurkac2f801e2011-07-12 14:19:46 -070091import java.lang.ref.WeakReference;
Michael Jurkac2f801e2011-07-12 14:19:46 -070092import java.util.ArrayList;
93import java.util.Collections;
Michael Jurkac2f801e2011-07-12 14:19:46 -070094import 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;
Sunny Goyaldd96a5e2017-02-17 11:22:34 -080099import java.util.concurrent.CancellationException;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700100import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -0700101
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102/**
103 * Maintains in-memory state of the Launcher. It is expected that there should be only one
104 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700105 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106 */
Kenny Guyed131872014-04-30 03:02:21 +0100107public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +0100108 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800109 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -0400110 private static final boolean DEBUG_RECEIVER = false;
Chris Wrenb358f812014-04-16 13:37:00 -0400111
Joe Onorato9c1289c2009-08-17 11:03:03 -0400112 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700113
Sunny Goyalb265ba72017-02-14 15:03:45 -0800114 private final MainThreadExecutor mUiExecutor = new MainThreadExecutor();
Adam Cohen091440a2015-03-18 14:16:05 -0700115 @Thunk final LauncherAppState mApp;
116 @Thunk final Object mLock = new Object();
Adam Cohen091440a2015-03-18 14:16:05 -0700117 @Thunk LoaderTask mLoaderTask;
118 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700119 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120
Adam Cohen091440a2015-03-18 14:16:05 -0700121 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700122 static {
123 sWorkerThread.start();
124 }
Adam Cohen091440a2015-03-18 14:16:05 -0700125 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700126
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800127 // Indicates whether the current model data is valid or not.
128 // We start off with everything not loaded. After that, we assume that
Joe Onoratocc67f472010-06-08 10:54:30 -0700129 // our monitoring of the package manager provides all updates and we never
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800130 // need to do a requery. This is only ever touched from the loader thread.
131 private boolean mModelLoaded;
Hyunyoung Song6aa37292017-02-06 10:46:24 -0800132 public boolean isModelLoaded() {
133 synchronized (mLock) {
134 return mModelLoaded && mLoaderTask == null;
135 }
136 }
Joe Onoratocc67f472010-06-08 10:54:30 -0700137
Adam Cohen091440a2015-03-18 14:16:05 -0700138 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700140 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800141 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700142 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800143 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800144
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700145 private boolean mHasShortcutHostPermission;
146 // Runnable to check if the shortcuts permission has changed.
147 private final Runnable mShortcutPermissionCheckRunnable = new Runnable() {
148 @Override
149 public void run() {
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800150 if (mModelLoaded) {
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530151 boolean hasShortcutHostPermission =
152 DeepShortcutManager.getInstance(mApp.getContext()).hasHostPermission();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700153 if (hasShortcutHostPermission != mHasShortcutHostPermission) {
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800154 forceReload();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700155 }
156 }
157 }
158 };
159
Sunny Goyale9956a72016-09-01 17:24:47 -0700160 /**
161 * All the static data should be accessed on the background thread, A lock should be acquired
162 * on this object when accessing any data from this model.
163 */
164 static final BgDataModel sBgDataModel = new BgDataModel();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700165
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700166 // </ only access in worker thread >
167
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700168 private final IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700170 private final LauncherAppsCompat mLauncherApps;
171 private final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100172
Joe Onorato9c1289c2009-08-17 11:03:03 -0400173 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700174 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400175 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700176 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400177 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700178 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
179 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700180 public void bindScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700181 public void finishFirstPageBind(ViewOnDrawExecutor executor);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800182 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400183 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200184 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700185 public void bindAppsAdded(ArrayList<Long> newScreens,
186 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700187 ArrayList<ItemInfo> addAnimated,
188 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200189 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700190 public void bindPromiseAppProgressUpdated(PromiseAppInfo app);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700191 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800192 ArrayList<ShortcutInfo> removed, UserHandle user);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700193 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700194 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700195 public void bindWorkspaceComponentsRemoved(
196 HashSet<String> packageNames, HashSet<ComponentName> components,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800197 UserHandle user);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700198 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800199 public void notifyWidgetProvidersChanged();
Sunny Goyald164b7f2016-10-12 20:49:31 -0700200 public void bindAllWidgets(MultiHashMap<PackageItemInfo, WidgetItem> widgets);
Adam Cohen1462de32012-07-24 22:34:36 -0700201 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700202 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700203 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMap);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400204 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530206 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800207 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400208 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100209 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Sunny Goyald164b7f2016-10-12 20:49:31 -0700210 mBgWidgetsModel = new WidgetsModel(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800211 mIconCache = iconCache;
212
Kenny Guyed131872014-04-30 03:02:21 +0100213 mLauncherApps = LauncherAppsCompat.getInstance(context);
214 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800215 }
216
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700217 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
218 * posted on the worker thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700219 private static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700220 if (sWorkerThread.getThreadId() == Process.myTid()) {
221 r.run();
222 } else {
223 // If we are not on the worker thread, then post to the worker handler
224 sWorker.post(r);
225 }
226 }
227
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700228 public void setPackageState(PackageInstallInfo installInfo) {
229 enqueueModelUpdateTask(new PackageInstallStateChangedTask(installInfo));
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500230 }
231
Sunny Goyal756adbc2015-04-16 15:20:51 -0700232 /**
233 * Updates the icons and label of all pending icons for the provided package name.
234 */
235 public void updateSessionDisplayInfo(final String packageName) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700236 HashSet<String> packages = new HashSet<>();
237 packages.add(packageName);
238 enqueueModelUpdateTask(new CacheDataUpdatedTask(
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800239 CacheDataUpdatedTask.OP_SESSION_UPDATE, Process.myUserHandle(), packages));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800240 }
241
242 /**
243 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800244 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700245 public void addAndBindAddedWorkspaceItems(
Sunny Goyala474a9b2017-05-04 16:47:11 -0700246 Provider<List<Pair<ItemInfo, Object>>> appsProvider) {
Sunny Goyal2bcbe132016-11-16 09:23:42 -0800247 enqueueModelUpdateTask(new AddWorkspaceItemsTask(appsProvider));
Winson Chung64359a52013-07-08 17:17:08 -0700248 }
249
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800250 public ModelWriter getWriter(boolean hasVerticalHotseat) {
251 return new ModelWriter(mApp.getContext(), sBgDataModel, hasVerticalHotseat);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800252 }
253
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700254 static void checkItemInfoLocked(
255 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
Sunny Goyale9956a72016-09-01 17:24:47 -0700256 ItemInfo modelItem = sBgDataModel.itemsIdMap.get(itemId);
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700257 if (modelItem != null && item != modelItem) {
258 // check all the data is consistent
259 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
260 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
261 ShortcutInfo shortcut = (ShortcutInfo) item;
262 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
263 modelShortcut.intent.filterEquals(shortcut.intent) &&
264 modelShortcut.id == shortcut.id &&
265 modelShortcut.itemType == shortcut.itemType &&
266 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700267 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700268 modelShortcut.cellX == shortcut.cellX &&
269 modelShortcut.cellY == shortcut.cellY &&
270 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700271 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700272 // For all intents and purposes, this is the same object
273 return;
274 }
275 }
276
277 // the modelItem needs to match up perfectly with item if our model is
278 // to be consistent with the database-- for now, just require
279 // modelItem == item or the equality check above
280 String msg = "item: " + ((item != null) ? item.toString() : "null") +
281 "modelItem: " +
282 ((modelItem != null) ? modelItem.toString() : "null") +
283 "Error: ItemInfo passed to checkItemInfo doesn't match original";
284 RuntimeException e = new RuntimeException(msg);
285 if (stackTrace != null) {
286 e.setStackTrace(stackTrace);
287 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800288 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700289 }
290 }
291
Michael Jurka816474f2012-06-25 14:49:02 -0700292 static void checkItemInfo(final ItemInfo item) {
293 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
294 final long itemId = item.id;
295 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700296 public void run() {
Sunny Goyale9956a72016-09-01 17:24:47 -0700297 synchronized (sBgDataModel) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700298 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700299 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700300 }
301 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700302 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700303 }
304
Joe Onorato9c1289c2009-08-17 11:03:03 -0400305 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700306 * Update the order of the workspace screens in the database. The array list contains
307 * a list of screen ids in the order that they should appear.
308 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700309 public static void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -0700310 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700311 final ContentResolver cr = context.getContentResolver();
312 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
313
314 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700315 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700316 while (iter.hasNext()) {
317 long id = iter.next();
318 if (id < 0) {
319 iter.remove();
320 }
321 }
322
323 Runnable r = new Runnable() {
324 @Override
325 public void run() {
Yura085c8532014-02-11 15:15:29 +0000326 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700327 // Clear the table
Yura085c8532014-02-11 15:15:29 +0000328 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -0700329 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700330 for (int i = 0; i < count; i++) {
331 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700332 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700333 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
334 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +0000335 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -0700336 }
Yura085c8532014-02-11 15:15:29 +0000337
338 try {
339 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
340 } catch (Exception ex) {
341 throw new RuntimeException(ex);
342 }
Winson Chung9e6a0a22013-08-27 11:58:12 -0700343
Sunny Goyale9956a72016-09-01 17:24:47 -0700344 synchronized (sBgDataModel) {
345 sBgDataModel.workspaceScreens.clear();
346 sBgDataModel.workspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -0700347 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700348 }
349 };
350 runOnWorkerThread(r);
351 }
352
353 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400354 * Set this as the current Launcher activity object for the loader.
355 */
356 public void initialize(Callbacks callbacks) {
357 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -0700358 Preconditions.assertUIThread();
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -0700359 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400360 }
361 }
362
Kenny Guyed131872014-04-30 03:02:21 +0100363 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800364 public void onPackageChanged(String packageName, UserHandle user) {
Kenny Guyed131872014-04-30 03:02:21 +0100365 int op = PackageUpdatedTask.OP_UPDATE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700366 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100367 }
368
369 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800370 public void onPackageRemoved(String packageName, UserHandle user) {
Sunny Goyalc2936bc2016-09-01 15:50:36 -0700371 onPackagesRemoved(user, packageName);
372 }
373
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800374 public void onPackagesRemoved(UserHandle user, String... packages) {
Kenny Guyed131872014-04-30 03:02:21 +0100375 int op = PackageUpdatedTask.OP_REMOVE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700376 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packages));
Kenny Guyed131872014-04-30 03:02:21 +0100377 }
378
379 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800380 public void onPackageAdded(String packageName, UserHandle user) {
Kenny Guyed131872014-04-30 03:02:21 +0100381 int op = PackageUpdatedTask.OP_ADD;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700382 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100383 }
384
385 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800386 public void onPackagesAvailable(String[] packageNames, UserHandle user,
Kenny Guyed131872014-04-30 03:02:21 +0100387 boolean replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700388 enqueueModelUpdateTask(
389 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100390 }
391
392 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800393 public void onPackagesUnavailable(String[] packageNames, UserHandle user,
Kenny Guyed131872014-04-30 03:02:21 +0100394 boolean replacing) {
395 if (!replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700396 enqueueModelUpdateTask(new PackageUpdatedTask(
397 PackageUpdatedTask.OP_UNAVAILABLE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100398 }
Kenny Guyed131872014-04-30 03:02:21 +0100399 }
400
Kenny Guy44cba692016-01-21 19:50:02 +0000401 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800402 public void onPackagesSuspended(String[] packageNames, UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700403 enqueueModelUpdateTask(new PackageUpdatedTask(
404 PackageUpdatedTask.OP_SUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000405 }
406
407 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800408 public void onPackagesUnsuspended(String[] packageNames, UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700409 enqueueModelUpdateTask(new PackageUpdatedTask(
410 PackageUpdatedTask.OP_UNSUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000411 }
412
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700413 @Override
414 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800415 UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700416 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, true));
Sunny Goyal50941fb2016-08-04 12:03:52 -0700417 }
418
419 public void updatePinnedShortcuts(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800420 UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700421 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, false));
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700422 }
423
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700424 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400425 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
426 * ACTION_PACKAGE_CHANGED.
427 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100428 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400429 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -0400430 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700431
Joe Onorato36115782010-06-17 13:28:48 -0400432 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +0100433 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -0700434 // If we have changed locale we need to clear out the labels in all apps/workspace.
435 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700436 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
437 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -0700438 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -0700439 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700440 } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
441 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
442 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800443 UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER);
Sunny Goyalda891c12016-03-18 18:29:24 -0700444 if (user != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700445 if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
446 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700447 enqueueModelUpdateTask(new PackageUpdatedTask(
448 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE, user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700449 }
450
451 // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so
452 // we need to run the state change task again.
453 if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
454 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700455 enqueueModelUpdateTask(new UserLockStateChangedTask(user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700456 }
Sunny Goyalda891c12016-03-18 18:29:24 -0700457 }
Tony Wickham827cef22016-03-17 15:39:39 -0700458 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
459 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700460 }
461 }
462
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800463 /**
464 * Reloads the workspace items from the DB and re-binds the workspace. This should generally
465 * not be called as DB updates are automatically followed by UI update
466 */
467 public void forceReload() {
468 synchronized (mLock) {
469 // Stop any existing loaders first, so they don't set mModelLoaded to true later
470 stopLoaderLocked();
471 mModelLoaded = false;
472 }
Winson Chungf0c6ae02012-03-21 16:10:31 -0700473
Reena Lee93f824a2011-09-23 17:20:28 -0700474 // Do this here because if the launcher activity is running it will be restarted.
475 // If it's not running startLoaderFromBackground will merely tell it that it needs
476 // to reload.
477 startLoaderFromBackground();
478 }
479
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700480 /**
481 * When the launcher is in the background, it's possible for it to miss paired
482 * configuration changes. So whenever we trigger the loader from the background
483 * tell the launcher that it needs to re-run the loader when it comes back instead
484 * of doing it now.
485 */
486 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800487 Callbacks callbacks = getCallback();
488 if (callbacks != null) {
489 // Only actually run the loader if they're not paused.
490 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -0700491 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700492 }
493 }
Joe Onorato36115782010-06-17 13:28:48 -0400494 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400495
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700496 /**
497 * If there is already a loader task running, tell it to stop.
498 */
499 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -0700500 LoaderTask oldTask = mLoaderTask;
501 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -0700502 oldTask.stopLocked();
503 }
Reena Lee93f824a2011-09-23 17:20:28 -0700504 }
505
Adam Cohen1a85c582014-09-30 09:48:49 -0700506 public boolean isCurrentCallbacks(Callbacks callbacks) {
507 return (mCallbacks != null && mCallbacks.get() == callbacks);
508 }
509
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700510 /**
511 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
512 * @return true if the page could be bound synchronously.
513 */
514 public boolean startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700515 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
Sunny Goyala474a9b2017-05-04 16:47:11 -0700516 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_LOADER_RUNNING);
Joe Onorato36115782010-06-17 13:28:48 -0400517 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -0400518 // Don't bother to start the thread if we know it's not going to do anything
519 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -0700520 final Callbacks oldCallbacks = mCallbacks.get();
521 // Clear any pending bind-runnables from the synchronized load process.
Sunny Goyalb265ba72017-02-14 15:03:45 -0800522 mUiExecutor.execute(new Runnable() {
523 public void run() {
524 oldCallbacks.clearPendingBinds();
525 }
526 });
Sunny Goyal527c7d32015-08-28 15:19:36 -0700527
Joe Onorato36115782010-06-17 13:28:48 -0400528 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700529 stopLoaderLocked();
Sunny Goyalf8d6ed22017-06-01 14:26:38 -0700530 LoaderResults loaderResults = new LoaderResults(mApp, sBgDataModel,
531 mBgAllAppsList, synchronousBindPage, mCallbacks);
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800532 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
533 && mModelLoaded && !mIsLoaderTaskRunning) {
Sunny Goyalf8d6ed22017-06-01 14:26:38 -0700534
535 // Divide the set of loaded items into those that we are binding synchronously,
536 // and everything else that is to be bound normally (asynchronously).
537 loaderResults.bindWorkspace();
538 // For now, continue posting the binding of AllApps as there are other
539 // issues that arise from that.
540 loaderResults.bindAllApps();
541 loaderResults.bindDeepShortcuts();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700542 return true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700543 } else {
Sunny Goyalf8d6ed22017-06-01 14:26:38 -0700544 mLoaderTask = new LoaderTask(mApp.getContext(), loaderResults);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700545 sWorker.post(mLoaderTask);
546 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400547 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400548 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700549 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400550 }
551
Joe Onorato36115782010-06-17 13:28:48 -0400552 public void stopLoader() {
553 synchronized (mLock) {
554 if (mLoaderTask != null) {
555 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400556 }
557 }
Joe Onorato36115782010-06-17 13:28:48 -0400558 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400559
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800560 /**
561 * Loads the workspace screen ids in an ordered list.
562 */
Sunny Goyale5bb7052015-07-27 14:36:07 -0700563 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -0700564 final ContentResolver contentResolver = context.getContentResolver();
565 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -0700566
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800567 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -0700568 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
569 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -0700570 }
571
Mario Bertschler817afa32017-03-15 11:56:47 -0700572 public void onInstallSessionCreated(final PackageInstallInfo sessionInfo) {
573 enqueueModelUpdateTask(new ExtendedModelTask() {
574 @Override
575 public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
576 apps.addPromiseApp(app.getContext(), sessionInfo);
577 if (!apps.added.isEmpty()) {
578 final ArrayList<AppInfo> arrayList = new ArrayList<>(apps.added);
579 apps.added.clear();
580 scheduleCallbackTask(new CallbackTask() {
581 @Override
582 public void execute(Callbacks callbacks) {
583 callbacks.bindAppsAdded(null, null, null, arrayList);
584 }
585 });
586 }
587 }
588 });
589 }
590
Joe Onorato36115782010-06-17 13:28:48 -0400591 /**
592 * Runnable for the thread that loads the contents of the launcher:
593 * - workspace icons
594 * - widgets
595 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700596 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -0400597 */
598 private class LoaderTask implements Runnable {
599 private Context mContext;
Sunny Goyalf8d6ed22017-06-01 14:26:38 -0700600 private final LoaderResults mResults;
Sunny Goyal93f878c2016-03-30 17:31:24 -0700601
Joe Onorato36115782010-06-17 13:28:48 -0400602 private boolean mStopped;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700603
Sunny Goyalf8d6ed22017-06-01 14:26:38 -0700604 LoaderTask(Context context, LoaderResults results) {
Joe Onorato36115782010-06-17 13:28:48 -0400605 mContext = context;
Sunny Goyalf8d6ed22017-06-01 14:26:38 -0700606 mResults = results;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400607 }
608
Joe Onorato36115782010-06-17 13:28:48 -0400609 private void waitForIdle() {
610 // Wait until the either we're stopped or the other threads are done.
611 // This way we don't start loading all apps until the workspace has settled
612 // down.
613 synchronized (LoaderTask.this) {
Sunny Goyalb265ba72017-02-14 15:03:45 -0800614 LooperIdleLock idleLock = new LooperIdleLock(this, Looper.getMainLooper());
615 // Just in case mFlushingWorkerThread changes but we aren't woken up,
616 // wait no longer than 1sec at a time
617 while (!mStopped && idleLock.awaitLocked(1000));
Daniel Sandler843e8602010-06-07 14:59:01 -0400618 }
Joe Onorato36115782010-06-17 13:28:48 -0400619 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400620
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800621 private void verifyNotStopped() throws CancellationException {
622 synchronized (LoaderTask.this) {
623 if (mStopped) {
624 throw new CancellationException("Loader stopped");
625 }
626 }
627 }
628
Joe Onorato36115782010-06-17 13:28:48 -0400629 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700630 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -0700631 if (mStopped) {
632 return;
633 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700634 mIsLoaderTaskRunning = true;
635 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400636
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800637 try {
Sunny Goyalb265ba72017-02-14 15:03:45 -0800638 long now = 0;
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800639 if (DEBUG_LOADERS) Log.d(TAG, "step 1.1: loading workspace");
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800640 loadWorkspace();
Joe Onorato36115782010-06-17 13:28:48 -0400641
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800642 verifyNotStopped();
643 if (DEBUG_LOADERS) Log.d(TAG, "step 1.2: bind workspace workspace");
Sunny Goyalf8d6ed22017-06-01 14:26:38 -0700644 mResults.bindWorkspace();
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800645
646 // Take a break
Sunny Goyalb265ba72017-02-14 15:03:45 -0800647 if (DEBUG_LOADERS) {
648 Log.d(TAG, "step 1 completed, wait for idle");
649 now = SystemClock.uptimeMillis();
650 }
Joe Onorato36115782010-06-17 13:28:48 -0400651 waitForIdle();
Sunny Goyalb265ba72017-02-14 15:03:45 -0800652 if (DEBUG_LOADERS) Log.d(TAG, "Waited " + (SystemClock.uptimeMillis() - now) + "ms");
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800653 verifyNotStopped();
Daniel Sandler843e8602010-06-07 14:59:01 -0400654
655 // second step
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800656 if (DEBUG_LOADERS) Log.d(TAG, "step 2.1: loading all apps");
657 loadAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700658
Sunny Goyalf8d6ed22017-06-01 14:26:38 -0700659 if (DEBUG_LOADERS) Log.d(TAG, "step 2.2: Binding all apps");
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800660 verifyNotStopped();
Sunny Goyalf8d6ed22017-06-01 14:26:38 -0700661 mResults.bindAllApps();
662
663 verifyNotStopped();
664 if (DEBUG_LOADERS) Log.d(TAG, "step 2.3: Update icon cache");
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800665 updateIconCache();
666
667 // Take a break
Sunny Goyalb265ba72017-02-14 15:03:45 -0800668 if (DEBUG_LOADERS) {
669 Log.d(TAG, "step 2 completed, wait for idle");
670 now = SystemClock.uptimeMillis();
671 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700672 waitForIdle();
Sunny Goyalb265ba72017-02-14 15:03:45 -0800673 if (DEBUG_LOADERS) Log.d(TAG, "Waited " + (SystemClock.uptimeMillis() - now) + "ms");
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800674 verifyNotStopped();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700675
676 // third step
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800677 if (DEBUG_LOADERS) Log.d(TAG, "step 3.1: loading deep shortcuts");
678 loadDeepShortcuts();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400679
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800680 verifyNotStopped();
681 if (DEBUG_LOADERS) Log.d(TAG, "step 3.2: bind deep shortcuts");
Sunny Goyalf8d6ed22017-06-01 14:26:38 -0700682 mResults.bindDeepShortcuts();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400683
Tony Wickhama7e1c1c2017-04-13 16:24:38 -0700684 // Take a break
685 if (DEBUG_LOADERS) Log.d(TAG, "step 3 completed, wait for idle");
686 waitForIdle();
687 verifyNotStopped();
688
689 // fourth step
690 if (DEBUG_LOADERS) Log.d(TAG, "step 4.1: loading widgets");
691 refreshAndBindWidgetsAndShortcuts(getCallback(), false /* bindFirst */,
692 null /* packageUser */);
693
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800694 synchronized (mLock) {
695 // Everything loaded bind the data.
696 mModelLoaded = true;
697 mHasLoaderCompletedOnce = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400698 }
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800699 } catch (CancellationException e) {
700 // Loader stopped, ignore
701 } finally {
702 // Clear out this reference, otherwise we end up holding it until all of the
703 // callback runnables are done.
704 mContext = null;
705
706 synchronized (mLock) {
707 // If we are still the last one to be scheduled, remove ourselves.
708 if (mLoaderTask == this) {
709 mLoaderTask = null;
710 }
711 mIsLoaderTaskRunning = false;
712 }
Joe Onorato36115782010-06-17 13:28:48 -0400713 }
Joe Onorato36115782010-06-17 13:28:48 -0400714 }
715
716 public void stopLocked() {
717 synchronized (LoaderTask.this) {
718 mStopped = true;
719 this.notify();
720 }
721 }
722
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800723 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -0700724 if (LauncherAppState.PROFILE_STARTUP) {
725 Trace.beginSection("Loading Workspace");
726 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400727
Joe Onorato36115782010-06-17 13:28:48 -0400728 final Context context = mContext;
729 final ContentResolver contentResolver = context.getContentResolver();
Sunny Goyal342e4662017-02-02 15:21:08 -0800730 final PackageManagerHelper pmHelper = new PackageManagerHelper(context);
731 final boolean isSafeMode = pmHelper.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700732 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530733 final DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -0700734 final boolean isSdCardReady = Utilities.isBootCompleted();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800735 final MultiHashMap<UserHandle, String> pendingPackages = new MultiHashMap<>();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400736
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700737 boolean clearDb = false;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -0700738 try {
739 ImportDataTask.performImportIfPossible(context);
740 } catch (Exception e) {
741 // Migration failed. Clear workspace.
742 clearDb = true;
743 }
744
745 if (!clearDb && GridSizeMigrationTask.ENABLED &&
Sunny Goyalf076eae2016-01-11 12:25:10 -0800746 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
747 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700748 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -0700749 }
750
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700751 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -0700752 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -0700753 LauncherSettings.Settings.call(contentResolver,
Sunny Goyale05b08f2017-02-23 18:30:22 -0800754 LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -0500755 }
756
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700757 Log.d(TAG, "loadWorkspace: loading default favorites");
758 LauncherSettings.Settings.call(contentResolver,
759 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -0700760
Sunny Goyale9956a72016-09-01 17:24:47 -0700761 synchronized (sBgDataModel) {
762 sBgDataModel.clear();
763
Sunny Goyal756adbc2015-04-16 15:20:51 -0700764 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -0700765 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyale9956a72016-09-01 17:24:47 -0700766 sBgDataModel.workspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -0800767
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700768 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800769 final LoaderCursor c = new LoaderCursor(contentResolver.query(
770 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null), mApp);
Daniel Sandler8802e962010-05-26 16:28:16 -0400771
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800772 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400773
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700774 try {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700775 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
776 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -0400777 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
778 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700779 final int spanXIndex = c.getColumnIndexOrThrow
780 (LauncherSettings.Favorites.SPANX);
781 final int spanYIndex = c.getColumnIndexOrThrow(
782 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800783 final int rankIndex = c.getColumnIndexOrThrow(
784 LauncherSettings.Favorites.RANK);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700785 final int optionsIndex = c.getColumnIndexOrThrow(
786 LauncherSettings.Favorites.OPTIONS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400787
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800788 final LongSparseArray<UserHandle> allUsers = c.allUsers;
Kenny Guyff05f432016-01-22 17:48:29 +0000789 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700790 final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800791 for (UserHandle user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +0000792 long serialNo = mUserManager.getSerialNumberForUser(user);
793 allUsers.put(serialNo, user);
794 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700795
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700796 boolean userUnlocked = mUserManager.isUserUnlocked(user);
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700797
798 // We can only query for shortcuts when the user is unlocked.
799 if (userUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -0700800 List<ShortcutInfoCompat> pinnedShortcuts =
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530801 shortcutManager.queryForPinnedShortcuts(null, user);
802 if (shortcutManager.wasLastCallSuccess()) {
Sunny Goyal49f4f032016-08-01 15:45:49 -0700803 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
804 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
805 shortcut);
806 }
807 } else {
808 // Shortcut manager can fail due to some race condition when the
809 // lock state changes too frequently. For the purpose of the loading
810 // shortcuts, consider the user is still locked.
811 userUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700812 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700813 }
Sunny Goyal49f4f032016-08-01 15:45:49 -0700814 unlockedUsers.put(serialNo, userUnlocked);
Sunny Goyal7f834d22015-04-21 10:10:23 -0700815 }
816
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700817 ShortcutInfo info;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700818 LauncherAppWidgetInfo appWidgetInfo;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700819 Intent intent;
Sunny Goyal81e44912017-01-14 15:05:14 -0800820 String targetPkg;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400821
Jon Miranda529af302017-02-28 14:18:54 -0800822 FolderIconPreviewVerifier verifier =
823 new FolderIconPreviewVerifier(mApp.getInvariantDeviceProfile());
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700824 while (!mStopped && c.moveToNext()) {
825 try {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800826 if (c.user == null) {
827 // User has been deleted, remove the item.
828 c.markDeleted("User has been deleted");
829 continue;
830 }
831
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700832 boolean allowMissingTarget = false;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800833 switch (c.itemType) {
Sunny Goyal81e44912017-01-14 15:05:14 -0800834 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700835 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700836 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Sunny Goyal81e44912017-01-14 15:05:14 -0800837 intent = c.parseIntent();
838 if (intent == null) {
839 c.markDeleted("Invalid or null intent");
840 continue;
841 }
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800842
Sunny Goyal81e44912017-01-14 15:05:14 -0800843 int disabledState = quietMode.get(c.serialNumber) ?
844 ShortcutInfo.FLAG_DISABLED_QUIET_USER : 0;
845 ComponentName cn = intent.getComponent();
846 targetPkg = cn == null ? intent.getPackage() : cn.getPackageName();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700847
Sunny Goyal81e44912017-01-14 15:05:14 -0800848 if (!Process.myUserHandle().equals(c.user)) {
849 if (c.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
850 c.markDeleted("Legacy shortcuts are only allowed for default user");
851 continue;
852 } else if (c.restoreFlag != 0) {
853 // Don't restore items for other profiles.
854 c.markDeleted("Restore from managed profile not supported");
855 continue;
856 }
857 }
858 if (TextUtils.isEmpty(targetPkg) &&
859 c.itemType != LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
860 c.markDeleted("Only legacy shortcuts can have null package");
861 continue;
862 }
Sunny Goyal34942622014-08-29 17:20:55 -0700863
Sunny Goyal81e44912017-01-14 15:05:14 -0800864 // If there is no target package, its an implicit intent
865 // (legacy shortcut) which is always valid
866 boolean validTarget = TextUtils.isEmpty(targetPkg) ||
867 launcherApps.isPackageEnabledForProfile(targetPkg, c.user);
Sunny Goyal94485362014-09-18 16:13:58 -0700868
Sunny Goyal81e44912017-01-14 15:05:14 -0800869 if (cn != null && validTarget) {
870 // If the apk is present and the shortcut points to a specific
871 // component.
872
873 // If the component is already present
874 if (launcherApps.isActivityEnabledForProfile(cn, c.user)) {
875 // no special handling necessary for this item
876 c.markRestored();
877 } else {
Mario Bertschlerc06af332017-03-28 12:23:22 -0700878 if (c.hasRestoreFlag(ShortcutInfo.FLAG_AUTOINSTALL_ICON)) {
Sunny Goyal81e44912017-01-14 15:05:14 -0800879 // We allow auto install apps to have their intent
880 // updated after an install.
Sunny Goyal342e4662017-02-02 15:21:08 -0800881 intent = pmHelper.getAppLaunchIntent(targetPkg, c.user);
Sunny Goyal81e44912017-01-14 15:05:14 -0800882 if (intent != null) {
883 c.restoreFlag = 0;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800884 c.updater().put(
Sunny Goyal81e44912017-01-14 15:05:14 -0800885 LauncherSettings.Favorites.INTENT,
886 intent.toUri(0)).commit();
887 cn = intent.getComponent();
Sunny Goyalb05a00a2016-08-29 10:06:57 -0700888 } else {
Sunny Goyal81e44912017-01-14 15:05:14 -0800889 c.markDeleted("Unable to find a launch target");
Sunny Goyal94485362014-09-18 16:13:58 -0700890 continue;
891 }
Sunny Goyal1a745e82014-10-02 15:58:31 -0700892 } else {
Sunny Goyal81e44912017-01-14 15:05:14 -0800893 // The app is installed but the component is no
894 // longer available.
895 c.markDeleted("Invalid component removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -0700896 continue;
Winson Chungee055712013-07-30 14:46:24 -0700897 }
Winson Chungee055712013-07-30 14:46:24 -0700898 }
Sunny Goyal81e44912017-01-14 15:05:14 -0800899 }
900 // else if cn == null => can't infer much, leave it
901 // else if !validPkg => could be restored icon or missing sd-card
902
903 if (!TextUtils.isEmpty(targetPkg) && !validTarget) {
904 // Points to a valid app (superset of cn != null) but the apk
905 // is not available.
906
907 if (c.restoreFlag != 0) {
908 // Package is not yet available but might be
909 // installed later.
910 FileLog.d(TAG, "package not yet restored: " + targetPkg);
911
912 if (c.hasRestoreFlag(ShortcutInfo.FLAG_RESTORE_STARTED)) {
913 // Restore has started once.
914 } else if (installingPkgs.containsKey(targetPkg)) {
915 // App restore has started. Update the flag
916 c.restoreFlag |= ShortcutInfo.FLAG_RESTORE_STARTED;
917 c.updater().commit();
918 } else {
919 c.markDeleted("Unrestored app removed: " + targetPkg);
920 continue;
921 }
Sunny Goyalad2e91a2017-02-28 15:14:58 -0800922 } else if (pmHelper.isAppOnSdcard(targetPkg, c.user)) {
Sunny Goyal81e44912017-01-14 15:05:14 -0800923 // Package is present but not available.
924 disabledState |= ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
925 // Add the icon on the workspace anyway.
926 allowMissingTarget = true;
927 } else if (!isSdCardReady) {
928 // SdCard is not ready yet. Package might get available,
929 // once it is ready.
930 Log.d(TAG, "Missing pkg, will check later: " + targetPkg);
931 pendingPackages.addToList(c.user, targetPkg);
932 // Add the icon on the workspace anyway.
933 allowMissingTarget = true;
934 } else {
935 // Do not wait for external media load anymore.
936 c.markDeleted("Invalid package removed: " + targetPkg);
937 continue;
938 }
939 }
940
941 if (validTarget) {
942 // The shortcut points to a valid target (either no target
943 // or something which is ready to be used)
944 c.markRestored();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700945 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400946
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800947 boolean useLowResIcon = !c.isOnWorkspaceOrHotseat() &&
Jon Miranda529af302017-02-28 14:18:54 -0800948 !verifier.isItemInPreview(c.getInt(rankIndex));
Sunny Goyal34b65272015-03-11 16:56:52 -0700949
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -0800950 if (c.restoreFlag != 0) {
Sunny Goyal81e44912017-01-14 15:05:14 -0800951 // Already verified above that user is same as default user
952 info = c.getRestoredItemInfo(intent);
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800953 } else if (c.itemType ==
Chris Wrenf4d08112014-01-16 18:13:56 -0500954 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800955 info = c.getAppShortcutInfo(
956 intent, allowMissingTarget, useLowResIcon);
957 } else if (c.itemType ==
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700958 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700959
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800960 ShortcutKey key = ShortcutKey.fromIntent(intent, c.user);
961 if (unlockedUsers.get(c.serialNumber)) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700962 ShortcutInfoCompat pinnedShortcut =
963 shortcutKeyToPinnedShortcuts.get(key);
964 if (pinnedShortcut == null) {
965 // The shortcut is no longer valid.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800966 c.markDeleted("Pinned shortcut not found");
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700967 continue;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700968 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700969 info = new ShortcutInfo(pinnedShortcut, context);
Sunny Goyal1b072632017-01-18 11:30:23 -0800970 info.iconBitmap = LauncherIcons
971 .createShortcutIcon(pinnedShortcut, context);
Charles He3ff90472017-02-15 12:22:56 +0000972 if (pmHelper.isAppSuspended(
Sunny Goyalad2e91a2017-02-28 15:14:58 -0800973 pinnedShortcut.getPackage(), info.user)) {
Charles He3ff90472017-02-15 12:22:56 +0000974 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
975 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700976 intent = info.intent;
977 } else {
978 // Create a shortcut info in disabled mode for now.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800979 info = c.loadSimpleShortcut();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700980 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700981 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700982 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800983 info = c.loadSimpleShortcut();
Michael Jurka96879562012-03-22 05:54:33 -0700984
Sunny Goyald09c3702016-04-06 16:18:20 -0700985 // Shortcuts are only available on the primary profile
Tony Wickham431a7132017-05-04 15:39:43 -0700986 if (!TextUtils.isEmpty(targetPkg)
987 && pmHelper.isAppSuspended(targetPkg, c.user)) {
Sunny Goyald09c3702016-04-06 16:18:20 -0700988 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
989 }
990
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700991 // App shortcuts that used to be automatically added to Launcher
992 // didn't always have the correct intent flags set, so do that
993 // here
994 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -0700995 intent.getCategories() != null &&
996 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -0700997 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700998 intent.addFlags(
999 Intent.FLAG_ACTIVITY_NEW_TASK |
1000 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1001 }
Michael Jurka96879562012-03-22 05:54:33 -07001002 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001003
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001004 if (info != null) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001005 c.applyCommonProperties(info);
1006
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001007 info.intent = intent;
Sunny Goyal08f72612015-01-05 13:41:43 -08001008 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001009 info.spanX = 1;
1010 info.spanY = 1;
Sunny Goyald09c3702016-04-06 16:18:20 -07001011 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001012 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1013 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1014 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001015
Sunny Goyal81e44912017-01-14 15:05:14 -08001016 if (c.restoreFlag != 0 && !TextUtils.isEmpty(targetPkg)) {
1017 Integer progress = installingPkgs.get(targetPkg);
1018 if (progress != null) {
1019 info.setInstallProgress(progress);
1020 } else {
1021 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001022 }
1023 }
1024
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001025 c.checkAndAddItem(info, sBgDataModel);
Winson Chung1323b482013-08-05 12:41:55 -07001026 } else {
1027 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001028 }
1029 break;
1030
1031 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001032 FolderInfo folderInfo = sBgDataModel.findOrMakeFolder(c.id);
1033 c.applyCommonProperties(folderInfo);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001034
Sunny Goyala508e4f2015-05-21 09:33:57 -07001035 // Do not trim the folder label, as is was set by the user.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001036 folderInfo.title = c.getString(c.titleIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001037 folderInfo.spanX = 1;
1038 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001039 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001040
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001041 // no special handling required for restored folders
1042 c.markRestored();
Chris Wrenf4d08112014-01-16 18:13:56 -05001043
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001044 c.checkAndAddItem(folderInfo, sBgDataModel);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001045 break;
1046
1047 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08001048 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001049 // Read all Launcher-specific widget details
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001050 boolean customWidget = c.itemType ==
Adam Cohen59400422014-03-05 18:07:04 -08001051 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
1052
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001053 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001054 String savedProvider = c.getString(appWidgetProviderIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001055
Sunny Goyalff572272014-07-23 13:58:07 -07001056 final ComponentName component =
1057 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04001058
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001059 final boolean isIdValid = !c.hasRestoreFlag(
1060 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID);
1061 final boolean wasProviderReady = !c.hasRestoreFlag(
1062 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY);
Sunny Goyal651077b2014-06-30 14:15:31 -07001063
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001064 if (widgetProvidersMap == null) {
1065 widgetProvidersMap = AppWidgetManagerCompat
1066 .getInstance(mContext).getAllProvidersMap();
1067 }
1068 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
1069 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00001070 ComponentName.unflattenFromString(savedProvider),
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001071 c.user));
Sunny Goyalff572272014-07-23 13:58:07 -07001072
1073 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08001074 if (!isSafeMode && !customWidget &&
1075 wasProviderReady && !isProviderReady) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001076 c.markDeleted(
1077 "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07001078 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001079 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07001080 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07001081 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1082 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08001083
Sunny Goyal53f96722015-07-13 19:54:53 -07001084 // The provider is available. So the widget is either
1085 // available or not available. We do not need to track
1086 // any future restore updates.
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001087 int status = c.restoreFlag &
Sunny Goyal53f96722015-07-13 19:54:53 -07001088 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07001089 if (!wasProviderReady) {
1090 // If provider was not previously ready, update the
1091 // status and UI flag.
1092
1093 // Id would be valid only if the widget restore broadcast was received.
1094 if (isIdValid) {
Sunny Goyal86df1382016-08-10 15:03:22 -07001095 status |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07001096 } else {
1097 status &= ~LauncherAppWidgetInfo
1098 .FLAG_PROVIDER_NOT_READY;
1099 }
1100 }
1101 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07001102 } else {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001103 Log.v(TAG, "Widget restore pending id=" + c.id
Sunny Goyal651077b2014-06-30 14:15:31 -07001104 + " appWidgetId=" + appWidgetId
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001105 + " status =" + c.restoreFlag);
Sunny Goyal651077b2014-06-30 14:15:31 -07001106 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07001107 component);
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001108 appWidgetInfo.restoreStatus = c.restoreFlag;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001109 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07001110
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001111 if (c.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_RESTORE_STARTED)) {
Sunny Goyal94485362014-09-18 16:13:58 -07001112 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001113 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07001114 // App restore has started. Update the flag
1115 appWidgetInfo.restoreStatus |=
1116 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001117 } else if (!isSafeMode) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001118 c.markDeleted("Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07001119 continue;
1120 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07001121
1122 appWidgetInfo.installProgress =
1123 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07001124 }
Sunny Goyal86df1382016-08-10 15:03:22 -07001125 if (appWidgetInfo.hasRestoreFlag(
1126 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG)) {
Sunny Goyal81e44912017-01-14 15:05:14 -08001127 appWidgetInfo.bindOptions = c.parseIntent();
Sunny Goyal86df1382016-08-10 15:03:22 -07001128 }
Sunny Goyalff572272014-07-23 13:58:07 -07001129
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001130 c.applyCommonProperties(appWidgetInfo);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001131 appWidgetInfo.spanX = c.getInt(spanXIndex);
1132 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001133 appWidgetInfo.user = c.user;
Joe Onorato36115782010-06-17 13:28:48 -04001134
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001135 if (!c.isOnWorkspaceOrHotseat()) {
1136 c.markDeleted("Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001137 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001138 continue;
1139 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001140
Adam Cohen59400422014-03-05 18:07:04 -08001141 if (!customWidget) {
1142 String providerName =
1143 appWidgetInfo.providerName.flattenToString();
1144 if (!providerName.equals(savedProvider) ||
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001145 (appWidgetInfo.restoreStatus != c.restoreFlag)) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001146 c.updater()
1147 .put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1148 providerName)
1149 .put(LauncherSettings.Favorites.RESTORED,
1150 appWidgetInfo.restoreStatus)
1151 .commit();
Adam Cohen59400422014-03-05 18:07:04 -08001152 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001153 }
Jon Miranda2b823f42017-05-02 18:36:18 -07001154
1155 if (appWidgetInfo.restoreStatus !=
1156 LauncherAppWidgetInfo.RESTORE_COMPLETED) {
1157 String pkg = appWidgetInfo.providerName.getPackageName();
1158 appWidgetInfo.pendingItemInfo = new PackageItemInfo(pkg);
1159 appWidgetInfo.pendingItemInfo.user = appWidgetInfo.user;
1160 mIconCache.getTitleAndIconForApp(
1161 appWidgetInfo.pendingItemInfo, false);
1162 }
1163
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001164 c.checkAndAddItem(appWidgetInfo, sBgDataModel);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001165 }
Joe Onorato36115782010-06-17 13:28:48 -04001166 break;
1167 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001168 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001169 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001170 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001171 }
1172 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001173 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001174 }
1175
Winson Chungba9c37f2013-08-30 14:11:37 -07001176 // Break early if we've stopped loading
1177 if (mStopped) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001178 sBgDataModel.clear();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001179 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07001180 }
1181
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001182 // Remove dead items
1183 if (c.commitDeleted()) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001184 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07001185 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
1186 .call(contentResolver,
1187 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
1188 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
1189 for (long folderId : deletedFolderIds) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001190 sBgDataModel.workspaceItems.remove(sBgDataModel.folders.get(folderId));
1191 sBgDataModel.folders.remove(folderId);
1192 sBgDataModel.itemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001193 }
Sunny Goyal55fddc82017-04-06 15:02:52 -07001194
1195 // Remove any ghost widgets
1196 LauncherSettings.Settings.call(contentResolver,
1197 LauncherSettings.Settings.METHOD_REMOVE_GHOST_WIDGETS);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001198 }
1199
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001200 // Unpin shortcuts that don't exist on the workspace.
Sunny Goyalf75baa92016-11-22 03:23:51 +05301201 HashSet<ShortcutKey> pendingShortcuts =
1202 InstallShortcutReceiver.getPendingShortcuts(context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001203 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001204 MutableInt numTimesPinned = sBgDataModel.pinnedShortcutCounts.get(key);
Sunny Goyalf75baa92016-11-22 03:23:51 +05301205 if ((numTimesPinned == null || numTimesPinned.value == 0)
1206 && !pendingShortcuts.contains(key)) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001207 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
Sunny Goyaldde4fd92016-11-21 16:02:39 +05301208 shortcutManager.unpinShortcut(key);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001209 }
1210 }
1211
Jon Miranda655ec422017-02-07 11:40:22 -08001212 FolderIconPreviewVerifier verifier =
1213 new FolderIconPreviewVerifier(mApp.getInvariantDeviceProfile());
1214 // Sort the folder items and make sure all items in the preview are high resolution.
Sunny Goyale9956a72016-09-01 17:24:47 -07001215 for (FolderInfo folder : sBgDataModel.folders) {
Sunny Goyal317698b2015-07-29 11:45:41 -07001216 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
Jon Miranda655ec422017-02-07 11:40:22 -08001217 verifier.setFolderInfo(folder);
1218
1219 int numItemsInPreview = 0;
Sunny Goyal317698b2015-07-29 11:45:41 -07001220 for (ShortcutInfo info : folder.contents) {
Jon Miranda655ec422017-02-07 11:40:22 -08001221 if (info.usingLowResIcon
1222 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION
1223 && verifier.isItemInPreview(info.rank)) {
Sunny Goyal3fe4a142016-12-15 17:40:07 -08001224 mIconCache.getTitleAndIcon(info, false);
Jon Miranda655ec422017-02-07 11:40:22 -08001225 numItemsInPreview++;
Sunny Goyal317698b2015-07-29 11:45:41 -07001226 }
Jon Miranda655ec422017-02-07 11:40:22 -08001227
1228 if (numItemsInPreview >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
Sunny Goyal317698b2015-07-29 11:45:41 -07001229 break;
1230 }
1231 }
1232 }
1233
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001234 c.commitRestoredItems();
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001235 if (!isSdCardReady && !pendingPackages.isEmpty()) {
1236 context.registerReceiver(
1237 new SdCardAvailableReceiver(
1238 LauncherModel.this, mContext, pendingPackages),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001239 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001240 null,
1241 sWorker);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001242 }
1243
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001244 // Remove any empty screens
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001245 ArrayList<Long> unusedScreens = new ArrayList<>(sBgDataModel.workspaceScreens);
Sunny Goyale9956a72016-09-01 17:24:47 -07001246 for (ItemInfo item: sBgDataModel.itemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001247 long screenId = item.screenId;
1248 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1249 unusedScreens.contains(screenId)) {
1250 unusedScreens.remove(screenId);
1251 }
1252 }
1253
1254 // If there are any empty screens remove them, and update.
1255 if (unusedScreens.size() != 0) {
Sunny Goyale9956a72016-09-01 17:24:47 -07001256 sBgDataModel.workspaceScreens.removeAll(unusedScreens);
1257 updateWorkspaceScreenOrder(context, sBgDataModel.workspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001258 }
Joe Onorato36115782010-06-17 13:28:48 -04001259 }
Sunny Goyale26d1002016-06-20 14:52:14 -07001260 if (LauncherAppState.PROFILE_STARTUP) {
1261 Trace.endSection();
1262 }
Adam Cohene25af792013-06-06 23:08:25 -07001263 }
1264
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001265 private void updateIconCache() {
1266 // Ignore packages which have a promise icon.
1267 HashSet<String> packagesToIgnore = new HashSet<>();
Sunny Goyale9956a72016-09-01 17:24:47 -07001268 synchronized (sBgDataModel) {
1269 for (ItemInfo info : sBgDataModel.itemsIdMap) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001270 if (info instanceof ShortcutInfo) {
1271 ShortcutInfo si = (ShortcutInfo) info;
1272 if (si.isPromise() && si.getTargetComponent() != null) {
1273 packagesToIgnore.add(si.getTargetComponent().getPackageName());
1274 }
1275 } else if (info instanceof LauncherAppWidgetInfo) {
1276 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
1277 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
1278 packagesToIgnore.add(lawi.providerName.getPackageName());
1279 }
1280 }
1281 }
1282 }
1283 mIconCache.updateDbIcons(packagesToIgnore);
1284 }
1285
Winson Chung64359a52013-07-08 17:17:08 -07001286 private void loadAllApps() {
1287 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04001288
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001289 final List<UserHandle> profiles = mUserManager.getUserProfiles();
Kenny Guyed131872014-04-30 03:02:21 +01001290
Winson Chung64359a52013-07-08 17:17:08 -07001291 // Clear the list of apps
1292 mBgAllAppsList.clear();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001293 for (UserHandle user : profiles) {
Kenny Guyed131872014-04-30 03:02:21 +01001294 // Query for the set of apps
1295 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal3e9be432017-01-05 15:22:41 -08001296 final List<LauncherActivityInfo> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01001297 if (DEBUG_LOADERS) {
1298 Log.d(TAG, "getActivityList took "
1299 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
1300 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
1301 }
1302 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08001303 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01001304 if (apps == null || apps.isEmpty()) {
1305 return;
1306 }
Kenny Guyff05f432016-01-22 17:48:29 +00001307 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01001308 // Create the ApplicationInfos
1309 for (int i = 0; i < apps.size(); i++) {
Sunny Goyal3e9be432017-01-05 15:22:41 -08001310 LauncherActivityInfo app = apps.get(i);
Kenny Guyed131872014-04-30 03:02:21 +01001311 // This builds the icon bitmaps.
Sunny Goyal24bb66a2017-03-21 15:12:01 -07001312 mBgAllAppsList.add(new AppInfo(app, user, quietMode), app);
Kenny Guyed131872014-04-30 03:02:21 +01001313 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08001314
Sunny Goyala474a9b2017-05-04 16:47:11 -07001315 ManagedProfileHeuristic.onAllAppsLoaded(mContext, apps, user);
Winson Chung64359a52013-07-08 17:17:08 -07001316 }
Mario Bertschler817afa32017-03-15 11:56:47 -07001317
1318 if (FeatureFlags.LAUNCHER3_PROMISE_APPS_IN_ALL_APPS) {
1319 // get all active sessions and add them to the all apps list
1320 PackageInstallerCompat installer = PackageInstallerCompat.getInstance(mContext);
1321 for (PackageInstaller.SessionInfo info : installer.getAllVerifiedSessions()) {
1322 mBgAllAppsList.addPromiseApp(mContext,
1323 PackageInstallInfo.fromInstallingState(info));
1324 }
1325 }
1326
Sunny Goyalf8d6ed22017-06-01 14:26:38 -07001327 mBgAllAppsList.added = new ArrayList<>();
Joe Onorato36115782010-06-17 13:28:48 -04001328 if (DEBUG_LOADERS) {
Sunny Goyalf8d6ed22017-06-01 14:26:38 -07001329 Log.d(TAG, "All apps loaded in in "
Winson Chung64359a52013-07-08 17:17:08 -07001330 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08001331 }
1332 }
1333
Sunny Goyaldd96a5e2017-02-17 11:22:34 -08001334 private void loadDeepShortcuts() {
Sunny Goyal4869bf32017-05-17 12:05:46 -07001335 sBgDataModel.deepShortcutMap.clear();
1336 DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(mContext);
1337 mHasShortcutHostPermission = shortcutManager.hasHostPermission();
1338 if (mHasShortcutHostPermission) {
1339 for (UserHandle user : mUserManager.getUserProfiles()) {
1340 if (mUserManager.isUserUnlocked(user)) {
1341 List<ShortcutInfoCompat> shortcuts =
1342 shortcutManager.queryForAllShortcuts(user);
1343 sBgDataModel.updateDeepShortcutMap(null, user, shortcuts);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001344 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001345 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001346 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001347 }
Joe Onorato36115782010-06-17 13:28:48 -04001348 }
1349
Sunny Goyal75b0f552015-05-20 21:57:06 -07001350 /**
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001351 * Refreshes the cached shortcuts if the shortcut permission has changed.
1352 * Current implementation simply reloads the workspace, but it can be optimized to
1353 * use partial updates similar to {@link UserManagerCompat}
1354 */
1355 public void refreshShortcutsIfRequired() {
Sunny Goyalf5e37442016-11-02 10:31:24 -07001356 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001357 sWorker.removeCallbacks(mShortcutPermissionCheckRunnable);
1358 sWorker.post(mShortcutPermissionCheckRunnable);
1359 }
1360 }
1361
1362 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -07001363 * Called when the icons for packages have been updated in the icon cache.
1364 */
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001365 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandle user) {
Sunny Goyal75b0f552015-05-20 21:57:06 -07001366 // If any package icon has changed (app was updated while launcher was dead),
1367 // update the corresponding shortcuts.
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001368 enqueueModelUpdateTask(new CacheDataUpdatedTask(
1369 CacheDataUpdatedTask.OP_CACHE_UPDATE, user, updatedPackages));
Sunny Goyal75b0f552015-05-20 21:57:06 -07001370 }
1371
Sunny Goyal0e947fb2017-05-19 12:21:50 -07001372 public void enqueueModelUpdateTask(BaseModelUpdateTask task) {
Jon Mirandaf28629a2017-02-23 15:12:42 -08001373 if (!mModelLoaded && mLoaderTask == null) {
1374 if (DEBUG_LOADERS) {
1375 Log.d(TAG, "enqueueModelUpdateTask Ignoring task since loader is pending=" + task);
1376 }
1377 return;
1378 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001379 task.init(this);
1380 runOnWorkerThread(task);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001381 }
1382
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001383 /**
1384 * A task to be executed on the current callbacks on the UI thread.
1385 * If there is no current callbacks, the task is ignored.
1386 */
1387 public interface CallbackTask {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001388
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001389 void execute(Callbacks callbacks);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001390 }
1391
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001392 /**
1393 * A runnable which changes/updates the data model of the launcher based on certain events.
1394 */
1395 public static abstract class BaseModelUpdateTask implements Runnable {
Joe Onorato36115782010-06-17 13:28:48 -04001396
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001397 private LauncherModel mModel;
Sunny Goyalb265ba72017-02-14 15:03:45 -08001398 private Executor mUiExecutor;
Joe Onorato36115782010-06-17 13:28:48 -04001399
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001400 /* package private */
1401 void init(LauncherModel model) {
1402 mModel = model;
Sunny Goyalb265ba72017-02-14 15:03:45 -08001403 mUiExecutor = mModel.mUiExecutor;
Joe Onorato36115782010-06-17 13:28:48 -04001404 }
1405
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001406 @Override
Joe Onorato36115782010-06-17 13:28:48 -04001407 public void run() {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001408 if (!mModel.mHasLoaderCompletedOnce) {
Sunny Goyalc905efc2015-05-06 09:54:53 -07001409 // Loader has not yet run.
1410 return;
1411 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001412 execute(mModel.mApp, sBgDataModel, mModel.mBgAllAppsList);
1413 }
Joe Onorato36115782010-06-17 13:28:48 -04001414
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001415 /**
1416 * Execute the actual task. Called on the worker thread.
1417 */
1418 public abstract void execute(
1419 LauncherAppState app, BgDataModel dataModel, AllAppsList apps);
Sunny Goyale0f58d72014-11-10 18:05:31 -08001420
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001421 /**
1422 * Schedules a {@param task} to be executed on the current callbacks.
1423 */
1424 public final void scheduleCallbackTask(final CallbackTask task) {
1425 final Callbacks callbacks = mModel.getCallback();
Sunny Goyalb265ba72017-02-14 15:03:45 -08001426 mUiExecutor.execute(new Runnable() {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001427 public void run() {
1428 Callbacks cb = mModel.getCallback();
1429 if (callbacks == cb && cb != null) {
1430 task.execute(callbacks);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07001431 }
1432 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001433 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001434 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001435
1436 public ModelWriter getModelWriter() {
1437 // Updates from model task, do not deal with icon position in hotseat.
1438 return mModel.getWriter(false /* hasVerticalHotseat */);
1439 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001440 }
1441
Sunny Goyal1b072632017-01-18 11:30:23 -08001442 public void updateAndBindShortcutInfo(final ShortcutInfo si, final ShortcutInfoCompat info) {
1443 updateAndBindShortcutInfo(new Provider<ShortcutInfo>() {
1444 @Override
1445 public ShortcutInfo get() {
1446 si.updateFromDeepShortcutInfo(info, mApp.getContext());
1447 si.iconBitmap = LauncherIcons.createShortcutIcon(info, mApp.getContext());
1448 return si;
1449 }
1450 });
1451 }
1452
Sunny Goyal10923b32016-07-20 15:42:44 -07001453 /**
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001454 * Utility method to update a shortcut on the background thread.
Sunny Goyal10923b32016-07-20 15:42:44 -07001455 */
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001456 public void updateAndBindShortcutInfo(final Provider<ShortcutInfo> shortcutProvider) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001457 enqueueModelUpdateTask(new ExtendedModelTask() {
Sunny Goyal10923b32016-07-20 15:42:44 -07001458 @Override
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001459 public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001460 ShortcutInfo info = shortcutProvider.get();
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001461 ArrayList<ShortcutInfo> update = new ArrayList<>();
Sunny Goyal10923b32016-07-20 15:42:44 -07001462 update.add(info);
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001463 bindUpdatedShortcuts(update, info.user);
Sunny Goyal10923b32016-07-20 15:42:44 -07001464 }
1465 });
1466 }
1467
Sunny Goyald164b7f2016-10-12 20:49:31 -07001468 private void bindWidgetsModel(final Callbacks callbacks) {
1469 final MultiHashMap<PackageItemInfo, WidgetItem> widgets
1470 = mBgWidgetsModel.getWidgetsMap().clone();
Sunny Goyalb265ba72017-02-14 15:03:45 -08001471 mUiExecutor.execute(new Runnable() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001472 @Override
1473 public void run() {
1474 Callbacks cb = getCallback();
1475 if (callbacks == cb && cb != null) {
Sunny Goyald164b7f2016-10-12 20:49:31 -07001476 callbacks.bindAllWidgets(widgets);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001477 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07001478 }
1479 });
1480 }
1481
Tony Wickham86222d22017-03-29 15:30:43 -07001482 public void refreshAndBindWidgetsAndShortcuts(final Callbacks callbacks,
1483 final boolean bindFirst, @Nullable final PackageUserKey packageUser) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001484 runOnWorkerThread(new Runnable() {
1485 @Override
1486 public void run() {
1487 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
Sunny Goyald164b7f2016-10-12 20:49:31 -07001488 bindWidgetsModel(callbacks);
Sunny Goyal31860582015-09-18 08:38:57 -07001489 }
Tony Wickham86222d22017-03-29 15:30:43 -07001490 ArrayList<WidgetItem> widgets = mBgWidgetsModel.update(
1491 mApp.getContext(), packageUser);
Sunny Goyald164b7f2016-10-12 20:49:31 -07001492 bindWidgetsModel(callbacks);
1493
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001494 // update the Widget entries inside DB on the worker thread.
Tony Wickham86222d22017-03-29 15:30:43 -07001495 mApp.getWidgetCache().removeObsoletePreviews(widgets, packageUser);
Sunny Goyal31860582015-09-18 08:38:57 -07001496 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001497 });
Michael Jurkac402cd92013-05-20 15:49:32 +02001498 }
1499
Sunny Goyal651077b2014-06-30 14:15:31 -07001500 static boolean isValidProvider(AppWidgetProviderInfo provider) {
1501 return (provider != null) && (provider.provider != null)
1502 && (provider.provider.getPackageName() != null);
1503 }
1504
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001505 public void dumpState(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
1506 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
1507 writer.println(prefix + "All apps list: size=" + mBgAllAppsList.data.size());
1508 for (AppInfo info : mBgAllAppsList.data) {
1509 writer.println(prefix + " title=\"" + info.title + "\" iconBitmap=" + info.iconBitmap
1510 + " componentName=" + info.componentName.getPackageName());
1511 }
Joe Onorato36115782010-06-17 13:28:48 -04001512 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001513 sBgDataModel.dump(prefix, fd, writer, args);
Joe Onoratobe386092009-11-17 17:32:16 -08001514 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08001515
1516 public Callbacks getCallback() {
1517 return mCallbacks != null ? mCallbacks.get() : null;
1518 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07001519
1520 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -07001521 * @return the looper for the worker thread which can be used to start background tasks.
1522 */
1523 public static Looper getWorkerLooper() {
1524 return sWorkerThread.getLooper();
1525 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526}