blob: d66ce6b91d16a146123a392b31b7d1a1a7131a40 [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 Goyal1acb9e92016-05-16 12:41:09 -070060import com.android.launcher3.logging.FileLog;
Sunny Goyalf862a262015-12-14 14:27:38 -080061import com.android.launcher3.model.GridSizeMigrationTask;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070062import com.android.launcher3.model.WidgetsModel;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -070063import com.android.launcher3.provider.ImportDataTask;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070064import com.android.launcher3.provider.LauncherDbUtils;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070065import com.android.launcher3.shortcuts.DeepShortcutManager;
66import com.android.launcher3.shortcuts.ShortcutInfoCompat;
67import com.android.launcher3.shortcuts.ShortcutKey;
Robin Lee26ace122015-03-16 19:41:43 +000068import com.android.launcher3.util.ComponentKey;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070069import com.android.launcher3.util.CursorIconInfo;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -070070import com.android.launcher3.util.FlagOp;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070071import com.android.launcher3.util.GridOccupancy;
Sunny Goyale2df0622015-04-24 11:27:00 -070072import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070073import com.android.launcher3.util.ManagedProfileHeuristic;
Tony Wickhamd82a39d2016-07-01 15:44:13 -070074import com.android.launcher3.util.MultiHashMap;
Sunny Goyald09c3702016-04-06 16:18:20 -070075import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070076import com.android.launcher3.util.Preconditions;
Sunny Goyalda891c12016-03-18 18:29:24 -070077import com.android.launcher3.util.StringFilter;
Adam Cohen091440a2015-03-18 14:16:05 -070078import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070079import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080080
Michael Jurkac2f801e2011-07-12 14:19:46 -070081import java.lang.ref.WeakReference;
82import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070083import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070084import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070085import java.util.Arrays;
Michael Jurkac2f801e2011-07-12 14:19:46 -070086import java.util.Collections;
87import java.util.Comparator;
88import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070089import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070090import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070091import java.util.List;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070092import java.util.Map;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070093import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070094import java.util.Set;
Sunny Goyal527c7d32015-08-28 15:19:36 -070095import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -070096
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097/**
98 * Maintains in-memory state of the Launcher. It is expected that there should be only one
99 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700100 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101 */
Kenny Guyed131872014-04-30 03:02:21 +0100102public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +0100103 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800104 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -0400105 private static final boolean DEBUG_RECEIVER = false;
Chris Wrenb358f812014-04-16 13:37:00 -0400106
Joe Onorato9c1289c2009-08-17 11:03:03 -0400107 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700108
Joe Onorato36115782010-06-17 13:28:48 -0400109 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500110 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800111
Adam Cohen091440a2015-03-18 14:16:05 -0700112 @Thunk final LauncherAppState mApp;
113 @Thunk final Object mLock = new Object();
114 @Thunk DeferredHandler mHandler = new DeferredHandler();
115 @Thunk LoaderTask mLoaderTask;
116 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700117 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118
Adam Cohen091440a2015-03-18 14:16:05 -0700119 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700120 static {
121 sWorkerThread.start();
122 }
Adam Cohen091440a2015-03-18 14:16:05 -0700123 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700124
Joe Onoratocc67f472010-06-08 10:54:30 -0700125 // We start off with everything not loaded. After that, we assume that
126 // our monitoring of the package manager provides all updates and we never
127 // need to do a requery. These are only ever touched from the loader thread.
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700128 private boolean mWorkspaceLoaded;
129 private boolean mAllAppsLoaded;
130 private boolean mDeepShortcutsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700131
Sunny Goyal756a28a2015-04-23 17:07:55 -0700132 /**
133 * Set of runnables to be called on the background thread after the workspace binding
134 * is complete.
135 */
136 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
137
Adam Cohen091440a2015-03-18 14:16:05 -0700138 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700140 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800141 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700142 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800143 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800144
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700145 // Maps all launcher activities to the id's of their shortcuts (if they have any).
146 private final MultiHashMap<ComponentKey, String> mBgDeepShortcutMap = new MultiHashMap<>();
147
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700148 private boolean mHasShortcutHostPermission;
149 // Runnable to check if the shortcuts permission has changed.
150 private final Runnable mShortcutPermissionCheckRunnable = new Runnable() {
151 @Override
152 public void run() {
153 if (mDeepShortcutsLoaded) {
154 boolean hasShortcutHostPermission = mDeepShortcutManager.hasHostPermission();
155 if (hasShortcutHostPermission != mHasShortcutHostPermission) {
156 mApp.reloadWorkspace();
157 }
158 }
159 }
160 };
161
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700162 // The lock that must be acquired before referencing any static bg data structures. Unlike
163 // other locks, this one can generally be held long-term because we never expect any of these
164 // static data structures to be referenced outside of the worker thread except on the first
165 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700166 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700167
Adam Cohen487f7dd2012-06-28 18:12:10 -0700168 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700169 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700170 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700171
Adam Cohen487f7dd2012-06-28 18:12:10 -0700172 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
173 // created by LauncherModel that are directly on the home screen (however, no widgets or
174 // shortcuts within folders).
175 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700176
Adam Cohen487f7dd2012-06-28 18:12:10 -0700177 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
178 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700179 new ArrayList<LauncherAppWidgetInfo>();
180
Adam Cohen487f7dd2012-06-28 18:12:10 -0700181 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700182 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700183
Adam Cohendcd297f2013-06-18 13:13:40 -0700184 // sBgWorkspaceScreens is the ordered set of workspace screens.
185 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
186
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700187 // sBgPinnedShortcutCounts is the ComponentKey representing a pinned shortcut to the number of
188 // times it is pinned.
189 static final Map<ShortcutKey, MutableInt> sBgPinnedShortcutCounts = new HashMap<>();
190
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700191 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700192 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
193 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700194
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700195 // </ only access in worker thread >
196
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700197 private IconCache mIconCache;
198 private DeepShortcutManager mDeepShortcutManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700200 private final LauncherAppsCompat mLauncherApps;
201 private final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100202
Joe Onorato9c1289c2009-08-17 11:03:03 -0400203 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700204 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400205 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700206 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400207 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700208 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
209 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700210 public void bindScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700211 public void finishFirstPageBind(ViewOnDrawExecutor executor);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800212 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400213 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200214 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700215 public void bindAppsAdded(ArrayList<Long> newScreens,
216 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700217 ArrayList<ItemInfo> addAnimated,
218 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200219 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700220 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
221 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
222 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700223 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700224 public void bindWorkspaceComponentsRemoved(
225 HashSet<String> packageNames, HashSet<ComponentName> components,
226 UserHandleCompat user);
227 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800228 public void notifyWidgetProvidersChanged();
229 public void bindWidgetsModel(WidgetsModel model);
Adam Cohen1462de32012-07-24 22:34:36 -0700230 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700231 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700232 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMap);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400233 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800234
Winson Chung64359a52013-07-08 17:17:08 -0700235 public interface ItemInfoFilter {
236 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
237 }
238
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700239 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter,
240 DeepShortcutManager deepShortcutManager) {
Winson Chunga6945242014-01-08 14:04:34 -0800241 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400242 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100243 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700244 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800245 mIconCache = iconCache;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700246 mDeepShortcutManager = deepShortcutManager;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800247
Kenny Guyed131872014-04-30 03:02:21 +0100248 mLauncherApps = LauncherAppsCompat.getInstance(context);
249 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800250 }
251
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700252 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
253 * posted on the main thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700254 private void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700255 if (sWorkerThread.getThreadId() == Process.myTid()) {
256 // If we are on the worker thread, post onto the main handler
257 mHandler.post(r);
258 } else {
259 r.run();
260 }
261 }
262
263 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
264 * posted on the worker thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700265 private static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700266 if (sWorkerThread.getThreadId() == Process.myTid()) {
267 r.run();
268 } else {
269 // If we are not on the worker thread, then post to the worker handler
270 sWorker.post(r);
271 }
272 }
273
Sunny Goyal756adbc2015-04-16 15:20:51 -0700274 public void setPackageState(final PackageInstallInfo installInfo) {
275 Runnable updateRunnable = new Runnable() {
276
277 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500278 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700279 synchronized (sBgLock) {
280 final HashSet<ItemInfo> updates = new HashSet<>();
281
282 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
283 // Ignore install success events as they are handled by Package add events.
284 return;
285 }
286
Sunny Goyale2df0622015-04-24 11:27:00 -0700287 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700288 if (info instanceof ShortcutInfo) {
289 ShortcutInfo si = (ShortcutInfo) info;
290 ComponentName cn = si.getTargetComponent();
291 if (si.isPromise() && (cn != null)
292 && installInfo.packageName.equals(cn.getPackageName())) {
293 si.setInstallProgress(installInfo.progress);
294
295 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
296 // Mark this info as broken.
297 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
298 }
299 updates.add(si);
300 }
301 }
302 }
303
304 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
305 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
306 widget.installProgress = installInfo.progress;
307 updates.add(widget);
308 }
309 }
310
311 if (!updates.isEmpty()) {
312 // Push changes to the callback.
313 Runnable r = new Runnable() {
314 public void run() {
315 Callbacks callbacks = getCallback();
316 if (callbacks != null) {
317 callbacks.bindRestoreItemsChange(updates);
318 }
319 }
320 };
321 mHandler.post(r);
322 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500323 }
324 }
325 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700326 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500327 }
328
Sunny Goyal756adbc2015-04-16 15:20:51 -0700329 /**
330 * Updates the icons and label of all pending icons for the provided package name.
331 */
332 public void updateSessionDisplayInfo(final String packageName) {
333 Runnable updateRunnable = new Runnable() {
334
335 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700336 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700337 synchronized (sBgLock) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700338 ArrayList<ShortcutInfo> updates = new ArrayList<>();
339 UserHandleCompat user = UserHandleCompat.myUserHandle();
Sunny Goyal756adbc2015-04-16 15:20:51 -0700340
Sunny Goyale2df0622015-04-24 11:27:00 -0700341 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700342 if (info instanceof ShortcutInfo) {
343 ShortcutInfo si = (ShortcutInfo) info;
344 ComponentName cn = si.getTargetComponent();
345 if (si.isPromise() && (cn != null)
346 && packageName.equals(cn.getPackageName())) {
347 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
348 // For auto install apps update the icon as well as label.
349 mIconCache.getTitleAndIcon(si,
350 si.promisedIntent, user,
351 si.shouldUseLowResIcon());
352 } else {
353 // Only update the icon for restored apps.
354 si.updateIcon(mIconCache);
355 }
356 updates.add(si);
357 }
358 }
359 }
360
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700361 bindUpdatedShortcuts(updates, user);
Sunny Goyala22666f2014-09-18 13:25:15 -0700362 }
363 }
364 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700365 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700366 }
367
Adam Cohen76a47a12014-02-05 11:47:43 -0800368 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800369 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800370
371 if (allAppsApps == null) {
372 throw new RuntimeException("allAppsApps must not be null");
373 }
374 if (allAppsApps.isEmpty()) {
375 return;
376 }
377
378 // Process the newly added applications and add them to the database first
379 Runnable r = new Runnable() {
380 public void run() {
381 runOnMainThread(new Runnable() {
382 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800383 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800384 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500385 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800386 }
387 }
388 });
389 }
390 };
391 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700392 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800393
Sunny Goyala9116722015-04-29 13:55:58 -0700394 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800395 int[] xy, int spanX, int spanY) {
396 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700397 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700398
399 GridOccupancy occupied = new GridOccupancy(profile.numColumns, profile.numRows);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800400 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700401 for (ItemInfo r : occupiedPos) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700402 occupied.markCells(r, true);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800403 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800404 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700405 return occupied.findVacantCell(xy, spanX, spanY);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800406 }
407
408 /**
409 * Find a position on the screen for the given size or adds a new screen.
410 * @return screenId and the coordinates for the item.
411 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700412 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyalc3642d42015-10-30 10:27:08 -0700413 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800414 ArrayList<Long> workspaceScreens,
415 ArrayList<Long> addedWorkspaceScreensFinal,
416 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700417 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800418
Sunny Goyala9116722015-04-29 13:55:58 -0700419 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700420 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700421 synchronized (sBgLock) {
422 for (ItemInfo info : sBgItemsIdMap) {
423 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
424 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
425 if (items == null) {
426 items = new ArrayList<>();
427 screenItems.put(info.screenId, items);
428 }
429 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800430 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800431 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800432 }
433
434 // Find appropriate space for the item.
435 long screenId = 0;
436 int[] cordinates = new int[2];
437 boolean found = false;
438
439 int screenCount = workspaceScreens.size();
440 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700441 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800442 if (preferredScreenIndex < screenCount) {
443 screenId = workspaceScreens.get(preferredScreenIndex);
444 found = findNextAvailableIconSpaceInScreen(
445 screenItems.get(screenId), cordinates, spanX, spanY);
446 }
447
448 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700449 // Search on any of the screens starting from the first screen.
450 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800451 screenId = workspaceScreens.get(screen);
452 if (findNextAvailableIconSpaceInScreen(
453 screenItems.get(screenId), cordinates, spanX, spanY)) {
454 // We found a space for it
455 found = true;
456 break;
457 }
458 }
459 }
460
461 if (!found) {
462 // Still no position found. Add a new screen to the end.
Sunny Goyald2497482015-09-22 18:24:19 -0700463 screenId = LauncherSettings.Settings.call(context.getContentResolver(),
464 LauncherSettings.Settings.METHOD_NEW_SCREEN_ID)
465 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800466
467 // Save the screen id for binding in the workspace
468 workspaceScreens.add(screenId);
469 addedWorkspaceScreensFinal.add(screenId);
470
471 // If we still can't find an empty space, then God help us all!!!
472 if (!findNextAvailableIconSpaceInScreen(
473 screenItems.get(screenId), cordinates, spanX, spanY)) {
474 throw new RuntimeException("Can't find space to add the item");
475 }
476 }
477 return Pair.create(screenId, cordinates);
478 }
479
480 /**
481 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800482 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700483 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700484 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800485 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800486 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700487 return;
Winson Chung997a9232013-07-24 15:33:46 -0700488 }
Winson Chung64359a52013-07-08 17:17:08 -0700489 // Process the newly added applications and add them to the database first
490 Runnable r = new Runnable() {
491 public void run() {
492 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
493 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
494
Winson Chung76828c82013-08-19 15:43:29 -0700495 // Get the list of workspace screens. We need to append to this list and
496 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
497 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800498 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700499 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800500 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700501 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800502 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700503 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800504 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700505 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800506 }
Winson Chung76828c82013-08-19 15:43:29 -0700507
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800508 // Find appropriate space for the item.
Sunny Goyalc3642d42015-10-30 10:27:08 -0700509 Pair<Long, int[]> coords = findSpaceForItem(context,
Sunny Goyal9eba1fd2015-10-16 08:58:57 -0700510 workspaceScreens, addedWorkspaceScreensFinal, 1, 1);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800511 long screenId = coords.first;
512 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700513
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700514 ItemInfo itemInfo;
515 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
516 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800517 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700518 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700519 } else {
520 throw new RuntimeException("Unexpected info type");
521 }
Winson Chung94d67682013-09-25 16:29:40 -0700522
Winson Chung64359a52013-07-08 17:17:08 -0700523 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700524 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700525 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700526 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700527 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700528 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700529 }
530 }
531
Winson Chung76828c82013-08-19 15:43:29 -0700532 // Update the workspace screens
533 updateWorkspaceScreenOrder(context, workspaceScreens);
534
Adam Cohen76a47a12014-02-05 11:47:43 -0800535 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700536 runOnMainThread(new Runnable() {
537 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800538 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700539 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700540 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
541 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700542 if (!addedShortcutsFinal.isEmpty()) {
543 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
544 long lastScreenId = info.screenId;
545 for (ItemInfo i : addedShortcutsFinal) {
546 if (i.screenId == lastScreenId) {
547 addAnimated.add(i);
548 } else {
549 addNotAnimated.add(i);
550 }
Winson Chung997a9232013-07-24 15:33:46 -0700551 }
552 }
Winson Chungd64d1762013-08-20 14:37:16 -0700553 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800554 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700555 }
Winson Chung64359a52013-07-08 17:17:08 -0700556 }
Winson Chung997a9232013-07-24 15:33:46 -0700557 });
558 }
Winson Chung64359a52013-07-08 17:17:08 -0700559 }
560 };
561 runOnWorkerThread(r);
562 }
563
Joe Onorato9c1289c2009-08-17 11:03:03 -0400564 /**
565 * Adds an item to the DB if it was not created previously, or move it to a new
566 * <container, screen, cellX, cellY>
567 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800568 public static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700569 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400570 if (item.container == ItemInfo.NO_ID) {
571 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700572 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400573 } else {
574 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700575 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800576 }
577 }
578
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700579 static void checkItemInfoLocked(
580 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
581 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
582 if (modelItem != null && item != modelItem) {
583 // check all the data is consistent
584 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
585 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
586 ShortcutInfo shortcut = (ShortcutInfo) item;
587 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
588 modelShortcut.intent.filterEquals(shortcut.intent) &&
589 modelShortcut.id == shortcut.id &&
590 modelShortcut.itemType == shortcut.itemType &&
591 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700592 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700593 modelShortcut.cellX == shortcut.cellX &&
594 modelShortcut.cellY == shortcut.cellY &&
595 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700596 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700597 // For all intents and purposes, this is the same object
598 return;
599 }
600 }
601
602 // the modelItem needs to match up perfectly with item if our model is
603 // to be consistent with the database-- for now, just require
604 // modelItem == item or the equality check above
605 String msg = "item: " + ((item != null) ? item.toString() : "null") +
606 "modelItem: " +
607 ((modelItem != null) ? modelItem.toString() : "null") +
608 "Error: ItemInfo passed to checkItemInfo doesn't match original";
609 RuntimeException e = new RuntimeException(msg);
610 if (stackTrace != null) {
611 e.setStackTrace(stackTrace);
612 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800613 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700614 }
615 }
616
Michael Jurka816474f2012-06-25 14:49:02 -0700617 static void checkItemInfo(final ItemInfo item) {
618 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
619 final long itemId = item.id;
620 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700621 public void run() {
622 synchronized (sBgLock) {
623 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700624 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700625 }
626 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700627 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700628 }
629
Michael Jurkac9d95c52011-08-29 14:03:34 -0700630 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
631 final ItemInfo item, final String callingFunction) {
632 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700633 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700634 final ContentResolver cr = context.getContentResolver();
635
Adam Cohen487f7dd2012-06-28 18:12:10 -0700636 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700637 Runnable r = new Runnable() {
638 public void run() {
639 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700640 updateItemArrays(item, itemId, stackTrace);
641 }
642 };
643 runOnWorkerThread(r);
644 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700645
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700646 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
647 final ArrayList<ItemInfo> items, final String callingFunction) {
648 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700649
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700650 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
651 Runnable r = new Runnable() {
652 public void run() {
653 ArrayList<ContentProviderOperation> ops =
654 new ArrayList<ContentProviderOperation>();
655 int count = items.size();
656 for (int i = 0; i < count; i++) {
657 ItemInfo item = items.get(i);
658 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700659 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700660 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700661
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700662 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
663 updateItemArrays(item, itemId, stackTrace);
664
665 }
666 try {
667 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
668 } catch (Exception e) {
669 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700670 }
671 }
672 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700673 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700674 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700675
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700676 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
677 // Lock on mBgLock *after* the db operation
678 synchronized (sBgLock) {
679 checkItemInfoLocked(itemId, item, stackTrace);
680
681 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
682 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
683 // Item is in a folder, make sure this folder exists
684 if (!sBgFolders.containsKey(item.container)) {
685 // An items container is being set to a that of an item which is not in
686 // the list of Folders.
687 String msg = "item: " + item + " container being set to: " +
688 item.container + ", not in the list of folders";
689 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700690 }
691 }
692
693 // Items are added/removed from the corresponding FolderInfo elsewhere, such
694 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
695 // that are on the desktop, as appropriate
696 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800697 if (modelItem != null &&
698 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
699 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700700 switch (modelItem.itemType) {
701 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
702 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700703 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700704 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
705 if (!sBgWorkspaceItems.contains(modelItem)) {
706 sBgWorkspaceItems.add(modelItem);
707 }
708 break;
709 default:
710 break;
711 }
712 } else {
713 sBgWorkspaceItems.remove(modelItem);
714 }
715 }
716 }
717
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800718 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400719 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700720 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700721 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700722 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400723 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400724 item.cellX = cellX;
725 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700726
Winson Chung3d503fb2011-07-13 17:25:49 -0700727 // We store hotseat items in canonical form which is this orientation invariant position
728 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700729 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700730 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700731 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700732 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700733 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700734 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400735
736 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400737 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700738 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
739 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800740 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700741 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400742
Michael Jurkac9d95c52011-08-29 14:03:34 -0700743 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700744 }
745
746 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700747 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
748 * cellX, cellY have already been updated on the ItemInfos.
749 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800750 public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700751 final long container, final int screen) {
752
753 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
754 int count = items.size();
755
756 for (int i = 0; i < count; i++) {
757 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700758 item.container = container;
759
760 // We store hotseat items in canonical form which is this orientation invariant position
761 // in the hotseat
762 if (context instanceof Launcher && screen < 0 &&
763 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700764 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700765 item.cellY);
766 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700767 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700768 }
769
770 final ContentValues values = new ContentValues();
771 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
772 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
773 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800774 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700775 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700776
777 contentValues.add(values);
778 }
779 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
780 }
781
782 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700783 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800784 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700785 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700786 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700787 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800788 item.cellX = cellX;
789 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700790 item.spanX = spanX;
791 item.spanY = spanY;
792
793 // We store hotseat items in canonical form which is this orientation invariant position
794 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700795 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700796 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700797 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700798 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700799 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700800 }
Adam Cohend4844c32011-02-18 19:25:06 -0800801
Adam Cohend4844c32011-02-18 19:25:06 -0800802 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800803 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700804 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
805 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800806 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700807 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
808 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700809 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800810
Michael Jurka816474f2012-06-25 14:49:02 -0700811 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700812 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700813
814 /**
815 * Update an item to the database in a specified container.
816 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700817 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700818 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100819 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700820 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800821 }
822
Sunny Goyal756a28a2015-04-23 17:07:55 -0700823 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700824 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700825 synchronized (mLock) {
826 if (!mHasLoaderCompletedOnce ||
827 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
828 throw new RuntimeException("Trying to add shortcut while loader is running");
829 }
830 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700831 }
832 }
833
Adam Cohend4844c32011-02-18 19:25:06 -0800834 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700835 * Returns true if the shortcuts already exists on the workspace. This must be called after
836 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800837 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700838 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
839 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700840 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700841 if (intent.getComponent() != null) {
842 // If component is not null, an intent with null package will produce
843 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700844 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700845 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700846 intentWithPkg = intent.toUri(0);
847 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700848 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700849 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
850 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700851 }
852 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700853 intentWithPkg = intent.toUri(0);
854 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700855 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700856
857 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700858 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700859 if (item instanceof ShortcutInfo) {
860 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700861 Intent targetIntent = info.promisedIntent == null
862 ? info.intent : info.promisedIntent;
863 if (targetIntent != null && info.user.equals(user)) {
Sunny Goyalcbfe71d2016-08-23 13:25:58 -0700864 Intent copyIntent = new Intent(targetIntent);
865 copyIntent.setSourceBounds(intent.getSourceBounds());
866 String s = copyIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700867 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
868 return true;
869 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700870 }
871 }
872 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800873 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700874 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700875 }
876
Joe Onorato9c1289c2009-08-17 11:03:03 -0400877 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400878 * Add an item to the database in a specified container. Sets the container, screen, cellX and
879 * cellY fields of the item. Also assigns an ID to the item.
880 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700881 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700882 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400883 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400884 item.cellX = cellX;
885 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700886 // We store hotseat items in canonical form which is this orientation invariant position
887 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700888 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700889 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700890 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700891 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700892 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700893 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400894
895 final ContentValues values = new ContentValues();
896 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100897 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400898
Sunny Goyald2497482015-09-22 18:24:19 -0700899 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
900 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
901
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700902 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700903
Jason Monk8e19cf22014-03-20 15:06:57 -0400904 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700905 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700906 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700907 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400908
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700909 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700910 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400911 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700912 sBgItemsIdMap.put(item.id, item);
913 switch (item.itemType) {
914 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
915 sBgFolders.put(item.id, (FolderInfo) item);
916 // Fall through
917 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
918 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700919 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700920 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
921 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
922 sBgWorkspaceItems.add(item);
923 } else {
924 if (!sBgFolders.containsKey(item.container)) {
925 // Adding an item to a folder that doesn't exist.
926 String msg = "adding item: " + item + " to a folder that " +
927 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700928 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700929 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700930 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700931 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700932 incrementPinnedShortcutCount(
Tony Wickhamfc02c1b2016-08-29 15:17:48 -0700933 ShortcutKey.fromShortcutInfo((ShortcutInfo) item),
934 true /* shouldPin */);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700935 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700936 break;
937 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
938 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
939 break;
940 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700941 }
942 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700943 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700944 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700945 }
946
Sunny Goyal34942622014-08-29 17:20:55 -0700947 private static ArrayList<ItemInfo> getItemsByPackageName(
948 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700949 ItemInfoFilter filter = new ItemInfoFilter() {
950 @Override
951 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
952 return cn.getPackageName().equals(pn) && info.user.equals(user);
953 }
954 };
Sunny Goyale2df0622015-04-24 11:27:00 -0700955 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -0700956 }
957
958 /**
959 * Removes all the items from the database corresponding to the specified package.
960 */
961 static void deletePackageFromDatabase(Context context, final String pn,
962 final UserHandleCompat user) {
963 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700964 }
965
966 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700967 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -0400968 */
Sunny Goyalfa401a12015-04-10 13:45:42 -0700969 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700970 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
971 items.add(item);
972 deleteItemsFromDatabase(context, items);
973 }
974
975 /**
976 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700977 */
Sunny Goyal4390ace2014-10-13 11:33:11 -0700978 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400979 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -0700980 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700981 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700982 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700983 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700984 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700985
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700986 // Lock on mBgLock *after* the db operation
987 synchronized (sBgLock) {
988 switch (item.itemType) {
989 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
990 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -0700991 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700992 if (info.container == item.id) {
993 // We are deleting a folder which still contains items that
994 // think they are contained by that folder.
995 String msg = "deleting a folder (" + item + ") which still " +
996 "contains items (" + info + ")";
997 Log.e(TAG, msg);
998 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700999 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001000 sBgWorkspaceItems.remove(item);
1001 break;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001002 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07001003 decrementPinnedShortcutCount(ShortcutKey.fromShortcutInfo(
1004 (ShortcutInfo) item));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001005 // Fall through.
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001006 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1007 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1008 sBgWorkspaceItems.remove(item);
1009 break;
1010 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1011 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1012 break;
1013 }
1014 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001015 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001016 }
1017 }
Michael Jurka83df1882011-08-31 20:59:26 -07001018 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001019 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001020 }
1021
1022 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001023 * Decrement the count for the given pinned shortcut, unpinning it if the count becomes 0.
1024 */
1025 private static void decrementPinnedShortcutCount(final ShortcutKey pinnedShortcut) {
1026 synchronized (sBgLock) {
1027 MutableInt count = sBgPinnedShortcutCounts.get(pinnedShortcut);
1028 if (count == null || --count.value == 0) {
1029 LauncherAppState.getInstance().getShortcutManager().unpinShortcut(pinnedShortcut);
1030 }
1031 }
1032 }
1033
1034 /**
1035 * Increment the count for the given shortcut, pinning it if the count becomes 1.
1036 *
1037 * As an optimization, the caller can pass shouldPin == false to avoid
1038 * unnecessary RPC's if the shortcut is already pinned.
1039 */
1040 private static void incrementPinnedShortcutCount(ShortcutKey pinnedShortcut, boolean shouldPin) {
1041 synchronized (sBgLock) {
1042 MutableInt count = sBgPinnedShortcutCounts.get(pinnedShortcut);
1043 if (count == null) {
1044 count = new MutableInt(1);
1045 sBgPinnedShortcutCounts.put(pinnedShortcut, count);
1046 } else {
1047 count.value++;
1048 }
1049 if (shouldPin && count.value == 1) {
1050 LauncherAppState.getInstance().getShortcutManager().pinShortcut(pinnedShortcut);
1051 }
1052 }
1053 }
1054
1055 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001056 * Update the order of the workspace screens in the database. The array list contains
1057 * a list of screen ids in the order that they should appear.
1058 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001059 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001060 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001061 final ContentResolver cr = context.getContentResolver();
1062 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1063
1064 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001065 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001066 while (iter.hasNext()) {
1067 long id = iter.next();
1068 if (id < 0) {
1069 iter.remove();
1070 }
1071 }
1072
1073 Runnable r = new Runnable() {
1074 @Override
1075 public void run() {
Yura085c8532014-02-11 15:15:29 +00001076 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001077 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001078 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001079 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001080 for (int i = 0; i < count; i++) {
1081 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001082 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001083 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1084 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001085 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001086 }
Yura085c8532014-02-11 15:15:29 +00001087
1088 try {
1089 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1090 } catch (Exception ex) {
1091 throw new RuntimeException(ex);
1092 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001093
Winson Chungba9c37f2013-08-30 14:11:37 -07001094 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001095 sBgWorkspaceScreens.clear();
1096 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001097 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001098 }
1099 };
1100 runOnWorkerThread(r);
1101 }
1102
1103 /**
Winsonc0b52fe2015-09-09 16:38:15 -07001104 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001105 */
Winsonc0b52fe2015-09-09 16:38:15 -07001106 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001107 final ContentResolver cr = context.getContentResolver();
1108
Michael Jurkac9d95c52011-08-29 14:03:34 -07001109 Runnable r = new Runnable() {
1110 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001111 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001112 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001113 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001114 sBgItemsIdMap.remove(info.id);
1115 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001116 sBgWorkspaceItems.remove(info);
1117 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001118
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001119 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001120 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001121 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001122 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001123 for (ItemInfo childInfo : info.contents) {
1124 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001125 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001126 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001127 }
1128 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001129 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001130 }
1131
1132 /**
1133 * Set this as the current Launcher activity object for the loader.
1134 */
1135 public void initialize(Callbacks callbacks) {
1136 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001137 Preconditions.assertUIThread();
1138 // Remove any queued UI runnables
1139 mHandler.cancelAll();
1140 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001141 }
1142 }
1143
Kenny Guyed131872014-04-30 03:02:21 +01001144 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001145 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001146 int op = PackageUpdatedTask.OP_UPDATE;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001147 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001148 user));
1149 }
1150
1151 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001152 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001153 int op = PackageUpdatedTask.OP_REMOVE;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001154 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001155 user));
1156 }
1157
1158 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001159 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001160 int op = PackageUpdatedTask.OP_ADD;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001161 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001162 user));
1163 }
1164
1165 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001166 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001167 boolean replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001168 enqueueItemUpdatedTask(
Sunny Goyal144154d2016-03-30 16:47:03 -07001169 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, packageNames, user));
Kenny Guyed131872014-04-30 03:02:21 +01001170 }
1171
1172 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001173 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001174 boolean replacing) {
1175 if (!replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001176 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guyed131872014-04-30 03:02:21 +01001177 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1178 user));
1179 }
Kenny Guyed131872014-04-30 03:02:21 +01001180 }
1181
Kenny Guy44cba692016-01-21 19:50:02 +00001182 @Override
1183 public void onPackagesSuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001184 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001185 PackageUpdatedTask.OP_SUSPEND, packageNames,
1186 user));
1187 }
1188
1189 @Override
1190 public void onPackagesUnsuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001191 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001192 PackageUpdatedTask.OP_UNSUSPEND, packageNames,
1193 user));
1194 }
1195
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001196 @Override
1197 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
1198 UserHandleCompat user) {
Sunny Goyal50941fb2016-08-04 12:03:52 -07001199 enqueueItemUpdatedTask(new ShortcutsChangedTask(packageName, shortcuts, user, true));
1200 }
1201
1202 public void updatePinnedShortcuts(String packageName, List<ShortcutInfoCompat> shortcuts,
1203 UserHandleCompat user) {
1204 enqueueItemUpdatedTask(new ShortcutsChangedTask(packageName, shortcuts, user, false));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001205 }
1206
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001207 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001208 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1209 * ACTION_PACKAGE_CHANGED.
1210 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001211 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001212 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001213 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001214
Joe Onorato36115782010-06-17 13:28:48 -04001215 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001216 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001217 // If we have changed locale we need to clear out the labels in all apps/workspace.
1218 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001219 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1220 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001221 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001222 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001223 } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1224 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
1225 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyalda891c12016-03-18 18:29:24 -07001226 UserHandleCompat user = UserHandleCompat.fromIntent(intent);
1227 if (user != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001228 if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1229 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
1230 enqueueItemUpdatedTask(new PackageUpdatedTask(
1231 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE,
1232 new String[0], user));
1233 }
1234
1235 // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so
1236 // we need to run the state change task again.
1237 if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
1238 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
1239 enqueueItemUpdatedTask(new UserLockStateChangedTask(user));
1240 }
Sunny Goyalda891c12016-03-18 18:29:24 -07001241 }
Tony Wickham827cef22016-03-17 15:39:39 -07001242 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
1243 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001244 }
1245 }
1246
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001247 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001248 resetLoadedState(true, true);
1249
Reena Lee93f824a2011-09-23 17:20:28 -07001250 // Do this here because if the launcher activity is running it will be restarted.
1251 // If it's not running startLoaderFromBackground will merely tell it that it needs
1252 // to reload.
1253 startLoaderFromBackground();
1254 }
1255
Winson Chungf0c6ae02012-03-21 16:10:31 -07001256 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1257 synchronized (mLock) {
1258 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1259 // mWorkspaceLoaded to true later
1260 stopLoaderLocked();
1261 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1262 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001263 // Always reset deep shortcuts loaded.
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001264 // TODO: why?
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001265 mDeepShortcutsLoaded = false;
Winson Chungf0c6ae02012-03-21 16:10:31 -07001266 }
1267 }
1268
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001269 /**
1270 * When the launcher is in the background, it's possible for it to miss paired
1271 * configuration changes. So whenever we trigger the loader from the background
1272 * tell the launcher that it needs to re-run the loader when it comes back instead
1273 * of doing it now.
1274 */
1275 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001276 Callbacks callbacks = getCallback();
1277 if (callbacks != null) {
1278 // Only actually run the loader if they're not paused.
1279 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -07001280 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001281 }
1282 }
Joe Onorato36115782010-06-17 13:28:48 -04001283 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001284
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001285 /**
1286 * If there is already a loader task running, tell it to stop.
1287 */
1288 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001289 LoaderTask oldTask = mLoaderTask;
1290 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001291 oldTask.stopLocked();
1292 }
Reena Lee93f824a2011-09-23 17:20:28 -07001293 }
1294
Adam Cohen1a85c582014-09-30 09:48:49 -07001295 public boolean isCurrentCallbacks(Callbacks callbacks) {
1296 return (mCallbacks != null && mCallbacks.get() == callbacks);
1297 }
1298
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001299 /**
1300 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
1301 * @return true if the page could be bound synchronously.
1302 */
1303 public boolean startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001304 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1305 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001306 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -04001307 // Don't bother to start the thread if we know it's not going to do anything
1308 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001309 final Callbacks oldCallbacks = mCallbacks.get();
1310 // Clear any pending bind-runnables from the synchronized load process.
1311 runOnMainThread(new Runnable() {
1312 public void run() {
1313 oldCallbacks.clearPendingBinds();
1314 }
1315 });
1316
Joe Onorato36115782010-06-17 13:28:48 -04001317 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001318 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001319 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001320 // TODO: mDeepShortcutsLoaded does not need to be true for synchronous bind.
Tony Wickham80f57872016-06-29 18:12:15 -07001321 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE && mAllAppsLoaded
1322 && mWorkspaceLoaded && mDeepShortcutsLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001323 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001324 return true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001325 } else {
1326 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1327 sWorker.post(mLoaderTask);
1328 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001329 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001330 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001331 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001332 }
1333
Joe Onorato36115782010-06-17 13:28:48 -04001334 public void stopLoader() {
1335 synchronized (mLock) {
1336 if (mLoaderTask != null) {
1337 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001338 }
1339 }
Joe Onorato36115782010-06-17 13:28:48 -04001340 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001341
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001342 /**
1343 * Loads the workspace screen ids in an ordered list.
1344 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001345 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001346 final ContentResolver contentResolver = context.getContentResolver();
1347 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001348
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001349 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001350 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
1351 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -07001352 }
1353
Joe Onorato36115782010-06-17 13:28:48 -04001354 /**
1355 * Runnable for the thread that loads the contents of the launcher:
1356 * - workspace icons
1357 * - widgets
1358 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001359 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -04001360 */
1361 private class LoaderTask implements Runnable {
1362 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001363 private int mPageToBindFirst;
1364
Adam Cohen091440a2015-03-18 14:16:05 -07001365 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001366 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001367 @Thunk boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001368
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001369 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -04001370 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001371 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001372 }
1373
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001374 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001375 mIsLoadingAndBindingWorkspace = true;
1376
Joe Onorato36115782010-06-17 13:28:48 -04001377 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001378 if (DEBUG_LOADERS) {
1379 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001380 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001381
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001382 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001383 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001384 synchronized (LoaderTask.this) {
1385 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001386 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001387 }
1388 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001389 }
1390 }
1391
Joe Onorato36115782010-06-17 13:28:48 -04001392 // Bind the workspace
Sunny Goyal93f878c2016-03-30 17:31:24 -07001393 bindWorkspace(mPageToBindFirst);
Joe Onorato36115782010-06-17 13:28:48 -04001394 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001395
Joe Onorato36115782010-06-17 13:28:48 -04001396 private void waitForIdle() {
1397 // Wait until the either we're stopped or the other threads are done.
1398 // This way we don't start loading all apps until the workspace has settled
1399 // down.
1400 synchronized (LoaderTask.this) {
1401 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001402
Joe Onorato36115782010-06-17 13:28:48 -04001403 mHandler.postIdle(new Runnable() {
1404 public void run() {
1405 synchronized (LoaderTask.this) {
1406 mLoadAndBindStepFinished = true;
1407 if (DEBUG_LOADERS) {
1408 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001409 }
Joe Onorato36115782010-06-17 13:28:48 -04001410 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001411 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001412 }
Joe Onorato36115782010-06-17 13:28:48 -04001413 });
1414
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001415 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001416 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001417 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1418 // wait no longer than 1sec at a time
1419 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001420 } catch (InterruptedException ex) {
1421 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001422 }
1423 }
Joe Onorato36115782010-06-17 13:28:48 -04001424 if (DEBUG_LOADERS) {
1425 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001426 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001427 + "ms for previous step to finish binding");
1428 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001429 }
Joe Onorato36115782010-06-17 13:28:48 -04001430 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001431
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001432 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001433 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001434 // Ensure that we have a valid page index to load synchronously
1435 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1436 "valid page index");
1437 }
1438 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1439 // Ensure that we don't try and bind a specified page when the pages have not been
1440 // loaded already (we should load everything asynchronously in that case)
1441 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1442 }
1443 synchronized (mLock) {
1444 if (mIsLoaderTaskRunning) {
1445 // Ensure that we are never running the background loading at this point since
1446 // we also touch the background collections
1447 throw new RuntimeException("Error! Background loading is already running");
1448 }
1449 }
1450
1451 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1452 // data structures, we can't allow any other thread to touch that data, but because
1453 // this call is synchronous, we can get away with not locking).
1454
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001455 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001456 // operations from the previous activity. We need to ensure that all queued operations
1457 // are executed before any synchronous binding work is done.
1458 mHandler.flush();
1459
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001460 // Divide the set of loaded items into those that we are binding synchronously, and
1461 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001462 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001463 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1464 // arise from that.
1465 onlyBindAllApps();
Tony Wickham80f57872016-06-29 18:12:15 -07001466
1467 bindDeepShortcuts();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001468 }
1469
Joe Onorato36115782010-06-17 13:28:48 -04001470 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001471 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001472 if (mStopped) {
1473 return;
1474 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001475 mIsLoaderTaskRunning = true;
1476 }
Joe Onorato36115782010-06-17 13:28:48 -04001477 // Optimize for end-user experience: if the Launcher is up and // running with the
1478 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1479 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001480 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001481 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001482 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001483
Joe Onorato36115782010-06-17 13:28:48 -04001484 if (mStopped) {
1485 break keep_running;
1486 }
1487
Joe Onorato36115782010-06-17 13:28:48 -04001488 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001489
1490 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001491 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1492 loadAndBindAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001493
1494 waitForIdle();
1495
1496 // third step
1497 if (DEBUG_LOADERS) Log.d(TAG, "step 3: loading deep shortcuts");
1498 loadAndBindDeepShortcuts();
Joe Onorato36115782010-06-17 13:28:48 -04001499 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001500
Joe Onorato36115782010-06-17 13:28:48 -04001501 // Clear out this reference, otherwise we end up holding it until all of the
1502 // callback runnables are done.
1503 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001504
Joe Onorato36115782010-06-17 13:28:48 -04001505 synchronized (mLock) {
1506 // If we are still the last one to be scheduled, remove ourselves.
1507 if (mLoaderTask == this) {
1508 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001509 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001510 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001511 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001512 }
Joe Onorato36115782010-06-17 13:28:48 -04001513 }
1514
1515 public void stopLocked() {
1516 synchronized (LoaderTask.this) {
1517 mStopped = true;
1518 this.notify();
1519 }
1520 }
1521
1522 /**
1523 * Gets the callbacks object. If we've been stopped, or if the launcher object
1524 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1525 * object that was around when the deferred message was scheduled, and if there's
1526 * a new Callbacks object around then also return null. This will save us from
1527 * calling onto it with data that will be ignored.
1528 */
1529 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1530 synchronized (mLock) {
1531 if (mStopped) {
1532 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001533 }
Joe Onorato36115782010-06-17 13:28:48 -04001534
1535 if (mCallbacks == null) {
1536 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001537 }
Joe Onorato36115782010-06-17 13:28:48 -04001538
1539 final Callbacks callbacks = mCallbacks.get();
1540 if (callbacks != oldCallbacks) {
1541 return null;
1542 }
1543 if (callbacks == null) {
1544 Log.w(TAG, "no mCallbacks");
1545 return null;
1546 }
1547
1548 return callbacks;
1549 }
1550 }
1551
1552 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001553 private boolean checkItemPlacement(LongArrayMap<GridOccupancy> occupied, ItemInfo item,
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001554 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001555 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001556 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung892c74d2013-08-22 16:15:50 -07001557
Adam Cohendcd297f2013-06-18 13:13:40 -07001558 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001559 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001560 // Return early if we detect that an item is under the hotseat button
Sunny Goyalbb011da2016-06-15 15:42:29 -07001561 if (!FeatureFlags.NO_ALL_APPS_ICON &&
1562 profile.isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001563 Log.e(TAG, "Error loading shortcut into hotseat " + item
1564 + " into position (" + item.screenId + ":" + item.cellX + ","
1565 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001566 return false;
1567 }
1568
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001569 final GridOccupancy hotseatOccupancy =
Dan Sandler295ae182013-12-10 16:05:47 -05001570 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1571
Adam Cohen2e6da152015-05-06 11:42:25 -07001572 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001573 Log.e(TAG, "Error loading shortcut " + item
1574 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001575 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001576 + ")");
1577 return false;
1578 }
1579
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001580 if (hotseatOccupancy != null) {
1581 if (hotseatOccupancy.cells[(int) item.screenId][0]) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001582 Log.e(TAG, "Error loading shortcut into hotseat " + item
1583 + " into position (" + item.screenId + ":" + item.cellX + ","
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001584 + item.cellY + ") already occupied");
Adam Cohendcd297f2013-06-18 13:13:40 -07001585 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001586 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001587 hotseatOccupancy.cells[(int) item.screenId][0] = true;
Dan Sandler295ae182013-12-10 16:05:47 -05001588 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001589 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001590 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001591 final GridOccupancy occupancy = new GridOccupancy(profile.numHotseatIcons, 1);
1592 occupancy.cells[(int) item.screenId][0] = true;
1593 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, occupancy);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001594 return true;
1595 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001596 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1597 if (!workspaceScreens.contains((Long) item.screenId)) {
1598 // The item has an invalid screen id.
1599 return false;
1600 }
1601 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001602 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001603 return true;
1604 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001605
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001606 final int countX = profile.numColumns;
1607 final int countY = profile.numRows;
Adam Cohenae4409d2013-11-26 10:34:59 -08001608 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1609 item.cellX < 0 || item.cellY < 0 ||
1610 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1611 Log.e(TAG, "Error loading shortcut " + item
1612 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1613 + item.cellX + "," + item.cellY
1614 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1615 return false;
1616 }
1617
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001618 if (!occupied.containsKey(item.screenId)) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001619 GridOccupancy screen = new GridOccupancy(countX + 1, countY + 1);
1620 if (item.screenId == Workspace.FIRST_SCREEN_ID) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001621 // Mark the first row as occupied (if the feature is enabled)
1622 // in order to account for the QSB.
1623 screen.markCells(0, 0, countX + 1, 1, FeatureFlags.QSB_ON_FIRST_SCREEN);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001624 }
1625 occupied.put(item.screenId, screen);
Joe Onorato36115782010-06-17 13:28:48 -04001626 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001627 final GridOccupancy occupancy = occupied.get(item.screenId);
Winson Chungf30ad5f2011-08-08 10:55:42 -07001628
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001629 // Check if any workspace icons overlap with each other
1630 if (occupancy.isRegionVacant(item.cellX, item.cellY, item.spanX, item.spanY)) {
1631 occupancy.markCells(item, true);
1632 return true;
1633 } else {
1634 Log.e(TAG, "Error loading shortcut " + item
1635 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1636 + item.cellX + "," + item.cellX + "," + item.spanX + "," + item.spanY
1637 + ") already occupied");
1638 return false;
1639 }
Joe Onorato36115782010-06-17 13:28:48 -04001640 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001641
Winson Chungba9c37f2013-08-30 14:11:37 -07001642 /** Clears all the sBg data structures */
1643 private void clearSBgDataStructures() {
1644 synchronized (sBgLock) {
1645 sBgWorkspaceItems.clear();
1646 sBgAppWidgets.clear();
1647 sBgFolders.clear();
1648 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001649 sBgWorkspaceScreens.clear();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001650 sBgPinnedShortcutCounts.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001651 }
1652 }
1653
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001654 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -07001655 if (LauncherAppState.PROFILE_STARTUP) {
1656 Trace.beginSection("Loading Workspace");
1657 }
Joe Onorato36115782010-06-17 13:28:48 -04001658 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001659
Joe Onorato36115782010-06-17 13:28:48 -04001660 final Context context = mContext;
1661 final ContentResolver contentResolver = context.getContentResolver();
1662 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001663 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001664 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001665 final boolean isSdCardReady = Utilities.isBootCompleted();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001666
Winson Chung892c74d2013-08-22 16:15:50 -07001667 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001668 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001669 int countX = profile.numColumns;
1670 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001671
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001672 boolean clearDb = false;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -07001673 try {
1674 ImportDataTask.performImportIfPossible(context);
1675 } catch (Exception e) {
1676 // Migration failed. Clear workspace.
1677 clearDb = true;
1678 }
1679
1680 if (!clearDb && GridSizeMigrationTask.ENABLED &&
Sunny Goyalf076eae2016-01-11 12:25:10 -08001681 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1682 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001683 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001684 }
1685
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001686 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -07001687 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001688 LauncherSettings.Settings.call(contentResolver,
1689 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001690 }
1691
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001692 Log.d(TAG, "loadWorkspace: loading default favorites");
1693 LauncherSettings.Settings.call(contentResolver,
1694 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -07001695
Winson Chung2abf94d2012-07-18 18:16:38 -07001696 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001697 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001698 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001699 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001700 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001701
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001702 final ArrayList<Long> itemsToRemove = new ArrayList<>();
1703 final ArrayList<Long> restoredRows = new ArrayList<>();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001704 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001705 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001706 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001707 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001708
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001709 // +1 for the hotseat (it can be larger than the workspace)
1710 // Load workspace in reverse order to ensure that latest items are loaded first (and
1711 // before any earlier duplicates)
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001712 final LongArrayMap<GridOccupancy> occupied = new LongArrayMap<>();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001713 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001714
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001715 try {
1716 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1717 final int intentIndex = c.getColumnIndexOrThrow
1718 (LauncherSettings.Favorites.INTENT);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001719 final int containerIndex = c.getColumnIndexOrThrow(
1720 LauncherSettings.Favorites.CONTAINER);
1721 final int itemTypeIndex = c.getColumnIndexOrThrow(
1722 LauncherSettings.Favorites.ITEM_TYPE);
1723 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1724 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001725 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1726 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001727 final int screenIndex = c.getColumnIndexOrThrow(
1728 LauncherSettings.Favorites.SCREEN);
1729 final int cellXIndex = c.getColumnIndexOrThrow
1730 (LauncherSettings.Favorites.CELLX);
1731 final int cellYIndex = c.getColumnIndexOrThrow
1732 (LauncherSettings.Favorites.CELLY);
1733 final int spanXIndex = c.getColumnIndexOrThrow
1734 (LauncherSettings.Favorites.SPANX);
1735 final int spanYIndex = c.getColumnIndexOrThrow(
1736 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001737 final int rankIndex = c.getColumnIndexOrThrow(
1738 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001739 final int restoredIndex = c.getColumnIndexOrThrow(
1740 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001741 final int profileIdIndex = c.getColumnIndexOrThrow(
1742 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001743 final int optionsIndex = c.getColumnIndexOrThrow(
1744 LauncherSettings.Favorites.OPTIONS);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001745 final CursorIconInfo cursorIconInfo = new CursorIconInfo(mContext, c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001746
Sunny Goyal7f834d22015-04-21 10:10:23 -07001747 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
Kenny Guyff05f432016-01-22 17:48:29 +00001748 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001749 final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>();
Sunny Goyal7f834d22015-04-21 10:10:23 -07001750 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001751 long serialNo = mUserManager.getSerialNumberForUser(user);
1752 allUsers.put(serialNo, user);
1753 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001754
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001755 boolean userUnlocked = mUserManager.isUserUnlocked(user);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001756
1757 // We can only query for shortcuts when the user is unlocked.
1758 if (userUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07001759 List<ShortcutInfoCompat> pinnedShortcuts =
1760 mDeepShortcutManager.queryForPinnedShortcuts(null, user);
1761 if (mDeepShortcutManager.wasLastCallSuccess()) {
1762 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
1763 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
1764 shortcut);
1765 }
1766 } else {
1767 // Shortcut manager can fail due to some race condition when the
1768 // lock state changes too frequently. For the purpose of the loading
1769 // shortcuts, consider the user is still locked.
1770 userUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001771 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001772 }
Sunny Goyal49f4f032016-08-01 15:45:49 -07001773 unlockedUsers.put(serialNo, userUnlocked);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001774 }
1775
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001776 ShortcutInfo info;
1777 String intentDescription;
1778 LauncherAppWidgetInfo appWidgetInfo;
1779 int container;
1780 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001781 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001782 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001783 UserHandleCompat user;
Sunny Goyald09c3702016-04-06 16:18:20 -07001784 String targetPackage;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001785
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001786 while (!mStopped && c.moveToNext()) {
1787 try {
1788 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001789 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001790 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001791 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001792
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001793 switch (itemType) {
1794 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1795 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001796 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001797 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001798 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001799 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001800 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001801 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001802 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001803 boolean itemReplaced = false;
Sunny Goyald09c3702016-04-06 16:18:20 -07001804 targetPackage = null;
Kenny Guyed131872014-04-30 03:02:21 +01001805 if (user == null) {
1806 // User has been deleted remove the item.
1807 itemsToRemove.add(id);
1808 continue;
1809 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001810 try {
1811 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001812 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001813 if (cn != null && cn.getPackageName() != null) {
1814 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1815 cn.getPackageName(), user);
1816 boolean validComponent = validPkg &&
1817 launcherApps.isActivityEnabledForProfile(cn, user);
Sunny Goyald09c3702016-04-06 16:18:20 -07001818 if (validPkg) {
1819 targetPackage = cn.getPackageName();
1820 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001821
1822 if (validComponent) {
1823 if (restored) {
1824 // no special handling necessary for this item
1825 restoredRows.add(id);
1826 restored = false;
1827 }
Kenny Guyff05f432016-01-22 17:48:29 +00001828 if (quietMode.get(serialNumber)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001829 disabledState = ShortcutInfo.FLAG_DISABLED_QUIET_USER;
Kenny Guyff05f432016-01-22 17:48:29 +00001830 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001831 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001832 intent = null;
1833 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1834 // We allow auto install apps to have their intent
1835 // updated after an install.
1836 intent = manager.getLaunchIntentForPackage(
1837 cn.getPackageName());
1838 if (intent != null) {
1839 ContentValues values = new ContentValues();
1840 values.put(LauncherSettings.Favorites.INTENT,
1841 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001842 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001843 }
1844 }
1845
1846 if (intent == null) {
1847 // The app is installed but the component is no
1848 // longer available.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001849 FileLog.d(TAG, "Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001850 itemsToRemove.add(id);
1851 continue;
1852 } else {
1853 // no special handling necessary for this item
1854 restoredRows.add(id);
1855 restored = false;
1856 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001857 } else if (restored) {
1858 // Package is not yet available but might be
1859 // installed later.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001860 FileLog.d(TAG, "package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001861
1862 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1863 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001864 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001865 // App restore has started. Update the flag
1866 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1867 ContentValues values = new ContentValues();
1868 values.put(LauncherSettings.Favorites.RESTORED,
1869 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001870 updateItem(id, values);
1871 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1872 // This is a common app. Try to replace this.
1873 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1874 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1875 if (parser.findDefaultApp()) {
1876 // Default app found. Replace it.
1877 intent = parser.parsedIntent;
1878 cn = intent.getComponent();
1879 ContentValues values = parser.parsedValues;
1880 values.put(LauncherSettings.Favorites.RESTORED, 0);
1881 updateItem(id, values);
1882 restored = false;
1883 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001884
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001885 } else {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001886 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001887 itemsToRemove.add(id);
1888 continue;
1889 }
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001890 } else {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001891 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001892 itemsToRemove.add(id);
1893 continue;
1894 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001895 } else if (PackageManagerHelper.isAppOnSdcard(
1896 manager, cn.getPackageName())) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07001897 // Package is present but not available.
1898 allowMissingTarget = true;
1899 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1900 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001901 // SdCard is not ready yet. Package might get available,
1902 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001903 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001904 HashSet<String> pkgs = sPendingPackages.get(user);
1905 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001906 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001907 sPendingPackages.put(user, pkgs);
1908 }
1909 pkgs.add(cn.getPackageName());
1910 allowMissingTarget = true;
1911 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001912
1913 } else {
1914 // Do not wait for external media load anymore.
1915 // Log the invalid package, and remove it
Sunny Goyal713edfc2016-05-06 09:58:34 -07001916 FileLog.d(TAG, "Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001917 itemsToRemove.add(id);
1918 continue;
Winson Chungee055712013-07-30 14:46:24 -07001919 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001920 } else if (cn == null) {
1921 // For shortcuts with no component, keep them as they are
1922 restoredRows.add(id);
1923 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001924 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001925 } catch (URISyntaxException e) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001926 FileLog.d(TAG, "Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001927 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001928 continue;
1929 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001930
Sunny Goyal34b65272015-03-11 16:56:52 -07001931 boolean useLowResIcon = container >= 0 &&
1932 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1933
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001934 if (itemReplaced) {
1935 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001936 info = getAppShortcutInfo(intent, user, null,
1937 cursorIconInfo, false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001938 } else {
1939 // Don't replace items for other profiles.
1940 itemsToRemove.add(id);
1941 continue;
1942 }
1943 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001944 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001945 info = getRestoredItemInfo(c, intent,
1946 promiseType, itemType, cursorIconInfo);
Kenny Guyed131872014-04-30 03:02:21 +01001947 intent = getRestoredItemIntent(c, context, intent);
1948 } else {
1949 // Don't restore items for other profiles.
1950 itemsToRemove.add(id);
1951 continue;
1952 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001953 } else if (itemType ==
1954 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001955 info = getAppShortcutInfo(intent, user, c,
1956 cursorIconInfo, allowMissingTarget, useLowResIcon);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001957 } else if (itemType ==
1958 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001959
1960 ShortcutKey key = ShortcutKey.fromIntent(intent, user);
1961 if (unlockedUsers.get(serialNumber)) {
1962 ShortcutInfoCompat pinnedShortcut =
1963 shortcutKeyToPinnedShortcuts.get(key);
1964 if (pinnedShortcut == null) {
1965 // The shortcut is no longer valid.
1966 itemsToRemove.add(id);
1967 continue;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001968 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001969 info = new ShortcutInfo(pinnedShortcut, context);
1970 intent = info.intent;
1971 } else {
1972 // Create a shortcut info in disabled mode for now.
1973 info = new ShortcutInfo();
1974 info.user = user;
1975 info.itemType = itemType;
1976 loadInfoFromCursor(info, c, cursorIconInfo);
1977
1978 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001979 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001980 incrementPinnedShortcutCount(key, false /* shouldPin */);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001981 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001982 info = getShortcutInfo(c, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001983
Sunny Goyald09c3702016-04-06 16:18:20 -07001984 // Shortcuts are only available on the primary profile
1985 if (PackageManagerHelper.isAppSuspended(manager, targetPackage)) {
1986 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1987 }
1988
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001989 // App shortcuts that used to be automatically added to Launcher
1990 // didn't always have the correct intent flags set, so do that
1991 // here
1992 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001993 intent.getCategories() != null &&
1994 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001995 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001996 intent.addFlags(
1997 Intent.FLAG_ACTIVITY_NEW_TASK |
1998 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1999 }
Michael Jurka96879562012-03-22 05:54:33 -07002000 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002001
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002002 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07002003 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002004 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002005 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002006 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002007 info.cellX = c.getInt(cellXIndex);
2008 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08002009 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002010 info.spanX = 1;
2011 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002012 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002013 if (info.promisedIntent != null) {
2014 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
2015 }
Sunny Goyald09c3702016-04-06 16:18:20 -07002016 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002017 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2018 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2019 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002020
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002021 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002022 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002023 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002024 break;
2025 }
2026
Sunny Goyal756adbc2015-04-16 15:20:51 -07002027 if (restored) {
2028 ComponentName cn = info.getTargetComponent();
2029 if (cn != null) {
2030 Integer progress = installingPkgs.get(cn.getPackageName());
2031 if (progress != null) {
2032 info.setInstallProgress(progress);
2033 } else {
2034 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2035 }
2036 }
2037 }
2038
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002039 switch (container) {
2040 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2041 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2042 sBgWorkspaceItems.add(info);
2043 break;
2044 default:
2045 // Item is in a user folder
2046 FolderInfo folderInfo =
2047 findOrMakeFolder(sBgFolders, container);
Sunny Goyalc52ba712016-04-05 15:59:05 -07002048 folderInfo.add(info, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002049 break;
2050 }
2051 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002052 } else {
2053 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002054 }
2055 break;
2056
2057 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2058 id = c.getLong(idIndex);
2059 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2060
Sunny Goyala508e4f2015-05-21 09:33:57 -07002061 // Do not trim the folder label, as is was set by the user.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002062 folderInfo.title = c.getString(cursorIconInfo.titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002063 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002064 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002065 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002066 folderInfo.cellX = c.getInt(cellXIndex);
2067 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002068 folderInfo.spanX = 1;
2069 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002070 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002071
Daniel Sandler8802e962010-05-26 16:28:16 -04002072 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002073 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002074 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002075 break;
2076 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002077
Joe Onorato9c1289c2009-08-17 11:03:03 -04002078 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002079 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2080 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2081 sBgWorkspaceItems.add(folderInfo);
2082 break;
Joe Onorato36115782010-06-17 13:28:48 -04002083 }
Joe Onorato17a89222011-02-08 17:26:11 -08002084
Chris Wrenf4d08112014-01-16 18:13:56 -05002085 if (restored) {
2086 // no special handling required for restored folders
2087 restoredRows.add(id);
2088 }
2089
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002090 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2091 sBgFolders.put(folderInfo.id, folderInfo);
2092 break;
2093
2094 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002095 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002096 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002097 boolean customWidget = itemType ==
2098 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2099
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002100 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002101 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002102 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002103 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002104 user = allUsers.get(serialNumber);
2105 if (user == null) {
2106 itemsToRemove.add(id);
2107 continue;
2108 }
2109
Sunny Goyalff572272014-07-23 13:58:07 -07002110 final ComponentName component =
2111 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002112
Sunny Goyal651077b2014-06-30 14:15:31 -07002113 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002114 final boolean isIdValid = (restoreStatus &
2115 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002116 final boolean wasProviderReady = (restoreStatus &
2117 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002118
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002119 if (widgetProvidersMap == null) {
2120 widgetProvidersMap = AppWidgetManagerCompat
2121 .getInstance(mContext).getAllProvidersMap();
2122 }
2123 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
2124 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00002125 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002126 user));
Sunny Goyalff572272014-07-23 13:58:07 -07002127
2128 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002129 if (!isSafeMode && !customWidget &&
2130 wasProviderReady && !isProviderReady) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002131 FileLog.d(TAG, "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07002132 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002133 itemsToRemove.add(id);
2134 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002135 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002136 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2137 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002138
Sunny Goyal53f96722015-07-13 19:54:53 -07002139 // The provider is available. So the widget is either
2140 // available or not available. We do not need to track
2141 // any future restore updates.
2142 int status = restoreStatus &
2143 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002144 if (!wasProviderReady) {
2145 // If provider was not previously ready, update the
2146 // status and UI flag.
2147
2148 // Id would be valid only if the widget restore broadcast was received.
2149 if (isIdValid) {
Sunny Goyal86df1382016-08-10 15:03:22 -07002150 status |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002151 } else {
2152 status &= ~LauncherAppWidgetInfo
2153 .FLAG_PROVIDER_NOT_READY;
2154 }
2155 }
2156 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002157 } else {
2158 Log.v(TAG, "Widget restore pending id=" + id
2159 + " appWidgetId=" + appWidgetId
2160 + " status =" + restoreStatus);
2161 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002162 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002163 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002164 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002165
2166 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2167 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002168 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002169 // App restore has started. Update the flag
2170 appWidgetInfo.restoreStatus |=
2171 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalb05a00a2016-08-29 10:06:57 -07002172 } else if (!isSafeMode) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002173 FileLog.d(TAG, "Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07002174 itemsToRemove.add(id);
2175 continue;
2176 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002177
2178 appWidgetInfo.installProgress =
2179 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002180 }
Sunny Goyal86df1382016-08-10 15:03:22 -07002181 if (appWidgetInfo.hasRestoreFlag(
2182 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG)) {
2183 intentDescription = c.getString(intentIndex);
2184 if (!TextUtils.isEmpty(intentDescription)) {
2185 appWidgetInfo.bindOptions =
2186 Intent.parseUri(intentDescription, 0);
2187 }
2188 }
Sunny Goyalff572272014-07-23 13:58:07 -07002189
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002190 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002191 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002192 appWidgetInfo.cellX = c.getInt(cellXIndex);
2193 appWidgetInfo.cellY = c.getInt(cellYIndex);
2194 appWidgetInfo.spanX = c.getInt(spanXIndex);
2195 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002196 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002197
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002198 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2199 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2200 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002201 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2202 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002203 continue;
2204 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002205
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002206 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002207 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002208 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002209 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002210 break;
2211 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002212
Adam Cohen59400422014-03-05 18:07:04 -08002213 if (!customWidget) {
2214 String providerName =
2215 appWidgetInfo.providerName.flattenToString();
2216 if (!providerName.equals(savedProvider) ||
2217 (appWidgetInfo.restoreStatus != restoreStatus)) {
2218 ContentValues values = new ContentValues();
2219 values.put(
2220 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2221 providerName);
2222 values.put(LauncherSettings.Favorites.RESTORED,
2223 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002224 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002225 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002226 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002227 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2228 sBgAppWidgets.add(appWidgetInfo);
2229 }
Joe Onorato36115782010-06-17 13:28:48 -04002230 break;
2231 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002232 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002233 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002234 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002235 }
2236 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002237 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002238 }
2239
Winson Chungba9c37f2013-08-30 14:11:37 -07002240 // Break early if we've stopped loading
2241 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002242 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002243 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002244 }
2245
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002246 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002247 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002248 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2249 Utilities.createDbSelectionQuery(
2250 LauncherSettings.Favorites._ID, itemsToRemove), null);
2251 if (DEBUG_LOADERS) {
2252 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2253 LauncherSettings.Favorites._ID, itemsToRemove));
2254 }
2255
2256 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07002257 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
2258 .call(contentResolver,
2259 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
2260 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
2261 for (long folderId : deletedFolderIds) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002262 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2263 sBgFolders.remove(folderId);
2264 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002265 }
2266 }
2267
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002268 // Unpin shortcuts that don't exist on the workspace.
2269 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
2270 MutableInt numTimesPinned = sBgPinnedShortcutCounts.get(key);
2271 if (numTimesPinned == null || numTimesPinned.value == 0) {
2272 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
2273 mDeepShortcutManager.unpinShortcut(key);
2274 }
2275 }
2276
Sunny Goyal317698b2015-07-29 11:45:41 -07002277 // Sort all the folder items and make sure the first 3 items are high resolution.
2278 for (FolderInfo folder : sBgFolders) {
2279 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2280 int pos = 0;
2281 for (ShortcutInfo info : folder.contents) {
2282 if (info.usingLowResIcon) {
2283 info.updateIcon(mIconCache, false);
2284 }
2285 pos ++;
2286 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2287 break;
2288 }
2289 }
2290 }
2291
Chris Wrenf4d08112014-01-16 18:13:56 -05002292 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002293 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002294 ContentValues values = new ContentValues();
2295 values.put(LauncherSettings.Favorites.RESTORED, 0);
2296 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2297 Utilities.createDbSelectionQuery(
2298 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002299 }
2300
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002301 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2302 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002303 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002304 null, sWorker);
2305 }
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
Adam Cohen091440a2015-03-18 14:16:05 -07002974 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002975
2976 @Override
2977 public void onReceive(Context context, Intent intent) {
2978 synchronized (sBgLock) {
2979 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2980 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002981 final PackageManager manager = context.getPackageManager();
2982 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2983 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002984 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2985 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002986 packagesRemoved.clear();
2987 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002988 for (String pkg : entry.getValue()) {
2989 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07002990 if (PackageManagerHelper.isAppOnSdcard(manager, pkg)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002991 packagesUnavailable.add(pkg);
2992 } else {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002993 packagesRemoved.add(pkg);
2994 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002995 }
2996 }
2997 if (!packagesRemoved.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002998 enqueueItemUpdatedTask(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002999 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
3000 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003001 if (!packagesUnavailable.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003002 enqueueItemUpdatedTask(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
Sunny Goyal1a745e82014-10-02 15:58:31 -07003003 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
3004 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003005 }
Sunny Goyal34942622014-08-29 17:20:55 -07003006 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003007 }
3008 }
3009 }
3010
Joe Onorato36115782010-06-17 13:28:48 -04003011 private class PackageUpdatedTask implements Runnable {
3012 int mOp;
3013 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01003014 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04003015
3016 public static final int OP_NONE = 0;
3017 public static final int OP_ADD = 1;
3018 public static final int OP_UPDATE = 2;
3019 public static final int OP_REMOVE = 3; // uninstlled
3020 public static final int OP_UNAVAILABLE = 4; // external media unmounted
Kenny Guy44cba692016-01-21 19:50:02 +00003021 public static final int OP_SUSPEND = 5; // package suspended
3022 public static final int OP_UNSUSPEND = 6; // package unsuspended
Sunny Goyalda891c12016-03-18 18:29:24 -07003023 public static final int OP_USER_AVAILABILITY_CHANGE = 7; // user available/unavailable
Joe Onorato36115782010-06-17 13:28:48 -04003024
Kenny Guyed131872014-04-30 03:02:21 +01003025 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04003026 mOp = op;
3027 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01003028 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04003029 }
3030
3031 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07003032 if (!mHasLoaderCompletedOnce) {
3033 // Loader has not yet run.
3034 return;
3035 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04003036 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04003037
3038 final String[] packages = mPackages;
3039 final int N = packages.length;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003040 FlagOp flagOp = FlagOp.NO_OP;
Sunny Goyalda891c12016-03-18 18:29:24 -07003041 StringFilter pkgFilter = StringFilter.of(new HashSet<>(Arrays.asList(packages)));
Joe Onorato36115782010-06-17 13:28:48 -04003042 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003043 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04003044 for (int i=0; i<N; i++) {
3045 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003046 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003047 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003048 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003049
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003050 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3051 if (heuristic != null) {
3052 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003053 }
Joe Onorato36115782010-06-17 13:28:48 -04003054 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003055 }
Joe Onorato36115782010-06-17 13:28:48 -04003056 case OP_UPDATE:
3057 for (int i=0; i<N; i++) {
3058 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003059 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003060 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003061 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003062 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003063 // Since package was just updated, the target must be available now.
3064 flagOp = FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04003065 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003066 case OP_REMOVE: {
3067 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3068 if (heuristic != null) {
3069 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003070 }
Joe Onorato36115782010-06-17 13:28:48 -04003071 for (int i=0; i<N; i++) {
3072 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003073 mIconCache.removeIconsForPkg(packages[i], mUser);
3074 }
3075 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003076 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003077 case OP_UNAVAILABLE:
3078 for (int i=0; i<N; i++) {
3079 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3080 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003081 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003082 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003083 flagOp = FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04003084 break;
Kenny Guy44cba692016-01-21 19:50:02 +00003085 case OP_SUSPEND:
3086 case OP_UNSUSPEND:
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003087 flagOp = mOp == OP_SUSPEND ?
3088 FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED) :
3089 FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED);
Sunny Goyal17763cb2016-03-24 13:53:22 -07003090 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.(un)suspend " + N);
Sunny Goyalda891c12016-03-18 18:29:24 -07003091 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
3092 break;
3093 case OP_USER_AVAILABILITY_CHANGE:
3094 flagOp = UserManagerCompat.getInstance(context).isQuietModeEnabled(mUser)
3095 ? FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER)
3096 : FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER);
3097 // We want to update all packages for this user.
3098 pkgFilter = StringFilter.matchesAll();
3099 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
Kenny Guy44cba692016-01-21 19:50:02 +00003100 break;
Joe Onorato36115782010-06-17 13:28:48 -04003101 }
3102
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003103 ArrayList<AppInfo> added = null;
3104 ArrayList<AppInfo> modified = null;
3105 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003106
Adam Cohen487f7dd2012-06-28 18:12:10 -07003107 if (mBgAllAppsList.added.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003108 added = new ArrayList<>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003109 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003110 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003111 if (mBgAllAppsList.modified.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003112 modified = new ArrayList<>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003113 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003114 }
Winson Chung5d55f332012-07-16 20:45:03 -07003115 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003116 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003117 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003118 }
3119
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003120 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps = new HashMap<>();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003121
Joe Onorato36115782010-06-17 13:28:48 -04003122 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003123 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003124 for (AppInfo ai : added) {
3125 addedOrUpdatedApps.put(ai.componentName, ai);
3126 }
Joe Onorato36115782010-06-17 13:28:48 -04003127 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003128
Joe Onorato36115782010-06-17 13:28:48 -04003129 if (modified != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003130 final Callbacks callbacks = getCallback();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003131 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003132 for (AppInfo ai : modified) {
3133 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003134 }
3135
Joe Onorato36115782010-06-17 13:28:48 -04003136 mHandler.post(new Runnable() {
3137 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003138 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003139 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003140 callbacks.bindAppsUpdated(modifiedFinal);
3141 }
3142 }
3143 });
3144 }
Winson Chung83892cc2013-05-01 16:53:33 -07003145
Sunny Goyal4390ace2014-10-13 11:33:11 -07003146 // Update shortcut infos
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003147 if (mOp == OP_ADD || flagOp != FlagOp.NO_OP) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003148 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3149 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3150 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3151
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003152 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003153 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003154 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3155 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003156 boolean infoUpdated = false;
3157 boolean shortcutUpdated = false;
3158
3159 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003160 if ((si.iconResource != null)
Sunny Goyalda891c12016-03-18 18:29:24 -07003161 && pkgFilter.matches(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003162 Bitmap icon = Utilities.createIconBitmap(
3163 si.iconResource.packageName,
3164 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003165 if (icon != null) {
3166 si.setIcon(icon);
3167 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003168 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003169 }
3170 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003171
3172 ComponentName cn = si.getTargetComponent();
Sunny Goyalda891c12016-03-18 18:29:24 -07003173 if (cn != null && pkgFilter.matches(cn.getPackageName())) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003174 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3175
3176 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003177 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3178 // Auto install icon
3179 PackageManager pm = context.getPackageManager();
3180 ResolveInfo matched = pm.resolveActivity(
3181 new Intent(Intent.ACTION_MAIN)
3182 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3183 PackageManager.MATCH_DEFAULT_ONLY);
3184 if (matched == null) {
3185 // Try to find the best match activity.
3186 Intent intent = pm.getLaunchIntentForPackage(
3187 cn.getPackageName());
3188 if (intent != null) {
3189 cn = intent.getComponent();
3190 appInfo = addedOrUpdatedApps.get(cn);
3191 }
3192
3193 if ((intent == null) || (appInfo == null)) {
3194 removedShortcuts.add(si);
3195 continue;
3196 }
3197 si.promisedIntent = intent;
3198 }
3199 }
3200
3201 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003202 if (appInfo != null) {
3203 si.flags = appInfo.flags;
3204 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003205
Sunny Goyal756adbc2015-04-16 15:20:51 -07003206 si.intent = si.promisedIntent;
3207 si.promisedIntent = null;
3208 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003209 infoUpdated = true;
3210 si.updateIcon(mIconCache);
3211 }
3212
3213 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3214 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3215 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003216 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003217 si.contentDescription = appInfo.contentDescription;
3218 infoUpdated = true;
3219 }
3220
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003221 int oldDisabledFlags = si.isDisabled;
3222 si.isDisabled = flagOp.apply(si.isDisabled);
3223 if (si.isDisabled != oldDisabledFlags) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003224 shortcutUpdated = true;
3225 }
3226 }
3227
3228 if (infoUpdated || shortcutUpdated) {
3229 updatedShortcuts.add(si);
3230 }
3231 if (infoUpdated) {
3232 updateItemInDatabase(context, si);
3233 }
Sunny Goyalda891c12016-03-18 18:29:24 -07003234 } else if (info instanceof LauncherAppWidgetInfo && mOp == OP_ADD) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003235 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3236 if (mUser.equals(widgetInfo.user)
3237 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
Sunny Goyalda891c12016-03-18 18:29:24 -07003238 && pkgFilter.matches(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003239 widgetInfo.restoreStatus &=
3240 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3241 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003242
3243 // adding this flag ensures that launcher shows 'click to setup'
3244 // if the widget has a config activity. In case there is no config
3245 // activity, it will be marked as 'restored' during bind.
3246 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3247
Sunny Goyal4390ace2014-10-13 11:33:11 -07003248 widgets.add(widgetInfo);
3249 updateItemInDatabase(context, widgetInfo);
3250 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003251 }
3252 }
3253 }
3254
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003255 bindUpdatedShortcuts(updatedShortcuts, removedShortcuts, mUser);
3256 if (!removedShortcuts.isEmpty()) {
3257 deleteItemsFromDatabase(context, removedShortcuts);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003258 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003259
Sunny Goyal4390ace2014-10-13 11:33:11 -07003260 if (!widgets.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003261 final Callbacks callbacks = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003262 mHandler.post(new Runnable() {
3263 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003264 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003265 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003266 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003267 }
3268 }
3269 });
3270 }
3271 }
3272
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003273 final HashSet<String> removedPackages = new HashSet<>();
3274 final HashSet<ComponentName> removedComponents = new HashSet<>();
3275 if (mOp == OP_REMOVE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003276 // Mark all packages in the broadcast to be removed
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003277 Collections.addAll(removedPackages, packages);
3278
3279 // No need to update the removedComponents as
3280 // removedPackages is a super-set of removedComponents
Winson Chungdf95eb12013-10-16 14:57:07 -07003281 } else if (mOp == OP_UPDATE) {
3282 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003283 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003284 if (isPackageDisabled(context, packages[i], mUser)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003285 removedPackages.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003286 }
3287 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003288
3289 // Update removedComponents as some components can get removed during package update
3290 for (AppInfo info : removedApps) {
3291 removedComponents.add(info.componentName);
3292 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003293 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003294
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003295 if (!removedPackages.isEmpty() || !removedComponents.isEmpty()) {
3296 for (String pn : removedPackages) {
3297 deletePackageFromDatabase(context, pn, mUser);
3298 }
3299 for (ComponentName cn : removedComponents) {
3300 deleteItemsFromDatabase(context, getItemInfoForComponentName(cn, mUser));
Sunny Goyal1a745e82014-10-02 15:58:31 -07003301 }
3302
Winson Chungdf95eb12013-10-16 14:57:07 -07003303 // Remove any queued items from the install queue
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003304 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackages, mUser);
3305
Winson Chungdf95eb12013-10-16 14:57:07 -07003306 // Call the components-removed callback
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003307 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003308 mHandler.post(new Runnable() {
3309 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003310 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003311 if (callbacks == cb && cb != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003312 callbacks.bindWorkspaceComponentsRemoved(
3313 removedPackages, removedComponents, mUser);
3314 }
3315 }
3316 });
3317 }
3318
3319 if (!removedApps.isEmpty()) {
3320 // Remove corresponding apps from All-Apps
3321 final Callbacks callbacks = getCallback();
3322 mHandler.post(new Runnable() {
3323 public void run() {
3324 Callbacks cb = getCallback();
3325 if (callbacks == cb && cb != null) {
3326 callbacks.bindAppInfosRemoved(removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04003327 }
3328 }
3329 });
Joe Onoratobe386092009-11-17 17:32:16 -08003330 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003331
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003332 // Notify launcher of widget update. From marshmallow onwards we use AppWidgetHost to
3333 // get widget update signals.
3334 if (!Utilities.ATLEAST_MARSHMALLOW &&
3335 (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003336 final Callbacks callbacks = getCallback();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003337 mHandler.post(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003338 public void run() {
3339 Callbacks cb = getCallback();
3340 if (callbacks == cb && cb != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003341 callbacks.notifyWidgetProvidersChanged();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003342 }
3343 }
3344 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003345 }
3346 }
3347 }
3348
Sunny Goyal10923b32016-07-20 15:42:44 -07003349 /**
3350 * Repopulates the shortcut info, possibly updating any icon already on the workspace.
3351 */
3352 public void updateShortcutInfo(final ShortcutInfoCompat fullDetail, final ShortcutInfo info) {
3353 enqueueItemUpdatedTask(new Runnable() {
3354 @Override
3355 public void run() {
3356 info.updateFromDeepShortcutInfo(
3357 fullDetail, LauncherAppState.getInstance().getContext());
3358 ArrayList<ShortcutInfo> update = new ArrayList<ShortcutInfo>();
3359 update.add(info);
3360 bindUpdatedShortcuts(update, fullDetail.getUserHandle());
3361 }
3362 });
3363 }
3364
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003365 private class ShortcutsChangedTask implements Runnable {
Sunny Goyal50941fb2016-08-04 12:03:52 -07003366 private final String mPackageName;
3367 private final List<ShortcutInfoCompat> mShortcuts;
3368 private final UserHandleCompat mUser;
3369 private final boolean mUpdateIdMap;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003370
3371 public ShortcutsChangedTask(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal50941fb2016-08-04 12:03:52 -07003372 UserHandleCompat user, boolean updateIdMap) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003373 mPackageName = packageName;
3374 mShortcuts = shortcuts;
3375 mUser = user;
Sunny Goyal50941fb2016-08-04 12:03:52 -07003376 mUpdateIdMap = updateIdMap;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003377 }
3378
3379 @Override
3380 public void run() {
3381 mDeepShortcutManager.onShortcutsChanged(mShortcuts);
3382
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003383 // Find ShortcutInfo's that have changed on the workspace.
Tony Wickham7695e602016-08-05 12:44:51 -07003384 final ArrayList<ShortcutInfo> removedShortcutInfos = new ArrayList<>();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003385 MultiHashMap<String, ShortcutInfo> idsToWorkspaceShortcutInfos = new MultiHashMap<>();
3386 for (ItemInfo itemInfo : sBgItemsIdMap) {
3387 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
3388 ShortcutInfo si = (ShortcutInfo) itemInfo;
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07003389 if (si.getPromisedIntent().getPackage().equals(mPackageName)
3390 && si.user.equals(mUser)) {
Tony Wickham7695e602016-08-05 12:44:51 -07003391 idsToWorkspaceShortcutInfos.addToList(si.getDeepShortcutId(), si);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003392 }
3393 }
3394 }
3395
Tony Wickham7695e602016-08-05 12:44:51 -07003396 final Context context = LauncherAppState.getInstance().getContext();
3397 final ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3398 if (!idsToWorkspaceShortcutInfos.isEmpty()) {
3399 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3400 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager.queryForFullDetails(
3401 mPackageName, new ArrayList<>(idsToWorkspaceShortcutInfos.keySet()), mUser);
3402 for (ShortcutInfoCompat fullDetails : shortcuts) {
3403 List<ShortcutInfo> shortcutInfos = idsToWorkspaceShortcutInfos
3404 .remove(fullDetails.getId());
3405 if (!fullDetails.isPinned()) {
3406 // The shortcut was previously pinned but is no longer, so remove it from
3407 // the workspace and our pinned shortcut counts.
3408 // Note that we put this check here, after querying for full details,
3409 // because there's a possible race condition between pinning and
3410 // receiving this callback.
3411 removedShortcutInfos.addAll(shortcutInfos);
3412 continue;
3413 }
3414 for (ShortcutInfo shortcutInfo : shortcutInfos) {
3415 shortcutInfo.updateFromDeepShortcutInfo(fullDetails, context);
3416 updatedShortcutInfos.add(shortcutInfo);
3417 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003418 }
3419 }
Tony Wickham7695e602016-08-05 12:44:51 -07003420
3421 // If there are still entries in idsToWorkspaceShortcutInfos, that means that
3422 // the corresponding shortcuts weren't passed in onShortcutsChanged(). This
3423 // means they were cleared, so we remove and unpin them now.
3424 for (String id : idsToWorkspaceShortcutInfos.keySet()) {
3425 removedShortcutInfos.addAll(idsToWorkspaceShortcutInfos.get(id));
3426 }
3427
3428 bindUpdatedShortcuts(updatedShortcutInfos, removedShortcutInfos, mUser);
3429 if (!removedShortcutInfos.isEmpty()) {
3430 deleteItemsFromDatabase(context, removedShortcutInfos);
3431 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003432
Sunny Goyal50941fb2016-08-04 12:03:52 -07003433 if (mUpdateIdMap) {
3434 // Update the deep shortcut map if the list of ids has changed for an activity.
3435 updateDeepShortcutMap(mPackageName, mUser, mShortcuts);
3436 bindDeepShortcuts();
3437 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003438 }
3439 }
3440
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003441 /**
3442 * Task to handle changing of lock state of the user
3443 */
3444 private class UserLockStateChangedTask implements Runnable {
3445
3446 private final UserHandleCompat mUser;
3447
3448 public UserLockStateChangedTask(UserHandleCompat user) {
3449 mUser = user;
3450 }
3451
3452 @Override
3453 public void run() {
3454 boolean isUserUnlocked = mUserManager.isUserUnlocked(mUser);
3455 Context context = mApp.getContext();
3456
3457 HashMap<ShortcutKey, ShortcutInfoCompat> pinnedShortcuts = new HashMap<>();
3458 if (isUserUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07003459 List<ShortcutInfoCompat> shortcuts =
3460 mDeepShortcutManager.queryForPinnedShortcuts(null, mUser);
3461 if (mDeepShortcutManager.wasLastCallSuccess()) {
3462 for (ShortcutInfoCompat shortcut : shortcuts) {
3463 pinnedShortcuts.put(ShortcutKey.fromInfo(shortcut), shortcut);
3464 }
3465 } else {
3466 // Shortcut manager can fail due to some race condition when the lock state
3467 // changes too frequently. For the purpose of the update,
3468 // consider it as still locked.
3469 isUserUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003470 }
3471 }
3472
3473 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3474 ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3475 ArrayList<ShortcutInfo> deletedShortcutInfos = new ArrayList<>();
3476 for (ItemInfo itemInfo : sBgItemsIdMap) {
3477 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT
3478 && mUser.equals(itemInfo.user)) {
3479 ShortcutInfo si = (ShortcutInfo) itemInfo;
3480 if (isUserUnlocked) {
3481 ShortcutInfoCompat shortcut =
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07003482 pinnedShortcuts.get(ShortcutKey.fromShortcutInfo(si));
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003483 // We couldn't verify the shortcut during loader. If its no longer available
3484 // (probably due to clear data), delete the workspace item as well
3485 if (shortcut == null) {
3486 deletedShortcutInfos.add(si);
3487 continue;
3488 }
3489 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
3490 si.updateFromDeepShortcutInfo(shortcut, context);
3491 } else {
3492 si.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
3493 }
3494 updatedShortcutInfos.add(si);
3495 }
3496 }
3497 bindUpdatedShortcuts(updatedShortcutInfos, deletedShortcutInfos, mUser);
3498 if (!deletedShortcutInfos.isEmpty()) {
3499 deleteItemsFromDatabase(context, deletedShortcutInfos);
3500 }
3501
3502 // Remove shortcut id map for that user
3503 Iterator<ComponentKey> keysIter = mBgDeepShortcutMap.keySet().iterator();
3504 while (keysIter.hasNext()) {
3505 if (keysIter.next().user.equals(mUser)) {
3506 keysIter.remove();
3507 }
3508 }
3509
3510 if (isUserUnlocked) {
3511 updateDeepShortcutMap(null, mUser, mDeepShortcutManager.queryForAllShortcuts(mUser));
3512 }
3513 bindDeepShortcuts();
3514 }
3515 }
3516
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003517 private void bindWidgetsModel(final Callbacks callbacks, final WidgetsModel model) {
3518 mHandler.post(new Runnable() {
3519 @Override
3520 public void run() {
3521 Callbacks cb = getCallback();
3522 if (callbacks == cb && cb != null) {
3523 callbacks.bindWidgetsModel(model);
3524 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003525 }
3526 });
3527 }
3528
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003529 public void refreshAndBindWidgetsAndShortcuts(
3530 final Callbacks callbacks, final boolean bindFirst) {
3531 runOnWorkerThread(new Runnable() {
3532 @Override
3533 public void run() {
3534 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
3535 bindWidgetsModel(callbacks, mBgWidgetsModel.clone());
Sunny Goyal31860582015-09-18 08:38:57 -07003536 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003537 final WidgetsModel model = mBgWidgetsModel.updateAndClone(mApp.getContext());
3538 bindWidgetsModel(callbacks, model);
3539 // update the Widget entries inside DB on the worker thread.
3540 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3541 model.getRawList());
Sunny Goyal31860582015-09-18 08:38:57 -07003542 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003543 });
Michael Jurkac402cd92013-05-20 15:49:32 +02003544 }
3545
Adam Cohen091440a2015-03-18 14:16:05 -07003546 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003547 UserHandleCompat user) {
3548 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3549 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003550 }
Adam Cohen556f6132014-01-15 15:18:08 -08003551
Kenny Guyed131872014-04-30 03:02:21 +01003552 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3553 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003554 if (cn == null) {
3555 return false;
3556 }
Kenny Guyed131872014-04-30 03:02:21 +01003557 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3558 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003559 return false;
3560 }
Kenny Guyed131872014-04-30 03:02:21 +01003561 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003562 }
3563
Adam Cohena28b78e2014-05-20 17:03:04 -07003564 public static boolean isValidPackage(Context context, String packageName,
3565 UserHandleCompat user) {
3566 if (packageName == null) {
3567 return false;
3568 }
3569 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3570 return launcherApps.isPackageEnabledForProfile(packageName, user);
3571 }
3572
Joe Onorato9c1289c2009-08-17 11:03:03 -04003573 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003574 * Make an ShortcutInfo object for a restored application or shortcut item that points
3575 * to a package that is not yet installed on the system.
3576 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003577 public ShortcutInfo getRestoredItemInfo(Cursor c, Intent intent,
3578 int promiseType, int itemType, CursorIconInfo iconInfo) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003579 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003580 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003581
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003582 Bitmap icon = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003583 // the fallback icon
3584 if (icon == null) {
3585 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3586 } else {
3587 info.setIcon(icon);
3588 }
Sunny Goyal34942622014-08-29 17:20:55 -07003589
3590 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003591 String title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07003592 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003593 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003594 }
Sunny Goyal34942622014-08-29 17:20:55 -07003595 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3596 if (TextUtils.isEmpty(info.title)) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003597 info.title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07003598 }
Sunny Goyal34942622014-08-29 17:20:55 -07003599 } else {
3600 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3601 }
3602
Winson Chung82b016c2015-05-08 17:00:10 -07003603 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003604 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003605 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003606 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003607 return info;
3608 }
3609
3610 /**
3611 * Make an Intent object for a restored application or shortcut item that points
3612 * to the market page for the item.
3613 */
Adam Cohen091440a2015-03-18 14:16:05 -07003614 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003615 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003616 return getMarketIntent(componentName.getPackageName());
3617 }
3618
3619 static Intent getMarketIntent(String packageName) {
3620 return new Intent(Intent.ACTION_VIEW)
3621 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003622 .scheme("market")
3623 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003624 .appendQueryParameter("id", packageName)
3625 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003626 }
3627
3628 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003629 * Make an ShortcutInfo object for a shortcut that is an application.
3630 *
3631 * If c is not null, then it will be used to fill in missing data like the title and icon.
3632 */
Sunny Goyald09c3702016-04-06 16:18:20 -07003633 public ShortcutInfo getAppShortcutInfo(Intent intent,
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003634 UserHandleCompat user, Cursor c, CursorIconInfo iconInfo,
Sunny Goyal34b65272015-03-11 16:56:52 -07003635 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003636 if (user == null) {
3637 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003638 return null;
3639 }
3640
Kenny Guyed131872014-04-30 03:02:21 +01003641 ComponentName componentName = intent.getComponent();
3642 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08003643 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01003644 return null;
3645 }
3646
3647 Intent newIntent = new Intent(intent.getAction(), null);
3648 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3649 newIntent.setComponent(componentName);
3650 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003651 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003652 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3653 return null;
3654 }
3655
3656 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003657 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003658 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003659 Bitmap icon = iconInfo.loadIcon(c);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003660 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003661 }
3662
Sunny Goyald09c3702016-04-06 16:18:20 -07003663 if (lai != null && PackageManagerHelper.isAppSuspended(lai.getApplicationInfo())) {
3664 info.isDisabled = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
3665 }
3666
Joe Onorato56d82912010-03-07 14:32:10 -05003667 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003668 if (TextUtils.isEmpty(info.title) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003669 info.title = iconInfo.getTitle(c);
Joe Onorato56d82912010-03-07 14:32:10 -05003670 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003671
Joe Onorato56d82912010-03-07 14:32:10 -05003672 // fall back to the class name of the activity
3673 if (info.title == null) {
3674 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003675 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003676
Joe Onorato9c1289c2009-08-17 11:03:03 -04003677 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003678 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003679 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003680 if (lai != null) {
3681 info.flags = AppInfo.initFlags(lai);
3682 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003683 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003684 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003685
Sunny Goyale2df0622015-04-24 11:27:00 -07003686 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003687 ItemInfoFilter f) {
3688 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3689 for (ItemInfo i : infos) {
3690 if (i instanceof ShortcutInfo) {
3691 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003692 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003693 if (cn != null && f.filterItem(null, info, cn)) {
3694 filtered.add(info);
3695 }
3696 } else if (i instanceof FolderInfo) {
3697 FolderInfo info = (FolderInfo) i;
3698 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003699 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003700 if (cn != null && f.filterItem(info, s, cn)) {
3701 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003702 }
3703 }
Winson Chung64359a52013-07-08 17:17:08 -07003704 } else if (i instanceof LauncherAppWidgetInfo) {
3705 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3706 ComponentName cn = info.providerName;
3707 if (cn != null && f.filterItem(null, info, cn)) {
3708 filtered.add(info);
3709 }
Winson Chung8a435102012-08-30 17:16:53 -07003710 }
3711 }
Winson Chung64359a52013-07-08 17:17:08 -07003712 return new ArrayList<ItemInfo>(filtered);
3713 }
3714
Adam Cohen091440a2015-03-18 14:16:05 -07003715 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003716 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003717 ItemInfoFilter filter = new ItemInfoFilter() {
3718 @Override
3719 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003720 if (info.user == null) {
3721 return cn.equals(cname);
3722 } else {
3723 return cn.equals(cname) && info.user.equals(user);
3724 }
Winson Chung64359a52013-07-08 17:17:08 -07003725 }
3726 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003727 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003728 }
3729
Sunny Goyal1a745e82014-10-02 15:58:31 -07003730 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003731 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003732 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003733 @Thunk ShortcutInfo getShortcutInfo(Cursor c, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003734 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003735 // Non-app shortcuts are only supported for current user.
3736 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003737 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003738
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003739 // TODO: If there's an explicit component and we can't install that, delete it.
3740
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003741 loadInfoFromCursor(info, c, iconInfo);
3742 return info;
3743 }
Joe Onorato56d82912010-03-07 14:32:10 -05003744
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003745 /**
3746 * Make an ShortcutInfo object for a shortcut that isn't an application.
3747 */
3748 public void loadInfoFromCursor(ShortcutInfo info, Cursor c, CursorIconInfo iconInfo) {
3749 info.title = iconInfo.getTitle(c);
3750 Bitmap icon = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003751 // the fallback icon
3752 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003753 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003754 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003755 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003756 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003757 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003758
Sunny Goyal2350bc92014-10-14 16:42:54 -07003759 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003760 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3761 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3762 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3763
Adam Cohend9198822011-11-22 16:42:47 -08003764 if (intent == null) {
3765 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3766 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3767 return null;
3768 }
3769
Joe Onorato0589f0f2010-02-08 13:44:00 -08003770 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003771 boolean customIcon = false;
3772 ShortcutIconResource iconResource = null;
3773
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003774 if (bitmap instanceof Bitmap) {
3775 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003776 customIcon = true;
3777 } else {
3778 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003779 if (extra instanceof ShortcutIconResource) {
3780 iconResource = (ShortcutIconResource) extra;
3781 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003782 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003783 }
3784 }
3785
Michael Jurkac9d95c52011-08-29 14:03:34 -07003786 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003787
Kenny Guyed131872014-04-30 03:02:21 +01003788 // Only support intents for current user for now. Intents sent from other
3789 // users wouldn't get here without intent forwarding anyway.
3790 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003791 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003792 icon = mIconCache.getDefaultIcon(info.user);
3793 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003794 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003795 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003796
Winson Chung82b016c2015-05-08 17:00:10 -07003797 info.title = Utilities.trim(name);
3798 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003799 info.intent = intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003800 info.iconResource = iconResource;
3801
3802 return info;
3803 }
3804
Joe Onorato9c1289c2009-08-17 11:03:03 -04003805 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003806 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003807 * or make a new one.
3808 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003809 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003810 // See if a placeholder was created for us already
3811 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003812 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003813 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003814 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003815 folders.put(id, folderInfo);
3816 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003817 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003818 }
3819
Joe Onoratobe386092009-11-17 17:32:16 -08003820
Sunny Goyal651077b2014-06-30 14:15:31 -07003821 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3822 return (provider != null) && (provider.provider != null)
3823 && (provider.provider.getPackageName() != null);
3824 }
3825
Joe Onoratobe386092009-11-17 17:32:16 -08003826 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003827 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003828 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3829 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3830 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3831 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003832 if (mLoaderTask != null) {
3833 mLoaderTask.dumpState();
3834 } else {
3835 Log.d(TAG, "mLoaderTask=null");
3836 }
Joe Onoratobe386092009-11-17 17:32:16 -08003837 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003838
3839 public Callbacks getCallback() {
3840 return mCallbacks != null ? mCallbacks.get() : null;
3841 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003842
3843 /**
3844 * @return {@link FolderInfo} if its already loaded.
3845 */
3846 public FolderInfo findFolderById(Long folderId) {
3847 synchronized (sBgLock) {
3848 return sBgFolders.get(folderId);
3849 }
3850 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003851
Sunny Goyal527c7d32015-08-28 15:19:36 -07003852 @Thunk class DeferredMainThreadExecutor implements Executor {
3853
3854 @Override
3855 public void execute(Runnable command) {
3856 runOnMainThread(command);
3857 }
3858 }
3859
Sunny Goyal756adbc2015-04-16 15:20:51 -07003860 /**
3861 * @return the looper for the worker thread which can be used to start background tasks.
3862 */
3863 public static Looper getWorkerLooper() {
3864 return sWorkerThread.getLooper();
3865 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003866}