blob: 3ac9773d9773bdfc450752c4d5eea9391b9000c2 [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) {
Tony2fd02082016-10-07 12:50:01 -0700731 item.screenId = Launcher.getLauncher(context).getHotseat()
732 .getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700733 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700734 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700735 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400736
737 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400738 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700739 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
740 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800741 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700742 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400743
Michael Jurkac9d95c52011-08-29 14:03:34 -0700744 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700745 }
746
747 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700748 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
749 * cellX, cellY have already been updated on the ItemInfos.
750 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800751 public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700752 final long container, final int screen) {
753
754 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
755 int count = items.size();
756
757 for (int i = 0; i < count; i++) {
758 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700759 item.container = container;
760
761 // We store hotseat items in canonical form which is this orientation invariant position
762 // in the hotseat
763 if (context instanceof Launcher && screen < 0 &&
764 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700765 item.screenId = Launcher.getLauncher(context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700766 item.cellY);
767 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700768 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700769 }
770
771 final ContentValues values = new ContentValues();
772 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
773 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
774 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800775 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700776 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700777
778 contentValues.add(values);
779 }
780 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
781 }
782
783 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700784 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800785 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700786 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700787 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700788 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800789 item.cellX = cellX;
790 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700791 item.spanX = spanX;
792 item.spanY = spanY;
793
794 // We store hotseat items in canonical form which is this orientation invariant position
795 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700796 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700797 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700798 item.screenId = Launcher.getLauncher(context).getHotseat()
799 .getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700800 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700801 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700802 }
Adam Cohend4844c32011-02-18 19:25:06 -0800803
Adam Cohend4844c32011-02-18 19:25:06 -0800804 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800805 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700806 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
807 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800808 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700809 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
810 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700811 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800812
Michael Jurka816474f2012-06-25 14:49:02 -0700813 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700814 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700815
816 /**
817 * Update an item to the database in a specified container.
818 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700819 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700820 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100821 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700822 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800823 }
824
Sunny Goyal756a28a2015-04-23 17:07:55 -0700825 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700826 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700827 synchronized (mLock) {
828 if (!mHasLoaderCompletedOnce ||
829 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
830 throw new RuntimeException("Trying to add shortcut while loader is running");
831 }
832 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700833 }
834 }
835
Adam Cohend4844c32011-02-18 19:25:06 -0800836 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700837 * Returns true if the shortcuts already exists on the workspace. This must be called after
838 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800839 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700840 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
841 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700842 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700843 if (intent.getComponent() != null) {
844 // If component is not null, an intent with null package will produce
845 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700846 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700847 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700848 intentWithPkg = intent.toUri(0);
849 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700850 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700851 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
852 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700853 }
854 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700855 intentWithPkg = intent.toUri(0);
856 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700857 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700858
859 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700860 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700861 if (item instanceof ShortcutInfo) {
862 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700863 Intent targetIntent = info.promisedIntent == null
864 ? info.intent : info.promisedIntent;
865 if (targetIntent != null && info.user.equals(user)) {
Sunny Goyalcbfe71d2016-08-23 13:25:58 -0700866 Intent copyIntent = new Intent(targetIntent);
867 copyIntent.setSourceBounds(intent.getSourceBounds());
868 String s = copyIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700869 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
870 return true;
871 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700872 }
873 }
874 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800875 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700876 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700877 }
878
Joe Onorato9c1289c2009-08-17 11:03:03 -0400879 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400880 * Add an item to the database in a specified container. Sets the container, screen, cellX and
881 * cellY fields of the item. Also assigns an ID to the item.
882 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700883 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700884 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400885 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400886 item.cellX = cellX;
887 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700888 // We store hotseat items in canonical form which is this orientation invariant position
889 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700890 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700891 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Tony2fd02082016-10-07 12:50:01 -0700892 item.screenId = Launcher.getLauncher(context).getHotseat()
893 .getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700894 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700895 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700896 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400897
898 final ContentValues values = new ContentValues();
899 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100900 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400901
Sunny Goyald2497482015-09-22 18:24:19 -0700902 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
903 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
904
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700905 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700906
Jason Monk8e19cf22014-03-20 15:06:57 -0400907 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700908 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700909 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700910 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400911
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700912 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700913 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400914 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700915 sBgItemsIdMap.put(item.id, item);
916 switch (item.itemType) {
917 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
918 sBgFolders.put(item.id, (FolderInfo) item);
919 // Fall through
920 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
921 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700922 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700923 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
924 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
925 sBgWorkspaceItems.add(item);
926 } else {
927 if (!sBgFolders.containsKey(item.container)) {
928 // Adding an item to a folder that doesn't exist.
929 String msg = "adding item: " + item + " to a folder that " +
930 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700931 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700932 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700933 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700934 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700935 incrementPinnedShortcutCount(
Tony Wickhamfc02c1b2016-08-29 15:17:48 -0700936 ShortcutKey.fromShortcutInfo((ShortcutInfo) item),
937 true /* shouldPin */);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700938 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700939 break;
940 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
941 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
942 break;
943 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700944 }
945 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700946 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700947 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700948 }
949
Sunny Goyal34942622014-08-29 17:20:55 -0700950 private static ArrayList<ItemInfo> getItemsByPackageName(
951 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700952 ItemInfoFilter filter = new ItemInfoFilter() {
953 @Override
954 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
955 return cn.getPackageName().equals(pn) && info.user.equals(user);
956 }
957 };
Sunny Goyale2df0622015-04-24 11:27:00 -0700958 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -0700959 }
960
961 /**
962 * Removes all the items from the database corresponding to the specified package.
963 */
964 static void deletePackageFromDatabase(Context context, final String pn,
965 final UserHandleCompat user) {
966 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700967 }
968
969 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700970 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -0400971 */
Sunny Goyalfa401a12015-04-10 13:45:42 -0700972 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700973 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
974 items.add(item);
975 deleteItemsFromDatabase(context, items);
976 }
977
978 /**
979 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700980 */
Sunny Goyal4390ace2014-10-13 11:33:11 -0700981 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400982 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -0700983 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700984 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700985 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700986 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700987 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700988
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700989 // Lock on mBgLock *after* the db operation
990 synchronized (sBgLock) {
991 switch (item.itemType) {
992 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
993 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -0700994 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700995 if (info.container == item.id) {
996 // We are deleting a folder which still contains items that
997 // think they are contained by that folder.
998 String msg = "deleting a folder (" + item + ") which still " +
999 "contains items (" + info + ")";
1000 Log.e(TAG, msg);
1001 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001002 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001003 sBgWorkspaceItems.remove(item);
1004 break;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001005 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07001006 decrementPinnedShortcutCount(ShortcutKey.fromShortcutInfo(
1007 (ShortcutInfo) item));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001008 // Fall through.
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001009 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1010 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1011 sBgWorkspaceItems.remove(item);
1012 break;
1013 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1014 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1015 break;
1016 }
1017 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001018 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001019 }
1020 }
Michael Jurka83df1882011-08-31 20:59:26 -07001021 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001022 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001023 }
1024
1025 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001026 * Decrement the count for the given pinned shortcut, unpinning it if the count becomes 0.
1027 */
1028 private static void decrementPinnedShortcutCount(final ShortcutKey pinnedShortcut) {
1029 synchronized (sBgLock) {
1030 MutableInt count = sBgPinnedShortcutCounts.get(pinnedShortcut);
1031 if (count == null || --count.value == 0) {
1032 LauncherAppState.getInstance().getShortcutManager().unpinShortcut(pinnedShortcut);
1033 }
1034 }
1035 }
1036
1037 /**
1038 * Increment the count for the given shortcut, pinning it if the count becomes 1.
1039 *
1040 * As an optimization, the caller can pass shouldPin == false to avoid
1041 * unnecessary RPC's if the shortcut is already pinned.
1042 */
1043 private static void incrementPinnedShortcutCount(ShortcutKey pinnedShortcut, boolean shouldPin) {
1044 synchronized (sBgLock) {
1045 MutableInt count = sBgPinnedShortcutCounts.get(pinnedShortcut);
1046 if (count == null) {
1047 count = new MutableInt(1);
1048 sBgPinnedShortcutCounts.put(pinnedShortcut, count);
1049 } else {
1050 count.value++;
1051 }
1052 if (shouldPin && count.value == 1) {
1053 LauncherAppState.getInstance().getShortcutManager().pinShortcut(pinnedShortcut);
1054 }
1055 }
1056 }
1057
1058 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001059 * Update the order of the workspace screens in the database. The array list contains
1060 * a list of screen ids in the order that they should appear.
1061 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001062 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001063 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001064 final ContentResolver cr = context.getContentResolver();
1065 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1066
1067 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001068 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001069 while (iter.hasNext()) {
1070 long id = iter.next();
1071 if (id < 0) {
1072 iter.remove();
1073 }
1074 }
1075
1076 Runnable r = new Runnable() {
1077 @Override
1078 public void run() {
Yura085c8532014-02-11 15:15:29 +00001079 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001080 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001081 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001082 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001083 for (int i = 0; i < count; i++) {
1084 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001085 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001086 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1087 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001088 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001089 }
Yura085c8532014-02-11 15:15:29 +00001090
1091 try {
1092 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1093 } catch (Exception ex) {
1094 throw new RuntimeException(ex);
1095 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001096
Winson Chungba9c37f2013-08-30 14:11:37 -07001097 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001098 sBgWorkspaceScreens.clear();
1099 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001100 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001101 }
1102 };
1103 runOnWorkerThread(r);
1104 }
1105
1106 /**
Winsonc0b52fe2015-09-09 16:38:15 -07001107 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001108 */
Winsonc0b52fe2015-09-09 16:38:15 -07001109 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001110 final ContentResolver cr = context.getContentResolver();
1111
Michael Jurkac9d95c52011-08-29 14:03:34 -07001112 Runnable r = new Runnable() {
1113 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001114 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001115 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001116 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001117 sBgItemsIdMap.remove(info.id);
1118 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001119 sBgWorkspaceItems.remove(info);
1120 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001121
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001122 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001123 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001124 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001125 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001126 for (ItemInfo childInfo : info.contents) {
1127 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001128 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001129 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001130 }
1131 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001132 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001133 }
1134
1135 /**
1136 * Set this as the current Launcher activity object for the loader.
1137 */
1138 public void initialize(Callbacks callbacks) {
1139 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001140 Preconditions.assertUIThread();
1141 // Remove any queued UI runnables
1142 mHandler.cancelAll();
1143 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001144 }
1145 }
1146
Kenny Guyed131872014-04-30 03:02:21 +01001147 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001148 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001149 int op = PackageUpdatedTask.OP_UPDATE;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001150 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001151 user));
1152 }
1153
1154 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001155 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001156 int op = PackageUpdatedTask.OP_REMOVE;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001157 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001158 user));
1159 }
1160
1161 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001162 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001163 int op = PackageUpdatedTask.OP_ADD;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001164 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001165 user));
1166 }
1167
1168 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001169 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001170 boolean replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001171 enqueueItemUpdatedTask(
Sunny Goyal144154d2016-03-30 16:47:03 -07001172 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, packageNames, user));
Kenny Guyed131872014-04-30 03:02:21 +01001173 }
1174
1175 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001176 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001177 boolean replacing) {
1178 if (!replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001179 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guyed131872014-04-30 03:02:21 +01001180 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1181 user));
1182 }
Kenny Guyed131872014-04-30 03:02:21 +01001183 }
1184
Kenny Guy44cba692016-01-21 19:50:02 +00001185 @Override
1186 public void onPackagesSuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001187 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001188 PackageUpdatedTask.OP_SUSPEND, packageNames,
1189 user));
1190 }
1191
1192 @Override
1193 public void onPackagesUnsuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001194 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001195 PackageUpdatedTask.OP_UNSUSPEND, packageNames,
1196 user));
1197 }
1198
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001199 @Override
1200 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
1201 UserHandleCompat user) {
Sunny Goyal50941fb2016-08-04 12:03:52 -07001202 enqueueItemUpdatedTask(new ShortcutsChangedTask(packageName, shortcuts, user, true));
1203 }
1204
1205 public void updatePinnedShortcuts(String packageName, List<ShortcutInfoCompat> shortcuts,
1206 UserHandleCompat user) {
1207 enqueueItemUpdatedTask(new ShortcutsChangedTask(packageName, shortcuts, user, false));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001208 }
1209
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001210 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001211 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1212 * ACTION_PACKAGE_CHANGED.
1213 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001214 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001215 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001216 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001217
Joe Onorato36115782010-06-17 13:28:48 -04001218 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001219 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001220 // If we have changed locale we need to clear out the labels in all apps/workspace.
1221 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001222 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1223 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001224 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001225 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001226 } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1227 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
1228 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyalda891c12016-03-18 18:29:24 -07001229 UserHandleCompat user = UserHandleCompat.fromIntent(intent);
1230 if (user != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001231 if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1232 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
1233 enqueueItemUpdatedTask(new PackageUpdatedTask(
1234 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE,
1235 new String[0], user));
1236 }
1237
1238 // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so
1239 // we need to run the state change task again.
1240 if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
1241 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
1242 enqueueItemUpdatedTask(new UserLockStateChangedTask(user));
1243 }
Sunny Goyalda891c12016-03-18 18:29:24 -07001244 }
Tony Wickham827cef22016-03-17 15:39:39 -07001245 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
1246 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001247 }
1248 }
1249
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001250 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001251 resetLoadedState(true, true);
1252
Reena Lee93f824a2011-09-23 17:20:28 -07001253 // Do this here because if the launcher activity is running it will be restarted.
1254 // If it's not running startLoaderFromBackground will merely tell it that it needs
1255 // to reload.
1256 startLoaderFromBackground();
1257 }
1258
Winson Chungf0c6ae02012-03-21 16:10:31 -07001259 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1260 synchronized (mLock) {
1261 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1262 // mWorkspaceLoaded to true later
1263 stopLoaderLocked();
1264 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1265 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001266 // Always reset deep shortcuts loaded.
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001267 // TODO: why?
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001268 mDeepShortcutsLoaded = false;
Winson Chungf0c6ae02012-03-21 16:10:31 -07001269 }
1270 }
1271
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001272 /**
1273 * When the launcher is in the background, it's possible for it to miss paired
1274 * configuration changes. So whenever we trigger the loader from the background
1275 * tell the launcher that it needs to re-run the loader when it comes back instead
1276 * of doing it now.
1277 */
1278 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001279 Callbacks callbacks = getCallback();
1280 if (callbacks != null) {
1281 // Only actually run the loader if they're not paused.
1282 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -07001283 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001284 }
1285 }
Joe Onorato36115782010-06-17 13:28:48 -04001286 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001287
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001288 /**
1289 * If there is already a loader task running, tell it to stop.
1290 */
1291 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001292 LoaderTask oldTask = mLoaderTask;
1293 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001294 oldTask.stopLocked();
1295 }
Reena Lee93f824a2011-09-23 17:20:28 -07001296 }
1297
Adam Cohen1a85c582014-09-30 09:48:49 -07001298 public boolean isCurrentCallbacks(Callbacks callbacks) {
1299 return (mCallbacks != null && mCallbacks.get() == callbacks);
1300 }
1301
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001302 /**
1303 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
1304 * @return true if the page could be bound synchronously.
1305 */
1306 public boolean startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001307 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1308 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001309 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -04001310 // Don't bother to start the thread if we know it's not going to do anything
1311 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001312 final Callbacks oldCallbacks = mCallbacks.get();
1313 // Clear any pending bind-runnables from the synchronized load process.
1314 runOnMainThread(new Runnable() {
1315 public void run() {
1316 oldCallbacks.clearPendingBinds();
1317 }
1318 });
1319
Joe Onorato36115782010-06-17 13:28:48 -04001320 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001321 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001322 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001323 // TODO: mDeepShortcutsLoaded does not need to be true for synchronous bind.
Tony Wickham80f57872016-06-29 18:12:15 -07001324 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE && mAllAppsLoaded
1325 && mWorkspaceLoaded && mDeepShortcutsLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001326 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001327 return true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001328 } else {
1329 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1330 sWorker.post(mLoaderTask);
1331 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001332 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001333 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001334 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001335 }
1336
Joe Onorato36115782010-06-17 13:28:48 -04001337 public void stopLoader() {
1338 synchronized (mLock) {
1339 if (mLoaderTask != null) {
1340 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001341 }
1342 }
Joe Onorato36115782010-06-17 13:28:48 -04001343 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001344
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001345 /**
1346 * Loads the workspace screen ids in an ordered list.
1347 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001348 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001349 final ContentResolver contentResolver = context.getContentResolver();
1350 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001351
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001352 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001353 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
1354 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -07001355 }
1356
Joe Onorato36115782010-06-17 13:28:48 -04001357 /**
1358 * Runnable for the thread that loads the contents of the launcher:
1359 * - workspace icons
1360 * - widgets
1361 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001362 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -04001363 */
1364 private class LoaderTask implements Runnable {
1365 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001366 private int mPageToBindFirst;
1367
Adam Cohen091440a2015-03-18 14:16:05 -07001368 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001369 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001370 @Thunk boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001371
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001372 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -04001373 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001374 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001375 }
1376
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001377 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001378 mIsLoadingAndBindingWorkspace = true;
1379
Joe Onorato36115782010-06-17 13:28:48 -04001380 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001381 if (DEBUG_LOADERS) {
1382 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001383 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001384
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001385 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001386 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001387 synchronized (LoaderTask.this) {
1388 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001389 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001390 }
1391 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001392 }
1393 }
1394
Joe Onorato36115782010-06-17 13:28:48 -04001395 // Bind the workspace
Sunny Goyal93f878c2016-03-30 17:31:24 -07001396 bindWorkspace(mPageToBindFirst);
Joe Onorato36115782010-06-17 13:28:48 -04001397 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001398
Joe Onorato36115782010-06-17 13:28:48 -04001399 private void waitForIdle() {
1400 // Wait until the either we're stopped or the other threads are done.
1401 // This way we don't start loading all apps until the workspace has settled
1402 // down.
1403 synchronized (LoaderTask.this) {
1404 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001405
Joe Onorato36115782010-06-17 13:28:48 -04001406 mHandler.postIdle(new Runnable() {
1407 public void run() {
1408 synchronized (LoaderTask.this) {
1409 mLoadAndBindStepFinished = true;
1410 if (DEBUG_LOADERS) {
1411 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001412 }
Joe Onorato36115782010-06-17 13:28:48 -04001413 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001414 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001415 }
Joe Onorato36115782010-06-17 13:28:48 -04001416 });
1417
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001418 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001419 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001420 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1421 // wait no longer than 1sec at a time
1422 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001423 } catch (InterruptedException ex) {
1424 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001425 }
1426 }
Joe Onorato36115782010-06-17 13:28:48 -04001427 if (DEBUG_LOADERS) {
1428 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001429 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001430 + "ms for previous step to finish binding");
1431 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001432 }
Joe Onorato36115782010-06-17 13:28:48 -04001433 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001434
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001435 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001436 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001437 // Ensure that we have a valid page index to load synchronously
1438 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1439 "valid page index");
1440 }
1441 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1442 // Ensure that we don't try and bind a specified page when the pages have not been
1443 // loaded already (we should load everything asynchronously in that case)
1444 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1445 }
1446 synchronized (mLock) {
1447 if (mIsLoaderTaskRunning) {
1448 // Ensure that we are never running the background loading at this point since
1449 // we also touch the background collections
1450 throw new RuntimeException("Error! Background loading is already running");
1451 }
1452 }
1453
1454 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1455 // data structures, we can't allow any other thread to touch that data, but because
1456 // this call is synchronous, we can get away with not locking).
1457
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001458 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001459 // operations from the previous activity. We need to ensure that all queued operations
1460 // are executed before any synchronous binding work is done.
1461 mHandler.flush();
1462
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001463 // Divide the set of loaded items into those that we are binding synchronously, and
1464 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001465 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001466 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1467 // arise from that.
1468 onlyBindAllApps();
Tony Wickham80f57872016-06-29 18:12:15 -07001469
1470 bindDeepShortcuts();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001471 }
1472
Joe Onorato36115782010-06-17 13:28:48 -04001473 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001474 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001475 if (mStopped) {
1476 return;
1477 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001478 mIsLoaderTaskRunning = true;
1479 }
Joe Onorato36115782010-06-17 13:28:48 -04001480 // Optimize for end-user experience: if the Launcher is up and // running with the
1481 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1482 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001483 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001484 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001485 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001486
Joe Onorato36115782010-06-17 13:28:48 -04001487 if (mStopped) {
1488 break keep_running;
1489 }
1490
Joe Onorato36115782010-06-17 13:28:48 -04001491 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001492
1493 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001494 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1495 loadAndBindAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001496
1497 waitForIdle();
1498
1499 // third step
1500 if (DEBUG_LOADERS) Log.d(TAG, "step 3: loading deep shortcuts");
1501 loadAndBindDeepShortcuts();
Joe Onorato36115782010-06-17 13:28:48 -04001502 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001503
Joe Onorato36115782010-06-17 13:28:48 -04001504 // Clear out this reference, otherwise we end up holding it until all of the
1505 // callback runnables are done.
1506 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001507
Joe Onorato36115782010-06-17 13:28:48 -04001508 synchronized (mLock) {
1509 // If we are still the last one to be scheduled, remove ourselves.
1510 if (mLoaderTask == this) {
1511 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001512 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001513 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001514 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001515 }
Joe Onorato36115782010-06-17 13:28:48 -04001516 }
1517
1518 public void stopLocked() {
1519 synchronized (LoaderTask.this) {
1520 mStopped = true;
1521 this.notify();
1522 }
1523 }
1524
1525 /**
1526 * Gets the callbacks object. If we've been stopped, or if the launcher object
1527 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1528 * object that was around when the deferred message was scheduled, and if there's
1529 * a new Callbacks object around then also return null. This will save us from
1530 * calling onto it with data that will be ignored.
1531 */
1532 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1533 synchronized (mLock) {
1534 if (mStopped) {
1535 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001536 }
Joe Onorato36115782010-06-17 13:28:48 -04001537
1538 if (mCallbacks == null) {
1539 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001540 }
Joe Onorato36115782010-06-17 13:28:48 -04001541
1542 final Callbacks callbacks = mCallbacks.get();
1543 if (callbacks != oldCallbacks) {
1544 return null;
1545 }
1546 if (callbacks == null) {
1547 Log.w(TAG, "no mCallbacks");
1548 return null;
1549 }
1550
1551 return callbacks;
1552 }
1553 }
1554
1555 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001556 private boolean checkItemPlacement(LongArrayMap<GridOccupancy> occupied, ItemInfo item,
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001557 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001558 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001559 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung892c74d2013-08-22 16:15:50 -07001560
Adam Cohendcd297f2013-06-18 13:13:40 -07001561 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001562 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001563 // Return early if we detect that an item is under the hotseat button
Sunny Goyalbb011da2016-06-15 15:42:29 -07001564 if (!FeatureFlags.NO_ALL_APPS_ICON &&
1565 profile.isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001566 Log.e(TAG, "Error loading shortcut into hotseat " + item
1567 + " into position (" + item.screenId + ":" + item.cellX + ","
1568 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001569 return false;
1570 }
1571
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001572 final GridOccupancy hotseatOccupancy =
Dan Sandler295ae182013-12-10 16:05:47 -05001573 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1574
Adam Cohen2e6da152015-05-06 11:42:25 -07001575 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001576 Log.e(TAG, "Error loading shortcut " + item
1577 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001578 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001579 + ")");
1580 return false;
1581 }
1582
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001583 if (hotseatOccupancy != null) {
1584 if (hotseatOccupancy.cells[(int) item.screenId][0]) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001585 Log.e(TAG, "Error loading shortcut into hotseat " + item
1586 + " into position (" + item.screenId + ":" + item.cellX + ","
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001587 + item.cellY + ") already occupied");
Adam Cohendcd297f2013-06-18 13:13:40 -07001588 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001589 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001590 hotseatOccupancy.cells[(int) item.screenId][0] = true;
Dan Sandler295ae182013-12-10 16:05:47 -05001591 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001592 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001593 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001594 final GridOccupancy occupancy = new GridOccupancy(profile.numHotseatIcons, 1);
1595 occupancy.cells[(int) item.screenId][0] = true;
1596 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, occupancy);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001597 return true;
1598 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001599 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1600 if (!workspaceScreens.contains((Long) item.screenId)) {
1601 // The item has an invalid screen id.
1602 return false;
1603 }
1604 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001605 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001606 return true;
1607 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001608
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001609 final int countX = profile.numColumns;
1610 final int countY = profile.numRows;
Adam Cohenae4409d2013-11-26 10:34:59 -08001611 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1612 item.cellX < 0 || item.cellY < 0 ||
1613 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1614 Log.e(TAG, "Error loading shortcut " + item
1615 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1616 + item.cellX + "," + item.cellY
1617 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1618 return false;
1619 }
1620
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001621 if (!occupied.containsKey(item.screenId)) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001622 GridOccupancy screen = new GridOccupancy(countX + 1, countY + 1);
1623 if (item.screenId == Workspace.FIRST_SCREEN_ID) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001624 // Mark the first row as occupied (if the feature is enabled)
1625 // in order to account for the QSB.
1626 screen.markCells(0, 0, countX + 1, 1, FeatureFlags.QSB_ON_FIRST_SCREEN);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001627 }
1628 occupied.put(item.screenId, screen);
Joe Onorato36115782010-06-17 13:28:48 -04001629 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001630 final GridOccupancy occupancy = occupied.get(item.screenId);
Winson Chungf30ad5f2011-08-08 10:55:42 -07001631
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001632 // Check if any workspace icons overlap with each other
1633 if (occupancy.isRegionVacant(item.cellX, item.cellY, item.spanX, item.spanY)) {
1634 occupancy.markCells(item, true);
1635 return true;
1636 } else {
1637 Log.e(TAG, "Error loading shortcut " + item
1638 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1639 + item.cellX + "," + item.cellX + "," + item.spanX + "," + item.spanY
1640 + ") already occupied");
1641 return false;
1642 }
Joe Onorato36115782010-06-17 13:28:48 -04001643 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001644
Winson Chungba9c37f2013-08-30 14:11:37 -07001645 /** Clears all the sBg data structures */
1646 private void clearSBgDataStructures() {
1647 synchronized (sBgLock) {
1648 sBgWorkspaceItems.clear();
1649 sBgAppWidgets.clear();
1650 sBgFolders.clear();
1651 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001652 sBgWorkspaceScreens.clear();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001653 sBgPinnedShortcutCounts.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001654 }
1655 }
1656
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001657 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -07001658 if (LauncherAppState.PROFILE_STARTUP) {
1659 Trace.beginSection("Loading Workspace");
1660 }
Joe Onorato36115782010-06-17 13:28:48 -04001661 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001662
Joe Onorato36115782010-06-17 13:28:48 -04001663 final Context context = mContext;
1664 final ContentResolver contentResolver = context.getContentResolver();
1665 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001666 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001667 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001668 final boolean isSdCardReady = Utilities.isBootCompleted();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001669
Winson Chung892c74d2013-08-22 16:15:50 -07001670 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001671 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001672 int countX = profile.numColumns;
1673 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001674
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001675 boolean clearDb = false;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -07001676 try {
1677 ImportDataTask.performImportIfPossible(context);
1678 } catch (Exception e) {
1679 // Migration failed. Clear workspace.
1680 clearDb = true;
1681 }
1682
1683 if (!clearDb && GridSizeMigrationTask.ENABLED &&
Sunny Goyalf076eae2016-01-11 12:25:10 -08001684 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1685 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001686 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001687 }
1688
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001689 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -07001690 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001691 LauncherSettings.Settings.call(contentResolver,
1692 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001693 }
1694
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001695 Log.d(TAG, "loadWorkspace: loading default favorites");
1696 LauncherSettings.Settings.call(contentResolver,
1697 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -07001698
Winson Chung2abf94d2012-07-18 18:16:38 -07001699 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001700 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001701 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001702 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001703 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001704
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001705 final ArrayList<Long> itemsToRemove = new ArrayList<>();
1706 final ArrayList<Long> restoredRows = new ArrayList<>();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001707 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001708 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001709 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001710 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001711
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001712 // +1 for the hotseat (it can be larger than the workspace)
1713 // Load workspace in reverse order to ensure that latest items are loaded first (and
1714 // before any earlier duplicates)
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001715 final LongArrayMap<GridOccupancy> occupied = new LongArrayMap<>();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001716 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001717
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001718 try {
1719 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1720 final int intentIndex = c.getColumnIndexOrThrow
1721 (LauncherSettings.Favorites.INTENT);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001722 final int containerIndex = c.getColumnIndexOrThrow(
1723 LauncherSettings.Favorites.CONTAINER);
1724 final int itemTypeIndex = c.getColumnIndexOrThrow(
1725 LauncherSettings.Favorites.ITEM_TYPE);
1726 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1727 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001728 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1729 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001730 final int screenIndex = c.getColumnIndexOrThrow(
1731 LauncherSettings.Favorites.SCREEN);
1732 final int cellXIndex = c.getColumnIndexOrThrow
1733 (LauncherSettings.Favorites.CELLX);
1734 final int cellYIndex = c.getColumnIndexOrThrow
1735 (LauncherSettings.Favorites.CELLY);
1736 final int spanXIndex = c.getColumnIndexOrThrow
1737 (LauncherSettings.Favorites.SPANX);
1738 final int spanYIndex = c.getColumnIndexOrThrow(
1739 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001740 final int rankIndex = c.getColumnIndexOrThrow(
1741 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001742 final int restoredIndex = c.getColumnIndexOrThrow(
1743 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001744 final int profileIdIndex = c.getColumnIndexOrThrow(
1745 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001746 final int optionsIndex = c.getColumnIndexOrThrow(
1747 LauncherSettings.Favorites.OPTIONS);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001748 final CursorIconInfo cursorIconInfo = new CursorIconInfo(mContext, c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001749
Sunny Goyal7f834d22015-04-21 10:10:23 -07001750 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
Kenny Guyff05f432016-01-22 17:48:29 +00001751 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001752 final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>();
Sunny Goyal7f834d22015-04-21 10:10:23 -07001753 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001754 long serialNo = mUserManager.getSerialNumberForUser(user);
1755 allUsers.put(serialNo, user);
1756 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001757
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001758 boolean userUnlocked = mUserManager.isUserUnlocked(user);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001759
1760 // We can only query for shortcuts when the user is unlocked.
1761 if (userUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07001762 List<ShortcutInfoCompat> pinnedShortcuts =
1763 mDeepShortcutManager.queryForPinnedShortcuts(null, user);
1764 if (mDeepShortcutManager.wasLastCallSuccess()) {
1765 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
1766 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
1767 shortcut);
1768 }
1769 } else {
1770 // Shortcut manager can fail due to some race condition when the
1771 // lock state changes too frequently. For the purpose of the loading
1772 // shortcuts, consider the user is still locked.
1773 userUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001774 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001775 }
Sunny Goyal49f4f032016-08-01 15:45:49 -07001776 unlockedUsers.put(serialNo, userUnlocked);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001777 }
1778
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001779 ShortcutInfo info;
1780 String intentDescription;
1781 LauncherAppWidgetInfo appWidgetInfo;
1782 int container;
1783 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001784 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001785 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001786 UserHandleCompat user;
Sunny Goyald09c3702016-04-06 16:18:20 -07001787 String targetPackage;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001788
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001789 while (!mStopped && c.moveToNext()) {
1790 try {
1791 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001792 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001793 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001794 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001795
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001796 switch (itemType) {
1797 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1798 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001799 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001800 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001801 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001802 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001803 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001804 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001805 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001806 boolean itemReplaced = false;
Sunny Goyald09c3702016-04-06 16:18:20 -07001807 targetPackage = null;
Kenny Guyed131872014-04-30 03:02:21 +01001808 if (user == null) {
1809 // User has been deleted remove the item.
1810 itemsToRemove.add(id);
1811 continue;
1812 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001813 try {
1814 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001815 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001816 if (cn != null && cn.getPackageName() != null) {
1817 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1818 cn.getPackageName(), user);
1819 boolean validComponent = validPkg &&
1820 launcherApps.isActivityEnabledForProfile(cn, user);
Sunny Goyald09c3702016-04-06 16:18:20 -07001821 if (validPkg) {
1822 targetPackage = cn.getPackageName();
1823 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001824
1825 if (validComponent) {
1826 if (restored) {
1827 // no special handling necessary for this item
1828 restoredRows.add(id);
1829 restored = false;
1830 }
Kenny Guyff05f432016-01-22 17:48:29 +00001831 if (quietMode.get(serialNumber)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001832 disabledState = ShortcutInfo.FLAG_DISABLED_QUIET_USER;
Kenny Guyff05f432016-01-22 17:48:29 +00001833 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001834 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001835 intent = null;
1836 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1837 // We allow auto install apps to have their intent
1838 // updated after an install.
1839 intent = manager.getLaunchIntentForPackage(
1840 cn.getPackageName());
1841 if (intent != null) {
1842 ContentValues values = new ContentValues();
1843 values.put(LauncherSettings.Favorites.INTENT,
1844 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001845 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001846 }
1847 }
1848
1849 if (intent == null) {
1850 // The app is installed but the component is no
1851 // longer available.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001852 FileLog.d(TAG, "Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001853 itemsToRemove.add(id);
1854 continue;
1855 } else {
1856 // no special handling necessary for this item
1857 restoredRows.add(id);
1858 restored = false;
1859 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001860 } else if (restored) {
1861 // Package is not yet available but might be
1862 // installed later.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001863 FileLog.d(TAG, "package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001864
1865 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1866 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001867 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001868 // App restore has started. Update the flag
1869 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1870 ContentValues values = new ContentValues();
1871 values.put(LauncherSettings.Favorites.RESTORED,
1872 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001873 updateItem(id, values);
1874 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1875 // This is a common app. Try to replace this.
1876 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1877 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1878 if (parser.findDefaultApp()) {
1879 // Default app found. Replace it.
1880 intent = parser.parsedIntent;
1881 cn = intent.getComponent();
1882 ContentValues values = parser.parsedValues;
1883 values.put(LauncherSettings.Favorites.RESTORED, 0);
1884 updateItem(id, values);
1885 restored = false;
1886 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001887
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001888 } else {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001889 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001890 itemsToRemove.add(id);
1891 continue;
1892 }
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001893 } else {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001894 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001895 itemsToRemove.add(id);
1896 continue;
1897 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001898 } else if (PackageManagerHelper.isAppOnSdcard(
1899 manager, cn.getPackageName())) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07001900 // Package is present but not available.
1901 allowMissingTarget = true;
1902 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1903 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001904 // SdCard is not ready yet. Package might get available,
1905 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001906 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001907 HashSet<String> pkgs = sPendingPackages.get(user);
1908 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001909 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001910 sPendingPackages.put(user, pkgs);
1911 }
1912 pkgs.add(cn.getPackageName());
1913 allowMissingTarget = true;
1914 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001915
1916 } else {
1917 // Do not wait for external media load anymore.
1918 // Log the invalid package, and remove it
Sunny Goyal713edfc2016-05-06 09:58:34 -07001919 FileLog.d(TAG, "Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001920 itemsToRemove.add(id);
1921 continue;
Winson Chungee055712013-07-30 14:46:24 -07001922 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001923 } else if (cn == null) {
1924 // For shortcuts with no component, keep them as they are
1925 restoredRows.add(id);
1926 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001927 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001928 } catch (URISyntaxException e) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001929 FileLog.d(TAG, "Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001930 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001931 continue;
1932 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001933
Sunny Goyal34b65272015-03-11 16:56:52 -07001934 boolean useLowResIcon = container >= 0 &&
1935 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1936
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001937 if (itemReplaced) {
1938 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001939 info = getAppShortcutInfo(intent, user, null,
1940 cursorIconInfo, false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001941 } else {
1942 // Don't replace items for other profiles.
1943 itemsToRemove.add(id);
1944 continue;
1945 }
1946 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001947 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001948 info = getRestoredItemInfo(c, intent,
1949 promiseType, itemType, cursorIconInfo);
Kenny Guyed131872014-04-30 03:02:21 +01001950 intent = getRestoredItemIntent(c, context, intent);
1951 } else {
1952 // Don't restore items for other profiles.
1953 itemsToRemove.add(id);
1954 continue;
1955 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001956 } else if (itemType ==
1957 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001958 info = getAppShortcutInfo(intent, user, c,
1959 cursorIconInfo, allowMissingTarget, useLowResIcon);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001960 } else if (itemType ==
1961 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001962
1963 ShortcutKey key = ShortcutKey.fromIntent(intent, user);
1964 if (unlockedUsers.get(serialNumber)) {
1965 ShortcutInfoCompat pinnedShortcut =
1966 shortcutKeyToPinnedShortcuts.get(key);
1967 if (pinnedShortcut == null) {
1968 // The shortcut is no longer valid.
1969 itemsToRemove.add(id);
1970 continue;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001971 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001972 info = new ShortcutInfo(pinnedShortcut, context);
1973 intent = info.intent;
1974 } else {
1975 // Create a shortcut info in disabled mode for now.
1976 info = new ShortcutInfo();
1977 info.user = user;
1978 info.itemType = itemType;
1979 loadInfoFromCursor(info, c, cursorIconInfo);
1980
1981 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001982 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001983 incrementPinnedShortcutCount(key, false /* shouldPin */);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001984 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001985 info = getShortcutInfo(c, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001986
Sunny Goyald09c3702016-04-06 16:18:20 -07001987 // Shortcuts are only available on the primary profile
1988 if (PackageManagerHelper.isAppSuspended(manager, targetPackage)) {
1989 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1990 }
1991
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001992 // App shortcuts that used to be automatically added to Launcher
1993 // didn't always have the correct intent flags set, so do that
1994 // here
1995 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001996 intent.getCategories() != null &&
1997 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001998 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001999 intent.addFlags(
2000 Intent.FLAG_ACTIVITY_NEW_TASK |
2001 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
2002 }
Michael Jurka96879562012-03-22 05:54:33 -07002003 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002004
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002005 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07002006 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002007 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002008 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002009 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002010 info.cellX = c.getInt(cellXIndex);
2011 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08002012 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002013 info.spanX = 1;
2014 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002015 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002016 if (info.promisedIntent != null) {
2017 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
2018 }
Sunny Goyald09c3702016-04-06 16:18:20 -07002019 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002020 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2021 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2022 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002023
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002024 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002025 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002026 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002027 break;
2028 }
2029
Sunny Goyal756adbc2015-04-16 15:20:51 -07002030 if (restored) {
2031 ComponentName cn = info.getTargetComponent();
2032 if (cn != null) {
2033 Integer progress = installingPkgs.get(cn.getPackageName());
2034 if (progress != null) {
2035 info.setInstallProgress(progress);
2036 } else {
2037 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2038 }
2039 }
2040 }
2041
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002042 switch (container) {
2043 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2044 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2045 sBgWorkspaceItems.add(info);
2046 break;
2047 default:
2048 // Item is in a user folder
2049 FolderInfo folderInfo =
2050 findOrMakeFolder(sBgFolders, container);
Sunny Goyalc52ba712016-04-05 15:59:05 -07002051 folderInfo.add(info, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002052 break;
2053 }
2054 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002055 } else {
2056 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002057 }
2058 break;
2059
2060 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2061 id = c.getLong(idIndex);
2062 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2063
Sunny Goyala508e4f2015-05-21 09:33:57 -07002064 // Do not trim the folder label, as is was set by the user.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002065 folderInfo.title = c.getString(cursorIconInfo.titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002066 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002067 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002068 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002069 folderInfo.cellX = c.getInt(cellXIndex);
2070 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002071 folderInfo.spanX = 1;
2072 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002073 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002074
Daniel Sandler8802e962010-05-26 16:28:16 -04002075 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002076 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002077 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002078 break;
2079 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002080
Joe Onorato9c1289c2009-08-17 11:03:03 -04002081 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002082 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2083 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2084 sBgWorkspaceItems.add(folderInfo);
2085 break;
Joe Onorato36115782010-06-17 13:28:48 -04002086 }
Joe Onorato17a89222011-02-08 17:26:11 -08002087
Chris Wrenf4d08112014-01-16 18:13:56 -05002088 if (restored) {
2089 // no special handling required for restored folders
2090 restoredRows.add(id);
2091 }
2092
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002093 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2094 sBgFolders.put(folderInfo.id, folderInfo);
2095 break;
2096
2097 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002098 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002099 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002100 boolean customWidget = itemType ==
2101 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2102
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002103 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002104 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002105 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002106 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002107 user = allUsers.get(serialNumber);
2108 if (user == null) {
2109 itemsToRemove.add(id);
2110 continue;
2111 }
2112
Sunny Goyalff572272014-07-23 13:58:07 -07002113 final ComponentName component =
2114 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002115
Sunny Goyal651077b2014-06-30 14:15:31 -07002116 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002117 final boolean isIdValid = (restoreStatus &
2118 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002119 final boolean wasProviderReady = (restoreStatus &
2120 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002121
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002122 if (widgetProvidersMap == null) {
2123 widgetProvidersMap = AppWidgetManagerCompat
2124 .getInstance(mContext).getAllProvidersMap();
2125 }
2126 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
2127 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00002128 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002129 user));
Sunny Goyalff572272014-07-23 13:58:07 -07002130
2131 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002132 if (!isSafeMode && !customWidget &&
2133 wasProviderReady && !isProviderReady) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002134 FileLog.d(TAG, "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07002135 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002136 itemsToRemove.add(id);
2137 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002138 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002139 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2140 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002141
Sunny Goyal53f96722015-07-13 19:54:53 -07002142 // The provider is available. So the widget is either
2143 // available or not available. We do not need to track
2144 // any future restore updates.
2145 int status = restoreStatus &
2146 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002147 if (!wasProviderReady) {
2148 // If provider was not previously ready, update the
2149 // status and UI flag.
2150
2151 // Id would be valid only if the widget restore broadcast was received.
2152 if (isIdValid) {
Sunny Goyal86df1382016-08-10 15:03:22 -07002153 status |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002154 } else {
2155 status &= ~LauncherAppWidgetInfo
2156 .FLAG_PROVIDER_NOT_READY;
2157 }
2158 }
2159 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002160 } else {
2161 Log.v(TAG, "Widget restore pending id=" + id
2162 + " appWidgetId=" + appWidgetId
2163 + " status =" + restoreStatus);
2164 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002165 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002166 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002167 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002168
2169 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2170 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002171 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002172 // App restore has started. Update the flag
2173 appWidgetInfo.restoreStatus |=
2174 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalb05a00a2016-08-29 10:06:57 -07002175 } else if (!isSafeMode) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002176 FileLog.d(TAG, "Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07002177 itemsToRemove.add(id);
2178 continue;
2179 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002180
2181 appWidgetInfo.installProgress =
2182 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002183 }
Sunny Goyal86df1382016-08-10 15:03:22 -07002184 if (appWidgetInfo.hasRestoreFlag(
2185 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG)) {
2186 intentDescription = c.getString(intentIndex);
2187 if (!TextUtils.isEmpty(intentDescription)) {
2188 appWidgetInfo.bindOptions =
2189 Intent.parseUri(intentDescription, 0);
2190 }
2191 }
Sunny Goyalff572272014-07-23 13:58:07 -07002192
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002193 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002194 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002195 appWidgetInfo.cellX = c.getInt(cellXIndex);
2196 appWidgetInfo.cellY = c.getInt(cellYIndex);
2197 appWidgetInfo.spanX = c.getInt(spanXIndex);
2198 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002199 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002200
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002201 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2202 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2203 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002204 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2205 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002206 continue;
2207 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002208
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002209 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002210 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002211 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002212 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002213 break;
2214 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002215
Adam Cohen59400422014-03-05 18:07:04 -08002216 if (!customWidget) {
2217 String providerName =
2218 appWidgetInfo.providerName.flattenToString();
2219 if (!providerName.equals(savedProvider) ||
2220 (appWidgetInfo.restoreStatus != restoreStatus)) {
2221 ContentValues values = new ContentValues();
2222 values.put(
2223 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2224 providerName);
2225 values.put(LauncherSettings.Favorites.RESTORED,
2226 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002227 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002228 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002229 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002230 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2231 sBgAppWidgets.add(appWidgetInfo);
2232 }
Joe Onorato36115782010-06-17 13:28:48 -04002233 break;
2234 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002235 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002236 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002237 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002238 }
2239 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002240 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002241 }
2242
Winson Chungba9c37f2013-08-30 14:11:37 -07002243 // Break early if we've stopped loading
2244 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002245 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002246 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002247 }
2248
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002249 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002250 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002251 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2252 Utilities.createDbSelectionQuery(
2253 LauncherSettings.Favorites._ID, itemsToRemove), null);
2254 if (DEBUG_LOADERS) {
2255 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2256 LauncherSettings.Favorites._ID, itemsToRemove));
2257 }
2258
2259 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07002260 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
2261 .call(contentResolver,
2262 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
2263 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
2264 for (long folderId : deletedFolderIds) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002265 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2266 sBgFolders.remove(folderId);
2267 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002268 }
2269 }
2270
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002271 // Unpin shortcuts that don't exist on the workspace.
2272 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
2273 MutableInt numTimesPinned = sBgPinnedShortcutCounts.get(key);
2274 if (numTimesPinned == null || numTimesPinned.value == 0) {
2275 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
2276 mDeepShortcutManager.unpinShortcut(key);
2277 }
2278 }
2279
Sunny Goyal317698b2015-07-29 11:45:41 -07002280 // Sort all the folder items and make sure the first 3 items are high resolution.
2281 for (FolderInfo folder : sBgFolders) {
2282 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2283 int pos = 0;
2284 for (ShortcutInfo info : folder.contents) {
2285 if (info.usingLowResIcon) {
2286 info.updateIcon(mIconCache, false);
2287 }
2288 pos ++;
2289 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2290 break;
2291 }
2292 }
2293 }
2294
Chris Wrenf4d08112014-01-16 18:13:56 -05002295 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002296 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002297 ContentValues values = new ContentValues();
2298 values.put(LauncherSettings.Favorites.RESTORED, 0);
2299 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2300 Utilities.createDbSelectionQuery(
2301 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002302 }
2303
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002304 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2305 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002306 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002307 null, sWorker);
2308 }
2309
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002310 // Remove any empty screens
2311 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002312 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002313 long screenId = item.screenId;
2314 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2315 unusedScreens.contains(screenId)) {
2316 unusedScreens.remove(screenId);
2317 }
2318 }
2319
2320 // If there are any empty screens remove them, and update.
2321 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002322 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002323 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002324 }
2325
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002326 if (DEBUG_LOADERS) {
2327 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2328 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002329 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002330 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002331 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002332
Sunny Goyale2df0622015-04-24 11:27:00 -07002333 for (int i = 0; i < nScreens; i++) {
2334 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002335 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002336 line += " | ";
2337 }
Joe Onorato36115782010-06-17 13:28:48 -04002338 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002339 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002340 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002341 }
Joe Onorato36115782010-06-17 13:28:48 -04002342 }
Sunny Goyale26d1002016-06-20 14:52:14 -07002343 if (LauncherAppState.PROFILE_STARTUP) {
2344 Trace.endSection();
2345 }
Adam Cohene25af792013-06-06 23:08:25 -07002346 }
2347
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002348 /**
2349 * Partially updates the item without any notification. Must be called on the worker thread.
2350 */
2351 private void updateItem(long itemId, ContentValues update) {
2352 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002353 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002354 update,
2355 BaseColumns._ID + "= ?",
2356 new String[]{Long.toString(itemId)});
2357 }
2358
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002359 /** Filters the set of items who are directly or indirectly (via another container) on the
2360 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002361 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002362 ArrayList<ItemInfo> allWorkspaceItems,
2363 ArrayList<ItemInfo> currentScreenItems,
2364 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002365 // Purge any null ItemInfos
2366 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2367 while (iter.hasNext()) {
2368 ItemInfo i = iter.next();
2369 if (i == null) {
2370 iter.remove();
2371 }
2372 }
2373
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002374 // Order the set of items by their containers first, this allows use to walk through the
2375 // list sequentially, build up a list of containers that are in the specified screen,
2376 // as well as all items in those containers.
2377 Set<Long> itemsOnScreen = new HashSet<Long>();
2378 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2379 @Override
2380 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07002381 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002382 }
2383 });
2384 for (ItemInfo info : allWorkspaceItems) {
2385 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002386 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002387 currentScreenItems.add(info);
2388 itemsOnScreen.add(info.id);
2389 } else {
2390 otherScreenItems.add(info);
2391 }
2392 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2393 currentScreenItems.add(info);
2394 itemsOnScreen.add(info.id);
2395 } else {
2396 if (itemsOnScreen.contains(info.container)) {
2397 currentScreenItems.add(info);
2398 itemsOnScreen.add(info.id);
2399 } else {
2400 otherScreenItems.add(info);
2401 }
2402 }
2403 }
2404 }
2405
2406 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002407 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002408 ArrayList<LauncherAppWidgetInfo> appWidgets,
2409 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2410 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002411
2412 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002413 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002414 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002415 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002416 currentScreenWidgets.add(widget);
2417 } else {
2418 otherScreenWidgets.add(widget);
2419 }
2420 }
2421 }
2422
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002423 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2424 * right) */
2425 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002426 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002427 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002428 final int screenCols = profile.numColumns;
2429 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002430 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002431 @Override
2432 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002433 if (lhs.container == rhs.container) {
2434 // Within containers, order by their spatial position in that container
2435 switch ((int) lhs.container) {
2436 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2437 long lr = (lhs.screenId * screenCellCount +
2438 lhs.cellY * screenCols + lhs.cellX);
2439 long rr = (rhs.screenId * screenCellCount +
2440 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07002441 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07002442 }
2443 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2444 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07002445 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07002446 }
2447 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002448 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002449 throw new RuntimeException("Unexpected container type when " +
2450 "sorting workspace items.");
2451 }
2452 return 0;
2453 }
2454 } else {
2455 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07002456 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07002457 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002458 }
2459 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002460 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002461
Adam Cohendcd297f2013-06-18 13:13:40 -07002462 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2463 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002464 final Runnable r = new Runnable() {
2465 @Override
2466 public void run() {
2467 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2468 if (callbacks != null) {
2469 callbacks.bindScreens(orderedScreens);
2470 }
2471 }
2472 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002473 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002474 }
2475
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002476 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2477 final ArrayList<ItemInfo> workspaceItems,
2478 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002479 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002480
2481 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002482 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002483 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002484 final int start = i;
2485 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002486 final Runnable r = new Runnable() {
2487 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002488 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002489 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002490 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002491 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2492 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002493 }
2494 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002495 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002496 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002497 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002498
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002499 // Bind the widgets, one at a time
2500 N = appWidgets.size();
2501 for (int i = 0; i < N; i++) {
2502 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2503 final Runnable r = new Runnable() {
2504 public void run() {
2505 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2506 if (callbacks != null) {
2507 callbacks.bindAppWidget(widget);
2508 }
2509 }
2510 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002511 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002512 }
2513 }
2514
2515 /**
2516 * Binds all loaded data to actual views on the main thread.
2517 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002518 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002519 final long t = SystemClock.uptimeMillis();
2520 Runnable r;
2521
2522 // Don't use these two variables in any of the callback runnables.
2523 // Otherwise we hold a reference to them.
2524 final Callbacks oldCallbacks = mCallbacks.get();
2525 if (oldCallbacks == null) {
2526 // This launcher has exited and nobody bothered to tell us. Just bail.
2527 Log.w(TAG, "LoaderTask running with no launcher");
2528 return;
2529 }
2530
Winson Chung9b9fb962013-11-15 15:39:34 -08002531 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07002532 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
2533 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
2534 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002535
Winson Chung2abf94d2012-07-18 18:16:38 -07002536 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002537 workspaceItems.addAll(sBgWorkspaceItems);
2538 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002539 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002540 }
2541
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002542 final int currentScreen;
2543 {
2544 int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE
2545 ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen();
2546 if (currScreen >= orderedScreenIds.size()) {
2547 // There may be no workspace screens (just hotseat items and an empty page).
2548 currScreen = PagedView.INVALID_RESTORE_PAGE;
2549 }
2550 currentScreen = currScreen;
Winson Chung9b9fb962013-11-15 15:39:34 -08002551 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002552 final boolean validFirstPage = currentScreen >= 0;
2553 final long currentScreenId =
2554 validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID;
Winson Chung9b9fb962013-11-15 15:39:34 -08002555
Winson Chung9b9fb962013-11-15 15:39:34 -08002556 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07002557 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
2558 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
2559 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
2560 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002561
Winson Chung9b9fb962013-11-15 15:39:34 -08002562 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002563 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002564 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002565 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002566 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2567 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2568
2569 // Tell the workspace that we're about to start binding items
2570 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002571 public void run() {
2572 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2573 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002574 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002575 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002576 }
2577 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002578 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002579 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002580
Adam Cohendcd297f2013-06-18 13:13:40 -07002581 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2582
Sunny Goyal527c7d32015-08-28 15:19:36 -07002583 Executor mainExecutor = new DeferredMainThreadExecutor();
2584 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07002585 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002586
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002587 // In case of validFirstPage, only bind the first screen, and defer binding the
2588 // remaining screens after first onDraw (and an optional the fade animation whichever
2589 // happens later).
2590 // This ensures that the first screen is immediately visible (eg. during rotation)
2591 // In case of !validFirstPage, bind all pages one after other.
2592 final Executor deferredExecutor =
2593 validFirstPage ? new ViewOnDrawExecutor(mHandler) : mainExecutor;
2594
2595 mainExecutor.execute(new Runnable() {
2596 @Override
2597 public void run() {
2598 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2599 if (callbacks != null) {
2600 callbacks.finishFirstPageBind(
2601 validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null);
2602 }
2603 }
2604 });
Sunny Goyal527c7d32015-08-28 15:19:36 -07002605
Sunny Goyal44c06432016-04-02 10:56:02 -07002606 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002607
2608 // Tell the workspace that we're done binding items
2609 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002610 public void run() {
2611 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2612 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002613 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002614 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002615
Sunny Goyal639e9062015-08-19 19:17:06 -07002616 mIsLoadingAndBindingWorkspace = false;
2617
2618 // Run all the bind complete runnables after workspace is bound.
2619 if (!mBindCompleteRunnables.isEmpty()) {
2620 synchronized (mBindCompleteRunnables) {
2621 for (final Runnable r : mBindCompleteRunnables) {
2622 runOnWorkerThread(r);
2623 }
2624 mBindCompleteRunnables.clear();
2625 }
2626 }
2627
Winson Chung98e030b2012-05-07 16:01:11 -07002628 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002629 if (DEBUG_LOADERS) {
2630 Log.d(TAG, "bound workspace in "
2631 + (SystemClock.uptimeMillis()-t) + "ms");
2632 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002633
Joe Onorato36115782010-06-17 13:28:48 -04002634 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002635 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002636 deferredExecutor.execute(r);
2637
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002638 if (validFirstPage) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002639 r = new Runnable() {
2640 public void run() {
2641 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2642 if (callbacks != null) {
2643 // We are loading synchronously, which means, some of the pages will be
2644 // bound after first draw. Inform the callbacks that page binding is
2645 // not complete, and schedule the remaining pages.
2646 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2647 callbacks.onPageBoundSynchronously(currentScreen);
2648 }
2649 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2650 }
2651 }
2652 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002653 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002654 }
Joe Onorato36115782010-06-17 13:28:48 -04002655 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002656
Joe Onorato36115782010-06-17 13:28:48 -04002657 private void loadAndBindAllApps() {
2658 if (DEBUG_LOADERS) {
2659 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2660 }
2661 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002662 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002663 synchronized (LoaderTask.this) {
2664 if (mStopped) {
2665 return;
2666 }
2667 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002668 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002669 synchronized (LoaderTask.this) {
2670 if (mStopped) {
2671 return;
2672 }
2673 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002674 }
Joe Onorato36115782010-06-17 13:28:48 -04002675 } else {
2676 onlyBindAllApps();
2677 }
2678 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002679
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002680 private void updateIconCache() {
2681 // Ignore packages which have a promise icon.
2682 HashSet<String> packagesToIgnore = new HashSet<>();
2683 synchronized (sBgLock) {
2684 for (ItemInfo info : sBgItemsIdMap) {
2685 if (info instanceof ShortcutInfo) {
2686 ShortcutInfo si = (ShortcutInfo) info;
2687 if (si.isPromise() && si.getTargetComponent() != null) {
2688 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2689 }
2690 } else if (info instanceof LauncherAppWidgetInfo) {
2691 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2692 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2693 packagesToIgnore.add(lawi.providerName.getPackageName());
2694 }
2695 }
2696 }
2697 }
2698 mIconCache.updateDbIcons(packagesToIgnore);
2699 }
2700
Joe Onorato36115782010-06-17 13:28:48 -04002701 private void onlyBindAllApps() {
2702 final Callbacks oldCallbacks = mCallbacks.get();
2703 if (oldCallbacks == null) {
2704 // This launcher has exited and nobody bothered to tell us. Just bail.
2705 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2706 return;
2707 }
2708
2709 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002710 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002711 final ArrayList<AppInfo> list
2712 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002713 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002714 public void run() {
2715 final long t = SystemClock.uptimeMillis();
2716 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2717 if (callbacks != null) {
2718 callbacks.bindAllApplications(list);
2719 }
2720 if (DEBUG_LOADERS) {
2721 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002722 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04002723 }
2724 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002725 };
Tony Wickham80f57872016-06-29 18:12:15 -07002726 runOnMainThread(r);
Joe Onorato36115782010-06-17 13:28:48 -04002727 }
2728
Winson Chung64359a52013-07-08 17:17:08 -07002729 private void loadAllApps() {
2730 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002731
Joe Onorato36115782010-06-17 13:28:48 -04002732 final Callbacks oldCallbacks = mCallbacks.get();
2733 if (oldCallbacks == null) {
2734 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002735 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002736 return;
2737 }
2738
Kenny Guyed131872014-04-30 03:02:21 +01002739 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2740
Winson Chung64359a52013-07-08 17:17:08 -07002741 // Clear the list of apps
2742 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002743 for (UserHandleCompat user : profiles) {
2744 // Query for the set of apps
2745 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002746 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002747 if (DEBUG_LOADERS) {
2748 Log.d(TAG, "getActivityList took "
2749 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2750 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2751 }
2752 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002753 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002754 if (apps == null || apps.isEmpty()) {
2755 return;
2756 }
Kenny Guyff05f432016-01-22 17:48:29 +00002757 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002758 // Create the ApplicationInfos
2759 for (int i = 0; i < apps.size(); i++) {
2760 LauncherActivityInfoCompat app = apps.get(i);
2761 // This builds the icon bitmaps.
Kenny Guyff05f432016-01-22 17:48:29 +00002762 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode));
Kenny Guyed131872014-04-30 03:02:21 +01002763 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002764
Sunny Goyal756a28a2015-04-23 17:07:55 -07002765 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2766 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002767 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002768
2769 @Override
2770 public void run() {
2771 heuristic.processUserApps(apps);
2772 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002773 };
2774 runOnMainThread(new Runnable() {
2775
2776 @Override
2777 public void run() {
2778 // Check isLoadingWorkspace on the UI thread, as it is updated on
2779 // the UI thread.
2780 if (mIsLoadingAndBindingWorkspace) {
2781 synchronized (mBindCompleteRunnables) {
2782 mBindCompleteRunnables.add(r);
2783 }
2784 } else {
2785 runOnWorkerThread(r);
2786 }
2787 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002788 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002789 }
Winson Chung64359a52013-07-08 17:17:08 -07002790 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002791 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002792 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2793 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002794
2795 // Post callback on main thread
2796 mHandler.post(new Runnable() {
2797 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002798
Winson Chung64359a52013-07-08 17:17:08 -07002799 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002800 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002801 if (callbacks != null) {
2802 callbacks.bindAllApplications(added);
2803 if (DEBUG_LOADERS) {
2804 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002805 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07002806 }
2807 } else {
2808 Log.i(TAG, "not binding apps: no Launcher activity");
2809 }
2810 }
2811 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002812 // Cleanup any data stored for a deleted user.
2813 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04002814 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002815 Log.d(TAG, "Icons processed in "
2816 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002817 }
2818 }
2819
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002820 private void loadAndBindDeepShortcuts() {
2821 if (DEBUG_LOADERS) {
2822 Log.d(TAG, "loadAndBindDeepShortcuts mDeepShortcutsLoaded=" + mDeepShortcutsLoaded);
2823 }
2824 if (!mDeepShortcutsLoaded) {
2825 mBgDeepShortcutMap.clear();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002826 mHasShortcutHostPermission = mDeepShortcutManager.hasHostPermission();
2827 if (mHasShortcutHostPermission) {
2828 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
2829 if (mUserManager.isUserUnlocked(user)) {
2830 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager
2831 .queryForAllShortcuts(user);
2832 updateDeepShortcutMap(null, user, shortcuts);
2833 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002834 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002835 }
2836 synchronized (LoaderTask.this) {
2837 if (mStopped) {
2838 return;
2839 }
2840 mDeepShortcutsLoaded = true;
2841 }
2842 }
Tony Wickham80f57872016-06-29 18:12:15 -07002843 bindDeepShortcuts();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002844 }
2845
Joe Onoratobe386092009-11-17 17:32:16 -08002846 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002847 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002848 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002849 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2850 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2851 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2852 }
Joe Onorato36115782010-06-17 13:28:48 -04002853 }
2854 }
2855
Sunny Goyal860538d2016-07-20 12:35:16 -07002856 /**
2857 * Clear all the shortcuts for the given package, and re-add the new shortcuts.
2858 */
2859 private void updateDeepShortcutMap(
2860 String packageName, UserHandleCompat user, List<ShortcutInfoCompat> shortcuts) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002861 if (packageName != null) {
2862 Iterator<ComponentKey> keysIter = mBgDeepShortcutMap.keySet().iterator();
2863 while (keysIter.hasNext()) {
Sunny Goyal860538d2016-07-20 12:35:16 -07002864 ComponentKey next = keysIter.next();
2865 if (next.componentName.getPackageName().equals(packageName)
2866 && next.user.equals(user)) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002867 keysIter.remove();
2868 }
2869 }
2870 }
2871
2872 // Now add the new shortcuts to the map.
2873 for (ShortcutInfoCompat shortcut : shortcuts) {
Tony Wickhamca258b32016-07-28 12:31:28 -07002874 boolean shouldShowInContainer = shortcut.isEnabled()
2875 && (shortcut.isDeclaredInManifest() || shortcut.isDynamic());
2876 if (shouldShowInContainer) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002877 ComponentKey targetComponent
2878 = new ComponentKey(shortcut.getActivity(), shortcut.getUserHandle());
2879 mBgDeepShortcutMap.addToList(targetComponent, shortcut.getId());
2880 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002881 }
2882 }
2883
Tony Wickham80f57872016-06-29 18:12:15 -07002884 public void bindDeepShortcuts() {
Sunny Goyal5f064012016-08-08 10:10:01 -07002885 final MultiHashMap<ComponentKey, String> shortcutMapCopy = mBgDeepShortcutMap.clone();
Tony Wickham80f57872016-06-29 18:12:15 -07002886 Runnable r = new Runnable() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002887 @Override
2888 public void run() {
2889 Callbacks callbacks = getCallback();
2890 if (callbacks != null) {
2891 callbacks.bindDeepShortcutMap(shortcutMapCopy);
2892 }
2893 }
Tony Wickham80f57872016-06-29 18:12:15 -07002894 };
2895 runOnMainThread(r);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002896 }
2897
Sunny Goyal75b0f552015-05-20 21:57:06 -07002898 /**
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002899 * Refreshes the cached shortcuts if the shortcut permission has changed.
2900 * Current implementation simply reloads the workspace, but it can be optimized to
2901 * use partial updates similar to {@link UserManagerCompat}
2902 */
2903 public void refreshShortcutsIfRequired() {
2904 if (Utilities.isNycMR1OrAbove()) {
2905 sWorker.removeCallbacks(mShortcutPermissionCheckRunnable);
2906 sWorker.post(mShortcutPermissionCheckRunnable);
2907 }
2908 }
2909
2910 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -07002911 * Called when the icons for packages have been updated in the icon cache.
2912 */
2913 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2914 final Callbacks callbacks = getCallback();
2915 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2916 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2917
2918 // If any package icon has changed (app was updated while launcher was dead),
2919 // update the corresponding shortcuts.
2920 synchronized (sBgLock) {
2921 for (ItemInfo info : sBgItemsIdMap) {
2922 if (info instanceof ShortcutInfo && user.equals(info.user)
2923 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2924 ShortcutInfo si = (ShortcutInfo) info;
2925 ComponentName cn = si.getTargetComponent();
2926 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2927 si.updateIcon(mIconCache);
2928 updatedShortcuts.add(si);
2929 }
2930 }
2931 }
2932 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2933 }
2934
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002935 bindUpdatedShortcuts(updatedShortcuts, user);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002936
2937 if (!updatedApps.isEmpty()) {
2938 mHandler.post(new Runnable() {
2939
2940 public void run() {
2941 Callbacks cb = getCallback();
2942 if (cb != null && callbacks == cb) {
2943 cb.bindAppsUpdated(updatedApps);
2944 }
2945 }
2946 });
2947 }
2948 }
2949
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002950 private void bindUpdatedShortcuts(
2951 ArrayList<ShortcutInfo> updatedShortcuts, UserHandleCompat user) {
2952 bindUpdatedShortcuts(updatedShortcuts, new ArrayList<ShortcutInfo>(), user);
2953 }
2954
2955 private void bindUpdatedShortcuts(
2956 final ArrayList<ShortcutInfo> updatedShortcuts,
2957 final ArrayList<ShortcutInfo> removedShortcuts,
2958 final UserHandleCompat user) {
2959 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002960 final Callbacks callbacks = getCallback();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002961 mHandler.post(new Runnable() {
2962
2963 public void run() {
2964 Callbacks cb = getCallback();
2965 if (cb != null && callbacks == cb) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002966 cb.bindShortcutsChanged(updatedShortcuts, removedShortcuts, user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002967 }
2968 }
2969 });
2970 }
2971 }
2972
2973 void enqueueItemUpdatedTask(Runnable task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002974 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002975 }
2976
Adam Cohen091440a2015-03-18 14:16:05 -07002977 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002978
2979 @Override
2980 public void onReceive(Context context, Intent intent) {
2981 synchronized (sBgLock) {
2982 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2983 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002984 final PackageManager manager = context.getPackageManager();
2985 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2986 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002987 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2988 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002989 packagesRemoved.clear();
2990 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002991 for (String pkg : entry.getValue()) {
2992 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07002993 if (PackageManagerHelper.isAppOnSdcard(manager, pkg)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002994 packagesUnavailable.add(pkg);
2995 } else {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002996 packagesRemoved.add(pkg);
2997 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002998 }
2999 }
3000 if (!packagesRemoved.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003001 enqueueItemUpdatedTask(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003002 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
3003 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003004 if (!packagesUnavailable.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003005 enqueueItemUpdatedTask(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
Sunny Goyal1a745e82014-10-02 15:58:31 -07003006 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
3007 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003008 }
Sunny Goyal34942622014-08-29 17:20:55 -07003009 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003010 }
3011 }
3012 }
3013
Joe Onorato36115782010-06-17 13:28:48 -04003014 private class PackageUpdatedTask implements Runnable {
3015 int mOp;
3016 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01003017 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04003018
3019 public static final int OP_NONE = 0;
3020 public static final int OP_ADD = 1;
3021 public static final int OP_UPDATE = 2;
3022 public static final int OP_REMOVE = 3; // uninstlled
3023 public static final int OP_UNAVAILABLE = 4; // external media unmounted
Kenny Guy44cba692016-01-21 19:50:02 +00003024 public static final int OP_SUSPEND = 5; // package suspended
3025 public static final int OP_UNSUSPEND = 6; // package unsuspended
Sunny Goyalda891c12016-03-18 18:29:24 -07003026 public static final int OP_USER_AVAILABILITY_CHANGE = 7; // user available/unavailable
Joe Onorato36115782010-06-17 13:28:48 -04003027
Kenny Guyed131872014-04-30 03:02:21 +01003028 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04003029 mOp = op;
3030 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01003031 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04003032 }
3033
3034 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07003035 if (!mHasLoaderCompletedOnce) {
3036 // Loader has not yet run.
3037 return;
3038 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04003039 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04003040
3041 final String[] packages = mPackages;
3042 final int N = packages.length;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003043 FlagOp flagOp = FlagOp.NO_OP;
Sunny Goyalda891c12016-03-18 18:29:24 -07003044 StringFilter pkgFilter = StringFilter.of(new HashSet<>(Arrays.asList(packages)));
Joe Onorato36115782010-06-17 13:28:48 -04003045 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003046 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04003047 for (int i=0; i<N; i++) {
3048 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003049 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003050 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003051 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003052
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003053 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3054 if (heuristic != null) {
3055 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003056 }
Joe Onorato36115782010-06-17 13:28:48 -04003057 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003058 }
Joe Onorato36115782010-06-17 13:28:48 -04003059 case OP_UPDATE:
3060 for (int i=0; i<N; i++) {
3061 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003062 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003063 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003064 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003065 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003066 // Since package was just updated, the target must be available now.
3067 flagOp = FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04003068 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003069 case OP_REMOVE: {
3070 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3071 if (heuristic != null) {
3072 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003073 }
Joe Onorato36115782010-06-17 13:28:48 -04003074 for (int i=0; i<N; i++) {
3075 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003076 mIconCache.removeIconsForPkg(packages[i], mUser);
3077 }
3078 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003079 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003080 case OP_UNAVAILABLE:
3081 for (int i=0; i<N; i++) {
3082 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3083 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003084 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003085 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003086 flagOp = FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04003087 break;
Kenny Guy44cba692016-01-21 19:50:02 +00003088 case OP_SUSPEND:
3089 case OP_UNSUSPEND:
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003090 flagOp = mOp == OP_SUSPEND ?
3091 FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED) :
3092 FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED);
Sunny Goyal17763cb2016-03-24 13:53:22 -07003093 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.(un)suspend " + N);
Sunny Goyalda891c12016-03-18 18:29:24 -07003094 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
3095 break;
3096 case OP_USER_AVAILABILITY_CHANGE:
3097 flagOp = UserManagerCompat.getInstance(context).isQuietModeEnabled(mUser)
3098 ? FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER)
3099 : FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER);
3100 // We want to update all packages for this user.
3101 pkgFilter = StringFilter.matchesAll();
3102 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
Kenny Guy44cba692016-01-21 19:50:02 +00003103 break;
Joe Onorato36115782010-06-17 13:28:48 -04003104 }
3105
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003106 ArrayList<AppInfo> added = null;
3107 ArrayList<AppInfo> modified = null;
3108 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003109
Adam Cohen487f7dd2012-06-28 18:12:10 -07003110 if (mBgAllAppsList.added.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003111 added = new ArrayList<>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003112 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003113 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003114 if (mBgAllAppsList.modified.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003115 modified = new ArrayList<>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003116 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003117 }
Winson Chung5d55f332012-07-16 20:45:03 -07003118 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003119 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003120 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003121 }
3122
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003123 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps = new HashMap<>();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003124
Joe Onorato36115782010-06-17 13:28:48 -04003125 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003126 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003127 for (AppInfo ai : added) {
3128 addedOrUpdatedApps.put(ai.componentName, ai);
3129 }
Joe Onorato36115782010-06-17 13:28:48 -04003130 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003131
Joe Onorato36115782010-06-17 13:28:48 -04003132 if (modified != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003133 final Callbacks callbacks = getCallback();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003134 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003135 for (AppInfo ai : modified) {
3136 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003137 }
3138
Joe Onorato36115782010-06-17 13:28:48 -04003139 mHandler.post(new Runnable() {
3140 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003141 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003142 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003143 callbacks.bindAppsUpdated(modifiedFinal);
3144 }
3145 }
3146 });
3147 }
Winson Chung83892cc2013-05-01 16:53:33 -07003148
Sunny Goyal4390ace2014-10-13 11:33:11 -07003149 // Update shortcut infos
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003150 if (mOp == OP_ADD || flagOp != FlagOp.NO_OP) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003151 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3152 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3153 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3154
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003155 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003156 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003157 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3158 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003159 boolean infoUpdated = false;
3160 boolean shortcutUpdated = false;
3161
3162 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003163 if ((si.iconResource != null)
Sunny Goyalda891c12016-03-18 18:29:24 -07003164 && pkgFilter.matches(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003165 Bitmap icon = Utilities.createIconBitmap(
3166 si.iconResource.packageName,
3167 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003168 if (icon != null) {
3169 si.setIcon(icon);
3170 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003171 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003172 }
3173 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003174
3175 ComponentName cn = si.getTargetComponent();
Sunny Goyalda891c12016-03-18 18:29:24 -07003176 if (cn != null && pkgFilter.matches(cn.getPackageName())) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003177 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3178
3179 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003180 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3181 // Auto install icon
3182 PackageManager pm = context.getPackageManager();
3183 ResolveInfo matched = pm.resolveActivity(
3184 new Intent(Intent.ACTION_MAIN)
3185 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3186 PackageManager.MATCH_DEFAULT_ONLY);
3187 if (matched == null) {
3188 // Try to find the best match activity.
3189 Intent intent = pm.getLaunchIntentForPackage(
3190 cn.getPackageName());
3191 if (intent != null) {
3192 cn = intent.getComponent();
3193 appInfo = addedOrUpdatedApps.get(cn);
3194 }
3195
3196 if ((intent == null) || (appInfo == null)) {
3197 removedShortcuts.add(si);
3198 continue;
3199 }
3200 si.promisedIntent = intent;
3201 }
3202 }
3203
3204 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003205 if (appInfo != null) {
3206 si.flags = appInfo.flags;
3207 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003208
Sunny Goyal756adbc2015-04-16 15:20:51 -07003209 si.intent = si.promisedIntent;
3210 si.promisedIntent = null;
3211 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003212 infoUpdated = true;
3213 si.updateIcon(mIconCache);
3214 }
3215
3216 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3217 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3218 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003219 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003220 si.contentDescription = appInfo.contentDescription;
3221 infoUpdated = true;
3222 }
3223
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003224 int oldDisabledFlags = si.isDisabled;
3225 si.isDisabled = flagOp.apply(si.isDisabled);
3226 if (si.isDisabled != oldDisabledFlags) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003227 shortcutUpdated = true;
3228 }
3229 }
3230
3231 if (infoUpdated || shortcutUpdated) {
3232 updatedShortcuts.add(si);
3233 }
3234 if (infoUpdated) {
3235 updateItemInDatabase(context, si);
3236 }
Sunny Goyalda891c12016-03-18 18:29:24 -07003237 } else if (info instanceof LauncherAppWidgetInfo && mOp == OP_ADD) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003238 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3239 if (mUser.equals(widgetInfo.user)
3240 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
Sunny Goyalda891c12016-03-18 18:29:24 -07003241 && pkgFilter.matches(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003242 widgetInfo.restoreStatus &=
3243 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3244 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003245
3246 // adding this flag ensures that launcher shows 'click to setup'
3247 // if the widget has a config activity. In case there is no config
3248 // activity, it will be marked as 'restored' during bind.
3249 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3250
Sunny Goyal4390ace2014-10-13 11:33:11 -07003251 widgets.add(widgetInfo);
3252 updateItemInDatabase(context, widgetInfo);
3253 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003254 }
3255 }
3256 }
3257
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003258 bindUpdatedShortcuts(updatedShortcuts, removedShortcuts, mUser);
3259 if (!removedShortcuts.isEmpty()) {
3260 deleteItemsFromDatabase(context, removedShortcuts);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003261 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003262
Sunny Goyal4390ace2014-10-13 11:33:11 -07003263 if (!widgets.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003264 final Callbacks callbacks = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003265 mHandler.post(new Runnable() {
3266 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003267 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003268 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003269 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003270 }
3271 }
3272 });
3273 }
3274 }
3275
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003276 final HashSet<String> removedPackages = new HashSet<>();
3277 final HashSet<ComponentName> removedComponents = new HashSet<>();
3278 if (mOp == OP_REMOVE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003279 // Mark all packages in the broadcast to be removed
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003280 Collections.addAll(removedPackages, packages);
3281
3282 // No need to update the removedComponents as
3283 // removedPackages is a super-set of removedComponents
Winson Chungdf95eb12013-10-16 14:57:07 -07003284 } else if (mOp == OP_UPDATE) {
3285 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003286 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003287 if (isPackageDisabled(context, packages[i], mUser)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003288 removedPackages.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003289 }
3290 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003291
3292 // Update removedComponents as some components can get removed during package update
3293 for (AppInfo info : removedApps) {
3294 removedComponents.add(info.componentName);
3295 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003296 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003297
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003298 if (!removedPackages.isEmpty() || !removedComponents.isEmpty()) {
3299 for (String pn : removedPackages) {
3300 deletePackageFromDatabase(context, pn, mUser);
3301 }
3302 for (ComponentName cn : removedComponents) {
3303 deleteItemsFromDatabase(context, getItemInfoForComponentName(cn, mUser));
Sunny Goyal1a745e82014-10-02 15:58:31 -07003304 }
3305
Winson Chungdf95eb12013-10-16 14:57:07 -07003306 // Remove any queued items from the install queue
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003307 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackages, mUser);
3308
Winson Chungdf95eb12013-10-16 14:57:07 -07003309 // Call the components-removed callback
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003310 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003311 mHandler.post(new Runnable() {
3312 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003313 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003314 if (callbacks == cb && cb != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003315 callbacks.bindWorkspaceComponentsRemoved(
3316 removedPackages, removedComponents, mUser);
3317 }
3318 }
3319 });
3320 }
3321
3322 if (!removedApps.isEmpty()) {
3323 // Remove corresponding apps from All-Apps
3324 final Callbacks callbacks = getCallback();
3325 mHandler.post(new Runnable() {
3326 public void run() {
3327 Callbacks cb = getCallback();
3328 if (callbacks == cb && cb != null) {
3329 callbacks.bindAppInfosRemoved(removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04003330 }
3331 }
3332 });
Joe Onoratobe386092009-11-17 17:32:16 -08003333 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003334
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003335 // Notify launcher of widget update. From marshmallow onwards we use AppWidgetHost to
3336 // get widget update signals.
3337 if (!Utilities.ATLEAST_MARSHMALLOW &&
3338 (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003339 final Callbacks callbacks = getCallback();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003340 mHandler.post(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003341 public void run() {
3342 Callbacks cb = getCallback();
3343 if (callbacks == cb && cb != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003344 callbacks.notifyWidgetProvidersChanged();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003345 }
3346 }
3347 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003348 }
3349 }
3350 }
3351
Sunny Goyal10923b32016-07-20 15:42:44 -07003352 /**
3353 * Repopulates the shortcut info, possibly updating any icon already on the workspace.
3354 */
3355 public void updateShortcutInfo(final ShortcutInfoCompat fullDetail, final ShortcutInfo info) {
3356 enqueueItemUpdatedTask(new Runnable() {
3357 @Override
3358 public void run() {
3359 info.updateFromDeepShortcutInfo(
3360 fullDetail, LauncherAppState.getInstance().getContext());
3361 ArrayList<ShortcutInfo> update = new ArrayList<ShortcutInfo>();
3362 update.add(info);
3363 bindUpdatedShortcuts(update, fullDetail.getUserHandle());
3364 }
3365 });
3366 }
3367
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003368 private class ShortcutsChangedTask implements Runnable {
Sunny Goyal50941fb2016-08-04 12:03:52 -07003369 private final String mPackageName;
3370 private final List<ShortcutInfoCompat> mShortcuts;
3371 private final UserHandleCompat mUser;
3372 private final boolean mUpdateIdMap;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003373
3374 public ShortcutsChangedTask(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal50941fb2016-08-04 12:03:52 -07003375 UserHandleCompat user, boolean updateIdMap) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003376 mPackageName = packageName;
3377 mShortcuts = shortcuts;
3378 mUser = user;
Sunny Goyal50941fb2016-08-04 12:03:52 -07003379 mUpdateIdMap = updateIdMap;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003380 }
3381
3382 @Override
3383 public void run() {
3384 mDeepShortcutManager.onShortcutsChanged(mShortcuts);
3385
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003386 // Find ShortcutInfo's that have changed on the workspace.
Tony Wickham7695e602016-08-05 12:44:51 -07003387 final ArrayList<ShortcutInfo> removedShortcutInfos = new ArrayList<>();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003388 MultiHashMap<String, ShortcutInfo> idsToWorkspaceShortcutInfos = new MultiHashMap<>();
3389 for (ItemInfo itemInfo : sBgItemsIdMap) {
3390 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
3391 ShortcutInfo si = (ShortcutInfo) itemInfo;
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07003392 if (si.getPromisedIntent().getPackage().equals(mPackageName)
3393 && si.user.equals(mUser)) {
Tony Wickham7695e602016-08-05 12:44:51 -07003394 idsToWorkspaceShortcutInfos.addToList(si.getDeepShortcutId(), si);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003395 }
3396 }
3397 }
3398
Tony Wickham7695e602016-08-05 12:44:51 -07003399 final Context context = LauncherAppState.getInstance().getContext();
3400 final ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3401 if (!idsToWorkspaceShortcutInfos.isEmpty()) {
3402 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3403 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager.queryForFullDetails(
3404 mPackageName, new ArrayList<>(idsToWorkspaceShortcutInfos.keySet()), mUser);
3405 for (ShortcutInfoCompat fullDetails : shortcuts) {
3406 List<ShortcutInfo> shortcutInfos = idsToWorkspaceShortcutInfos
3407 .remove(fullDetails.getId());
3408 if (!fullDetails.isPinned()) {
3409 // The shortcut was previously pinned but is no longer, so remove it from
3410 // the workspace and our pinned shortcut counts.
3411 // Note that we put this check here, after querying for full details,
3412 // because there's a possible race condition between pinning and
3413 // receiving this callback.
3414 removedShortcutInfos.addAll(shortcutInfos);
3415 continue;
3416 }
3417 for (ShortcutInfo shortcutInfo : shortcutInfos) {
3418 shortcutInfo.updateFromDeepShortcutInfo(fullDetails, context);
3419 updatedShortcutInfos.add(shortcutInfo);
3420 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003421 }
3422 }
Tony Wickham7695e602016-08-05 12:44:51 -07003423
3424 // If there are still entries in idsToWorkspaceShortcutInfos, that means that
3425 // the corresponding shortcuts weren't passed in onShortcutsChanged(). This
3426 // means they were cleared, so we remove and unpin them now.
3427 for (String id : idsToWorkspaceShortcutInfos.keySet()) {
3428 removedShortcutInfos.addAll(idsToWorkspaceShortcutInfos.get(id));
3429 }
3430
3431 bindUpdatedShortcuts(updatedShortcutInfos, removedShortcutInfos, mUser);
3432 if (!removedShortcutInfos.isEmpty()) {
3433 deleteItemsFromDatabase(context, removedShortcutInfos);
3434 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003435
Sunny Goyal50941fb2016-08-04 12:03:52 -07003436 if (mUpdateIdMap) {
3437 // Update the deep shortcut map if the list of ids has changed for an activity.
3438 updateDeepShortcutMap(mPackageName, mUser, mShortcuts);
3439 bindDeepShortcuts();
3440 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003441 }
3442 }
3443
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003444 /**
3445 * Task to handle changing of lock state of the user
3446 */
3447 private class UserLockStateChangedTask implements Runnable {
3448
3449 private final UserHandleCompat mUser;
3450
3451 public UserLockStateChangedTask(UserHandleCompat user) {
3452 mUser = user;
3453 }
3454
3455 @Override
3456 public void run() {
3457 boolean isUserUnlocked = mUserManager.isUserUnlocked(mUser);
3458 Context context = mApp.getContext();
3459
3460 HashMap<ShortcutKey, ShortcutInfoCompat> pinnedShortcuts = new HashMap<>();
3461 if (isUserUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07003462 List<ShortcutInfoCompat> shortcuts =
3463 mDeepShortcutManager.queryForPinnedShortcuts(null, mUser);
3464 if (mDeepShortcutManager.wasLastCallSuccess()) {
3465 for (ShortcutInfoCompat shortcut : shortcuts) {
3466 pinnedShortcuts.put(ShortcutKey.fromInfo(shortcut), shortcut);
3467 }
3468 } else {
3469 // Shortcut manager can fail due to some race condition when the lock state
3470 // changes too frequently. For the purpose of the update,
3471 // consider it as still locked.
3472 isUserUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003473 }
3474 }
3475
3476 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3477 ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3478 ArrayList<ShortcutInfo> deletedShortcutInfos = new ArrayList<>();
3479 for (ItemInfo itemInfo : sBgItemsIdMap) {
3480 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT
3481 && mUser.equals(itemInfo.user)) {
3482 ShortcutInfo si = (ShortcutInfo) itemInfo;
3483 if (isUserUnlocked) {
3484 ShortcutInfoCompat shortcut =
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07003485 pinnedShortcuts.get(ShortcutKey.fromShortcutInfo(si));
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003486 // We couldn't verify the shortcut during loader. If its no longer available
3487 // (probably due to clear data), delete the workspace item as well
3488 if (shortcut == null) {
3489 deletedShortcutInfos.add(si);
3490 continue;
3491 }
3492 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
3493 si.updateFromDeepShortcutInfo(shortcut, context);
3494 } else {
3495 si.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
3496 }
3497 updatedShortcutInfos.add(si);
3498 }
3499 }
3500 bindUpdatedShortcuts(updatedShortcutInfos, deletedShortcutInfos, mUser);
3501 if (!deletedShortcutInfos.isEmpty()) {
3502 deleteItemsFromDatabase(context, deletedShortcutInfos);
3503 }
3504
3505 // Remove shortcut id map for that user
3506 Iterator<ComponentKey> keysIter = mBgDeepShortcutMap.keySet().iterator();
3507 while (keysIter.hasNext()) {
3508 if (keysIter.next().user.equals(mUser)) {
3509 keysIter.remove();
3510 }
3511 }
3512
3513 if (isUserUnlocked) {
3514 updateDeepShortcutMap(null, mUser, mDeepShortcutManager.queryForAllShortcuts(mUser));
3515 }
3516 bindDeepShortcuts();
3517 }
3518 }
3519
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003520 private void bindWidgetsModel(final Callbacks callbacks, final WidgetsModel model) {
3521 mHandler.post(new Runnable() {
3522 @Override
3523 public void run() {
3524 Callbacks cb = getCallback();
3525 if (callbacks == cb && cb != null) {
3526 callbacks.bindWidgetsModel(model);
3527 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003528 }
3529 });
3530 }
3531
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003532 public void refreshAndBindWidgetsAndShortcuts(
3533 final Callbacks callbacks, final boolean bindFirst) {
3534 runOnWorkerThread(new Runnable() {
3535 @Override
3536 public void run() {
3537 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
3538 bindWidgetsModel(callbacks, mBgWidgetsModel.clone());
Sunny Goyal31860582015-09-18 08:38:57 -07003539 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003540 final WidgetsModel model = mBgWidgetsModel.updateAndClone(mApp.getContext());
3541 bindWidgetsModel(callbacks, model);
3542 // update the Widget entries inside DB on the worker thread.
3543 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3544 model.getRawList());
Sunny Goyal31860582015-09-18 08:38:57 -07003545 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003546 });
Michael Jurkac402cd92013-05-20 15:49:32 +02003547 }
3548
Adam Cohen091440a2015-03-18 14:16:05 -07003549 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003550 UserHandleCompat user) {
3551 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3552 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003553 }
Adam Cohen556f6132014-01-15 15:18:08 -08003554
Kenny Guyed131872014-04-30 03:02:21 +01003555 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3556 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003557 if (cn == null) {
3558 return false;
3559 }
Kenny Guyed131872014-04-30 03:02:21 +01003560 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3561 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003562 return false;
3563 }
Kenny Guyed131872014-04-30 03:02:21 +01003564 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003565 }
3566
Adam Cohena28b78e2014-05-20 17:03:04 -07003567 public static boolean isValidPackage(Context context, String packageName,
3568 UserHandleCompat user) {
3569 if (packageName == null) {
3570 return false;
3571 }
3572 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3573 return launcherApps.isPackageEnabledForProfile(packageName, user);
3574 }
3575
Joe Onorato9c1289c2009-08-17 11:03:03 -04003576 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003577 * Make an ShortcutInfo object for a restored application or shortcut item that points
3578 * to a package that is not yet installed on the system.
3579 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003580 public ShortcutInfo getRestoredItemInfo(Cursor c, Intent intent,
3581 int promiseType, int itemType, CursorIconInfo iconInfo) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003582 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003583 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003584
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003585 Bitmap icon = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003586 // the fallback icon
3587 if (icon == null) {
3588 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3589 } else {
3590 info.setIcon(icon);
3591 }
Sunny Goyal34942622014-08-29 17:20:55 -07003592
3593 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003594 String title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07003595 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003596 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003597 }
Sunny Goyal34942622014-08-29 17:20:55 -07003598 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3599 if (TextUtils.isEmpty(info.title)) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003600 info.title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07003601 }
Sunny Goyal34942622014-08-29 17:20:55 -07003602 } else {
3603 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3604 }
3605
Winson Chung82b016c2015-05-08 17:00:10 -07003606 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003607 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003608 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003609 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003610 return info;
3611 }
3612
3613 /**
3614 * Make an Intent object for a restored application or shortcut item that points
3615 * to the market page for the item.
3616 */
Adam Cohen091440a2015-03-18 14:16:05 -07003617 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003618 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003619 return getMarketIntent(componentName.getPackageName());
3620 }
3621
3622 static Intent getMarketIntent(String packageName) {
3623 return new Intent(Intent.ACTION_VIEW)
3624 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003625 .scheme("market")
3626 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003627 .appendQueryParameter("id", packageName)
3628 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003629 }
3630
3631 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003632 * Make an ShortcutInfo object for a shortcut that is an application.
3633 *
3634 * If c is not null, then it will be used to fill in missing data like the title and icon.
3635 */
Sunny Goyald09c3702016-04-06 16:18:20 -07003636 public ShortcutInfo getAppShortcutInfo(Intent intent,
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003637 UserHandleCompat user, Cursor c, CursorIconInfo iconInfo,
Sunny Goyal34b65272015-03-11 16:56:52 -07003638 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003639 if (user == null) {
3640 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003641 return null;
3642 }
3643
Kenny Guyed131872014-04-30 03:02:21 +01003644 ComponentName componentName = intent.getComponent();
3645 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08003646 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01003647 return null;
3648 }
3649
3650 Intent newIntent = new Intent(intent.getAction(), null);
3651 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3652 newIntent.setComponent(componentName);
3653 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003654 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003655 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3656 return null;
3657 }
3658
3659 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003660 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003661 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003662 Bitmap icon = iconInfo.loadIcon(c);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003663 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003664 }
3665
Sunny Goyald09c3702016-04-06 16:18:20 -07003666 if (lai != null && PackageManagerHelper.isAppSuspended(lai.getApplicationInfo())) {
3667 info.isDisabled = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
3668 }
3669
Joe Onorato56d82912010-03-07 14:32:10 -05003670 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003671 if (TextUtils.isEmpty(info.title) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003672 info.title = iconInfo.getTitle(c);
Joe Onorato56d82912010-03-07 14:32:10 -05003673 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003674
Joe Onorato56d82912010-03-07 14:32:10 -05003675 // fall back to the class name of the activity
3676 if (info.title == null) {
3677 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003678 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003679
Joe Onorato9c1289c2009-08-17 11:03:03 -04003680 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003681 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003682 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003683 if (lai != null) {
3684 info.flags = AppInfo.initFlags(lai);
3685 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003686 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003687 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003688
Sunny Goyale2df0622015-04-24 11:27:00 -07003689 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003690 ItemInfoFilter f) {
3691 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3692 for (ItemInfo i : infos) {
3693 if (i instanceof ShortcutInfo) {
3694 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003695 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003696 if (cn != null && f.filterItem(null, info, cn)) {
3697 filtered.add(info);
3698 }
3699 } else if (i instanceof FolderInfo) {
3700 FolderInfo info = (FolderInfo) i;
3701 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003702 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003703 if (cn != null && f.filterItem(info, s, cn)) {
3704 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003705 }
3706 }
Winson Chung64359a52013-07-08 17:17:08 -07003707 } else if (i instanceof LauncherAppWidgetInfo) {
3708 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3709 ComponentName cn = info.providerName;
3710 if (cn != null && f.filterItem(null, info, cn)) {
3711 filtered.add(info);
3712 }
Winson Chung8a435102012-08-30 17:16:53 -07003713 }
3714 }
Winson Chung64359a52013-07-08 17:17:08 -07003715 return new ArrayList<ItemInfo>(filtered);
3716 }
3717
Adam Cohen091440a2015-03-18 14:16:05 -07003718 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003719 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003720 ItemInfoFilter filter = new ItemInfoFilter() {
3721 @Override
3722 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003723 if (info.user == null) {
3724 return cn.equals(cname);
3725 } else {
3726 return cn.equals(cname) && info.user.equals(user);
3727 }
Winson Chung64359a52013-07-08 17:17:08 -07003728 }
3729 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003730 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003731 }
3732
Sunny Goyal1a745e82014-10-02 15:58:31 -07003733 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003734 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003735 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003736 @Thunk ShortcutInfo getShortcutInfo(Cursor c, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003737 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003738 // Non-app shortcuts are only supported for current user.
3739 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003740 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003741
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003742 // TODO: If there's an explicit component and we can't install that, delete it.
3743
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003744 loadInfoFromCursor(info, c, iconInfo);
3745 return info;
3746 }
Joe Onorato56d82912010-03-07 14:32:10 -05003747
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003748 /**
3749 * Make an ShortcutInfo object for a shortcut that isn't an application.
3750 */
3751 public void loadInfoFromCursor(ShortcutInfo info, Cursor c, CursorIconInfo iconInfo) {
3752 info.title = iconInfo.getTitle(c);
3753 Bitmap icon = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003754 // the fallback icon
3755 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003756 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003757 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003758 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003759 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003760 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003761
Sunny Goyal2350bc92014-10-14 16:42:54 -07003762 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003763 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3764 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3765 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3766
Adam Cohend9198822011-11-22 16:42:47 -08003767 if (intent == null) {
3768 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3769 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3770 return null;
3771 }
3772
Joe Onorato0589f0f2010-02-08 13:44:00 -08003773 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003774 boolean customIcon = false;
3775 ShortcutIconResource iconResource = null;
3776
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003777 if (bitmap instanceof Bitmap) {
3778 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003779 customIcon = true;
3780 } else {
3781 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003782 if (extra instanceof ShortcutIconResource) {
3783 iconResource = (ShortcutIconResource) extra;
3784 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003785 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003786 }
3787 }
3788
Michael Jurkac9d95c52011-08-29 14:03:34 -07003789 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003790
Kenny Guyed131872014-04-30 03:02:21 +01003791 // Only support intents for current user for now. Intents sent from other
3792 // users wouldn't get here without intent forwarding anyway.
3793 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003794 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003795 icon = mIconCache.getDefaultIcon(info.user);
3796 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003797 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003798 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003799
Winson Chung82b016c2015-05-08 17:00:10 -07003800 info.title = Utilities.trim(name);
3801 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003802 info.intent = intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003803 info.iconResource = iconResource;
3804
3805 return info;
3806 }
3807
Joe Onorato9c1289c2009-08-17 11:03:03 -04003808 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003809 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003810 * or make a new one.
3811 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003812 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003813 // See if a placeholder was created for us already
3814 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003815 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003816 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003817 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003818 folders.put(id, folderInfo);
3819 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003820 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003821 }
3822
Joe Onoratobe386092009-11-17 17:32:16 -08003823
Sunny Goyal651077b2014-06-30 14:15:31 -07003824 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3825 return (provider != null) && (provider.provider != null)
3826 && (provider.provider.getPackageName() != null);
3827 }
3828
Joe Onoratobe386092009-11-17 17:32:16 -08003829 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003830 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003831 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3832 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3833 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3834 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003835 if (mLoaderTask != null) {
3836 mLoaderTask.dumpState();
3837 } else {
3838 Log.d(TAG, "mLoaderTask=null");
3839 }
Joe Onoratobe386092009-11-17 17:32:16 -08003840 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003841
3842 public Callbacks getCallback() {
3843 return mCallbacks != null ? mCallbacks.get() : null;
3844 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003845
3846 /**
3847 * @return {@link FolderInfo} if its already loaded.
3848 */
3849 public FolderInfo findFolderById(Long folderId) {
3850 synchronized (sBgLock) {
3851 return sBgFolders.get(folderId);
3852 }
3853 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003854
Sunny Goyal527c7d32015-08-28 15:19:36 -07003855 @Thunk class DeferredMainThreadExecutor implements Executor {
3856
3857 @Override
3858 public void execute(Runnable command) {
3859 runOnMainThread(command);
3860 }
3861 }
3862
Sunny Goyal756adbc2015-04-16 15:20:51 -07003863 /**
3864 * @return the looper for the worker thread which can be used to start background tasks.
3865 */
3866 public static Looper getWorkerLooper() {
3867 return sWorkerThread.getLooper();
3868 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003869}