blob: 265c7e33ad364dbd74a958ff0c9f910a8ec53f9c [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;
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;
Michael Jurkac2f801e2011-07-12 14:19:46 -070093import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070094import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070095import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070096import java.util.List;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070097import java.util.Map;
Sunny Goyaldd96a5e2017-02-17 11:22:34 -080098import java.util.concurrent.CancellationException;
Sunny Goyal527c7d32015-08-28 15:19:36 -070099import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -0700100
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101/**
102 * Maintains in-memory state of the Launcher. It is expected that there should be only one
103 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700104 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800105 */
Kenny Guyed131872014-04-30 03:02:21 +0100106public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +0100107 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800108 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -0400109 private static final boolean DEBUG_RECEIVER = false;
Chris Wrenb358f812014-04-16 13:37:00 -0400110
Joe Onorato9c1289c2009-08-17 11:03:03 -0400111 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700112
Sunny Goyalb265ba72017-02-14 15:03:45 -0800113 private final MainThreadExecutor mUiExecutor = new MainThreadExecutor();
Adam Cohen091440a2015-03-18 14:16:05 -0700114 @Thunk final LauncherAppState mApp;
115 @Thunk final Object mLock = new Object();
Adam Cohen091440a2015-03-18 14:16:05 -0700116 @Thunk LoaderTask mLoaderTask;
117 @Thunk boolean mIsLoaderTaskRunning;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118
Adam Cohen091440a2015-03-18 14:16:05 -0700119 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700120 static {
121 sWorkerThread.start();
122 }
Adam Cohen091440a2015-03-18 14:16:05 -0700123 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700124
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800125 // Indicates whether the current model data is valid or not.
126 // We start off with everything not loaded. After that, we assume that
Joe Onoratocc67f472010-06-08 10:54:30 -0700127 // our monitoring of the package manager provides all updates and we never
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800128 // need to do a requery. This is only ever touched from the loader thread.
129 private boolean mModelLoaded;
Hyunyoung Song6aa37292017-02-06 10:46:24 -0800130 public boolean isModelLoaded() {
131 synchronized (mLock) {
132 return mModelLoaded && mLoaderTask == null;
133 }
134 }
Joe Onoratocc67f472010-06-08 10:54:30 -0700135
Adam Cohen091440a2015-03-18 14:16:05 -0700136 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700138 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800139 private final AllAppsList mBgAllAppsList;
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700140
Sunny Goyale9956a72016-09-01 17:24:47 -0700141 /**
142 * All the static data should be accessed on the background thread, A lock should be acquired
143 * on this object when accessing any data from this model.
144 */
145 static final BgDataModel sBgDataModel = new BgDataModel();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700146
Sunny Goyalc6e97692017-06-02 13:46:55 -0700147 // Runnable to check if the shortcuts permission has changed.
148 private final Runnable mShortcutPermissionCheckRunnable = new Runnable() {
149 @Override
150 public void run() {
151 if (mModelLoaded) {
152 boolean hasShortcutHostPermission =
153 DeepShortcutManager.getInstance(mApp.getContext()).hasHostPermission();
154 if (hasShortcutHostPermission != sBgDataModel.hasShortcutHostPermission) {
155 forceReload();
156 }
157 }
158 }
159 };
Kenny Guyed131872014-04-30 03:02:21 +0100160
Joe Onorato9c1289c2009-08-17 11:03:03 -0400161 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700162 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400163 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700164 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400165 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700166 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
167 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700168 public void bindScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700169 public void finishFirstPageBind(ViewOnDrawExecutor executor);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800170 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400171 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200172 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700173 public void bindAppsAdded(ArrayList<Long> newScreens,
174 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700175 ArrayList<ItemInfo> addAnimated,
176 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200177 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Mario Bertschler08ffaae2017-03-20 11:30:27 -0700178 public void bindPromiseAppProgressUpdated(PromiseAppInfo app);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700179 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800180 ArrayList<ShortcutInfo> removed, UserHandle user);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700181 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700182 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700183 public void bindWorkspaceComponentsRemoved(
184 HashSet<String> packageNames, HashSet<ComponentName> components,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800185 UserHandle user);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700186 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800187 public void notifyWidgetProvidersChanged();
Sunny Goyald164b7f2016-10-12 20:49:31 -0700188 public void bindAllWidgets(MultiHashMap<PackageItemInfo, WidgetItem> widgets);
Adam Cohen1462de32012-07-24 22:34:36 -0700189 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700190 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700191 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMap);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400192 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193
Sunny Goyaldde4fd92016-11-21 16:02:39 +0530194 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400195 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100196 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800197 }
198
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700199 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
200 * posted on the worker thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700201 private static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700202 if (sWorkerThread.getThreadId() == Process.myTid()) {
203 r.run();
204 } else {
205 // If we are not on the worker thread, then post to the worker handler
206 sWorker.post(r);
207 }
208 }
209
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700210 public void setPackageState(PackageInstallInfo installInfo) {
211 enqueueModelUpdateTask(new PackageInstallStateChangedTask(installInfo));
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500212 }
213
Sunny Goyal756adbc2015-04-16 15:20:51 -0700214 /**
215 * Updates the icons and label of all pending icons for the provided package name.
216 */
217 public void updateSessionDisplayInfo(final String packageName) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700218 HashSet<String> packages = new HashSet<>();
219 packages.add(packageName);
220 enqueueModelUpdateTask(new CacheDataUpdatedTask(
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800221 CacheDataUpdatedTask.OP_SESSION_UPDATE, Process.myUserHandle(), packages));
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800222 }
223
224 /**
225 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800226 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700227 public void addAndBindAddedWorkspaceItems(
Sunny Goyala474a9b2017-05-04 16:47:11 -0700228 Provider<List<Pair<ItemInfo, Object>>> appsProvider) {
Sunny Goyal2bcbe132016-11-16 09:23:42 -0800229 enqueueModelUpdateTask(new AddWorkspaceItemsTask(appsProvider));
Winson Chung64359a52013-07-08 17:17:08 -0700230 }
231
Sunny Goyal43bf11d2017-02-02 13:52:53 -0800232 public ModelWriter getWriter(boolean hasVerticalHotseat) {
233 return new ModelWriter(mApp.getContext(), sBgDataModel, hasVerticalHotseat);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800234 }
235
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700236 static void checkItemInfoLocked(
237 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
Sunny Goyale9956a72016-09-01 17:24:47 -0700238 ItemInfo modelItem = sBgDataModel.itemsIdMap.get(itemId);
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700239 if (modelItem != null && item != modelItem) {
240 // check all the data is consistent
241 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
242 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
243 ShortcutInfo shortcut = (ShortcutInfo) item;
244 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
245 modelShortcut.intent.filterEquals(shortcut.intent) &&
246 modelShortcut.id == shortcut.id &&
247 modelShortcut.itemType == shortcut.itemType &&
248 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700249 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700250 modelShortcut.cellX == shortcut.cellX &&
251 modelShortcut.cellY == shortcut.cellY &&
252 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700253 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700254 // For all intents and purposes, this is the same object
255 return;
256 }
257 }
258
259 // the modelItem needs to match up perfectly with item if our model is
260 // to be consistent with the database-- for now, just require
261 // modelItem == item or the equality check above
262 String msg = "item: " + ((item != null) ? item.toString() : "null") +
263 "modelItem: " +
264 ((modelItem != null) ? modelItem.toString() : "null") +
265 "Error: ItemInfo passed to checkItemInfo doesn't match original";
266 RuntimeException e = new RuntimeException(msg);
267 if (stackTrace != null) {
268 e.setStackTrace(stackTrace);
269 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800270 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700271 }
272 }
273
Michael Jurka816474f2012-06-25 14:49:02 -0700274 static void checkItemInfo(final ItemInfo item) {
275 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
276 final long itemId = item.id;
277 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700278 public void run() {
Sunny Goyale9956a72016-09-01 17:24:47 -0700279 synchronized (sBgDataModel) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700280 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700281 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700282 }
283 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700284 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700285 }
286
Joe Onorato9c1289c2009-08-17 11:03:03 -0400287 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700288 * Update the order of the workspace screens in the database. The array list contains
289 * a list of screen ids in the order that they should appear.
290 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700291 public static void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -0700292 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700293 final ContentResolver cr = context.getContentResolver();
294 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
295
296 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700297 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700298 while (iter.hasNext()) {
299 long id = iter.next();
300 if (id < 0) {
301 iter.remove();
302 }
303 }
304
305 Runnable r = new Runnable() {
306 @Override
307 public void run() {
Yura085c8532014-02-11 15:15:29 +0000308 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700309 // Clear the table
Yura085c8532014-02-11 15:15:29 +0000310 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -0700311 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700312 for (int i = 0; i < count; i++) {
313 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700314 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700315 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
316 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +0000317 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -0700318 }
Yura085c8532014-02-11 15:15:29 +0000319
320 try {
321 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
322 } catch (Exception ex) {
323 throw new RuntimeException(ex);
324 }
Winson Chung9e6a0a22013-08-27 11:58:12 -0700325
Sunny Goyale9956a72016-09-01 17:24:47 -0700326 synchronized (sBgDataModel) {
327 sBgDataModel.workspaceScreens.clear();
328 sBgDataModel.workspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -0700329 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700330 }
331 };
332 runOnWorkerThread(r);
333 }
334
335 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400336 * Set this as the current Launcher activity object for the loader.
337 */
338 public void initialize(Callbacks callbacks) {
339 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -0700340 Preconditions.assertUIThread();
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -0700341 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400342 }
343 }
344
Kenny Guyed131872014-04-30 03:02:21 +0100345 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800346 public void onPackageChanged(String packageName, UserHandle user) {
Kenny Guyed131872014-04-30 03:02:21 +0100347 int op = PackageUpdatedTask.OP_UPDATE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700348 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100349 }
350
351 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800352 public void onPackageRemoved(String packageName, UserHandle user) {
Sunny Goyalc2936bc2016-09-01 15:50:36 -0700353 onPackagesRemoved(user, packageName);
354 }
355
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800356 public void onPackagesRemoved(UserHandle user, String... packages) {
Kenny Guyed131872014-04-30 03:02:21 +0100357 int op = PackageUpdatedTask.OP_REMOVE;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700358 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packages));
Kenny Guyed131872014-04-30 03:02:21 +0100359 }
360
361 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800362 public void onPackageAdded(String packageName, UserHandle user) {
Kenny Guyed131872014-04-30 03:02:21 +0100363 int op = PackageUpdatedTask.OP_ADD;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700364 enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
Kenny Guyed131872014-04-30 03:02:21 +0100365 }
366
367 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800368 public void onPackagesAvailable(String[] packageNames, UserHandle user,
Kenny Guyed131872014-04-30 03:02:21 +0100369 boolean replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700370 enqueueModelUpdateTask(
371 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100372 }
373
374 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800375 public void onPackagesUnavailable(String[] packageNames, UserHandle user,
Kenny Guyed131872014-04-30 03:02:21 +0100376 boolean replacing) {
377 if (!replacing) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700378 enqueueModelUpdateTask(new PackageUpdatedTask(
379 PackageUpdatedTask.OP_UNAVAILABLE, user, packageNames));
Kenny Guyed131872014-04-30 03:02:21 +0100380 }
Kenny Guyed131872014-04-30 03:02:21 +0100381 }
382
Kenny Guy44cba692016-01-21 19:50:02 +0000383 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800384 public void onPackagesSuspended(String[] packageNames, UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700385 enqueueModelUpdateTask(new PackageUpdatedTask(
386 PackageUpdatedTask.OP_SUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000387 }
388
389 @Override
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800390 public void onPackagesUnsuspended(String[] packageNames, UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700391 enqueueModelUpdateTask(new PackageUpdatedTask(
392 PackageUpdatedTask.OP_UNSUSPEND, user, packageNames));
Kenny Guy44cba692016-01-21 19:50:02 +0000393 }
394
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700395 @Override
396 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800397 UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700398 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, true));
Sunny Goyal50941fb2016-08-04 12:03:52 -0700399 }
400
401 public void updatePinnedShortcuts(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800402 UserHandle user) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700403 enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, false));
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700404 }
405
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700406 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400407 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
408 * ACTION_PACKAGE_CHANGED.
409 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100410 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -0400411 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -0400412 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -0700413
Joe Onorato36115782010-06-17 13:28:48 -0400414 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +0100415 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -0700416 // If we have changed locale we need to clear out the labels in all apps/workspace.
417 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700418 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
419 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -0700420 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -0700421 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700422 } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
423 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
424 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800425 UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER);
Sunny Goyalda891c12016-03-18 18:29:24 -0700426 if (user != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700427 if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
428 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700429 enqueueModelUpdateTask(new PackageUpdatedTask(
430 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE, user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700431 }
432
433 // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so
434 // we need to run the state change task again.
435 if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
436 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700437 enqueueModelUpdateTask(new UserLockStateChangedTask(user));
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700438 }
Sunny Goyalda891c12016-03-18 18:29:24 -0700439 }
Tony Wickham827cef22016-03-17 15:39:39 -0700440 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
441 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700442 }
443 }
444
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800445 /**
446 * Reloads the workspace items from the DB and re-binds the workspace. This should generally
447 * not be called as DB updates are automatically followed by UI update
448 */
449 public void forceReload() {
450 synchronized (mLock) {
451 // Stop any existing loaders first, so they don't set mModelLoaded to true later
452 stopLoaderLocked();
453 mModelLoaded = false;
454 }
Winson Chungf0c6ae02012-03-21 16:10:31 -0700455
Reena Lee93f824a2011-09-23 17:20:28 -0700456 // Do this here because if the launcher activity is running it will be restarted.
457 // If it's not running startLoaderFromBackground will merely tell it that it needs
458 // to reload.
459 startLoaderFromBackground();
460 }
461
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700462 /**
463 * When the launcher is in the background, it's possible for it to miss paired
464 * configuration changes. So whenever we trigger the loader from the background
465 * tell the launcher that it needs to re-run the loader when it comes back instead
466 * of doing it now.
467 */
468 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800469 Callbacks callbacks = getCallback();
470 if (callbacks != null) {
471 // Only actually run the loader if they're not paused.
472 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -0700473 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -0700474 }
475 }
Joe Onorato36115782010-06-17 13:28:48 -0400476 }
Joe Onoratof99f8c12009-10-31 17:27:36 -0400477
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700478 /**
479 * If there is already a loader task running, tell it to stop.
480 */
481 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -0700482 LoaderTask oldTask = mLoaderTask;
483 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -0700484 oldTask.stopLocked();
485 }
Reena Lee93f824a2011-09-23 17:20:28 -0700486 }
487
Adam Cohen1a85c582014-09-30 09:48:49 -0700488 public boolean isCurrentCallbacks(Callbacks callbacks) {
489 return (mCallbacks != null && mCallbacks.get() == callbacks);
490 }
491
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700492 /**
493 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
494 * @return true if the page could be bound synchronously.
495 */
496 public boolean startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700497 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
Sunny Goyala474a9b2017-05-04 16:47:11 -0700498 InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_LOADER_RUNNING);
Joe Onorato36115782010-06-17 13:28:48 -0400499 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -0400500 // Don't bother to start the thread if we know it's not going to do anything
501 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -0700502 final Callbacks oldCallbacks = mCallbacks.get();
503 // Clear any pending bind-runnables from the synchronized load process.
Sunny Goyalb265ba72017-02-14 15:03:45 -0800504 mUiExecutor.execute(new Runnable() {
505 public void run() {
506 oldCallbacks.clearPendingBinds();
507 }
508 });
Sunny Goyal527c7d32015-08-28 15:19:36 -0700509
Joe Onorato36115782010-06-17 13:28:48 -0400510 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700511 stopLoaderLocked();
Sunny Goyalf8d6ed22017-06-01 14:26:38 -0700512 LoaderResults loaderResults = new LoaderResults(mApp, sBgDataModel,
513 mBgAllAppsList, synchronousBindPage, mCallbacks);
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800514 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
515 && mModelLoaded && !mIsLoaderTaskRunning) {
Sunny Goyalf8d6ed22017-06-01 14:26:38 -0700516
517 // Divide the set of loaded items into those that we are binding synchronously,
518 // and everything else that is to be bound normally (asynchronously).
519 loaderResults.bindWorkspace();
520 // For now, continue posting the binding of AllApps as there are other
521 // issues that arise from that.
522 loaderResults.bindAllApps();
523 loaderResults.bindDeepShortcuts();
Sunny Goyalc6e97692017-06-02 13:46:55 -0700524 loaderResults.bindWidgets();
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700525 return true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700526 } else {
Sunny Goyalc6e97692017-06-02 13:46:55 -0700527 mLoaderTask = new LoaderTask(mApp, mBgAllAppsList, sBgDataModel, loaderResults);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700528 sWorker.post(mLoaderTask);
529 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400530 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400531 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700532 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400533 }
534
Joe Onorato36115782010-06-17 13:28:48 -0400535 public void stopLoader() {
536 synchronized (mLock) {
537 if (mLoaderTask != null) {
538 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400539 }
540 }
Joe Onorato36115782010-06-17 13:28:48 -0400541 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400542
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800543 /**
544 * Loads the workspace screen ids in an ordered list.
545 */
Sunny Goyale5bb7052015-07-27 14:36:07 -0700546 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -0700547 final ContentResolver contentResolver = context.getContentResolver();
548 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -0700549
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800550 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -0700551 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
552 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -0700553 }
554
Mario Bertschler817afa32017-03-15 11:56:47 -0700555 public void onInstallSessionCreated(final PackageInstallInfo sessionInfo) {
556 enqueueModelUpdateTask(new ExtendedModelTask() {
557 @Override
558 public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
559 apps.addPromiseApp(app.getContext(), sessionInfo);
560 if (!apps.added.isEmpty()) {
561 final ArrayList<AppInfo> arrayList = new ArrayList<>(apps.added);
562 apps.added.clear();
563 scheduleCallbackTask(new CallbackTask() {
564 @Override
565 public void execute(Callbacks callbacks) {
566 callbacks.bindAppsAdded(null, null, null, arrayList);
567 }
568 });
569 }
570 }
571 });
572 }
573
Sunny Goyalc6e97692017-06-02 13:46:55 -0700574 public class LoaderTransaction implements AutoCloseable {
575
576 private final LoaderTask mTask;
577
578 private LoaderTransaction(LoaderTask task) throws CancellationException {
579 synchronized (mLock) {
580 if (mLoaderTask != task) {
581 throw new CancellationException("Loader already stopped");
582 }
583 mTask = task;
584 mIsLoaderTaskRunning = true;
585 mModelLoaded = false;
586 }
587 }
588
589 public void commit() {
590 synchronized (mLock) {
591 // Everything loaded bind the data.
592 mModelLoaded = true;
593 }
594 }
595
596 @Override
597 public void close() {
598 synchronized (mLock) {
599 // If we are still the last one to be scheduled, remove ourselves.
600 if (mLoaderTask == mTask) {
601 mLoaderTask = null;
602 }
603 mIsLoaderTaskRunning = false;
604 }
605 }
606 }
607
608 public LoaderTransaction beginLoader(LoaderTask task) throws CancellationException {
609 return new LoaderTransaction(task);
610 }
611
Joe Onorato36115782010-06-17 13:28:48 -0400612 /**
613 * Runnable for the thread that loads the contents of the launcher:
614 * - workspace icons
615 * - widgets
616 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700617 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -0400618 */
Sunny Goyalc6e97692017-06-02 13:46:55 -0700619 private static class LoaderTask implements Runnable {
620 private final LauncherAppState mApp;
621 private final AllAppsList mBgAllAppsList;
622 private final BgDataModel mBgDataModel;
623
Sunny Goyalf8d6ed22017-06-01 14:26:38 -0700624 private final LoaderResults mResults;
Sunny Goyal93f878c2016-03-30 17:31:24 -0700625
Sunny Goyalc6e97692017-06-02 13:46:55 -0700626 private final LauncherAppsCompat mLauncherApps;
627 private final UserManagerCompat mUserManager;
628 private final DeepShortcutManager mShortcutManager;
629 private final PackageInstallerCompat mPackageInstaller;
630 private final AppWidgetManagerCompat mAppWidgetManager;
631 private final IconCache mIconCache;
632
Joe Onorato36115782010-06-17 13:28:48 -0400633 private boolean mStopped;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700634
Sunny Goyalc6e97692017-06-02 13:46:55 -0700635 LoaderTask(LauncherAppState app, AllAppsList bgAllAppsList, BgDataModel dataModel,
636 LoaderResults results) {
637 mApp = app;
638 mBgAllAppsList = bgAllAppsList;
639 mBgDataModel = dataModel;
Sunny Goyalf8d6ed22017-06-01 14:26:38 -0700640 mResults = results;
Sunny Goyalc6e97692017-06-02 13:46:55 -0700641
642 mLauncherApps = LauncherAppsCompat.getInstance(mApp.getContext());
643 mUserManager = UserManagerCompat.getInstance(mApp.getContext());
644 mShortcutManager = DeepShortcutManager.getInstance(mApp.getContext());
645 mPackageInstaller = PackageInstallerCompat.getInstance(mApp.getContext());
646 mAppWidgetManager = AppWidgetManagerCompat.getInstance(mApp.getContext());
647 mIconCache = mApp.getIconCache();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400648 }
649
Sunny Goyalc6e97692017-06-02 13:46:55 -0700650 private synchronized void waitForIdle() {
Joe Onorato36115782010-06-17 13:28:48 -0400651 // Wait until the either we're stopped or the other threads are done.
652 // This way we don't start loading all apps until the workspace has settled
653 // down.
Sunny Goyalc6e97692017-06-02 13:46:55 -0700654 LooperIdleLock idleLock = new LooperIdleLock(this, Looper.getMainLooper());
655 // Just in case mFlushingWorkerThread changes but we aren't woken up,
656 // wait no longer than 1sec at a time
657 while (!mStopped && idleLock.awaitLocked(1000));
Joe Onorato36115782010-06-17 13:28:48 -0400658 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400659
Sunny Goyalc6e97692017-06-02 13:46:55 -0700660 private synchronized void verifyNotStopped() throws CancellationException {
661 if (mStopped) {
662 throw new CancellationException("Loader stopped");
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800663 }
664 }
665
Joe Onorato36115782010-06-17 13:28:48 -0400666 public void run() {
Sunny Goyalc6e97692017-06-02 13:46:55 -0700667 synchronized (this) {
668 // Skip fast if we are already stopped.
Sunny Goyalf5cd9982015-05-18 15:19:29 -0700669 if (mStopped) {
670 return;
671 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700672 }
Daniel Sandler843e8602010-06-07 14:59:01 -0400673
Sunny Goyalc6e97692017-06-02 13:46:55 -0700674 try (LoaderTransaction transaction = mApp.getModel().beginLoader(this)) {
Sunny Goyalb265ba72017-02-14 15:03:45 -0800675 long now = 0;
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800676 if (DEBUG_LOADERS) Log.d(TAG, "step 1.1: loading workspace");
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800677 loadWorkspace();
Joe Onorato36115782010-06-17 13:28:48 -0400678
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800679 verifyNotStopped();
680 if (DEBUG_LOADERS) Log.d(TAG, "step 1.2: bind workspace workspace");
Sunny Goyalf8d6ed22017-06-01 14:26:38 -0700681 mResults.bindWorkspace();
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800682
683 // Take a break
Sunny Goyalb265ba72017-02-14 15:03:45 -0800684 if (DEBUG_LOADERS) {
685 Log.d(TAG, "step 1 completed, wait for idle");
686 now = SystemClock.uptimeMillis();
687 }
Joe Onorato36115782010-06-17 13:28:48 -0400688 waitForIdle();
Sunny Goyalb265ba72017-02-14 15:03:45 -0800689 if (DEBUG_LOADERS) Log.d(TAG, "Waited " + (SystemClock.uptimeMillis() - now) + "ms");
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800690 verifyNotStopped();
Daniel Sandler843e8602010-06-07 14:59:01 -0400691
692 // second step
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800693 if (DEBUG_LOADERS) Log.d(TAG, "step 2.1: loading all apps");
694 loadAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700695
Sunny Goyalf8d6ed22017-06-01 14:26:38 -0700696 if (DEBUG_LOADERS) Log.d(TAG, "step 2.2: Binding all apps");
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800697 verifyNotStopped();
Sunny Goyalf8d6ed22017-06-01 14:26:38 -0700698 mResults.bindAllApps();
699
700 verifyNotStopped();
701 if (DEBUG_LOADERS) Log.d(TAG, "step 2.3: Update icon cache");
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800702 updateIconCache();
703
704 // Take a break
Sunny Goyalb265ba72017-02-14 15:03:45 -0800705 if (DEBUG_LOADERS) {
706 Log.d(TAG, "step 2 completed, wait for idle");
707 now = SystemClock.uptimeMillis();
708 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700709 waitForIdle();
Sunny Goyalb265ba72017-02-14 15:03:45 -0800710 if (DEBUG_LOADERS) Log.d(TAG, "Waited " + (SystemClock.uptimeMillis() - now) + "ms");
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800711 verifyNotStopped();
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700712
713 // third step
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800714 if (DEBUG_LOADERS) Log.d(TAG, "step 3.1: loading deep shortcuts");
715 loadDeepShortcuts();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400716
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800717 verifyNotStopped();
718 if (DEBUG_LOADERS) Log.d(TAG, "step 3.2: bind deep shortcuts");
Sunny Goyalf8d6ed22017-06-01 14:26:38 -0700719 mResults.bindDeepShortcuts();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400720
Tony Wickhama7e1c1c2017-04-13 16:24:38 -0700721 // Take a break
722 if (DEBUG_LOADERS) Log.d(TAG, "step 3 completed, wait for idle");
723 waitForIdle();
724 verifyNotStopped();
725
726 // fourth step
727 if (DEBUG_LOADERS) Log.d(TAG, "step 4.1: loading widgets");
Sunny Goyalc6e97692017-06-02 13:46:55 -0700728 mBgDataModel.widgetsModel.update(mApp, null);
Tony Wickhama7e1c1c2017-04-13 16:24:38 -0700729
Sunny Goyalc6e97692017-06-02 13:46:55 -0700730 verifyNotStopped();
731 if (DEBUG_LOADERS) Log.d(TAG, "step 4.2: Binding widgets");
732 mResults.bindWidgets();
733
734 transaction.commit();
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800735 } catch (CancellationException e) {
736 // Loader stopped, ignore
Joe Onorato36115782010-06-17 13:28:48 -0400737 }
Joe Onorato36115782010-06-17 13:28:48 -0400738 }
739
Sunny Goyalc6e97692017-06-02 13:46:55 -0700740 public synchronized void stopLocked() {
741 mStopped = true;
742 this.notify();
Joe Onorato36115782010-06-17 13:28:48 -0400743 }
744
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800745 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -0700746 if (LauncherAppState.PROFILE_STARTUP) {
747 Trace.beginSection("Loading Workspace");
748 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400749
Sunny Goyalc6e97692017-06-02 13:46:55 -0700750 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -0400751 final ContentResolver contentResolver = context.getContentResolver();
Sunny Goyal342e4662017-02-02 15:21:08 -0800752 final PackageManagerHelper pmHelper = new PackageManagerHelper(context);
753 final boolean isSafeMode = pmHelper.isSafeMode();
Sunny Goyal25aba0a2015-07-16 15:07:47 -0700754 final boolean isSdCardReady = Utilities.isBootCompleted();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800755 final MultiHashMap<UserHandle, String> pendingPackages = new MultiHashMap<>();
Joe Onorato3c2f7e12009-10-31 19:17:31 -0400756
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700757 boolean clearDb = false;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -0700758 try {
759 ImportDataTask.performImportIfPossible(context);
760 } catch (Exception e) {
761 // Migration failed. Clear workspace.
762 clearDb = true;
763 }
764
765 if (!clearDb && GridSizeMigrationTask.ENABLED &&
Sunny Goyalc6e97692017-06-02 13:46:55 -0700766 !GridSizeMigrationTask.migrateGridIfNeeded(context)) {
Sunny Goyalf076eae2016-01-11 12:25:10 -0800767 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700768 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -0700769 }
770
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700771 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -0700772 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -0700773 LauncherSettings.Settings.call(contentResolver,
Sunny Goyale05b08f2017-02-23 18:30:22 -0800774 LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -0500775 }
776
Sunny Goyalded0fdb2016-05-23 15:55:41 -0700777 Log.d(TAG, "loadWorkspace: loading default favorites");
778 LauncherSettings.Settings.call(contentResolver,
779 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -0700780
Sunny Goyalc6e97692017-06-02 13:46:55 -0700781 synchronized (mBgDataModel) {
782 mBgDataModel.clear();
Sunny Goyale9956a72016-09-01 17:24:47 -0700783
Sunny Goyalc6e97692017-06-02 13:46:55 -0700784 final HashMap<String, Integer> installingPkgs =
785 mPackageInstaller.updateAndGetActiveSessionCache();
786 mBgDataModel.workspaceScreens.addAll(loadWorkspaceScreensDb(context));
Romain Guy5c16f3e2010-01-12 17:24:58 -0800787
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700788 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800789 final LoaderCursor c = new LoaderCursor(contentResolver.query(
790 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null), mApp);
Daniel Sandler8802e962010-05-26 16:28:16 -0400791
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800792 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400793
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700794 try {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700795 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
796 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -0400797 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
798 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700799 final int spanXIndex = c.getColumnIndexOrThrow
800 (LauncherSettings.Favorites.SPANX);
801 final int spanYIndex = c.getColumnIndexOrThrow(
802 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800803 final int rankIndex = c.getColumnIndexOrThrow(
804 LauncherSettings.Favorites.RANK);
Sunny Goyal5d85c442015-03-10 13:14:47 -0700805 final int optionsIndex = c.getColumnIndexOrThrow(
806 LauncherSettings.Favorites.OPTIONS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400807
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800808 final LongSparseArray<UserHandle> allUsers = c.allUsers;
Kenny Guyff05f432016-01-22 17:48:29 +0000809 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700810 final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800811 for (UserHandle user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +0000812 long serialNo = mUserManager.getSerialNumberForUser(user);
813 allUsers.put(serialNo, user);
814 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700815
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700816 boolean userUnlocked = mUserManager.isUserUnlocked(user);
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700817
818 // We can only query for shortcuts when the user is unlocked.
819 if (userUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -0700820 List<ShortcutInfoCompat> pinnedShortcuts =
Sunny Goyalc6e97692017-06-02 13:46:55 -0700821 mShortcutManager.queryForPinnedShortcuts(null, user);
822 if (mShortcutManager.wasLastCallSuccess()) {
Sunny Goyal49f4f032016-08-01 15:45:49 -0700823 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
824 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
825 shortcut);
826 }
827 } else {
828 // Shortcut manager can fail due to some race condition when the
829 // lock state changes too frequently. For the purpose of the loading
830 // shortcuts, consider the user is still locked.
831 userUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700832 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700833 }
Sunny Goyal49f4f032016-08-01 15:45:49 -0700834 unlockedUsers.put(serialNo, userUnlocked);
Sunny Goyal7f834d22015-04-21 10:10:23 -0700835 }
836
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700837 ShortcutInfo info;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700838 LauncherAppWidgetInfo appWidgetInfo;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700839 Intent intent;
Sunny Goyal81e44912017-01-14 15:05:14 -0800840 String targetPkg;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400841
Jon Miranda529af302017-02-28 14:18:54 -0800842 FolderIconPreviewVerifier verifier =
843 new FolderIconPreviewVerifier(mApp.getInvariantDeviceProfile());
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700844 while (!mStopped && c.moveToNext()) {
845 try {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800846 if (c.user == null) {
847 // User has been deleted, remove the item.
848 c.markDeleted("User has been deleted");
849 continue;
850 }
851
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700852 boolean allowMissingTarget = false;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800853 switch (c.itemType) {
Sunny Goyal81e44912017-01-14 15:05:14 -0800854 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700855 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700856 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Sunny Goyal81e44912017-01-14 15:05:14 -0800857 intent = c.parseIntent();
858 if (intent == null) {
859 c.markDeleted("Invalid or null intent");
860 continue;
861 }
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800862
Sunny Goyal81e44912017-01-14 15:05:14 -0800863 int disabledState = quietMode.get(c.serialNumber) ?
864 ShortcutInfo.FLAG_DISABLED_QUIET_USER : 0;
865 ComponentName cn = intent.getComponent();
866 targetPkg = cn == null ? intent.getPackage() : cn.getPackageName();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700867
Sunny Goyal81e44912017-01-14 15:05:14 -0800868 if (!Process.myUserHandle().equals(c.user)) {
869 if (c.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
870 c.markDeleted("Legacy shortcuts are only allowed for default user");
871 continue;
872 } else if (c.restoreFlag != 0) {
873 // Don't restore items for other profiles.
874 c.markDeleted("Restore from managed profile not supported");
875 continue;
876 }
877 }
878 if (TextUtils.isEmpty(targetPkg) &&
879 c.itemType != LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
880 c.markDeleted("Only legacy shortcuts can have null package");
881 continue;
882 }
Sunny Goyal34942622014-08-29 17:20:55 -0700883
Sunny Goyal81e44912017-01-14 15:05:14 -0800884 // If there is no target package, its an implicit intent
885 // (legacy shortcut) which is always valid
886 boolean validTarget = TextUtils.isEmpty(targetPkg) ||
Sunny Goyalc6e97692017-06-02 13:46:55 -0700887 mLauncherApps.isPackageEnabledForProfile(targetPkg, c.user);
Sunny Goyal94485362014-09-18 16:13:58 -0700888
Sunny Goyal81e44912017-01-14 15:05:14 -0800889 if (cn != null && validTarget) {
890 // If the apk is present and the shortcut points to a specific
891 // component.
892
893 // If the component is already present
Sunny Goyalc6e97692017-06-02 13:46:55 -0700894 if (mLauncherApps.isActivityEnabledForProfile(cn, c.user)) {
Sunny Goyal81e44912017-01-14 15:05:14 -0800895 // no special handling necessary for this item
896 c.markRestored();
897 } else {
Mario Bertschlerc06af332017-03-28 12:23:22 -0700898 if (c.hasRestoreFlag(ShortcutInfo.FLAG_AUTOINSTALL_ICON)) {
Sunny Goyal81e44912017-01-14 15:05:14 -0800899 // We allow auto install apps to have their intent
900 // updated after an install.
Sunny Goyal342e4662017-02-02 15:21:08 -0800901 intent = pmHelper.getAppLaunchIntent(targetPkg, c.user);
Sunny Goyal81e44912017-01-14 15:05:14 -0800902 if (intent != null) {
903 c.restoreFlag = 0;
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800904 c.updater().put(
Sunny Goyal81e44912017-01-14 15:05:14 -0800905 LauncherSettings.Favorites.INTENT,
906 intent.toUri(0)).commit();
907 cn = intent.getComponent();
Sunny Goyalb05a00a2016-08-29 10:06:57 -0700908 } else {
Sunny Goyal81e44912017-01-14 15:05:14 -0800909 c.markDeleted("Unable to find a launch target");
Sunny Goyal94485362014-09-18 16:13:58 -0700910 continue;
911 }
Sunny Goyal1a745e82014-10-02 15:58:31 -0700912 } else {
Sunny Goyal81e44912017-01-14 15:05:14 -0800913 // The app is installed but the component is no
914 // longer available.
915 c.markDeleted("Invalid component removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -0700916 continue;
Winson Chungee055712013-07-30 14:46:24 -0700917 }
Winson Chungee055712013-07-30 14:46:24 -0700918 }
Sunny Goyal81e44912017-01-14 15:05:14 -0800919 }
920 // else if cn == null => can't infer much, leave it
921 // else if !validPkg => could be restored icon or missing sd-card
922
923 if (!TextUtils.isEmpty(targetPkg) && !validTarget) {
924 // Points to a valid app (superset of cn != null) but the apk
925 // is not available.
926
927 if (c.restoreFlag != 0) {
928 // Package is not yet available but might be
929 // installed later.
930 FileLog.d(TAG, "package not yet restored: " + targetPkg);
931
932 if (c.hasRestoreFlag(ShortcutInfo.FLAG_RESTORE_STARTED)) {
933 // Restore has started once.
934 } else if (installingPkgs.containsKey(targetPkg)) {
935 // App restore has started. Update the flag
936 c.restoreFlag |= ShortcutInfo.FLAG_RESTORE_STARTED;
937 c.updater().commit();
938 } else {
939 c.markDeleted("Unrestored app removed: " + targetPkg);
940 continue;
941 }
Sunny Goyalad2e91a2017-02-28 15:14:58 -0800942 } else if (pmHelper.isAppOnSdcard(targetPkg, c.user)) {
Sunny Goyal81e44912017-01-14 15:05:14 -0800943 // Package is present but not available.
944 disabledState |= ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
945 // Add the icon on the workspace anyway.
946 allowMissingTarget = true;
947 } else if (!isSdCardReady) {
948 // SdCard is not ready yet. Package might get available,
949 // once it is ready.
950 Log.d(TAG, "Missing pkg, will check later: " + targetPkg);
951 pendingPackages.addToList(c.user, targetPkg);
952 // Add the icon on the workspace anyway.
953 allowMissingTarget = true;
954 } else {
955 // Do not wait for external media load anymore.
956 c.markDeleted("Invalid package removed: " + targetPkg);
957 continue;
958 }
959 }
960
961 if (validTarget) {
962 // The shortcut points to a valid target (either no target
963 // or something which is ready to be used)
964 c.markRestored();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700965 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400966
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800967 boolean useLowResIcon = !c.isOnWorkspaceOrHotseat() &&
Jon Miranda529af302017-02-28 14:18:54 -0800968 !verifier.isItemInPreview(c.getInt(rankIndex));
Sunny Goyal34b65272015-03-11 16:56:52 -0700969
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -0800970 if (c.restoreFlag != 0) {
Sunny Goyal81e44912017-01-14 15:05:14 -0800971 // Already verified above that user is same as default user
972 info = c.getRestoredItemInfo(intent);
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800973 } else if (c.itemType ==
Chris Wrenf4d08112014-01-16 18:13:56 -0500974 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800975 info = c.getAppShortcutInfo(
976 intent, allowMissingTarget, useLowResIcon);
977 } else if (c.itemType ==
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700978 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700979
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800980 ShortcutKey key = ShortcutKey.fromIntent(intent, c.user);
981 if (unlockedUsers.get(c.serialNumber)) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700982 ShortcutInfoCompat pinnedShortcut =
983 shortcutKeyToPinnedShortcuts.get(key);
984 if (pinnedShortcut == null) {
985 // The shortcut is no longer valid.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800986 c.markDeleted("Pinned shortcut not found");
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700987 continue;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700988 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700989 info = new ShortcutInfo(pinnedShortcut, context);
Sunny Goyal1b072632017-01-18 11:30:23 -0800990 info.iconBitmap = LauncherIcons
991 .createShortcutIcon(pinnedShortcut, context);
Charles He3ff90472017-02-15 12:22:56 +0000992 if (pmHelper.isAppSuspended(
Sunny Goyalad2e91a2017-02-28 15:14:58 -0800993 pinnedShortcut.getPackage(), info.user)) {
Charles He3ff90472017-02-15 12:22:56 +0000994 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
995 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700996 intent = info.intent;
997 } else {
998 // Create a shortcut info in disabled mode for now.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -0800999 info = c.loadSimpleShortcut();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001000 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001001 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001002 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001003 info = c.loadSimpleShortcut();
Michael Jurka96879562012-03-22 05:54:33 -07001004
Sunny Goyald09c3702016-04-06 16:18:20 -07001005 // Shortcuts are only available on the primary profile
Tony Wickham431a7132017-05-04 15:39:43 -07001006 if (!TextUtils.isEmpty(targetPkg)
1007 && pmHelper.isAppSuspended(targetPkg, c.user)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001008 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1009 }
1010
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001011 // App shortcuts that used to be automatically added to Launcher
1012 // didn't always have the correct intent flags set, so do that
1013 // here
1014 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001015 intent.getCategories() != null &&
1016 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001017 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001018 intent.addFlags(
1019 Intent.FLAG_ACTIVITY_NEW_TASK |
1020 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1021 }
Michael Jurka96879562012-03-22 05:54:33 -07001022 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001023
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001024 if (info != null) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001025 c.applyCommonProperties(info);
1026
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001027 info.intent = intent;
Sunny Goyal08f72612015-01-05 13:41:43 -08001028 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001029 info.spanX = 1;
1030 info.spanY = 1;
Sunny Goyald09c3702016-04-06 16:18:20 -07001031 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001032 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1033 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1034 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001035
Sunny Goyal81e44912017-01-14 15:05:14 -08001036 if (c.restoreFlag != 0 && !TextUtils.isEmpty(targetPkg)) {
1037 Integer progress = installingPkgs.get(targetPkg);
1038 if (progress != null) {
1039 info.setInstallProgress(progress);
1040 } else {
1041 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001042 }
1043 }
1044
Sunny Goyalc6e97692017-06-02 13:46:55 -07001045 c.checkAndAddItem(info, mBgDataModel);
Winson Chung1323b482013-08-05 12:41:55 -07001046 } else {
1047 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001048 }
1049 break;
1050
1051 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyalc6e97692017-06-02 13:46:55 -07001052 FolderInfo folderInfo = mBgDataModel.findOrMakeFolder(c.id);
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001053 c.applyCommonProperties(folderInfo);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001054
Sunny Goyala508e4f2015-05-21 09:33:57 -07001055 // Do not trim the folder label, as is was set by the user.
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001056 folderInfo.title = c.getString(c.titleIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001057 folderInfo.spanX = 1;
1058 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001059 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001060
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001061 // no special handling required for restored folders
1062 c.markRestored();
Chris Wrenf4d08112014-01-16 18:13:56 -05001063
Sunny Goyalc6e97692017-06-02 13:46:55 -07001064 c.checkAndAddItem(folderInfo, mBgDataModel);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001065 break;
1066
1067 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08001068 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001069 // Read all Launcher-specific widget details
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001070 boolean customWidget = c.itemType ==
Adam Cohen59400422014-03-05 18:07:04 -08001071 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
1072
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001073 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001074 String savedProvider = c.getString(appWidgetProviderIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001075
Sunny Goyalff572272014-07-23 13:58:07 -07001076 final ComponentName component =
1077 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04001078
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001079 final boolean isIdValid = !c.hasRestoreFlag(
1080 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID);
1081 final boolean wasProviderReady = !c.hasRestoreFlag(
1082 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY);
Sunny Goyal651077b2014-06-30 14:15:31 -07001083
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001084 if (widgetProvidersMap == null) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07001085 widgetProvidersMap = mAppWidgetManager.getAllProvidersMap();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001086 }
1087 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
1088 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00001089 ComponentName.unflattenFromString(savedProvider),
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001090 c.user));
Sunny Goyalff572272014-07-23 13:58:07 -07001091
1092 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08001093 if (!isSafeMode && !customWidget &&
1094 wasProviderReady && !isProviderReady) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001095 c.markDeleted(
1096 "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07001097 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001098 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07001099 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07001100 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1101 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08001102
Sunny Goyal53f96722015-07-13 19:54:53 -07001103 // The provider is available. So the widget is either
1104 // available or not available. We do not need to track
1105 // any future restore updates.
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001106 int status = c.restoreFlag &
Sunny Goyal53f96722015-07-13 19:54:53 -07001107 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07001108 if (!wasProviderReady) {
1109 // If provider was not previously ready, update the
1110 // status and UI flag.
1111
1112 // Id would be valid only if the widget restore broadcast was received.
1113 if (isIdValid) {
Sunny Goyal86df1382016-08-10 15:03:22 -07001114 status |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07001115 } else {
1116 status &= ~LauncherAppWidgetInfo
1117 .FLAG_PROVIDER_NOT_READY;
1118 }
1119 }
1120 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07001121 } else {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001122 Log.v(TAG, "Widget restore pending id=" + c.id
Sunny Goyal651077b2014-06-30 14:15:31 -07001123 + " appWidgetId=" + appWidgetId
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001124 + " status =" + c.restoreFlag);
Sunny Goyal651077b2014-06-30 14:15:31 -07001125 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07001126 component);
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001127 appWidgetInfo.restoreStatus = c.restoreFlag;
Sunny Goyal756adbc2015-04-16 15:20:51 -07001128 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07001129
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001130 if (c.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_RESTORE_STARTED)) {
Sunny Goyal94485362014-09-18 16:13:58 -07001131 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001132 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07001133 // App restore has started. Update the flag
1134 appWidgetInfo.restoreStatus |=
1135 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001136 } else if (!isSafeMode) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001137 c.markDeleted("Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07001138 continue;
1139 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07001140
1141 appWidgetInfo.installProgress =
1142 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07001143 }
Sunny Goyal86df1382016-08-10 15:03:22 -07001144 if (appWidgetInfo.hasRestoreFlag(
1145 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG)) {
Sunny Goyal81e44912017-01-14 15:05:14 -08001146 appWidgetInfo.bindOptions = c.parseIntent();
Sunny Goyal86df1382016-08-10 15:03:22 -07001147 }
Sunny Goyalff572272014-07-23 13:58:07 -07001148
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001149 c.applyCommonProperties(appWidgetInfo);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001150 appWidgetInfo.spanX = c.getInt(spanXIndex);
1151 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001152 appWidgetInfo.user = c.user;
Joe Onorato36115782010-06-17 13:28:48 -04001153
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001154 if (!c.isOnWorkspaceOrHotseat()) {
1155 c.markDeleted("Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001156 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001157 continue;
1158 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001159
Adam Cohen59400422014-03-05 18:07:04 -08001160 if (!customWidget) {
1161 String providerName =
1162 appWidgetInfo.providerName.flattenToString();
1163 if (!providerName.equals(savedProvider) ||
Sunny Goyalc1ad0ce2017-01-11 14:33:38 -08001164 (appWidgetInfo.restoreStatus != c.restoreFlag)) {
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001165 c.updater()
1166 .put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1167 providerName)
1168 .put(LauncherSettings.Favorites.RESTORED,
1169 appWidgetInfo.restoreStatus)
1170 .commit();
Adam Cohen59400422014-03-05 18:07:04 -08001171 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001172 }
Jon Miranda2b823f42017-05-02 18:36:18 -07001173
1174 if (appWidgetInfo.restoreStatus !=
1175 LauncherAppWidgetInfo.RESTORE_COMPLETED) {
1176 String pkg = appWidgetInfo.providerName.getPackageName();
1177 appWidgetInfo.pendingItemInfo = new PackageItemInfo(pkg);
1178 appWidgetInfo.pendingItemInfo.user = appWidgetInfo.user;
1179 mIconCache.getTitleAndIconForApp(
1180 appWidgetInfo.pendingItemInfo, false);
1181 }
1182
Sunny Goyalc6e97692017-06-02 13:46:55 -07001183 c.checkAndAddItem(appWidgetInfo, mBgDataModel);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001184 }
Joe Onorato36115782010-06-17 13:28:48 -04001185 break;
1186 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001187 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07001188 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001189 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001190 }
1191 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001192 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001193 }
1194
Winson Chungba9c37f2013-08-30 14:11:37 -07001195 // Break early if we've stopped loading
1196 if (mStopped) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07001197 mBgDataModel.clear();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001198 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07001199 }
1200
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001201 // Remove dead items
1202 if (c.commitDeleted()) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001203 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07001204 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
1205 .call(contentResolver,
1206 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
1207 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
1208 for (long folderId : deletedFolderIds) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07001209 mBgDataModel.workspaceItems.remove(mBgDataModel.folders.get(folderId));
1210 mBgDataModel.folders.remove(folderId);
1211 mBgDataModel.itemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001212 }
Sunny Goyal55fddc82017-04-06 15:02:52 -07001213
1214 // Remove any ghost widgets
1215 LauncherSettings.Settings.call(contentResolver,
1216 LauncherSettings.Settings.METHOD_REMOVE_GHOST_WIDGETS);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001217 }
1218
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001219 // Unpin shortcuts that don't exist on the workspace.
Sunny Goyalf75baa92016-11-22 03:23:51 +05301220 HashSet<ShortcutKey> pendingShortcuts =
1221 InstallShortcutReceiver.getPendingShortcuts(context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001222 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07001223 MutableInt numTimesPinned = mBgDataModel.pinnedShortcutCounts.get(key);
Sunny Goyalf75baa92016-11-22 03:23:51 +05301224 if ((numTimesPinned == null || numTimesPinned.value == 0)
1225 && !pendingShortcuts.contains(key)) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001226 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
Sunny Goyalc6e97692017-06-02 13:46:55 -07001227 mShortcutManager.unpinShortcut(key);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001228 }
1229 }
1230
Jon Miranda655ec422017-02-07 11:40:22 -08001231 FolderIconPreviewVerifier verifier =
1232 new FolderIconPreviewVerifier(mApp.getInvariantDeviceProfile());
1233 // Sort the folder items and make sure all items in the preview are high resolution.
Sunny Goyalc6e97692017-06-02 13:46:55 -07001234 for (FolderInfo folder : mBgDataModel.folders) {
Sunny Goyal317698b2015-07-29 11:45:41 -07001235 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
Jon Miranda655ec422017-02-07 11:40:22 -08001236 verifier.setFolderInfo(folder);
1237
1238 int numItemsInPreview = 0;
Sunny Goyal317698b2015-07-29 11:45:41 -07001239 for (ShortcutInfo info : folder.contents) {
Jon Miranda655ec422017-02-07 11:40:22 -08001240 if (info.usingLowResIcon
1241 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION
1242 && verifier.isItemInPreview(info.rank)) {
Sunny Goyal3fe4a142016-12-15 17:40:07 -08001243 mIconCache.getTitleAndIcon(info, false);
Jon Miranda655ec422017-02-07 11:40:22 -08001244 numItemsInPreview++;
Sunny Goyal317698b2015-07-29 11:45:41 -07001245 }
Jon Miranda655ec422017-02-07 11:40:22 -08001246
1247 if (numItemsInPreview >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
Sunny Goyal317698b2015-07-29 11:45:41 -07001248 break;
1249 }
1250 }
1251 }
1252
Sunny Goyalaaf86fe2017-01-05 21:50:27 -08001253 c.commitRestoredItems();
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001254 if (!isSdCardReady && !pendingPackages.isEmpty()) {
1255 context.registerReceiver(
Sunny Goyalc6e97692017-06-02 13:46:55 -07001256 new SdCardAvailableReceiver(mApp, pendingPackages),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001257 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001258 null,
1259 sWorker);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001260 }
1261
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001262 // Remove any empty screens
Sunny Goyalc6e97692017-06-02 13:46:55 -07001263 ArrayList<Long> unusedScreens = new ArrayList<>(mBgDataModel.workspaceScreens);
1264 for (ItemInfo item: mBgDataModel.itemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001265 long screenId = item.screenId;
1266 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1267 unusedScreens.contains(screenId)) {
1268 unusedScreens.remove(screenId);
1269 }
1270 }
1271
1272 // If there are any empty screens remove them, and update.
1273 if (unusedScreens.size() != 0) {
Sunny Goyalc6e97692017-06-02 13:46:55 -07001274 mBgDataModel.workspaceScreens.removeAll(unusedScreens);
1275 updateWorkspaceScreenOrder(context, mBgDataModel.workspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001276 }
Joe Onorato36115782010-06-17 13:28:48 -04001277 }
Sunny Goyale26d1002016-06-20 14:52:14 -07001278 if (LauncherAppState.PROFILE_STARTUP) {
1279 Trace.endSection();
1280 }
Adam Cohene25af792013-06-06 23:08:25 -07001281 }
1282
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001283 private void updateIconCache() {
1284 // Ignore packages which have a promise icon.
1285 HashSet<String> packagesToIgnore = new HashSet<>();
Sunny Goyalc6e97692017-06-02 13:46:55 -07001286 synchronized (mBgDataModel) {
1287 for (ItemInfo info : mBgDataModel.itemsIdMap) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001288 if (info instanceof ShortcutInfo) {
1289 ShortcutInfo si = (ShortcutInfo) info;
1290 if (si.isPromise() && si.getTargetComponent() != null) {
1291 packagesToIgnore.add(si.getTargetComponent().getPackageName());
1292 }
1293 } else if (info instanceof LauncherAppWidgetInfo) {
1294 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
1295 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
1296 packagesToIgnore.add(lawi.providerName.getPackageName());
1297 }
1298 }
1299 }
1300 }
1301 mIconCache.updateDbIcons(packagesToIgnore);
1302 }
1303
Winson Chung64359a52013-07-08 17:17:08 -07001304 private void loadAllApps() {
1305 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04001306
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001307 final List<UserHandle> profiles = mUserManager.getUserProfiles();
Kenny Guyed131872014-04-30 03:02:21 +01001308
Winson Chung64359a52013-07-08 17:17:08 -07001309 // Clear the list of apps
1310 mBgAllAppsList.clear();
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001311 for (UserHandle user : profiles) {
Kenny Guyed131872014-04-30 03:02:21 +01001312 // Query for the set of apps
1313 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal3e9be432017-01-05 15:22:41 -08001314 final List<LauncherActivityInfo> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01001315 if (DEBUG_LOADERS) {
1316 Log.d(TAG, "getActivityList took "
1317 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
1318 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
1319 }
1320 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08001321 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01001322 if (apps == null || apps.isEmpty()) {
1323 return;
1324 }
Kenny Guyff05f432016-01-22 17:48:29 +00001325 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01001326 // Create the ApplicationInfos
1327 for (int i = 0; i < apps.size(); i++) {
Sunny Goyal3e9be432017-01-05 15:22:41 -08001328 LauncherActivityInfo app = apps.get(i);
Kenny Guyed131872014-04-30 03:02:21 +01001329 // This builds the icon bitmaps.
Sunny Goyal24bb66a2017-03-21 15:12:01 -07001330 mBgAllAppsList.add(new AppInfo(app, user, quietMode), app);
Kenny Guyed131872014-04-30 03:02:21 +01001331 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08001332
Sunny Goyalc6e97692017-06-02 13:46:55 -07001333 ManagedProfileHeuristic.onAllAppsLoaded(mApp.getContext(), apps, user);
Winson Chung64359a52013-07-08 17:17:08 -07001334 }
Mario Bertschler817afa32017-03-15 11:56:47 -07001335
1336 if (FeatureFlags.LAUNCHER3_PROMISE_APPS_IN_ALL_APPS) {
1337 // get all active sessions and add them to the all apps list
Sunny Goyalc6e97692017-06-02 13:46:55 -07001338 for (PackageInstaller.SessionInfo info :
1339 mPackageInstaller.getAllVerifiedSessions()) {
1340 mBgAllAppsList.addPromiseApp(mApp.getContext(),
Mario Bertschler817afa32017-03-15 11:56:47 -07001341 PackageInstallInfo.fromInstallingState(info));
1342 }
1343 }
1344
Sunny Goyalf8d6ed22017-06-01 14:26:38 -07001345 mBgAllAppsList.added = new ArrayList<>();
Joe Onorato36115782010-06-17 13:28:48 -04001346 if (DEBUG_LOADERS) {
Sunny Goyalf8d6ed22017-06-01 14:26:38 -07001347 Log.d(TAG, "All apps loaded in in "
Winson Chung64359a52013-07-08 17:17:08 -07001348 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08001349 }
1350 }
1351
Sunny Goyaldd96a5e2017-02-17 11:22:34 -08001352 private void loadDeepShortcuts() {
Sunny Goyalc6e97692017-06-02 13:46:55 -07001353 mBgDataModel.deepShortcutMap.clear();
1354 mBgDataModel.hasShortcutHostPermission = mShortcutManager.hasHostPermission();
1355 if (mBgDataModel.hasShortcutHostPermission) {
Sunny Goyal4869bf32017-05-17 12:05:46 -07001356 for (UserHandle user : mUserManager.getUserProfiles()) {
1357 if (mUserManager.isUserUnlocked(user)) {
1358 List<ShortcutInfoCompat> shortcuts =
Sunny Goyalc6e97692017-06-02 13:46:55 -07001359 mShortcutManager.queryForAllShortcuts(user);
1360 mBgDataModel.updateDeepShortcutMap(null, user, shortcuts);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001361 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001362 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001363 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001364 }
Joe Onorato36115782010-06-17 13:28:48 -04001365 }
1366
Sunny Goyal75b0f552015-05-20 21:57:06 -07001367 /**
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001368 * Refreshes the cached shortcuts if the shortcut permission has changed.
1369 * Current implementation simply reloads the workspace, but it can be optimized to
1370 * use partial updates similar to {@link UserManagerCompat}
1371 */
1372 public void refreshShortcutsIfRequired() {
Sunny Goyalf5e37442016-11-02 10:31:24 -07001373 if (Utilities.ATLEAST_NOUGAT_MR1) {
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001374 sWorker.removeCallbacks(mShortcutPermissionCheckRunnable);
1375 sWorker.post(mShortcutPermissionCheckRunnable);
1376 }
1377 }
1378
1379 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -07001380 * Called when the icons for packages have been updated in the icon cache.
1381 */
Sunny Goyal7c74e4a2016-12-15 15:53:17 -08001382 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandle user) {
Sunny Goyal75b0f552015-05-20 21:57:06 -07001383 // If any package icon has changed (app was updated while launcher was dead),
1384 // update the corresponding shortcuts.
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001385 enqueueModelUpdateTask(new CacheDataUpdatedTask(
1386 CacheDataUpdatedTask.OP_CACHE_UPDATE, user, updatedPackages));
Sunny Goyal75b0f552015-05-20 21:57:06 -07001387 }
1388
Sunny Goyal0e947fb2017-05-19 12:21:50 -07001389 public void enqueueModelUpdateTask(BaseModelUpdateTask task) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001390 task.init(this);
1391 runOnWorkerThread(task);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001392 }
1393
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001394 /**
1395 * A task to be executed on the current callbacks on the UI thread.
1396 * If there is no current callbacks, the task is ignored.
1397 */
1398 public interface CallbackTask {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001399
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001400 void execute(Callbacks callbacks);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001401 }
1402
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001403 /**
1404 * A runnable which changes/updates the data model of the launcher based on certain events.
1405 */
1406 public static abstract class BaseModelUpdateTask implements Runnable {
Joe Onorato36115782010-06-17 13:28:48 -04001407
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001408 private LauncherModel mModel;
Sunny Goyalb265ba72017-02-14 15:03:45 -08001409 private Executor mUiExecutor;
Joe Onorato36115782010-06-17 13:28:48 -04001410
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001411 /* package private */
1412 void init(LauncherModel model) {
1413 mModel = model;
Sunny Goyalb265ba72017-02-14 15:03:45 -08001414 mUiExecutor = mModel.mUiExecutor;
Joe Onorato36115782010-06-17 13:28:48 -04001415 }
1416
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001417 @Override
Sunny Goyalc6e97692017-06-02 13:46:55 -07001418 public final void run() {
1419 if (!mModel.mModelLoaded) {
1420 if (DEBUG_LOADERS) {
1421 Log.d(TAG, "Ignoring model task since loader is pending=" + this);
1422 }
Sunny Goyalc905efc2015-05-06 09:54:53 -07001423 // Loader has not yet run.
1424 return;
1425 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001426 execute(mModel.mApp, sBgDataModel, mModel.mBgAllAppsList);
1427 }
Joe Onorato36115782010-06-17 13:28:48 -04001428
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001429 /**
1430 * Execute the actual task. Called on the worker thread.
1431 */
1432 public abstract void execute(
1433 LauncherAppState app, BgDataModel dataModel, AllAppsList apps);
Sunny Goyale0f58d72014-11-10 18:05:31 -08001434
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001435 /**
1436 * Schedules a {@param task} to be executed on the current callbacks.
1437 */
1438 public final void scheduleCallbackTask(final CallbackTask task) {
1439 final Callbacks callbacks = mModel.getCallback();
Sunny Goyalb265ba72017-02-14 15:03:45 -08001440 mUiExecutor.execute(new Runnable() {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001441 public void run() {
1442 Callbacks cb = mModel.getCallback();
1443 if (callbacks == cb && cb != null) {
1444 task.execute(callbacks);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07001445 }
1446 }
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001447 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001448 }
Sunny Goyal43bf11d2017-02-02 13:52:53 -08001449
1450 public ModelWriter getModelWriter() {
1451 // Updates from model task, do not deal with icon position in hotseat.
1452 return mModel.getWriter(false /* hasVerticalHotseat */);
1453 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001454 }
1455
Sunny Goyal1b072632017-01-18 11:30:23 -08001456 public void updateAndBindShortcutInfo(final ShortcutInfo si, final ShortcutInfoCompat info) {
1457 updateAndBindShortcutInfo(new Provider<ShortcutInfo>() {
1458 @Override
1459 public ShortcutInfo get() {
1460 si.updateFromDeepShortcutInfo(info, mApp.getContext());
1461 si.iconBitmap = LauncherIcons.createShortcutIcon(info, mApp.getContext());
1462 return si;
1463 }
1464 });
1465 }
1466
Sunny Goyal10923b32016-07-20 15:42:44 -07001467 /**
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001468 * Utility method to update a shortcut on the background thread.
Sunny Goyal10923b32016-07-20 15:42:44 -07001469 */
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001470 public void updateAndBindShortcutInfo(final Provider<ShortcutInfo> shortcutProvider) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001471 enqueueModelUpdateTask(new ExtendedModelTask() {
Sunny Goyal10923b32016-07-20 15:42:44 -07001472 @Override
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001473 public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001474 ShortcutInfo info = shortcutProvider.get();
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001475 ArrayList<ShortcutInfo> update = new ArrayList<>();
Sunny Goyal10923b32016-07-20 15:42:44 -07001476 update.add(info);
Sunny Goyal1cc1c9a2017-01-06 16:32:57 -08001477 bindUpdatedShortcuts(update, info.user);
Sunny Goyal10923b32016-07-20 15:42:44 -07001478 }
1479 });
1480 }
1481
Sunny Goyalc6e97692017-06-02 13:46:55 -07001482 public void refreshAndBindWidgetsAndShortcuts(@Nullable final PackageUserKey packageUser) {
1483 enqueueModelUpdateTask(new ExtendedModelTask() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001484 @Override
Sunny Goyalc6e97692017-06-02 13:46:55 -07001485 public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
1486 dataModel.widgetsModel.update(app, packageUser);
1487 bindUpdatedWidgets(dataModel);
Sunny Goyal31860582015-09-18 08:38:57 -07001488 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001489 });
Michael Jurkac402cd92013-05-20 15:49:32 +02001490 }
1491
Sunny Goyal651077b2014-06-30 14:15:31 -07001492 static boolean isValidProvider(AppWidgetProviderInfo provider) {
1493 return (provider != null) && (provider.provider != null)
1494 && (provider.provider.getPackageName() != null);
1495 }
1496
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001497 public void dumpState(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
1498 if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
1499 writer.println(prefix + "All apps list: size=" + mBgAllAppsList.data.size());
1500 for (AppInfo info : mBgAllAppsList.data) {
1501 writer.println(prefix + " title=\"" + info.title + "\" iconBitmap=" + info.iconBitmap
1502 + " componentName=" + info.componentName.getPackageName());
1503 }
Joe Onorato36115782010-06-17 13:28:48 -04001504 }
Hyunyoung Song3c7d9cb2017-01-30 15:11:27 -08001505 sBgDataModel.dump(prefix, fd, writer, args);
Joe Onoratobe386092009-11-17 17:32:16 -08001506 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08001507
1508 public Callbacks getCallback() {
1509 return mCallbacks != null ? mCallbacks.get() : null;
1510 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07001511
1512 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -07001513 * @return the looper for the worker thread which can be used to start background tasks.
1514 */
1515 public static Looper getWorkerLooper() {
1516 return sWorkerThread.getLooper();
1517 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518}