blob: 2e3d6036a519654485e2e1ad23c43f865e09838c [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Romain Guy629de3e2010-01-13 12:20:59 -080019import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070020import android.content.BroadcastReceiver;
21import android.content.ComponentName;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070022import android.content.ContentProviderOperation;
23import android.content.ContentResolver;
24import android.content.ContentValues;
25import android.content.Context;
26import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080027import android.content.Intent.ShortcutIconResource;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070028import android.content.IntentFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.content.pm.PackageManager;
30import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.database.Cursor;
32import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.net.Uri;
Joe Onorato36115782010-06-17 13:28:48 -040034import android.os.Handler;
35import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070036import android.os.Looper;
Joe Onorato0589f0f2010-02-08 13:44:00 -080037import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040039import android.os.SystemClock;
Sunny Goyale26d1002016-06-20 14:52:14 -070040import android.os.Trace;
Chris Wrenc3919c02013-09-18 09:48:33 -040041import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080042import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070043import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080044import android.util.LongSparseArray;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070045import android.util.MutableInt;
Winson Chungc9168342013-06-26 14:54:55 -070046import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010047
Sunny Goyalffe83f12014-08-14 17:39:34 -070048import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010049import com.android.launcher3.compat.LauncherActivityInfoCompat;
50import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070051import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070052import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010053import com.android.launcher3.compat.UserHandleCompat;
54import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070055import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -070056import com.android.launcher3.config.ProviderConfig;
Tony Wickham827cef22016-03-17 15:39:39 -070057import com.android.launcher3.dynamicui.ExtractionUtils;
Sunny Goyal26119432016-02-18 22:09:23 +000058import com.android.launcher3.folder.Folder;
59import com.android.launcher3.folder.FolderIcon;
Sunny Goyal10629b02016-09-01 12:50:11 -070060import com.android.launcher3.graphics.LauncherIcons;
Sunny Goyal1acb9e92016-05-16 12:41:09 -070061import com.android.launcher3.logging.FileLog;
Sunny Goyalf862a262015-12-14 14:27:38 -080062import com.android.launcher3.model.GridSizeMigrationTask;
Sunny Goyalc2936bc2016-09-01 15:50:36 -070063import com.android.launcher3.model.SdCardAvailableReceiver;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070064import com.android.launcher3.model.WidgetsModel;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -070065import com.android.launcher3.provider.ImportDataTask;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070066import com.android.launcher3.provider.LauncherDbUtils;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070067import com.android.launcher3.shortcuts.DeepShortcutManager;
68import com.android.launcher3.shortcuts.ShortcutInfoCompat;
69import com.android.launcher3.shortcuts.ShortcutKey;
Robin Lee26ace122015-03-16 19:41:43 +000070import com.android.launcher3.util.ComponentKey;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070071import com.android.launcher3.util.CursorIconInfo;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -070072import com.android.launcher3.util.FlagOp;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070073import com.android.launcher3.util.GridOccupancy;
Sunny Goyale2df0622015-04-24 11:27:00 -070074import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070075import com.android.launcher3.util.ManagedProfileHeuristic;
Tony Wickhamd82a39d2016-07-01 15:44:13 -070076import com.android.launcher3.util.MultiHashMap;
Sunny Goyald09c3702016-04-06 16:18:20 -070077import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070078import com.android.launcher3.util.Preconditions;
Sunny Goyalda891c12016-03-18 18:29:24 -070079import com.android.launcher3.util.StringFilter;
Adam Cohen091440a2015-03-18 14:16:05 -070080import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070081import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080082
Michael Jurkac2f801e2011-07-12 14:19:46 -070083import java.lang.ref.WeakReference;
84import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070085import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070086import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070087import java.util.Arrays;
Michael Jurkac2f801e2011-07-12 14:19:46 -070088import java.util.Collections;
89import java.util.Comparator;
90import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070091import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070092import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070093import java.util.List;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070094import java.util.Map;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070095import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070096import java.util.Set;
Sunny Goyal527c7d32015-08-28 15:19:36 -070097import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -070098
The Android Open Source Project31dd5032009-03-03 19:32:27 -080099/**
100 * Maintains in-memory state of the Launcher. It is expected that there should be only one
101 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700102 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800103 */
Kenny Guyed131872014-04-30 03:02:21 +0100104public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +0100105 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800106 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -0400107 private static final boolean DEBUG_RECEIVER = false;
Chris Wrenb358f812014-04-16 13:37:00 -0400108
Joe Onorato9c1289c2009-08-17 11:03:03 -0400109 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700110
Joe Onorato36115782010-06-17 13:28:48 -0400111 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500112 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800113
Adam Cohen091440a2015-03-18 14:16:05 -0700114 @Thunk final LauncherAppState mApp;
115 @Thunk final Object mLock = new Object();
116 @Thunk DeferredHandler mHandler = new DeferredHandler();
117 @Thunk LoaderTask mLoaderTask;
118 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700119 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120
Adam Cohen091440a2015-03-18 14:16:05 -0700121 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700122 static {
123 sWorkerThread.start();
124 }
Adam Cohen091440a2015-03-18 14:16:05 -0700125 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700126
Joe Onoratocc67f472010-06-08 10:54:30 -0700127 // We start off with everything not loaded. After that, we assume that
128 // our monitoring of the package manager provides all updates and we never
129 // need to do a requery. These are only ever touched from the loader thread.
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700130 private boolean mWorkspaceLoaded;
131 private boolean mAllAppsLoaded;
132 private boolean mDeepShortcutsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700133
Sunny Goyal756a28a2015-04-23 17:07:55 -0700134 /**
135 * Set of runnables to be called on the background thread after the workspace binding
136 * is complete.
137 */
138 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
139
Adam Cohen091440a2015-03-18 14:16:05 -0700140 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700142 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800143 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700144 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800145 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800146
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700147 // Maps all launcher activities to the id's of their shortcuts (if they have any).
148 private final MultiHashMap<ComponentKey, String> mBgDeepShortcutMap = new MultiHashMap<>();
149
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700150 private boolean mHasShortcutHostPermission;
151 // Runnable to check if the shortcuts permission has changed.
152 private final Runnable mShortcutPermissionCheckRunnable = new Runnable() {
153 @Override
154 public void run() {
155 if (mDeepShortcutsLoaded) {
156 boolean hasShortcutHostPermission = mDeepShortcutManager.hasHostPermission();
157 if (hasShortcutHostPermission != mHasShortcutHostPermission) {
158 mApp.reloadWorkspace();
159 }
160 }
161 }
162 };
163
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700164 // The lock that must be acquired before referencing any static bg data structures. Unlike
165 // other locks, this one can generally be held long-term because we never expect any of these
166 // static data structures to be referenced outside of the worker thread except on the first
167 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700168 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700169
Adam Cohen487f7dd2012-06-28 18:12:10 -0700170 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700171 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700172 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700173
Adam Cohen487f7dd2012-06-28 18:12:10 -0700174 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
175 // created by LauncherModel that are directly on the home screen (however, no widgets or
176 // shortcuts within folders).
177 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700178
Adam Cohen487f7dd2012-06-28 18:12:10 -0700179 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
180 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700181 new ArrayList<LauncherAppWidgetInfo>();
182
Adam Cohen487f7dd2012-06-28 18:12:10 -0700183 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700184 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700185
Adam Cohendcd297f2013-06-18 13:13:40 -0700186 // sBgWorkspaceScreens is the ordered set of workspace screens.
187 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
188
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700189 // sBgPinnedShortcutCounts is the ComponentKey representing a pinned shortcut to the number of
190 // times it is pinned.
191 static final Map<ShortcutKey, MutableInt> sBgPinnedShortcutCounts = new HashMap<>();
192
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700193 // </ only access in worker thread >
194
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700195 private IconCache mIconCache;
196 private DeepShortcutManager mDeepShortcutManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700198 private final LauncherAppsCompat mLauncherApps;
199 private final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100200
Joe Onorato9c1289c2009-08-17 11:03:03 -0400201 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700202 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400203 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700204 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400205 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700206 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
207 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700208 public void bindScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700209 public void finishFirstPageBind(ViewOnDrawExecutor executor);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800210 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400211 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200212 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700213 public void bindAppsAdded(ArrayList<Long> newScreens,
214 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700215 ArrayList<ItemInfo> addAnimated,
216 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200217 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700218 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
219 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
220 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700221 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700222 public void bindWorkspaceComponentsRemoved(
223 HashSet<String> packageNames, HashSet<ComponentName> components,
224 UserHandleCompat user);
225 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800226 public void notifyWidgetProvidersChanged();
227 public void bindWidgetsModel(WidgetsModel model);
Adam Cohen1462de32012-07-24 22:34:36 -0700228 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700229 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700230 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMap);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400231 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232
Winson Chung64359a52013-07-08 17:17:08 -0700233 public interface ItemInfoFilter {
234 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
235 }
236
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700237 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter,
238 DeepShortcutManager deepShortcutManager) {
Winson Chunga6945242014-01-08 14:04:34 -0800239 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400240 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100241 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700242 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800243 mIconCache = iconCache;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700244 mDeepShortcutManager = deepShortcutManager;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800245
Kenny Guyed131872014-04-30 03:02:21 +0100246 mLauncherApps = LauncherAppsCompat.getInstance(context);
247 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800248 }
249
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700250 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
251 * posted on the main thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700252 private void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700253 if (sWorkerThread.getThreadId() == Process.myTid()) {
254 // If we are on the worker thread, post onto the main handler
255 mHandler.post(r);
256 } else {
257 r.run();
258 }
259 }
260
261 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
262 * posted on the worker thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700263 private static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700264 if (sWorkerThread.getThreadId() == Process.myTid()) {
265 r.run();
266 } else {
267 // If we are not on the worker thread, then post to the worker handler
268 sWorker.post(r);
269 }
270 }
271
Sunny Goyal756adbc2015-04-16 15:20:51 -0700272 public void setPackageState(final PackageInstallInfo installInfo) {
273 Runnable updateRunnable = new Runnable() {
274
275 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500276 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700277 synchronized (sBgLock) {
278 final HashSet<ItemInfo> updates = new HashSet<>();
279
280 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
281 // Ignore install success events as they are handled by Package add events.
282 return;
283 }
284
Sunny Goyale2df0622015-04-24 11:27:00 -0700285 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700286 if (info instanceof ShortcutInfo) {
287 ShortcutInfo si = (ShortcutInfo) info;
288 ComponentName cn = si.getTargetComponent();
289 if (si.isPromise() && (cn != null)
290 && installInfo.packageName.equals(cn.getPackageName())) {
291 si.setInstallProgress(installInfo.progress);
292
293 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
294 // Mark this info as broken.
295 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
296 }
297 updates.add(si);
298 }
299 }
300 }
301
302 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
303 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
304 widget.installProgress = installInfo.progress;
305 updates.add(widget);
306 }
307 }
308
309 if (!updates.isEmpty()) {
310 // Push changes to the callback.
311 Runnable r = new Runnable() {
312 public void run() {
313 Callbacks callbacks = getCallback();
314 if (callbacks != null) {
315 callbacks.bindRestoreItemsChange(updates);
316 }
317 }
318 };
319 mHandler.post(r);
320 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500321 }
322 }
323 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700324 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500325 }
326
Sunny Goyal756adbc2015-04-16 15:20:51 -0700327 /**
328 * Updates the icons and label of all pending icons for the provided package name.
329 */
330 public void updateSessionDisplayInfo(final String packageName) {
331 Runnable updateRunnable = new Runnable() {
332
333 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700334 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700335 synchronized (sBgLock) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700336 ArrayList<ShortcutInfo> updates = new ArrayList<>();
337 UserHandleCompat user = UserHandleCompat.myUserHandle();
Sunny Goyal756adbc2015-04-16 15:20:51 -0700338
Sunny Goyale2df0622015-04-24 11:27:00 -0700339 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700340 if (info instanceof ShortcutInfo) {
341 ShortcutInfo si = (ShortcutInfo) info;
342 ComponentName cn = si.getTargetComponent();
343 if (si.isPromise() && (cn != null)
344 && packageName.equals(cn.getPackageName())) {
345 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
346 // For auto install apps update the icon as well as label.
347 mIconCache.getTitleAndIcon(si,
348 si.promisedIntent, user,
349 si.shouldUseLowResIcon());
350 } else {
351 // Only update the icon for restored apps.
352 si.updateIcon(mIconCache);
353 }
354 updates.add(si);
355 }
356 }
357 }
358
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700359 bindUpdatedShortcuts(updates, user);
Sunny Goyala22666f2014-09-18 13:25:15 -0700360 }
361 }
362 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700363 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700364 }
365
Adam Cohen76a47a12014-02-05 11:47:43 -0800366 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800367 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800368
369 if (allAppsApps == null) {
370 throw new RuntimeException("allAppsApps must not be null");
371 }
372 if (allAppsApps.isEmpty()) {
373 return;
374 }
375
376 // Process the newly added applications and add them to the database first
377 Runnable r = new Runnable() {
378 public void run() {
379 runOnMainThread(new Runnable() {
380 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800381 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800382 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500383 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800384 }
385 }
386 });
387 }
388 };
389 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700390 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800391
Sunny Goyala9116722015-04-29 13:55:58 -0700392 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800393 int[] xy, int spanX, int spanY) {
394 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700395 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700396
397 GridOccupancy occupied = new GridOccupancy(profile.numColumns, profile.numRows);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800398 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700399 for (ItemInfo r : occupiedPos) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700400 occupied.markCells(r, true);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800401 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800402 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700403 return occupied.findVacantCell(xy, spanX, spanY);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800404 }
405
406 /**
407 * Find a position on the screen for the given size or adds a new screen.
408 * @return screenId and the coordinates for the item.
409 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700410 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyalc3642d42015-10-30 10:27:08 -0700411 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800412 ArrayList<Long> workspaceScreens,
413 ArrayList<Long> addedWorkspaceScreensFinal,
414 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700415 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800416
Sunny Goyala9116722015-04-29 13:55:58 -0700417 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700418 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700419 synchronized (sBgLock) {
420 for (ItemInfo info : sBgItemsIdMap) {
421 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
422 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
423 if (items == null) {
424 items = new ArrayList<>();
425 screenItems.put(info.screenId, items);
426 }
427 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800428 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800429 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800430 }
431
432 // Find appropriate space for the item.
433 long screenId = 0;
434 int[] cordinates = new int[2];
435 boolean found = false;
436
437 int screenCount = workspaceScreens.size();
438 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700439 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800440 if (preferredScreenIndex < screenCount) {
441 screenId = workspaceScreens.get(preferredScreenIndex);
442 found = findNextAvailableIconSpaceInScreen(
443 screenItems.get(screenId), cordinates, spanX, spanY);
444 }
445
446 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700447 // Search on any of the screens starting from the first screen.
448 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800449 screenId = workspaceScreens.get(screen);
450 if (findNextAvailableIconSpaceInScreen(
451 screenItems.get(screenId), cordinates, spanX, spanY)) {
452 // We found a space for it
453 found = true;
454 break;
455 }
456 }
457 }
458
459 if (!found) {
460 // Still no position found. Add a new screen to the end.
Sunny Goyald2497482015-09-22 18:24:19 -0700461 screenId = LauncherSettings.Settings.call(context.getContentResolver(),
462 LauncherSettings.Settings.METHOD_NEW_SCREEN_ID)
463 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800464
465 // Save the screen id for binding in the workspace
466 workspaceScreens.add(screenId);
467 addedWorkspaceScreensFinal.add(screenId);
468
469 // If we still can't find an empty space, then God help us all!!!
470 if (!findNextAvailableIconSpaceInScreen(
471 screenItems.get(screenId), cordinates, spanX, spanY)) {
472 throw new RuntimeException("Can't find space to add the item");
473 }
474 }
475 return Pair.create(screenId, cordinates);
476 }
477
478 /**
479 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800480 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700481 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700482 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800483 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800484 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700485 return;
Winson Chung997a9232013-07-24 15:33:46 -0700486 }
Winson Chung64359a52013-07-08 17:17:08 -0700487 // Process the newly added applications and add them to the database first
488 Runnable r = new Runnable() {
489 public void run() {
490 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
491 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
492
Winson Chung76828c82013-08-19 15:43:29 -0700493 // Get the list of workspace screens. We need to append to this list and
494 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
495 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800496 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700497 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800498 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700499 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800500 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700501 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800502 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700503 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800504 }
Winson Chung76828c82013-08-19 15:43:29 -0700505
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800506 // Find appropriate space for the item.
Sunny Goyalc3642d42015-10-30 10:27:08 -0700507 Pair<Long, int[]> coords = findSpaceForItem(context,
Sunny Goyal9eba1fd2015-10-16 08:58:57 -0700508 workspaceScreens, addedWorkspaceScreensFinal, 1, 1);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800509 long screenId = coords.first;
510 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700511
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700512 ItemInfo itemInfo;
513 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
514 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800515 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700516 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700517 } else {
518 throw new RuntimeException("Unexpected info type");
519 }
Winson Chung94d67682013-09-25 16:29:40 -0700520
Winson Chung64359a52013-07-08 17:17:08 -0700521 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700522 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700523 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700524 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700525 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700526 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700527 }
528 }
529
Winson Chung76828c82013-08-19 15:43:29 -0700530 // Update the workspace screens
531 updateWorkspaceScreenOrder(context, workspaceScreens);
532
Adam Cohen76a47a12014-02-05 11:47:43 -0800533 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700534 runOnMainThread(new Runnable() {
535 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800536 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700537 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700538 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
539 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700540 if (!addedShortcutsFinal.isEmpty()) {
541 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
542 long lastScreenId = info.screenId;
543 for (ItemInfo i : addedShortcutsFinal) {
544 if (i.screenId == lastScreenId) {
545 addAnimated.add(i);
546 } else {
547 addNotAnimated.add(i);
548 }
Winson Chung997a9232013-07-24 15:33:46 -0700549 }
550 }
Winson Chungd64d1762013-08-20 14:37:16 -0700551 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800552 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700553 }
Winson Chung64359a52013-07-08 17:17:08 -0700554 }
Winson Chung997a9232013-07-24 15:33:46 -0700555 });
556 }
Winson Chung64359a52013-07-08 17:17:08 -0700557 }
558 };
559 runOnWorkerThread(r);
560 }
561
Joe Onorato9c1289c2009-08-17 11:03:03 -0400562 /**
563 * Adds an item to the DB if it was not created previously, or move it to a new
564 * <container, screen, cellX, cellY>
565 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800566 public static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700567 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400568 if (item.container == ItemInfo.NO_ID) {
569 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700570 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400571 } else {
572 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700573 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800574 }
575 }
576
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700577 static void checkItemInfoLocked(
578 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
579 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
580 if (modelItem != null && item != modelItem) {
581 // check all the data is consistent
582 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
583 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
584 ShortcutInfo shortcut = (ShortcutInfo) item;
585 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
586 modelShortcut.intent.filterEquals(shortcut.intent) &&
587 modelShortcut.id == shortcut.id &&
588 modelShortcut.itemType == shortcut.itemType &&
589 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700590 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700591 modelShortcut.cellX == shortcut.cellX &&
592 modelShortcut.cellY == shortcut.cellY &&
593 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700594 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700595 // For all intents and purposes, this is the same object
596 return;
597 }
598 }
599
600 // the modelItem needs to match up perfectly with item if our model is
601 // to be consistent with the database-- for now, just require
602 // modelItem == item or the equality check above
603 String msg = "item: " + ((item != null) ? item.toString() : "null") +
604 "modelItem: " +
605 ((modelItem != null) ? modelItem.toString() : "null") +
606 "Error: ItemInfo passed to checkItemInfo doesn't match original";
607 RuntimeException e = new RuntimeException(msg);
608 if (stackTrace != null) {
609 e.setStackTrace(stackTrace);
610 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800611 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700612 }
613 }
614
Michael Jurka816474f2012-06-25 14:49:02 -0700615 static void checkItemInfo(final ItemInfo item) {
616 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
617 final long itemId = item.id;
618 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700619 public void run() {
620 synchronized (sBgLock) {
621 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700622 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700623 }
624 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700625 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700626 }
627
Michael Jurkac9d95c52011-08-29 14:03:34 -0700628 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
629 final ItemInfo item, final String callingFunction) {
630 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700631 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700632 final ContentResolver cr = context.getContentResolver();
633
Adam Cohen487f7dd2012-06-28 18:12:10 -0700634 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700635 Runnable r = new Runnable() {
636 public void run() {
637 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700638 updateItemArrays(item, itemId, stackTrace);
639 }
640 };
641 runOnWorkerThread(r);
642 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700643
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700644 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
645 final ArrayList<ItemInfo> items, final String callingFunction) {
646 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700647
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700648 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
649 Runnable r = new Runnable() {
650 public void run() {
651 ArrayList<ContentProviderOperation> ops =
652 new ArrayList<ContentProviderOperation>();
653 int count = items.size();
654 for (int i = 0; i < count; i++) {
655 ItemInfo item = items.get(i);
656 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700657 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700658 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700659
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700660 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
661 updateItemArrays(item, itemId, stackTrace);
662
663 }
664 try {
665 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
666 } catch (Exception e) {
667 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700668 }
669 }
670 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700671 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700672 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700673
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700674 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
675 // Lock on mBgLock *after* the db operation
676 synchronized (sBgLock) {
677 checkItemInfoLocked(itemId, item, stackTrace);
678
679 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
680 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
681 // Item is in a folder, make sure this folder exists
682 if (!sBgFolders.containsKey(item.container)) {
683 // An items container is being set to a that of an item which is not in
684 // the list of Folders.
685 String msg = "item: " + item + " container being set to: " +
686 item.container + ", not in the list of folders";
687 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700688 }
689 }
690
691 // Items are added/removed from the corresponding FolderInfo elsewhere, such
692 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
693 // that are on the desktop, as appropriate
694 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800695 if (modelItem != null &&
696 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
697 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700698 switch (modelItem.itemType) {
699 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
700 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700701 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700702 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
703 if (!sBgWorkspaceItems.contains(modelItem)) {
704 sBgWorkspaceItems.add(modelItem);
705 }
706 break;
707 default:
708 break;
709 }
710 } else {
711 sBgWorkspaceItems.remove(modelItem);
712 }
713 }
714 }
715
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800716 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400717 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700718 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700719 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700720 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400721 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400722 item.cellX = cellX;
723 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700724
Winson Chung3d503fb2011-07-13 17:25:49 -0700725 // We store hotseat items in canonical form which is this orientation invariant position
726 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700727 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700728 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700729 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700730 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700731 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700732 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400733
734 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400735 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700736 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
737 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800738 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700739 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400740
Michael Jurkac9d95c52011-08-29 14:03:34 -0700741 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700742 }
743
744 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700745 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
746 * cellX, cellY have already been updated on the ItemInfos.
747 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800748 public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700749 final long container, final int screen) {
750
751 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
752 int count = items.size();
753
754 for (int i = 0; i < count; i++) {
755 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700756 item.container = container;
757
758 // We store hotseat items in canonical form which is this orientation invariant position
759 // in the hotseat
760 if (context instanceof Launcher && screen < 0 &&
761 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700762 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700763 item.cellY);
764 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700765 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700766 }
767
768 final ContentValues values = new ContentValues();
769 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
770 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
771 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800772 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700773 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700774
775 contentValues.add(values);
776 }
777 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
778 }
779
780 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700781 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800782 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700783 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700784 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700785 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800786 item.cellX = cellX;
787 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700788 item.spanX = spanX;
789 item.spanY = spanY;
790
791 // We store hotseat items in canonical form which is this orientation invariant position
792 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700793 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700794 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700795 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700796 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700797 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700798 }
Adam Cohend4844c32011-02-18 19:25:06 -0800799
Adam Cohend4844c32011-02-18 19:25:06 -0800800 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800801 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700802 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
803 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800804 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700805 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
806 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700807 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800808
Michael Jurka816474f2012-06-25 14:49:02 -0700809 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700810 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700811
812 /**
813 * Update an item to the database in a specified container.
814 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700815 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700816 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100817 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700818 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800819 }
820
Sunny Goyal756a28a2015-04-23 17:07:55 -0700821 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700822 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700823 synchronized (mLock) {
824 if (!mHasLoaderCompletedOnce ||
825 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
826 throw new RuntimeException("Trying to add shortcut while loader is running");
827 }
828 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700829 }
830 }
831
Adam Cohend4844c32011-02-18 19:25:06 -0800832 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700833 * Returns true if the shortcuts already exists on the workspace. This must be called after
834 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700836 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
837 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700838 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700839 if (intent.getComponent() != null) {
840 // If component is not null, an intent with null package will produce
841 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700842 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700843 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700844 intentWithPkg = intent.toUri(0);
845 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700846 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700847 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
848 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700849 }
850 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700851 intentWithPkg = intent.toUri(0);
852 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700853 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700854
855 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700856 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700857 if (item instanceof ShortcutInfo) {
858 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700859 Intent targetIntent = info.promisedIntent == null
860 ? info.intent : info.promisedIntent;
861 if (targetIntent != null && info.user.equals(user)) {
Sunny Goyalcbfe71d2016-08-23 13:25:58 -0700862 Intent copyIntent = new Intent(targetIntent);
863 copyIntent.setSourceBounds(intent.getSourceBounds());
864 String s = copyIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700865 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
866 return true;
867 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700868 }
869 }
870 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800871 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700872 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700873 }
874
Joe Onorato9c1289c2009-08-17 11:03:03 -0400875 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400876 * Add an item to the database in a specified container. Sets the container, screen, cellX and
877 * cellY fields of the item. Also assigns an ID to the item.
878 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700879 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700880 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400881 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400882 item.cellX = cellX;
883 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700884 // We store hotseat items in canonical form which is this orientation invariant position
885 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700886 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700887 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700888 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700889 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700890 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700891 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400892
893 final ContentValues values = new ContentValues();
894 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100895 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400896
Sunny Goyald2497482015-09-22 18:24:19 -0700897 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
898 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
899
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700900 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700901
Jason Monk8e19cf22014-03-20 15:06:57 -0400902 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700903 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700904 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700905 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400906
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700907 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700908 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400909 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700910 sBgItemsIdMap.put(item.id, item);
911 switch (item.itemType) {
912 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
913 sBgFolders.put(item.id, (FolderInfo) item);
914 // Fall through
915 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
916 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700917 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700918 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
919 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
920 sBgWorkspaceItems.add(item);
921 } else {
922 if (!sBgFolders.containsKey(item.container)) {
923 // Adding an item to a folder that doesn't exist.
924 String msg = "adding item: " + item + " to a folder that " +
925 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700926 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700927 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700928 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700929 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700930 incrementPinnedShortcutCount(
Tony Wickhamfc02c1b2016-08-29 15:17:48 -0700931 ShortcutKey.fromShortcutInfo((ShortcutInfo) item),
932 true /* shouldPin */);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700933 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700934 break;
935 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
936 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
937 break;
938 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700939 }
940 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700941 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700942 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700943 }
944
Sunny Goyal34942622014-08-29 17:20:55 -0700945 private static ArrayList<ItemInfo> getItemsByPackageName(
946 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700947 ItemInfoFilter filter = new ItemInfoFilter() {
948 @Override
949 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
950 return cn.getPackageName().equals(pn) && info.user.equals(user);
951 }
952 };
Sunny Goyale2df0622015-04-24 11:27:00 -0700953 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -0700954 }
955
956 /**
957 * Removes all the items from the database corresponding to the specified package.
958 */
959 static void deletePackageFromDatabase(Context context, final String pn,
960 final UserHandleCompat user) {
961 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700962 }
963
964 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700965 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -0400966 */
Sunny Goyalfa401a12015-04-10 13:45:42 -0700967 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700968 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
969 items.add(item);
970 deleteItemsFromDatabase(context, items);
971 }
972
973 /**
974 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700975 */
Sunny Goyal4390ace2014-10-13 11:33:11 -0700976 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400977 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -0700978 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700979 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700980 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700981 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700982 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700983
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700984 // Lock on mBgLock *after* the db operation
985 synchronized (sBgLock) {
986 switch (item.itemType) {
987 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
988 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -0700989 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700990 if (info.container == item.id) {
991 // We are deleting a folder which still contains items that
992 // think they are contained by that folder.
993 String msg = "deleting a folder (" + item + ") which still " +
994 "contains items (" + info + ")";
995 Log.e(TAG, msg);
996 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700997 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700998 sBgWorkspaceItems.remove(item);
999 break;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001000 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07001001 decrementPinnedShortcutCount(ShortcutKey.fromShortcutInfo(
1002 (ShortcutInfo) item));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001003 // Fall through.
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001004 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1005 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1006 sBgWorkspaceItems.remove(item);
1007 break;
1008 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1009 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1010 break;
1011 }
1012 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001013 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001014 }
1015 }
Michael Jurka83df1882011-08-31 20:59:26 -07001016 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001017 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001018 }
1019
1020 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001021 * Decrement the count for the given pinned shortcut, unpinning it if the count becomes 0.
1022 */
1023 private static void decrementPinnedShortcutCount(final ShortcutKey pinnedShortcut) {
1024 synchronized (sBgLock) {
1025 MutableInt count = sBgPinnedShortcutCounts.get(pinnedShortcut);
1026 if (count == null || --count.value == 0) {
1027 LauncherAppState.getInstance().getShortcutManager().unpinShortcut(pinnedShortcut);
1028 }
1029 }
1030 }
1031
1032 /**
1033 * Increment the count for the given shortcut, pinning it if the count becomes 1.
1034 *
1035 * As an optimization, the caller can pass shouldPin == false to avoid
1036 * unnecessary RPC's if the shortcut is already pinned.
1037 */
1038 private static void incrementPinnedShortcutCount(ShortcutKey pinnedShortcut, boolean shouldPin) {
1039 synchronized (sBgLock) {
1040 MutableInt count = sBgPinnedShortcutCounts.get(pinnedShortcut);
1041 if (count == null) {
1042 count = new MutableInt(1);
1043 sBgPinnedShortcutCounts.put(pinnedShortcut, count);
1044 } else {
1045 count.value++;
1046 }
1047 if (shouldPin && count.value == 1) {
1048 LauncherAppState.getInstance().getShortcutManager().pinShortcut(pinnedShortcut);
1049 }
1050 }
1051 }
1052
1053 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001054 * Update the order of the workspace screens in the database. The array list contains
1055 * a list of screen ids in the order that they should appear.
1056 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001057 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001058 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001059 final ContentResolver cr = context.getContentResolver();
1060 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1061
1062 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001063 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001064 while (iter.hasNext()) {
1065 long id = iter.next();
1066 if (id < 0) {
1067 iter.remove();
1068 }
1069 }
1070
1071 Runnable r = new Runnable() {
1072 @Override
1073 public void run() {
Yura085c8532014-02-11 15:15:29 +00001074 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001075 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001076 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001077 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001078 for (int i = 0; i < count; i++) {
1079 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001080 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001081 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1082 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001083 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001084 }
Yura085c8532014-02-11 15:15:29 +00001085
1086 try {
1087 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1088 } catch (Exception ex) {
1089 throw new RuntimeException(ex);
1090 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001091
Winson Chungba9c37f2013-08-30 14:11:37 -07001092 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001093 sBgWorkspaceScreens.clear();
1094 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001095 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001096 }
1097 };
1098 runOnWorkerThread(r);
1099 }
1100
1101 /**
Winsonc0b52fe2015-09-09 16:38:15 -07001102 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001103 */
Winsonc0b52fe2015-09-09 16:38:15 -07001104 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001105 final ContentResolver cr = context.getContentResolver();
1106
Michael Jurkac9d95c52011-08-29 14:03:34 -07001107 Runnable r = new Runnable() {
1108 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001109 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001110 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001111 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001112 sBgItemsIdMap.remove(info.id);
1113 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001114 sBgWorkspaceItems.remove(info);
1115 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001116
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001117 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001118 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001119 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001120 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001121 for (ItemInfo childInfo : info.contents) {
1122 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001123 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001124 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001125 }
1126 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001127 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001128 }
1129
1130 /**
1131 * Set this as the current Launcher activity object for the loader.
1132 */
1133 public void initialize(Callbacks callbacks) {
1134 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001135 Preconditions.assertUIThread();
1136 // Remove any queued UI runnables
1137 mHandler.cancelAll();
1138 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001139 }
1140 }
1141
Kenny Guyed131872014-04-30 03:02:21 +01001142 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001143 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001144 int op = PackageUpdatedTask.OP_UPDATE;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001145 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001146 user));
1147 }
1148
1149 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001150 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001151 onPackagesRemoved(user, packageName);
1152 }
1153
1154 public void onPackagesRemoved(UserHandleCompat user, String... packages) {
Kenny Guyed131872014-04-30 03:02:21 +01001155 int op = PackageUpdatedTask.OP_REMOVE;
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001156 enqueueItemUpdatedTask(new PackageUpdatedTask(op, packages, user));
Kenny Guyed131872014-04-30 03:02:21 +01001157 }
1158
1159 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001160 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001161 int op = PackageUpdatedTask.OP_ADD;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001162 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001163 user));
1164 }
1165
1166 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001167 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001168 boolean replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001169 enqueueItemUpdatedTask(
Sunny Goyal144154d2016-03-30 16:47:03 -07001170 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, packageNames, user));
Kenny Guyed131872014-04-30 03:02:21 +01001171 }
1172
1173 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001174 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001175 boolean replacing) {
1176 if (!replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001177 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guyed131872014-04-30 03:02:21 +01001178 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1179 user));
1180 }
Kenny Guyed131872014-04-30 03:02:21 +01001181 }
1182
Kenny Guy44cba692016-01-21 19:50:02 +00001183 @Override
1184 public void onPackagesSuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001185 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001186 PackageUpdatedTask.OP_SUSPEND, packageNames,
1187 user));
1188 }
1189
1190 @Override
1191 public void onPackagesUnsuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001192 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001193 PackageUpdatedTask.OP_UNSUSPEND, packageNames,
1194 user));
1195 }
1196
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001197 @Override
1198 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
1199 UserHandleCompat user) {
Sunny Goyal50941fb2016-08-04 12:03:52 -07001200 enqueueItemUpdatedTask(new ShortcutsChangedTask(packageName, shortcuts, user, true));
1201 }
1202
1203 public void updatePinnedShortcuts(String packageName, List<ShortcutInfoCompat> shortcuts,
1204 UserHandleCompat user) {
1205 enqueueItemUpdatedTask(new ShortcutsChangedTask(packageName, shortcuts, user, false));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001206 }
1207
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001208 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001209 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1210 * ACTION_PACKAGE_CHANGED.
1211 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001212 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001213 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001214 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001215
Joe Onorato36115782010-06-17 13:28:48 -04001216 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001217 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001218 // If we have changed locale we need to clear out the labels in all apps/workspace.
1219 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001220 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1221 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001222 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001223 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001224 } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1225 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
1226 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyalda891c12016-03-18 18:29:24 -07001227 UserHandleCompat user = UserHandleCompat.fromIntent(intent);
1228 if (user != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001229 if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1230 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
1231 enqueueItemUpdatedTask(new PackageUpdatedTask(
1232 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE,
1233 new String[0], user));
1234 }
1235
1236 // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so
1237 // we need to run the state change task again.
1238 if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
1239 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
1240 enqueueItemUpdatedTask(new UserLockStateChangedTask(user));
1241 }
Sunny Goyalda891c12016-03-18 18:29:24 -07001242 }
Tony Wickham827cef22016-03-17 15:39:39 -07001243 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
1244 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001245 }
1246 }
1247
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001248 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001249 resetLoadedState(true, true);
1250
Reena Lee93f824a2011-09-23 17:20:28 -07001251 // Do this here because if the launcher activity is running it will be restarted.
1252 // If it's not running startLoaderFromBackground will merely tell it that it needs
1253 // to reload.
1254 startLoaderFromBackground();
1255 }
1256
Winson Chungf0c6ae02012-03-21 16:10:31 -07001257 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1258 synchronized (mLock) {
1259 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1260 // mWorkspaceLoaded to true later
1261 stopLoaderLocked();
1262 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1263 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001264 // Always reset deep shortcuts loaded.
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001265 // TODO: why?
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001266 mDeepShortcutsLoaded = false;
Winson Chungf0c6ae02012-03-21 16:10:31 -07001267 }
1268 }
1269
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001270 /**
1271 * When the launcher is in the background, it's possible for it to miss paired
1272 * configuration changes. So whenever we trigger the loader from the background
1273 * tell the launcher that it needs to re-run the loader when it comes back instead
1274 * of doing it now.
1275 */
1276 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001277 Callbacks callbacks = getCallback();
1278 if (callbacks != null) {
1279 // Only actually run the loader if they're not paused.
1280 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -07001281 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001282 }
1283 }
Joe Onorato36115782010-06-17 13:28:48 -04001284 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001285
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001286 /**
1287 * If there is already a loader task running, tell it to stop.
1288 */
1289 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001290 LoaderTask oldTask = mLoaderTask;
1291 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001292 oldTask.stopLocked();
1293 }
Reena Lee93f824a2011-09-23 17:20:28 -07001294 }
1295
Adam Cohen1a85c582014-09-30 09:48:49 -07001296 public boolean isCurrentCallbacks(Callbacks callbacks) {
1297 return (mCallbacks != null && mCallbacks.get() == callbacks);
1298 }
1299
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001300 /**
1301 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
1302 * @return true if the page could be bound synchronously.
1303 */
1304 public boolean startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001305 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1306 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001307 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -04001308 // Don't bother to start the thread if we know it's not going to do anything
1309 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001310 final Callbacks oldCallbacks = mCallbacks.get();
1311 // Clear any pending bind-runnables from the synchronized load process.
1312 runOnMainThread(new Runnable() {
1313 public void run() {
1314 oldCallbacks.clearPendingBinds();
1315 }
1316 });
1317
Joe Onorato36115782010-06-17 13:28:48 -04001318 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001319 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001320 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001321 // TODO: mDeepShortcutsLoaded does not need to be true for synchronous bind.
Tony Wickham80f57872016-06-29 18:12:15 -07001322 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE && mAllAppsLoaded
1323 && mWorkspaceLoaded && mDeepShortcutsLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001324 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001325 return true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001326 } else {
1327 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1328 sWorker.post(mLoaderTask);
1329 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001330 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001331 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001332 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001333 }
1334
Joe Onorato36115782010-06-17 13:28:48 -04001335 public void stopLoader() {
1336 synchronized (mLock) {
1337 if (mLoaderTask != null) {
1338 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001339 }
1340 }
Joe Onorato36115782010-06-17 13:28:48 -04001341 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001342
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001343 /**
1344 * Loads the workspace screen ids in an ordered list.
1345 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001346 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001347 final ContentResolver contentResolver = context.getContentResolver();
1348 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001349
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001350 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001351 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
1352 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -07001353 }
1354
Joe Onorato36115782010-06-17 13:28:48 -04001355 /**
1356 * Runnable for the thread that loads the contents of the launcher:
1357 * - workspace icons
1358 * - widgets
1359 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001360 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -04001361 */
1362 private class LoaderTask implements Runnable {
1363 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001364 private int mPageToBindFirst;
1365
Adam Cohen091440a2015-03-18 14:16:05 -07001366 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001367 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001368 @Thunk boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001369
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001370 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -04001371 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001372 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001373 }
1374
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001375 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001376 mIsLoadingAndBindingWorkspace = true;
1377
Joe Onorato36115782010-06-17 13:28:48 -04001378 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001379 if (DEBUG_LOADERS) {
1380 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001381 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001382
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001383 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001384 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001385 synchronized (LoaderTask.this) {
1386 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001387 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001388 }
1389 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001390 }
1391 }
1392
Joe Onorato36115782010-06-17 13:28:48 -04001393 // Bind the workspace
Sunny Goyal93f878c2016-03-30 17:31:24 -07001394 bindWorkspace(mPageToBindFirst);
Joe Onorato36115782010-06-17 13:28:48 -04001395 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001396
Joe Onorato36115782010-06-17 13:28:48 -04001397 private void waitForIdle() {
1398 // Wait until the either we're stopped or the other threads are done.
1399 // This way we don't start loading all apps until the workspace has settled
1400 // down.
1401 synchronized (LoaderTask.this) {
1402 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001403
Joe Onorato36115782010-06-17 13:28:48 -04001404 mHandler.postIdle(new Runnable() {
1405 public void run() {
1406 synchronized (LoaderTask.this) {
1407 mLoadAndBindStepFinished = true;
1408 if (DEBUG_LOADERS) {
1409 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001410 }
Joe Onorato36115782010-06-17 13:28:48 -04001411 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001412 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001413 }
Joe Onorato36115782010-06-17 13:28:48 -04001414 });
1415
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001416 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001417 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001418 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1419 // wait no longer than 1sec at a time
1420 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001421 } catch (InterruptedException ex) {
1422 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001423 }
1424 }
Joe Onorato36115782010-06-17 13:28:48 -04001425 if (DEBUG_LOADERS) {
1426 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001427 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001428 + "ms for previous step to finish binding");
1429 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001430 }
Joe Onorato36115782010-06-17 13:28:48 -04001431 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001432
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001433 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001434 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001435 // Ensure that we have a valid page index to load synchronously
1436 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1437 "valid page index");
1438 }
1439 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1440 // Ensure that we don't try and bind a specified page when the pages have not been
1441 // loaded already (we should load everything asynchronously in that case)
1442 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1443 }
1444 synchronized (mLock) {
1445 if (mIsLoaderTaskRunning) {
1446 // Ensure that we are never running the background loading at this point since
1447 // we also touch the background collections
1448 throw new RuntimeException("Error! Background loading is already running");
1449 }
1450 }
1451
1452 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1453 // data structures, we can't allow any other thread to touch that data, but because
1454 // this call is synchronous, we can get away with not locking).
1455
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001456 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001457 // operations from the previous activity. We need to ensure that all queued operations
1458 // are executed before any synchronous binding work is done.
1459 mHandler.flush();
1460
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001461 // Divide the set of loaded items into those that we are binding synchronously, and
1462 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001463 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001464 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1465 // arise from that.
1466 onlyBindAllApps();
Tony Wickham80f57872016-06-29 18:12:15 -07001467
1468 bindDeepShortcuts();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001469 }
1470
Joe Onorato36115782010-06-17 13:28:48 -04001471 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001472 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001473 if (mStopped) {
1474 return;
1475 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001476 mIsLoaderTaskRunning = true;
1477 }
Joe Onorato36115782010-06-17 13:28:48 -04001478 // Optimize for end-user experience: if the Launcher is up and // running with the
1479 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1480 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001481 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001482 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001483 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001484
Joe Onorato36115782010-06-17 13:28:48 -04001485 if (mStopped) {
1486 break keep_running;
1487 }
1488
Joe Onorato36115782010-06-17 13:28:48 -04001489 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001490
1491 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001492 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1493 loadAndBindAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001494
1495 waitForIdle();
1496
1497 // third step
1498 if (DEBUG_LOADERS) Log.d(TAG, "step 3: loading deep shortcuts");
1499 loadAndBindDeepShortcuts();
Joe Onorato36115782010-06-17 13:28:48 -04001500 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001501
Joe Onorato36115782010-06-17 13:28:48 -04001502 // Clear out this reference, otherwise we end up holding it until all of the
1503 // callback runnables are done.
1504 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001505
Joe Onorato36115782010-06-17 13:28:48 -04001506 synchronized (mLock) {
1507 // If we are still the last one to be scheduled, remove ourselves.
1508 if (mLoaderTask == this) {
1509 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001510 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001511 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001512 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001513 }
Joe Onorato36115782010-06-17 13:28:48 -04001514 }
1515
1516 public void stopLocked() {
1517 synchronized (LoaderTask.this) {
1518 mStopped = true;
1519 this.notify();
1520 }
1521 }
1522
1523 /**
1524 * Gets the callbacks object. If we've been stopped, or if the launcher object
1525 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1526 * object that was around when the deferred message was scheduled, and if there's
1527 * a new Callbacks object around then also return null. This will save us from
1528 * calling onto it with data that will be ignored.
1529 */
1530 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1531 synchronized (mLock) {
1532 if (mStopped) {
1533 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001534 }
Joe Onorato36115782010-06-17 13:28:48 -04001535
1536 if (mCallbacks == null) {
1537 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001538 }
Joe Onorato36115782010-06-17 13:28:48 -04001539
1540 final Callbacks callbacks = mCallbacks.get();
1541 if (callbacks != oldCallbacks) {
1542 return null;
1543 }
1544 if (callbacks == null) {
1545 Log.w(TAG, "no mCallbacks");
1546 return null;
1547 }
1548
1549 return callbacks;
1550 }
1551 }
1552
1553 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001554 private boolean checkItemPlacement(LongArrayMap<GridOccupancy> occupied, ItemInfo item,
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001555 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001556 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001557 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung892c74d2013-08-22 16:15:50 -07001558
Adam Cohendcd297f2013-06-18 13:13:40 -07001559 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001560 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001561 // Return early if we detect that an item is under the hotseat button
Sunny Goyalbb011da2016-06-15 15:42:29 -07001562 if (!FeatureFlags.NO_ALL_APPS_ICON &&
1563 profile.isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001564 Log.e(TAG, "Error loading shortcut into hotseat " + item
1565 + " into position (" + item.screenId + ":" + item.cellX + ","
1566 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001567 return false;
1568 }
1569
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001570 final GridOccupancy hotseatOccupancy =
Dan Sandler295ae182013-12-10 16:05:47 -05001571 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1572
Adam Cohen2e6da152015-05-06 11:42:25 -07001573 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001574 Log.e(TAG, "Error loading shortcut " + item
1575 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001576 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001577 + ")");
1578 return false;
1579 }
1580
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001581 if (hotseatOccupancy != null) {
1582 if (hotseatOccupancy.cells[(int) item.screenId][0]) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001583 Log.e(TAG, "Error loading shortcut into hotseat " + item
1584 + " into position (" + item.screenId + ":" + item.cellX + ","
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001585 + item.cellY + ") already occupied");
Adam Cohendcd297f2013-06-18 13:13:40 -07001586 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001587 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001588 hotseatOccupancy.cells[(int) item.screenId][0] = true;
Dan Sandler295ae182013-12-10 16:05:47 -05001589 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001590 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001591 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001592 final GridOccupancy occupancy = new GridOccupancy(profile.numHotseatIcons, 1);
1593 occupancy.cells[(int) item.screenId][0] = true;
1594 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, occupancy);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001595 return true;
1596 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001597 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1598 if (!workspaceScreens.contains((Long) item.screenId)) {
1599 // The item has an invalid screen id.
1600 return false;
1601 }
1602 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001603 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001604 return true;
1605 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001606
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001607 final int countX = profile.numColumns;
1608 final int countY = profile.numRows;
Adam Cohenae4409d2013-11-26 10:34:59 -08001609 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1610 item.cellX < 0 || item.cellY < 0 ||
1611 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1612 Log.e(TAG, "Error loading shortcut " + item
1613 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1614 + item.cellX + "," + item.cellY
1615 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1616 return false;
1617 }
1618
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001619 if (!occupied.containsKey(item.screenId)) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001620 GridOccupancy screen = new GridOccupancy(countX + 1, countY + 1);
1621 if (item.screenId == Workspace.FIRST_SCREEN_ID) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001622 // Mark the first row as occupied (if the feature is enabled)
1623 // in order to account for the QSB.
1624 screen.markCells(0, 0, countX + 1, 1, FeatureFlags.QSB_ON_FIRST_SCREEN);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001625 }
1626 occupied.put(item.screenId, screen);
Joe Onorato36115782010-06-17 13:28:48 -04001627 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001628 final GridOccupancy occupancy = occupied.get(item.screenId);
Winson Chungf30ad5f2011-08-08 10:55:42 -07001629
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001630 // Check if any workspace icons overlap with each other
1631 if (occupancy.isRegionVacant(item.cellX, item.cellY, item.spanX, item.spanY)) {
1632 occupancy.markCells(item, true);
1633 return true;
1634 } else {
1635 Log.e(TAG, "Error loading shortcut " + item
1636 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1637 + item.cellX + "," + item.cellX + "," + item.spanX + "," + item.spanY
1638 + ") already occupied");
1639 return false;
1640 }
Joe Onorato36115782010-06-17 13:28:48 -04001641 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001642
Winson Chungba9c37f2013-08-30 14:11:37 -07001643 /** Clears all the sBg data structures */
1644 private void clearSBgDataStructures() {
1645 synchronized (sBgLock) {
1646 sBgWorkspaceItems.clear();
1647 sBgAppWidgets.clear();
1648 sBgFolders.clear();
1649 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001650 sBgWorkspaceScreens.clear();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001651 sBgPinnedShortcutCounts.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001652 }
1653 }
1654
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001655 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -07001656 if (LauncherAppState.PROFILE_STARTUP) {
1657 Trace.beginSection("Loading Workspace");
1658 }
Joe Onorato36115782010-06-17 13:28:48 -04001659 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001660
Joe Onorato36115782010-06-17 13:28:48 -04001661 final Context context = mContext;
1662 final ContentResolver contentResolver = context.getContentResolver();
1663 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001664 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001665 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001666 final boolean isSdCardReady = Utilities.isBootCompleted();
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001667 final MultiHashMap<UserHandleCompat, String> pendingPackages = new MultiHashMap<>();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001668
Winson Chung892c74d2013-08-22 16:15:50 -07001669 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001670 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001671 int countX = profile.numColumns;
1672 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001673
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001674 boolean clearDb = false;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -07001675 try {
1676 ImportDataTask.performImportIfPossible(context);
1677 } catch (Exception e) {
1678 // Migration failed. Clear workspace.
1679 clearDb = true;
1680 }
1681
1682 if (!clearDb && GridSizeMigrationTask.ENABLED &&
Sunny Goyalf076eae2016-01-11 12:25:10 -08001683 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1684 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001685 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001686 }
1687
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001688 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -07001689 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001690 LauncherSettings.Settings.call(contentResolver,
1691 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001692 }
1693
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001694 Log.d(TAG, "loadWorkspace: loading default favorites");
1695 LauncherSettings.Settings.call(contentResolver,
1696 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -07001697
Winson Chung2abf94d2012-07-18 18:16:38 -07001698 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001699 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001700 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001701 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001702 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001703
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001704 final ArrayList<Long> itemsToRemove = new ArrayList<>();
1705 final ArrayList<Long> restoredRows = new ArrayList<>();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001706 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001707 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001708 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001709 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001710
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001711 // +1 for the hotseat (it can be larger than the workspace)
1712 // Load workspace in reverse order to ensure that latest items are loaded first (and
1713 // before any earlier duplicates)
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001714 final LongArrayMap<GridOccupancy> occupied = new LongArrayMap<>();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001715 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001716
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001717 try {
1718 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1719 final int intentIndex = c.getColumnIndexOrThrow
1720 (LauncherSettings.Favorites.INTENT);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001721 final int containerIndex = c.getColumnIndexOrThrow(
1722 LauncherSettings.Favorites.CONTAINER);
1723 final int itemTypeIndex = c.getColumnIndexOrThrow(
1724 LauncherSettings.Favorites.ITEM_TYPE);
1725 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1726 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001727 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1728 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001729 final int screenIndex = c.getColumnIndexOrThrow(
1730 LauncherSettings.Favorites.SCREEN);
1731 final int cellXIndex = c.getColumnIndexOrThrow
1732 (LauncherSettings.Favorites.CELLX);
1733 final int cellYIndex = c.getColumnIndexOrThrow
1734 (LauncherSettings.Favorites.CELLY);
1735 final int spanXIndex = c.getColumnIndexOrThrow
1736 (LauncherSettings.Favorites.SPANX);
1737 final int spanYIndex = c.getColumnIndexOrThrow(
1738 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001739 final int rankIndex = c.getColumnIndexOrThrow(
1740 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001741 final int restoredIndex = c.getColumnIndexOrThrow(
1742 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001743 final int profileIdIndex = c.getColumnIndexOrThrow(
1744 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001745 final int optionsIndex = c.getColumnIndexOrThrow(
1746 LauncherSettings.Favorites.OPTIONS);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001747 final CursorIconInfo cursorIconInfo = new CursorIconInfo(mContext, c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001748
Sunny Goyal7f834d22015-04-21 10:10:23 -07001749 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
Kenny Guyff05f432016-01-22 17:48:29 +00001750 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001751 final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>();
Sunny Goyal7f834d22015-04-21 10:10:23 -07001752 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001753 long serialNo = mUserManager.getSerialNumberForUser(user);
1754 allUsers.put(serialNo, user);
1755 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001756
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001757 boolean userUnlocked = mUserManager.isUserUnlocked(user);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001758
1759 // We can only query for shortcuts when the user is unlocked.
1760 if (userUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07001761 List<ShortcutInfoCompat> pinnedShortcuts =
1762 mDeepShortcutManager.queryForPinnedShortcuts(null, user);
1763 if (mDeepShortcutManager.wasLastCallSuccess()) {
1764 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
1765 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
1766 shortcut);
1767 }
1768 } else {
1769 // Shortcut manager can fail due to some race condition when the
1770 // lock state changes too frequently. For the purpose of the loading
1771 // shortcuts, consider the user is still locked.
1772 userUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001773 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001774 }
Sunny Goyal49f4f032016-08-01 15:45:49 -07001775 unlockedUsers.put(serialNo, userUnlocked);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001776 }
1777
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001778 ShortcutInfo info;
1779 String intentDescription;
1780 LauncherAppWidgetInfo appWidgetInfo;
1781 int container;
1782 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001783 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001784 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001785 UserHandleCompat user;
Sunny Goyald09c3702016-04-06 16:18:20 -07001786 String targetPackage;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001787
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001788 while (!mStopped && c.moveToNext()) {
1789 try {
1790 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001791 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001792 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001793 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001794
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001795 switch (itemType) {
1796 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1797 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001798 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001799 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001800 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001801 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001802 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001803 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001804 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001805 boolean itemReplaced = false;
Sunny Goyald09c3702016-04-06 16:18:20 -07001806 targetPackage = null;
Kenny Guyed131872014-04-30 03:02:21 +01001807 if (user == null) {
1808 // User has been deleted remove the item.
1809 itemsToRemove.add(id);
1810 continue;
1811 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001812 try {
1813 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001814 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001815 if (cn != null && cn.getPackageName() != null) {
1816 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1817 cn.getPackageName(), user);
1818 boolean validComponent = validPkg &&
1819 launcherApps.isActivityEnabledForProfile(cn, user);
Sunny Goyald09c3702016-04-06 16:18:20 -07001820 if (validPkg) {
1821 targetPackage = cn.getPackageName();
1822 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001823
1824 if (validComponent) {
1825 if (restored) {
1826 // no special handling necessary for this item
1827 restoredRows.add(id);
1828 restored = false;
1829 }
Kenny Guyff05f432016-01-22 17:48:29 +00001830 if (quietMode.get(serialNumber)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001831 disabledState = ShortcutInfo.FLAG_DISABLED_QUIET_USER;
Kenny Guyff05f432016-01-22 17:48:29 +00001832 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001833 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001834 intent = null;
1835 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1836 // We allow auto install apps to have their intent
1837 // updated after an install.
1838 intent = manager.getLaunchIntentForPackage(
1839 cn.getPackageName());
1840 if (intent != null) {
1841 ContentValues values = new ContentValues();
1842 values.put(LauncherSettings.Favorites.INTENT,
1843 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001844 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001845 }
1846 }
1847
1848 if (intent == null) {
1849 // The app is installed but the component is no
1850 // longer available.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001851 FileLog.d(TAG, "Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001852 itemsToRemove.add(id);
1853 continue;
1854 } else {
1855 // no special handling necessary for this item
1856 restoredRows.add(id);
1857 restored = false;
1858 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001859 } else if (restored) {
1860 // Package is not yet available but might be
1861 // installed later.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001862 FileLog.d(TAG, "package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001863
1864 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1865 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001866 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001867 // App restore has started. Update the flag
1868 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1869 ContentValues values = new ContentValues();
1870 values.put(LauncherSettings.Favorites.RESTORED,
1871 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001872 updateItem(id, values);
1873 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1874 // This is a common app. Try to replace this.
1875 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1876 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1877 if (parser.findDefaultApp()) {
1878 // Default app found. Replace it.
1879 intent = parser.parsedIntent;
1880 cn = intent.getComponent();
1881 ContentValues values = parser.parsedValues;
1882 values.put(LauncherSettings.Favorites.RESTORED, 0);
1883 updateItem(id, values);
1884 restored = false;
1885 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001886
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001887 } else {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001888 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001889 itemsToRemove.add(id);
1890 continue;
1891 }
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001892 } else {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001893 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001894 itemsToRemove.add(id);
1895 continue;
1896 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001897 } else if (PackageManagerHelper.isAppOnSdcard(
1898 manager, cn.getPackageName())) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07001899 // Package is present but not available.
1900 allowMissingTarget = true;
1901 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1902 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001903 // SdCard is not ready yet. Package might get available,
1904 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001905 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001906 pendingPackages.addToList(user, cn.getPackageName());
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001907 allowMissingTarget = true;
1908 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001909
1910 } else {
1911 // Do not wait for external media load anymore.
1912 // Log the invalid package, and remove it
Sunny Goyal713edfc2016-05-06 09:58:34 -07001913 FileLog.d(TAG, "Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001914 itemsToRemove.add(id);
1915 continue;
Winson Chungee055712013-07-30 14:46:24 -07001916 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001917 } else if (cn == null) {
1918 // For shortcuts with no component, keep them as they are
1919 restoredRows.add(id);
1920 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001921 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001922 } catch (URISyntaxException e) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001923 FileLog.d(TAG, "Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001924 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001925 continue;
1926 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001927
Sunny Goyal34b65272015-03-11 16:56:52 -07001928 boolean useLowResIcon = container >= 0 &&
1929 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1930
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001931 if (itemReplaced) {
1932 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001933 info = getAppShortcutInfo(intent, user, null,
1934 cursorIconInfo, false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001935 } else {
1936 // Don't replace items for other profiles.
1937 itemsToRemove.add(id);
1938 continue;
1939 }
1940 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001941 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001942 info = getRestoredItemInfo(c, intent,
1943 promiseType, itemType, cursorIconInfo);
Kenny Guyed131872014-04-30 03:02:21 +01001944 intent = getRestoredItemIntent(c, context, intent);
1945 } else {
1946 // Don't restore items for other profiles.
1947 itemsToRemove.add(id);
1948 continue;
1949 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001950 } else if (itemType ==
1951 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001952 info = getAppShortcutInfo(intent, user, c,
1953 cursorIconInfo, allowMissingTarget, useLowResIcon);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001954 } else if (itemType ==
1955 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001956
1957 ShortcutKey key = ShortcutKey.fromIntent(intent, user);
1958 if (unlockedUsers.get(serialNumber)) {
1959 ShortcutInfoCompat pinnedShortcut =
1960 shortcutKeyToPinnedShortcuts.get(key);
1961 if (pinnedShortcut == null) {
1962 // The shortcut is no longer valid.
1963 itemsToRemove.add(id);
1964 continue;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001965 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001966 info = new ShortcutInfo(pinnedShortcut, context);
1967 intent = info.intent;
1968 } else {
1969 // Create a shortcut info in disabled mode for now.
1970 info = new ShortcutInfo();
1971 info.user = user;
1972 info.itemType = itemType;
1973 loadInfoFromCursor(info, c, cursorIconInfo);
1974
1975 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001976 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001977 incrementPinnedShortcutCount(key, false /* shouldPin */);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001978 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001979 info = getShortcutInfo(c, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001980
Sunny Goyald09c3702016-04-06 16:18:20 -07001981 // Shortcuts are only available on the primary profile
1982 if (PackageManagerHelper.isAppSuspended(manager, targetPackage)) {
1983 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1984 }
1985
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001986 // App shortcuts that used to be automatically added to Launcher
1987 // didn't always have the correct intent flags set, so do that
1988 // here
1989 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001990 intent.getCategories() != null &&
1991 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001992 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001993 intent.addFlags(
1994 Intent.FLAG_ACTIVITY_NEW_TASK |
1995 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1996 }
Michael Jurka96879562012-03-22 05:54:33 -07001997 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001998
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001999 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07002000 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002001 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002002 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002003 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002004 info.cellX = c.getInt(cellXIndex);
2005 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08002006 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002007 info.spanX = 1;
2008 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002009 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002010 if (info.promisedIntent != null) {
2011 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
2012 }
Sunny Goyald09c3702016-04-06 16:18:20 -07002013 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002014 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2015 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2016 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002017
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002018 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002019 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002020 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002021 break;
2022 }
2023
Sunny Goyal756adbc2015-04-16 15:20:51 -07002024 if (restored) {
2025 ComponentName cn = info.getTargetComponent();
2026 if (cn != null) {
2027 Integer progress = installingPkgs.get(cn.getPackageName());
2028 if (progress != null) {
2029 info.setInstallProgress(progress);
2030 } else {
2031 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2032 }
2033 }
2034 }
2035
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002036 switch (container) {
2037 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2038 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2039 sBgWorkspaceItems.add(info);
2040 break;
2041 default:
2042 // Item is in a user folder
2043 FolderInfo folderInfo =
2044 findOrMakeFolder(sBgFolders, container);
Sunny Goyalc52ba712016-04-05 15:59:05 -07002045 folderInfo.add(info, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002046 break;
2047 }
2048 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002049 } else {
2050 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002051 }
2052 break;
2053
2054 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2055 id = c.getLong(idIndex);
2056 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2057
Sunny Goyala508e4f2015-05-21 09:33:57 -07002058 // Do not trim the folder label, as is was set by the user.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002059 folderInfo.title = c.getString(cursorIconInfo.titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002060 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002061 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002062 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002063 folderInfo.cellX = c.getInt(cellXIndex);
2064 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002065 folderInfo.spanX = 1;
2066 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002067 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002068
Daniel Sandler8802e962010-05-26 16:28:16 -04002069 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002070 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002071 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002072 break;
2073 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002074
Joe Onorato9c1289c2009-08-17 11:03:03 -04002075 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002076 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2077 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2078 sBgWorkspaceItems.add(folderInfo);
2079 break;
Joe Onorato36115782010-06-17 13:28:48 -04002080 }
Joe Onorato17a89222011-02-08 17:26:11 -08002081
Chris Wrenf4d08112014-01-16 18:13:56 -05002082 if (restored) {
2083 // no special handling required for restored folders
2084 restoredRows.add(id);
2085 }
2086
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002087 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2088 sBgFolders.put(folderInfo.id, folderInfo);
2089 break;
2090
2091 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002092 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002093 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002094 boolean customWidget = itemType ==
2095 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2096
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002097 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002098 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002099 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002100 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002101 user = allUsers.get(serialNumber);
2102 if (user == null) {
2103 itemsToRemove.add(id);
2104 continue;
2105 }
2106
Sunny Goyalff572272014-07-23 13:58:07 -07002107 final ComponentName component =
2108 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002109
Sunny Goyal651077b2014-06-30 14:15:31 -07002110 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002111 final boolean isIdValid = (restoreStatus &
2112 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002113 final boolean wasProviderReady = (restoreStatus &
2114 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002115
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002116 if (widgetProvidersMap == null) {
2117 widgetProvidersMap = AppWidgetManagerCompat
2118 .getInstance(mContext).getAllProvidersMap();
2119 }
2120 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
2121 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00002122 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002123 user));
Sunny Goyalff572272014-07-23 13:58:07 -07002124
2125 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002126 if (!isSafeMode && !customWidget &&
2127 wasProviderReady && !isProviderReady) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002128 FileLog.d(TAG, "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07002129 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002130 itemsToRemove.add(id);
2131 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002132 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002133 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2134 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002135
Sunny Goyal53f96722015-07-13 19:54:53 -07002136 // The provider is available. So the widget is either
2137 // available or not available. We do not need to track
2138 // any future restore updates.
2139 int status = restoreStatus &
2140 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002141 if (!wasProviderReady) {
2142 // If provider was not previously ready, update the
2143 // status and UI flag.
2144
2145 // Id would be valid only if the widget restore broadcast was received.
2146 if (isIdValid) {
Sunny Goyal86df1382016-08-10 15:03:22 -07002147 status |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002148 } else {
2149 status &= ~LauncherAppWidgetInfo
2150 .FLAG_PROVIDER_NOT_READY;
2151 }
2152 }
2153 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002154 } else {
2155 Log.v(TAG, "Widget restore pending id=" + id
2156 + " appWidgetId=" + appWidgetId
2157 + " status =" + restoreStatus);
2158 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002159 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002160 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002161 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002162
2163 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2164 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002165 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002166 // App restore has started. Update the flag
2167 appWidgetInfo.restoreStatus |=
2168 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalb05a00a2016-08-29 10:06:57 -07002169 } else if (!isSafeMode) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002170 FileLog.d(TAG, "Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07002171 itemsToRemove.add(id);
2172 continue;
2173 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002174
2175 appWidgetInfo.installProgress =
2176 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002177 }
Sunny Goyal86df1382016-08-10 15:03:22 -07002178 if (appWidgetInfo.hasRestoreFlag(
2179 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG)) {
2180 intentDescription = c.getString(intentIndex);
2181 if (!TextUtils.isEmpty(intentDescription)) {
2182 appWidgetInfo.bindOptions =
2183 Intent.parseUri(intentDescription, 0);
2184 }
2185 }
Sunny Goyalff572272014-07-23 13:58:07 -07002186
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002187 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002188 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002189 appWidgetInfo.cellX = c.getInt(cellXIndex);
2190 appWidgetInfo.cellY = c.getInt(cellYIndex);
2191 appWidgetInfo.spanX = c.getInt(spanXIndex);
2192 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002193 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002194
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002195 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2196 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2197 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002198 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2199 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002200 continue;
2201 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002202
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002203 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002204 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002205 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002206 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002207 break;
2208 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002209
Adam Cohen59400422014-03-05 18:07:04 -08002210 if (!customWidget) {
2211 String providerName =
2212 appWidgetInfo.providerName.flattenToString();
2213 if (!providerName.equals(savedProvider) ||
2214 (appWidgetInfo.restoreStatus != restoreStatus)) {
2215 ContentValues values = new ContentValues();
2216 values.put(
2217 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2218 providerName);
2219 values.put(LauncherSettings.Favorites.RESTORED,
2220 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002221 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002222 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002223 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002224 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2225 sBgAppWidgets.add(appWidgetInfo);
2226 }
Joe Onorato36115782010-06-17 13:28:48 -04002227 break;
2228 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002229 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002230 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002231 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002232 }
2233 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002234 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002235 }
2236
Winson Chungba9c37f2013-08-30 14:11:37 -07002237 // Break early if we've stopped loading
2238 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002239 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002240 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002241 }
2242
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002243 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002244 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002245 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2246 Utilities.createDbSelectionQuery(
2247 LauncherSettings.Favorites._ID, itemsToRemove), null);
2248 if (DEBUG_LOADERS) {
2249 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2250 LauncherSettings.Favorites._ID, itemsToRemove));
2251 }
2252
2253 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07002254 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
2255 .call(contentResolver,
2256 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
2257 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
2258 for (long folderId : deletedFolderIds) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002259 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2260 sBgFolders.remove(folderId);
2261 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002262 }
2263 }
2264
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002265 // Unpin shortcuts that don't exist on the workspace.
2266 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
2267 MutableInt numTimesPinned = sBgPinnedShortcutCounts.get(key);
2268 if (numTimesPinned == null || numTimesPinned.value == 0) {
2269 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
2270 mDeepShortcutManager.unpinShortcut(key);
2271 }
2272 }
2273
Sunny Goyal317698b2015-07-29 11:45:41 -07002274 // Sort all the folder items and make sure the first 3 items are high resolution.
2275 for (FolderInfo folder : sBgFolders) {
2276 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2277 int pos = 0;
2278 for (ShortcutInfo info : folder.contents) {
2279 if (info.usingLowResIcon) {
2280 info.updateIcon(mIconCache, false);
2281 }
2282 pos ++;
2283 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2284 break;
2285 }
2286 }
2287 }
2288
Chris Wrenf4d08112014-01-16 18:13:56 -05002289 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002290 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002291 ContentValues values = new ContentValues();
2292 values.put(LauncherSettings.Favorites.RESTORED, 0);
2293 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2294 Utilities.createDbSelectionQuery(
2295 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002296 }
2297
Sunny Goyalc2936bc2016-09-01 15:50:36 -07002298 if (!isSdCardReady && !pendingPackages.isEmpty()) {
2299 context.registerReceiver(
2300 new SdCardAvailableReceiver(
2301 LauncherModel.this, mContext, pendingPackages),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002302 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalc2936bc2016-09-01 15:50:36 -07002303 null,
2304 sWorker);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002305 }
2306
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002307 // Remove any empty screens
2308 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002309 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002310 long screenId = item.screenId;
2311 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2312 unusedScreens.contains(screenId)) {
2313 unusedScreens.remove(screenId);
2314 }
2315 }
2316
2317 // If there are any empty screens remove them, and update.
2318 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002319 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002320 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002321 }
2322
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002323 if (DEBUG_LOADERS) {
2324 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2325 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002326 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002327 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002328 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002329
Sunny Goyale2df0622015-04-24 11:27:00 -07002330 for (int i = 0; i < nScreens; i++) {
2331 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002332 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002333 line += " | ";
2334 }
Joe Onorato36115782010-06-17 13:28:48 -04002335 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002336 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002337 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002338 }
Joe Onorato36115782010-06-17 13:28:48 -04002339 }
Sunny Goyale26d1002016-06-20 14:52:14 -07002340 if (LauncherAppState.PROFILE_STARTUP) {
2341 Trace.endSection();
2342 }
Adam Cohene25af792013-06-06 23:08:25 -07002343 }
2344
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002345 /**
2346 * Partially updates the item without any notification. Must be called on the worker thread.
2347 */
2348 private void updateItem(long itemId, ContentValues update) {
2349 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002350 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002351 update,
2352 BaseColumns._ID + "= ?",
2353 new String[]{Long.toString(itemId)});
2354 }
2355
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002356 /** Filters the set of items who are directly or indirectly (via another container) on the
2357 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002358 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002359 ArrayList<ItemInfo> allWorkspaceItems,
2360 ArrayList<ItemInfo> currentScreenItems,
2361 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002362 // Purge any null ItemInfos
2363 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2364 while (iter.hasNext()) {
2365 ItemInfo i = iter.next();
2366 if (i == null) {
2367 iter.remove();
2368 }
2369 }
2370
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002371 // Order the set of items by their containers first, this allows use to walk through the
2372 // list sequentially, build up a list of containers that are in the specified screen,
2373 // as well as all items in those containers.
2374 Set<Long> itemsOnScreen = new HashSet<Long>();
2375 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2376 @Override
2377 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07002378 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002379 }
2380 });
2381 for (ItemInfo info : allWorkspaceItems) {
2382 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002383 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002384 currentScreenItems.add(info);
2385 itemsOnScreen.add(info.id);
2386 } else {
2387 otherScreenItems.add(info);
2388 }
2389 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2390 currentScreenItems.add(info);
2391 itemsOnScreen.add(info.id);
2392 } else {
2393 if (itemsOnScreen.contains(info.container)) {
2394 currentScreenItems.add(info);
2395 itemsOnScreen.add(info.id);
2396 } else {
2397 otherScreenItems.add(info);
2398 }
2399 }
2400 }
2401 }
2402
2403 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002404 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002405 ArrayList<LauncherAppWidgetInfo> appWidgets,
2406 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2407 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002408
2409 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002410 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002411 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002412 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002413 currentScreenWidgets.add(widget);
2414 } else {
2415 otherScreenWidgets.add(widget);
2416 }
2417 }
2418 }
2419
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002420 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2421 * right) */
2422 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002423 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002424 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002425 final int screenCols = profile.numColumns;
2426 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002427 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002428 @Override
2429 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002430 if (lhs.container == rhs.container) {
2431 // Within containers, order by their spatial position in that container
2432 switch ((int) lhs.container) {
2433 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2434 long lr = (lhs.screenId * screenCellCount +
2435 lhs.cellY * screenCols + lhs.cellX);
2436 long rr = (rhs.screenId * screenCellCount +
2437 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07002438 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07002439 }
2440 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2441 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07002442 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07002443 }
2444 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002445 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002446 throw new RuntimeException("Unexpected container type when " +
2447 "sorting workspace items.");
2448 }
2449 return 0;
2450 }
2451 } else {
2452 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07002453 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07002454 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002455 }
2456 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002457 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002458
Adam Cohendcd297f2013-06-18 13:13:40 -07002459 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2460 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002461 final Runnable r = new Runnable() {
2462 @Override
2463 public void run() {
2464 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2465 if (callbacks != null) {
2466 callbacks.bindScreens(orderedScreens);
2467 }
2468 }
2469 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002470 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002471 }
2472
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002473 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2474 final ArrayList<ItemInfo> workspaceItems,
2475 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002476 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002477
2478 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002479 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002480 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002481 final int start = i;
2482 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002483 final Runnable r = new Runnable() {
2484 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002485 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002486 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002487 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002488 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2489 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002490 }
2491 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002492 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002493 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002494 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002495
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002496 // Bind the widgets, one at a time
2497 N = appWidgets.size();
2498 for (int i = 0; i < N; i++) {
2499 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2500 final Runnable r = new Runnable() {
2501 public void run() {
2502 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2503 if (callbacks != null) {
2504 callbacks.bindAppWidget(widget);
2505 }
2506 }
2507 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002508 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002509 }
2510 }
2511
2512 /**
2513 * Binds all loaded data to actual views on the main thread.
2514 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002515 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002516 final long t = SystemClock.uptimeMillis();
2517 Runnable r;
2518
2519 // Don't use these two variables in any of the callback runnables.
2520 // Otherwise we hold a reference to them.
2521 final Callbacks oldCallbacks = mCallbacks.get();
2522 if (oldCallbacks == null) {
2523 // This launcher has exited and nobody bothered to tell us. Just bail.
2524 Log.w(TAG, "LoaderTask running with no launcher");
2525 return;
2526 }
2527
Winson Chung9b9fb962013-11-15 15:39:34 -08002528 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07002529 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
2530 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
2531 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002532
Winson Chung2abf94d2012-07-18 18:16:38 -07002533 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002534 workspaceItems.addAll(sBgWorkspaceItems);
2535 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002536 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002537 }
2538
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002539 final int currentScreen;
2540 {
2541 int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE
2542 ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen();
2543 if (currScreen >= orderedScreenIds.size()) {
2544 // There may be no workspace screens (just hotseat items and an empty page).
2545 currScreen = PagedView.INVALID_RESTORE_PAGE;
2546 }
2547 currentScreen = currScreen;
Winson Chung9b9fb962013-11-15 15:39:34 -08002548 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002549 final boolean validFirstPage = currentScreen >= 0;
2550 final long currentScreenId =
2551 validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID;
Winson Chung9b9fb962013-11-15 15:39:34 -08002552
Winson Chung9b9fb962013-11-15 15:39:34 -08002553 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07002554 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
2555 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
2556 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
2557 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002558
Winson Chung9b9fb962013-11-15 15:39:34 -08002559 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002560 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002561 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002562 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002563 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2564 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2565
2566 // Tell the workspace that we're about to start binding items
2567 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002568 public void run() {
2569 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2570 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002571 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002572 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002573 }
2574 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002575 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002576 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002577
Adam Cohendcd297f2013-06-18 13:13:40 -07002578 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2579
Sunny Goyal527c7d32015-08-28 15:19:36 -07002580 Executor mainExecutor = new DeferredMainThreadExecutor();
2581 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07002582 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002583
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002584 // In case of validFirstPage, only bind the first screen, and defer binding the
2585 // remaining screens after first onDraw (and an optional the fade animation whichever
2586 // happens later).
2587 // This ensures that the first screen is immediately visible (eg. during rotation)
2588 // In case of !validFirstPage, bind all pages one after other.
2589 final Executor deferredExecutor =
2590 validFirstPage ? new ViewOnDrawExecutor(mHandler) : mainExecutor;
2591
2592 mainExecutor.execute(new Runnable() {
2593 @Override
2594 public void run() {
2595 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2596 if (callbacks != null) {
2597 callbacks.finishFirstPageBind(
2598 validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null);
2599 }
2600 }
2601 });
Sunny Goyal527c7d32015-08-28 15:19:36 -07002602
Sunny Goyal44c06432016-04-02 10:56:02 -07002603 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002604
2605 // Tell the workspace that we're done binding items
2606 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002607 public void run() {
2608 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2609 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002610 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002611 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002612
Sunny Goyal639e9062015-08-19 19:17:06 -07002613 mIsLoadingAndBindingWorkspace = false;
2614
2615 // Run all the bind complete runnables after workspace is bound.
2616 if (!mBindCompleteRunnables.isEmpty()) {
2617 synchronized (mBindCompleteRunnables) {
2618 for (final Runnable r : mBindCompleteRunnables) {
2619 runOnWorkerThread(r);
2620 }
2621 mBindCompleteRunnables.clear();
2622 }
2623 }
2624
Winson Chung98e030b2012-05-07 16:01:11 -07002625 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002626 if (DEBUG_LOADERS) {
2627 Log.d(TAG, "bound workspace in "
2628 + (SystemClock.uptimeMillis()-t) + "ms");
2629 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002630
Joe Onorato36115782010-06-17 13:28:48 -04002631 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002632 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002633 deferredExecutor.execute(r);
2634
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002635 if (validFirstPage) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002636 r = new Runnable() {
2637 public void run() {
2638 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2639 if (callbacks != null) {
2640 // We are loading synchronously, which means, some of the pages will be
2641 // bound after first draw. Inform the callbacks that page binding is
2642 // not complete, and schedule the remaining pages.
2643 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2644 callbacks.onPageBoundSynchronously(currentScreen);
2645 }
2646 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2647 }
2648 }
2649 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002650 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002651 }
Joe Onorato36115782010-06-17 13:28:48 -04002652 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002653
Joe Onorato36115782010-06-17 13:28:48 -04002654 private void loadAndBindAllApps() {
2655 if (DEBUG_LOADERS) {
2656 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2657 }
2658 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002659 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002660 synchronized (LoaderTask.this) {
2661 if (mStopped) {
2662 return;
2663 }
2664 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002665 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002666 synchronized (LoaderTask.this) {
2667 if (mStopped) {
2668 return;
2669 }
2670 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002671 }
Joe Onorato36115782010-06-17 13:28:48 -04002672 } else {
2673 onlyBindAllApps();
2674 }
2675 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002676
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002677 private void updateIconCache() {
2678 // Ignore packages which have a promise icon.
2679 HashSet<String> packagesToIgnore = new HashSet<>();
2680 synchronized (sBgLock) {
2681 for (ItemInfo info : sBgItemsIdMap) {
2682 if (info instanceof ShortcutInfo) {
2683 ShortcutInfo si = (ShortcutInfo) info;
2684 if (si.isPromise() && si.getTargetComponent() != null) {
2685 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2686 }
2687 } else if (info instanceof LauncherAppWidgetInfo) {
2688 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2689 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2690 packagesToIgnore.add(lawi.providerName.getPackageName());
2691 }
2692 }
2693 }
2694 }
2695 mIconCache.updateDbIcons(packagesToIgnore);
2696 }
2697
Joe Onorato36115782010-06-17 13:28:48 -04002698 private void onlyBindAllApps() {
2699 final Callbacks oldCallbacks = mCallbacks.get();
2700 if (oldCallbacks == null) {
2701 // This launcher has exited and nobody bothered to tell us. Just bail.
2702 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2703 return;
2704 }
2705
2706 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002707 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002708 final ArrayList<AppInfo> list
2709 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002710 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002711 public void run() {
2712 final long t = SystemClock.uptimeMillis();
2713 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2714 if (callbacks != null) {
2715 callbacks.bindAllApplications(list);
2716 }
2717 if (DEBUG_LOADERS) {
2718 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002719 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04002720 }
2721 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002722 };
Tony Wickham80f57872016-06-29 18:12:15 -07002723 runOnMainThread(r);
Joe Onorato36115782010-06-17 13:28:48 -04002724 }
2725
Winson Chung64359a52013-07-08 17:17:08 -07002726 private void loadAllApps() {
2727 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002728
Joe Onorato36115782010-06-17 13:28:48 -04002729 final Callbacks oldCallbacks = mCallbacks.get();
2730 if (oldCallbacks == null) {
2731 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002732 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002733 return;
2734 }
2735
Kenny Guyed131872014-04-30 03:02:21 +01002736 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2737
Winson Chung64359a52013-07-08 17:17:08 -07002738 // Clear the list of apps
2739 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002740 for (UserHandleCompat user : profiles) {
2741 // Query for the set of apps
2742 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002743 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002744 if (DEBUG_LOADERS) {
2745 Log.d(TAG, "getActivityList took "
2746 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2747 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2748 }
2749 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002750 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002751 if (apps == null || apps.isEmpty()) {
2752 return;
2753 }
Kenny Guyff05f432016-01-22 17:48:29 +00002754 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002755 // Create the ApplicationInfos
2756 for (int i = 0; i < apps.size(); i++) {
2757 LauncherActivityInfoCompat app = apps.get(i);
2758 // This builds the icon bitmaps.
Kenny Guyff05f432016-01-22 17:48:29 +00002759 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode));
Kenny Guyed131872014-04-30 03:02:21 +01002760 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002761
Sunny Goyal756a28a2015-04-23 17:07:55 -07002762 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2763 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002764 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002765
2766 @Override
2767 public void run() {
2768 heuristic.processUserApps(apps);
2769 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002770 };
2771 runOnMainThread(new Runnable() {
2772
2773 @Override
2774 public void run() {
2775 // Check isLoadingWorkspace on the UI thread, as it is updated on
2776 // the UI thread.
2777 if (mIsLoadingAndBindingWorkspace) {
2778 synchronized (mBindCompleteRunnables) {
2779 mBindCompleteRunnables.add(r);
2780 }
2781 } else {
2782 runOnWorkerThread(r);
2783 }
2784 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002785 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002786 }
Winson Chung64359a52013-07-08 17:17:08 -07002787 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002788 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002789 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2790 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002791
2792 // Post callback on main thread
2793 mHandler.post(new Runnable() {
2794 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002795
Winson Chung64359a52013-07-08 17:17:08 -07002796 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002797 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002798 if (callbacks != null) {
2799 callbacks.bindAllApplications(added);
2800 if (DEBUG_LOADERS) {
2801 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002802 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07002803 }
2804 } else {
2805 Log.i(TAG, "not binding apps: no Launcher activity");
2806 }
2807 }
2808 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002809 // Cleanup any data stored for a deleted user.
2810 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04002811 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002812 Log.d(TAG, "Icons processed in "
2813 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002814 }
2815 }
2816
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002817 private void loadAndBindDeepShortcuts() {
2818 if (DEBUG_LOADERS) {
2819 Log.d(TAG, "loadAndBindDeepShortcuts mDeepShortcutsLoaded=" + mDeepShortcutsLoaded);
2820 }
2821 if (!mDeepShortcutsLoaded) {
2822 mBgDeepShortcutMap.clear();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002823 mHasShortcutHostPermission = mDeepShortcutManager.hasHostPermission();
2824 if (mHasShortcutHostPermission) {
2825 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
2826 if (mUserManager.isUserUnlocked(user)) {
2827 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager
2828 .queryForAllShortcuts(user);
2829 updateDeepShortcutMap(null, user, shortcuts);
2830 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002831 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002832 }
2833 synchronized (LoaderTask.this) {
2834 if (mStopped) {
2835 return;
2836 }
2837 mDeepShortcutsLoaded = true;
2838 }
2839 }
Tony Wickham80f57872016-06-29 18:12:15 -07002840 bindDeepShortcuts();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002841 }
2842
Joe Onoratobe386092009-11-17 17:32:16 -08002843 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002844 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002845 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002846 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2847 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2848 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2849 }
Joe Onorato36115782010-06-17 13:28:48 -04002850 }
2851 }
2852
Sunny Goyal860538d2016-07-20 12:35:16 -07002853 /**
2854 * Clear all the shortcuts for the given package, and re-add the new shortcuts.
2855 */
2856 private void updateDeepShortcutMap(
2857 String packageName, UserHandleCompat user, List<ShortcutInfoCompat> shortcuts) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002858 if (packageName != null) {
2859 Iterator<ComponentKey> keysIter = mBgDeepShortcutMap.keySet().iterator();
2860 while (keysIter.hasNext()) {
Sunny Goyal860538d2016-07-20 12:35:16 -07002861 ComponentKey next = keysIter.next();
2862 if (next.componentName.getPackageName().equals(packageName)
2863 && next.user.equals(user)) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002864 keysIter.remove();
2865 }
2866 }
2867 }
2868
2869 // Now add the new shortcuts to the map.
2870 for (ShortcutInfoCompat shortcut : shortcuts) {
Tony Wickhamca258b32016-07-28 12:31:28 -07002871 boolean shouldShowInContainer = shortcut.isEnabled()
2872 && (shortcut.isDeclaredInManifest() || shortcut.isDynamic());
2873 if (shouldShowInContainer) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002874 ComponentKey targetComponent
2875 = new ComponentKey(shortcut.getActivity(), shortcut.getUserHandle());
2876 mBgDeepShortcutMap.addToList(targetComponent, shortcut.getId());
2877 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002878 }
2879 }
2880
Tony Wickham80f57872016-06-29 18:12:15 -07002881 public void bindDeepShortcuts() {
Sunny Goyal5f064012016-08-08 10:10:01 -07002882 final MultiHashMap<ComponentKey, String> shortcutMapCopy = mBgDeepShortcutMap.clone();
Tony Wickham80f57872016-06-29 18:12:15 -07002883 Runnable r = new Runnable() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002884 @Override
2885 public void run() {
2886 Callbacks callbacks = getCallback();
2887 if (callbacks != null) {
2888 callbacks.bindDeepShortcutMap(shortcutMapCopy);
2889 }
2890 }
Tony Wickham80f57872016-06-29 18:12:15 -07002891 };
2892 runOnMainThread(r);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002893 }
2894
Sunny Goyal75b0f552015-05-20 21:57:06 -07002895 /**
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002896 * Refreshes the cached shortcuts if the shortcut permission has changed.
2897 * Current implementation simply reloads the workspace, but it can be optimized to
2898 * use partial updates similar to {@link UserManagerCompat}
2899 */
2900 public void refreshShortcutsIfRequired() {
2901 if (Utilities.isNycMR1OrAbove()) {
2902 sWorker.removeCallbacks(mShortcutPermissionCheckRunnable);
2903 sWorker.post(mShortcutPermissionCheckRunnable);
2904 }
2905 }
2906
2907 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -07002908 * Called when the icons for packages have been updated in the icon cache.
2909 */
2910 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2911 final Callbacks callbacks = getCallback();
2912 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2913 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2914
2915 // If any package icon has changed (app was updated while launcher was dead),
2916 // update the corresponding shortcuts.
2917 synchronized (sBgLock) {
2918 for (ItemInfo info : sBgItemsIdMap) {
2919 if (info instanceof ShortcutInfo && user.equals(info.user)
2920 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2921 ShortcutInfo si = (ShortcutInfo) info;
2922 ComponentName cn = si.getTargetComponent();
2923 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2924 si.updateIcon(mIconCache);
2925 updatedShortcuts.add(si);
2926 }
2927 }
2928 }
2929 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2930 }
2931
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002932 bindUpdatedShortcuts(updatedShortcuts, user);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002933
2934 if (!updatedApps.isEmpty()) {
2935 mHandler.post(new Runnable() {
2936
2937 public void run() {
2938 Callbacks cb = getCallback();
2939 if (cb != null && callbacks == cb) {
2940 cb.bindAppsUpdated(updatedApps);
2941 }
2942 }
2943 });
2944 }
2945 }
2946
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002947 private void bindUpdatedShortcuts(
2948 ArrayList<ShortcutInfo> updatedShortcuts, UserHandleCompat user) {
2949 bindUpdatedShortcuts(updatedShortcuts, new ArrayList<ShortcutInfo>(), user);
2950 }
2951
2952 private void bindUpdatedShortcuts(
2953 final ArrayList<ShortcutInfo> updatedShortcuts,
2954 final ArrayList<ShortcutInfo> removedShortcuts,
2955 final UserHandleCompat user) {
2956 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002957 final Callbacks callbacks = getCallback();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002958 mHandler.post(new Runnable() {
2959
2960 public void run() {
2961 Callbacks cb = getCallback();
2962 if (cb != null && callbacks == cb) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002963 cb.bindShortcutsChanged(updatedShortcuts, removedShortcuts, user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002964 }
2965 }
2966 });
2967 }
2968 }
2969
2970 void enqueueItemUpdatedTask(Runnable task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002971 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002972 }
2973
2974 private class PackageUpdatedTask implements Runnable {
2975 int mOp;
2976 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002977 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002978
2979 public static final int OP_NONE = 0;
2980 public static final int OP_ADD = 1;
2981 public static final int OP_UPDATE = 2;
2982 public static final int OP_REMOVE = 3; // uninstlled
2983 public static final int OP_UNAVAILABLE = 4; // external media unmounted
Kenny Guy44cba692016-01-21 19:50:02 +00002984 public static final int OP_SUSPEND = 5; // package suspended
2985 public static final int OP_UNSUSPEND = 6; // package unsuspended
Sunny Goyalda891c12016-03-18 18:29:24 -07002986 public static final int OP_USER_AVAILABILITY_CHANGE = 7; // user available/unavailable
Joe Onorato36115782010-06-17 13:28:48 -04002987
Kenny Guyed131872014-04-30 03:02:21 +01002988 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002989 mOp = op;
2990 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002991 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002992 }
2993
2994 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002995 if (!mHasLoaderCompletedOnce) {
2996 // Loader has not yet run.
2997 return;
2998 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002999 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04003000
3001 final String[] packages = mPackages;
3002 final int N = packages.length;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003003 FlagOp flagOp = FlagOp.NO_OP;
Sunny Goyalda891c12016-03-18 18:29:24 -07003004 StringFilter pkgFilter = StringFilter.of(new HashSet<>(Arrays.asList(packages)));
Joe Onorato36115782010-06-17 13:28:48 -04003005 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003006 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04003007 for (int i=0; i<N; i++) {
3008 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003009 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003010 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003011 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003012
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003013 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3014 if (heuristic != null) {
3015 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003016 }
Joe Onorato36115782010-06-17 13:28:48 -04003017 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003018 }
Joe Onorato36115782010-06-17 13:28:48 -04003019 case OP_UPDATE:
3020 for (int i=0; i<N; i++) {
3021 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003022 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003023 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003024 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003025 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003026 // Since package was just updated, the target must be available now.
3027 flagOp = FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04003028 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003029 case OP_REMOVE: {
3030 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3031 if (heuristic != null) {
3032 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003033 }
Joe Onorato36115782010-06-17 13:28:48 -04003034 for (int i=0; i<N; i++) {
3035 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003036 mIconCache.removeIconsForPkg(packages[i], mUser);
3037 }
3038 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003039 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003040 case OP_UNAVAILABLE:
3041 for (int i=0; i<N; i++) {
3042 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3043 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003044 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003045 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003046 flagOp = FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04003047 break;
Kenny Guy44cba692016-01-21 19:50:02 +00003048 case OP_SUSPEND:
3049 case OP_UNSUSPEND:
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003050 flagOp = mOp == OP_SUSPEND ?
3051 FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED) :
3052 FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED);
Sunny Goyal17763cb2016-03-24 13:53:22 -07003053 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.(un)suspend " + N);
Sunny Goyalda891c12016-03-18 18:29:24 -07003054 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
3055 break;
3056 case OP_USER_AVAILABILITY_CHANGE:
3057 flagOp = UserManagerCompat.getInstance(context).isQuietModeEnabled(mUser)
3058 ? FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER)
3059 : FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER);
3060 // We want to update all packages for this user.
3061 pkgFilter = StringFilter.matchesAll();
3062 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
Kenny Guy44cba692016-01-21 19:50:02 +00003063 break;
Joe Onorato36115782010-06-17 13:28:48 -04003064 }
3065
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003066 ArrayList<AppInfo> added = null;
3067 ArrayList<AppInfo> modified = null;
3068 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003069
Adam Cohen487f7dd2012-06-28 18:12:10 -07003070 if (mBgAllAppsList.added.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003071 added = new ArrayList<>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003072 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003073 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003074 if (mBgAllAppsList.modified.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003075 modified = new ArrayList<>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003076 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003077 }
Winson Chung5d55f332012-07-16 20:45:03 -07003078 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003079 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003080 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003081 }
3082
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003083 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps = new HashMap<>();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003084
Joe Onorato36115782010-06-17 13:28:48 -04003085 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003086 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003087 for (AppInfo ai : added) {
3088 addedOrUpdatedApps.put(ai.componentName, ai);
3089 }
Joe Onorato36115782010-06-17 13:28:48 -04003090 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003091
Joe Onorato36115782010-06-17 13:28:48 -04003092 if (modified != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003093 final Callbacks callbacks = getCallback();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003094 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003095 for (AppInfo ai : modified) {
3096 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003097 }
3098
Joe Onorato36115782010-06-17 13:28:48 -04003099 mHandler.post(new Runnable() {
3100 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003101 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003102 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003103 callbacks.bindAppsUpdated(modifiedFinal);
3104 }
3105 }
3106 });
3107 }
Winson Chung83892cc2013-05-01 16:53:33 -07003108
Sunny Goyal4390ace2014-10-13 11:33:11 -07003109 // Update shortcut infos
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003110 if (mOp == OP_ADD || flagOp != FlagOp.NO_OP) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003111 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3112 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3113 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3114
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003115 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003116 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003117 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3118 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003119 boolean infoUpdated = false;
3120 boolean shortcutUpdated = false;
3121
3122 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003123 if ((si.iconResource != null)
Sunny Goyalda891c12016-03-18 18:29:24 -07003124 && pkgFilter.matches(si.iconResource.packageName)) {
Sunny Goyal10629b02016-09-01 12:50:11 -07003125 Bitmap icon = LauncherIcons.createIconBitmap(
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003126 si.iconResource.packageName,
3127 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003128 if (icon != null) {
3129 si.setIcon(icon);
3130 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003131 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003132 }
3133 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003134
3135 ComponentName cn = si.getTargetComponent();
Sunny Goyalda891c12016-03-18 18:29:24 -07003136 if (cn != null && pkgFilter.matches(cn.getPackageName())) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003137 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3138
3139 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003140 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3141 // Auto install icon
3142 PackageManager pm = context.getPackageManager();
3143 ResolveInfo matched = pm.resolveActivity(
3144 new Intent(Intent.ACTION_MAIN)
3145 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3146 PackageManager.MATCH_DEFAULT_ONLY);
3147 if (matched == null) {
3148 // Try to find the best match activity.
3149 Intent intent = pm.getLaunchIntentForPackage(
3150 cn.getPackageName());
3151 if (intent != null) {
3152 cn = intent.getComponent();
3153 appInfo = addedOrUpdatedApps.get(cn);
3154 }
3155
3156 if ((intent == null) || (appInfo == null)) {
3157 removedShortcuts.add(si);
3158 continue;
3159 }
3160 si.promisedIntent = intent;
3161 }
3162 }
3163
3164 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003165 if (appInfo != null) {
3166 si.flags = appInfo.flags;
3167 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003168
Sunny Goyal756adbc2015-04-16 15:20:51 -07003169 si.intent = si.promisedIntent;
3170 si.promisedIntent = null;
3171 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003172 infoUpdated = true;
3173 si.updateIcon(mIconCache);
3174 }
3175
3176 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3177 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3178 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003179 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003180 si.contentDescription = appInfo.contentDescription;
3181 infoUpdated = true;
3182 }
3183
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003184 int oldDisabledFlags = si.isDisabled;
3185 si.isDisabled = flagOp.apply(si.isDisabled);
3186 if (si.isDisabled != oldDisabledFlags) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003187 shortcutUpdated = true;
3188 }
3189 }
3190
3191 if (infoUpdated || shortcutUpdated) {
3192 updatedShortcuts.add(si);
3193 }
3194 if (infoUpdated) {
3195 updateItemInDatabase(context, si);
3196 }
Sunny Goyalda891c12016-03-18 18:29:24 -07003197 } else if (info instanceof LauncherAppWidgetInfo && mOp == OP_ADD) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003198 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3199 if (mUser.equals(widgetInfo.user)
3200 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
Sunny Goyalda891c12016-03-18 18:29:24 -07003201 && pkgFilter.matches(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003202 widgetInfo.restoreStatus &=
3203 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3204 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003205
3206 // adding this flag ensures that launcher shows 'click to setup'
3207 // if the widget has a config activity. In case there is no config
3208 // activity, it will be marked as 'restored' during bind.
3209 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3210
Sunny Goyal4390ace2014-10-13 11:33:11 -07003211 widgets.add(widgetInfo);
3212 updateItemInDatabase(context, widgetInfo);
3213 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003214 }
3215 }
3216 }
3217
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003218 bindUpdatedShortcuts(updatedShortcuts, removedShortcuts, mUser);
3219 if (!removedShortcuts.isEmpty()) {
3220 deleteItemsFromDatabase(context, removedShortcuts);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003221 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003222
Sunny Goyal4390ace2014-10-13 11:33:11 -07003223 if (!widgets.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003224 final Callbacks callbacks = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003225 mHandler.post(new Runnable() {
3226 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003227 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003228 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003229 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003230 }
3231 }
3232 });
3233 }
3234 }
3235
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003236 final HashSet<String> removedPackages = new HashSet<>();
3237 final HashSet<ComponentName> removedComponents = new HashSet<>();
3238 if (mOp == OP_REMOVE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003239 // Mark all packages in the broadcast to be removed
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003240 Collections.addAll(removedPackages, packages);
3241
3242 // No need to update the removedComponents as
3243 // removedPackages is a super-set of removedComponents
Winson Chungdf95eb12013-10-16 14:57:07 -07003244 } else if (mOp == OP_UPDATE) {
3245 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003246 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003247 if (isPackageDisabled(context, packages[i], mUser)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003248 removedPackages.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003249 }
3250 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003251
3252 // Update removedComponents as some components can get removed during package update
3253 for (AppInfo info : removedApps) {
3254 removedComponents.add(info.componentName);
3255 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003256 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003257
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003258 if (!removedPackages.isEmpty() || !removedComponents.isEmpty()) {
3259 for (String pn : removedPackages) {
3260 deletePackageFromDatabase(context, pn, mUser);
3261 }
3262 for (ComponentName cn : removedComponents) {
3263 deleteItemsFromDatabase(context, getItemInfoForComponentName(cn, mUser));
Sunny Goyal1a745e82014-10-02 15:58:31 -07003264 }
3265
Winson Chungdf95eb12013-10-16 14:57:07 -07003266 // Remove any queued items from the install queue
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003267 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackages, mUser);
3268
Winson Chungdf95eb12013-10-16 14:57:07 -07003269 // Call the components-removed callback
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003270 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003271 mHandler.post(new Runnable() {
3272 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003273 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003274 if (callbacks == cb && cb != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003275 callbacks.bindWorkspaceComponentsRemoved(
3276 removedPackages, removedComponents, mUser);
3277 }
3278 }
3279 });
3280 }
3281
3282 if (!removedApps.isEmpty()) {
3283 // Remove corresponding apps from All-Apps
3284 final Callbacks callbacks = getCallback();
3285 mHandler.post(new Runnable() {
3286 public void run() {
3287 Callbacks cb = getCallback();
3288 if (callbacks == cb && cb != null) {
3289 callbacks.bindAppInfosRemoved(removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04003290 }
3291 }
3292 });
Joe Onoratobe386092009-11-17 17:32:16 -08003293 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003294
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003295 // Notify launcher of widget update. From marshmallow onwards we use AppWidgetHost to
3296 // get widget update signals.
3297 if (!Utilities.ATLEAST_MARSHMALLOW &&
3298 (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003299 final Callbacks callbacks = getCallback();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003300 mHandler.post(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003301 public void run() {
3302 Callbacks cb = getCallback();
3303 if (callbacks == cb && cb != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003304 callbacks.notifyWidgetProvidersChanged();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003305 }
3306 }
3307 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003308 }
3309 }
3310 }
3311
Sunny Goyal10923b32016-07-20 15:42:44 -07003312 /**
3313 * Repopulates the shortcut info, possibly updating any icon already on the workspace.
3314 */
3315 public void updateShortcutInfo(final ShortcutInfoCompat fullDetail, final ShortcutInfo info) {
3316 enqueueItemUpdatedTask(new Runnable() {
3317 @Override
3318 public void run() {
3319 info.updateFromDeepShortcutInfo(
3320 fullDetail, LauncherAppState.getInstance().getContext());
3321 ArrayList<ShortcutInfo> update = new ArrayList<ShortcutInfo>();
3322 update.add(info);
3323 bindUpdatedShortcuts(update, fullDetail.getUserHandle());
3324 }
3325 });
3326 }
3327
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003328 private class ShortcutsChangedTask implements Runnable {
Sunny Goyal50941fb2016-08-04 12:03:52 -07003329 private final String mPackageName;
3330 private final List<ShortcutInfoCompat> mShortcuts;
3331 private final UserHandleCompat mUser;
3332 private final boolean mUpdateIdMap;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003333
3334 public ShortcutsChangedTask(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal50941fb2016-08-04 12:03:52 -07003335 UserHandleCompat user, boolean updateIdMap) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003336 mPackageName = packageName;
3337 mShortcuts = shortcuts;
3338 mUser = user;
Sunny Goyal50941fb2016-08-04 12:03:52 -07003339 mUpdateIdMap = updateIdMap;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003340 }
3341
3342 @Override
3343 public void run() {
3344 mDeepShortcutManager.onShortcutsChanged(mShortcuts);
3345
3346 Map<String, ShortcutInfoCompat> idsToShortcuts = new HashMap<>();
3347 for (ShortcutInfoCompat shortcut : mShortcuts) {
3348 idsToShortcuts.put(shortcut.getId(), shortcut);
3349 }
3350
3351 // Find ShortcutInfo's that have changed on the workspace.
3352 MultiHashMap<String, ShortcutInfo> idsToWorkspaceShortcutInfos = new MultiHashMap<>();
3353 for (ItemInfo itemInfo : sBgItemsIdMap) {
3354 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
3355 ShortcutInfo si = (ShortcutInfo) itemInfo;
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07003356 if (si.getPromisedIntent().getPackage().equals(mPackageName)
3357 && si.user.equals(mUser)) {
Tony Wickham672d07f2016-07-27 18:22:41 -07003358 String shortcutId = si.getDeepShortcutId();
3359 if (idsToShortcuts.containsKey(shortcutId)) {
3360 idsToWorkspaceShortcutInfos.addToList(shortcutId, si);
3361 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003362 }
3363 }
3364 }
3365
3366 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3367 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager.queryForFullDetails(
3368 mPackageName, new ArrayList<>(idsToWorkspaceShortcutInfos.keySet()), mUser);
3369 ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3370 Context context = LauncherAppState.getInstance().getContext();
3371 for (ShortcutInfoCompat fullDetails : shortcuts) {
3372 List<ShortcutInfo> shortcutInfos = idsToWorkspaceShortcutInfos
3373 .get(fullDetails.getId());
3374 for (ShortcutInfo shortcutInfo : shortcutInfos) {
Sunny Goyal9994b2b2016-06-23 14:17:24 -07003375 shortcutInfo.updateFromDeepShortcutInfo(fullDetails, context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003376 updatedShortcutInfos.add(shortcutInfo);
3377 }
3378 }
3379 bindUpdatedShortcuts(updatedShortcutInfos, mUser);
3380
Sunny Goyal50941fb2016-08-04 12:03:52 -07003381 if (mUpdateIdMap) {
3382 // Update the deep shortcut map if the list of ids has changed for an activity.
3383 updateDeepShortcutMap(mPackageName, mUser, mShortcuts);
3384 bindDeepShortcuts();
3385 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003386 }
3387 }
3388
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003389 /**
3390 * Task to handle changing of lock state of the user
3391 */
3392 private class UserLockStateChangedTask implements Runnable {
3393
3394 private final UserHandleCompat mUser;
3395
3396 public UserLockStateChangedTask(UserHandleCompat user) {
3397 mUser = user;
3398 }
3399
3400 @Override
3401 public void run() {
3402 boolean isUserUnlocked = mUserManager.isUserUnlocked(mUser);
3403 Context context = mApp.getContext();
3404
3405 HashMap<ShortcutKey, ShortcutInfoCompat> pinnedShortcuts = new HashMap<>();
3406 if (isUserUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07003407 List<ShortcutInfoCompat> shortcuts =
3408 mDeepShortcutManager.queryForPinnedShortcuts(null, mUser);
3409 if (mDeepShortcutManager.wasLastCallSuccess()) {
3410 for (ShortcutInfoCompat shortcut : shortcuts) {
3411 pinnedShortcuts.put(ShortcutKey.fromInfo(shortcut), shortcut);
3412 }
3413 } else {
3414 // Shortcut manager can fail due to some race condition when the lock state
3415 // changes too frequently. For the purpose of the update,
3416 // consider it as still locked.
3417 isUserUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003418 }
3419 }
3420
3421 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3422 ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3423 ArrayList<ShortcutInfo> deletedShortcutInfos = new ArrayList<>();
3424 for (ItemInfo itemInfo : sBgItemsIdMap) {
3425 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT
3426 && mUser.equals(itemInfo.user)) {
3427 ShortcutInfo si = (ShortcutInfo) itemInfo;
3428 if (isUserUnlocked) {
3429 ShortcutInfoCompat shortcut =
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07003430 pinnedShortcuts.get(ShortcutKey.fromShortcutInfo(si));
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003431 // We couldn't verify the shortcut during loader. If its no longer available
3432 // (probably due to clear data), delete the workspace item as well
3433 if (shortcut == null) {
3434 deletedShortcutInfos.add(si);
3435 continue;
3436 }
3437 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
3438 si.updateFromDeepShortcutInfo(shortcut, context);
3439 } else {
3440 si.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
3441 }
3442 updatedShortcutInfos.add(si);
3443 }
3444 }
3445 bindUpdatedShortcuts(updatedShortcutInfos, deletedShortcutInfos, mUser);
3446 if (!deletedShortcutInfos.isEmpty()) {
3447 deleteItemsFromDatabase(context, deletedShortcutInfos);
3448 }
3449
3450 // Remove shortcut id map for that user
3451 Iterator<ComponentKey> keysIter = mBgDeepShortcutMap.keySet().iterator();
3452 while (keysIter.hasNext()) {
3453 if (keysIter.next().user.equals(mUser)) {
3454 keysIter.remove();
3455 }
3456 }
3457
3458 if (isUserUnlocked) {
3459 updateDeepShortcutMap(null, mUser, mDeepShortcutManager.queryForAllShortcuts(mUser));
3460 }
3461 bindDeepShortcuts();
3462 }
3463 }
3464
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003465 private void bindWidgetsModel(final Callbacks callbacks, final WidgetsModel model) {
3466 mHandler.post(new Runnable() {
3467 @Override
3468 public void run() {
3469 Callbacks cb = getCallback();
3470 if (callbacks == cb && cb != null) {
3471 callbacks.bindWidgetsModel(model);
3472 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003473 }
3474 });
3475 }
3476
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003477 public void refreshAndBindWidgetsAndShortcuts(
3478 final Callbacks callbacks, final boolean bindFirst) {
3479 runOnWorkerThread(new Runnable() {
3480 @Override
3481 public void run() {
3482 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
3483 bindWidgetsModel(callbacks, mBgWidgetsModel.clone());
Sunny Goyal31860582015-09-18 08:38:57 -07003484 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003485 final WidgetsModel model = mBgWidgetsModel.updateAndClone(mApp.getContext());
3486 bindWidgetsModel(callbacks, model);
3487 // update the Widget entries inside DB on the worker thread.
3488 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3489 model.getRawList());
Sunny Goyal31860582015-09-18 08:38:57 -07003490 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003491 });
Michael Jurkac402cd92013-05-20 15:49:32 +02003492 }
3493
Adam Cohen091440a2015-03-18 14:16:05 -07003494 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003495 UserHandleCompat user) {
3496 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3497 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003498 }
Adam Cohen556f6132014-01-15 15:18:08 -08003499
Kenny Guyed131872014-04-30 03:02:21 +01003500 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3501 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003502 if (cn == null) {
3503 return false;
3504 }
Kenny Guyed131872014-04-30 03:02:21 +01003505 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3506 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003507 return false;
3508 }
Kenny Guyed131872014-04-30 03:02:21 +01003509 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003510 }
3511
Adam Cohena28b78e2014-05-20 17:03:04 -07003512 public static boolean isValidPackage(Context context, String packageName,
3513 UserHandleCompat user) {
3514 if (packageName == null) {
3515 return false;
3516 }
3517 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3518 return launcherApps.isPackageEnabledForProfile(packageName, user);
3519 }
3520
Joe Onorato9c1289c2009-08-17 11:03:03 -04003521 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003522 * Make an ShortcutInfo object for a restored application or shortcut item that points
3523 * to a package that is not yet installed on the system.
3524 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003525 public ShortcutInfo getRestoredItemInfo(Cursor c, Intent intent,
3526 int promiseType, int itemType, CursorIconInfo iconInfo) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003527 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003528 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003529
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003530 Bitmap icon = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003531 // the fallback icon
3532 if (icon == null) {
3533 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3534 } else {
3535 info.setIcon(icon);
3536 }
Sunny Goyal34942622014-08-29 17:20:55 -07003537
3538 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003539 String title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07003540 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003541 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003542 }
Sunny Goyal34942622014-08-29 17:20:55 -07003543 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3544 if (TextUtils.isEmpty(info.title)) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003545 info.title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07003546 }
Sunny Goyal34942622014-08-29 17:20:55 -07003547 } else {
3548 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3549 }
3550
Winson Chung82b016c2015-05-08 17:00:10 -07003551 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003552 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003553 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003554 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003555 return info;
3556 }
3557
3558 /**
3559 * Make an Intent object for a restored application or shortcut item that points
3560 * to the market page for the item.
3561 */
Adam Cohen091440a2015-03-18 14:16:05 -07003562 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003563 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003564 return getMarketIntent(componentName.getPackageName());
3565 }
3566
3567 static Intent getMarketIntent(String packageName) {
3568 return new Intent(Intent.ACTION_VIEW)
3569 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003570 .scheme("market")
3571 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003572 .appendQueryParameter("id", packageName)
3573 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003574 }
3575
3576 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003577 * Make an ShortcutInfo object for a shortcut that is an application.
3578 *
3579 * If c is not null, then it will be used to fill in missing data like the title and icon.
3580 */
Sunny Goyald09c3702016-04-06 16:18:20 -07003581 public ShortcutInfo getAppShortcutInfo(Intent intent,
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003582 UserHandleCompat user, Cursor c, CursorIconInfo iconInfo,
Sunny Goyal34b65272015-03-11 16:56:52 -07003583 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003584 if (user == null) {
3585 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003586 return null;
3587 }
3588
Kenny Guyed131872014-04-30 03:02:21 +01003589 ComponentName componentName = intent.getComponent();
3590 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08003591 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01003592 return null;
3593 }
3594
3595 Intent newIntent = new Intent(intent.getAction(), null);
3596 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3597 newIntent.setComponent(componentName);
3598 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003599 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003600 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3601 return null;
3602 }
3603
3604 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003605 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003606 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003607 Bitmap icon = iconInfo.loadIcon(c);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003608 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003609 }
3610
Sunny Goyald09c3702016-04-06 16:18:20 -07003611 if (lai != null && PackageManagerHelper.isAppSuspended(lai.getApplicationInfo())) {
3612 info.isDisabled = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
3613 }
3614
Joe Onorato56d82912010-03-07 14:32:10 -05003615 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003616 if (TextUtils.isEmpty(info.title) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003617 info.title = iconInfo.getTitle(c);
Joe Onorato56d82912010-03-07 14:32:10 -05003618 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003619
Joe Onorato56d82912010-03-07 14:32:10 -05003620 // fall back to the class name of the activity
3621 if (info.title == null) {
3622 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003623 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003624
Joe Onorato9c1289c2009-08-17 11:03:03 -04003625 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003626 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003627 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003628 if (lai != null) {
3629 info.flags = AppInfo.initFlags(lai);
3630 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003631 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003632 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003633
Sunny Goyale2df0622015-04-24 11:27:00 -07003634 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003635 ItemInfoFilter f) {
3636 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3637 for (ItemInfo i : infos) {
3638 if (i instanceof ShortcutInfo) {
3639 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003640 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003641 if (cn != null && f.filterItem(null, info, cn)) {
3642 filtered.add(info);
3643 }
3644 } else if (i instanceof FolderInfo) {
3645 FolderInfo info = (FolderInfo) i;
3646 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003647 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003648 if (cn != null && f.filterItem(info, s, cn)) {
3649 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003650 }
3651 }
Winson Chung64359a52013-07-08 17:17:08 -07003652 } else if (i instanceof LauncherAppWidgetInfo) {
3653 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3654 ComponentName cn = info.providerName;
3655 if (cn != null && f.filterItem(null, info, cn)) {
3656 filtered.add(info);
3657 }
Winson Chung8a435102012-08-30 17:16:53 -07003658 }
3659 }
Winson Chung64359a52013-07-08 17:17:08 -07003660 return new ArrayList<ItemInfo>(filtered);
3661 }
3662
Adam Cohen091440a2015-03-18 14:16:05 -07003663 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003664 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003665 ItemInfoFilter filter = new ItemInfoFilter() {
3666 @Override
3667 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003668 if (info.user == null) {
3669 return cn.equals(cname);
3670 } else {
3671 return cn.equals(cname) && info.user.equals(user);
3672 }
Winson Chung64359a52013-07-08 17:17:08 -07003673 }
3674 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003675 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003676 }
3677
Sunny Goyal1a745e82014-10-02 15:58:31 -07003678 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003679 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003680 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003681 @Thunk ShortcutInfo getShortcutInfo(Cursor c, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003682 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003683 // Non-app shortcuts are only supported for current user.
3684 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003685 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003686
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003687 // TODO: If there's an explicit component and we can't install that, delete it.
3688
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003689 loadInfoFromCursor(info, c, iconInfo);
3690 return info;
3691 }
Joe Onorato56d82912010-03-07 14:32:10 -05003692
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003693 /**
3694 * Make an ShortcutInfo object for a shortcut that isn't an application.
3695 */
3696 public void loadInfoFromCursor(ShortcutInfo info, Cursor c, CursorIconInfo iconInfo) {
3697 info.title = iconInfo.getTitle(c);
3698 Bitmap icon = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003699 // the fallback icon
3700 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003701 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003702 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003703 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003704 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003705 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003706
Sunny Goyal2350bc92014-10-14 16:42:54 -07003707 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003708 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3709 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3710 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3711
Adam Cohend9198822011-11-22 16:42:47 -08003712 if (intent == null) {
3713 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3714 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3715 return null;
3716 }
3717
Joe Onorato0589f0f2010-02-08 13:44:00 -08003718 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003719 ShortcutIconResource iconResource = null;
3720
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003721 if (bitmap instanceof Bitmap) {
Sunny Goyal10629b02016-09-01 12:50:11 -07003722 icon = LauncherIcons.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003723 } else {
3724 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003725 if (extra instanceof ShortcutIconResource) {
3726 iconResource = (ShortcutIconResource) extra;
Sunny Goyal10629b02016-09-01 12:50:11 -07003727 icon = LauncherIcons.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003728 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003729 }
3730 }
3731
Michael Jurkac9d95c52011-08-29 14:03:34 -07003732 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003733
Kenny Guyed131872014-04-30 03:02:21 +01003734 // Only support intents for current user for now. Intents sent from other
3735 // users wouldn't get here without intent forwarding anyway.
3736 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003737 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003738 icon = mIconCache.getDefaultIcon(info.user);
3739 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003740 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003741 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003742
Winson Chung82b016c2015-05-08 17:00:10 -07003743 info.title = Utilities.trim(name);
3744 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003745 info.intent = intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003746 info.iconResource = iconResource;
3747
3748 return info;
3749 }
3750
Joe Onorato9c1289c2009-08-17 11:03:03 -04003751 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003752 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003753 * or make a new one.
3754 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003755 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003756 // See if a placeholder was created for us already
3757 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003758 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003759 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003760 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003761 folders.put(id, folderInfo);
3762 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003763 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003764 }
3765
Joe Onoratobe386092009-11-17 17:32:16 -08003766
Sunny Goyal651077b2014-06-30 14:15:31 -07003767 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3768 return (provider != null) && (provider.provider != null)
3769 && (provider.provider.getPackageName() != null);
3770 }
3771
Joe Onoratobe386092009-11-17 17:32:16 -08003772 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003773 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003774 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3775 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3776 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3777 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003778 if (mLoaderTask != null) {
3779 mLoaderTask.dumpState();
3780 } else {
3781 Log.d(TAG, "mLoaderTask=null");
3782 }
Joe Onoratobe386092009-11-17 17:32:16 -08003783 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003784
3785 public Callbacks getCallback() {
3786 return mCallbacks != null ? mCallbacks.get() : null;
3787 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003788
3789 /**
3790 * @return {@link FolderInfo} if its already loaded.
3791 */
3792 public FolderInfo findFolderById(Long folderId) {
3793 synchronized (sBgLock) {
3794 return sBgFolders.get(folderId);
3795 }
3796 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003797
Sunny Goyal527c7d32015-08-28 15:19:36 -07003798 @Thunk class DeferredMainThreadExecutor implements Executor {
3799
3800 @Override
3801 public void execute(Runnable command) {
3802 runOnMainThread(command);
3803 }
3804 }
3805
Sunny Goyal756adbc2015-04-16 15:20:51 -07003806 /**
3807 * @return the looper for the worker thread which can be used to start background tasks.
3808 */
3809 public static Looper getWorkerLooper() {
3810 return sWorkerThread.getLooper();
3811 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003812}