blob: 59c823b4b5960f4fc0151031f4a8f58b0520bee5 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Romain Guy629de3e2010-01-13 12:20:59 -080019import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070020import android.content.BroadcastReceiver;
21import android.content.ComponentName;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070022import android.content.ContentProviderOperation;
23import android.content.ContentResolver;
24import android.content.ContentValues;
25import android.content.Context;
26import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080027import android.content.Intent.ShortcutIconResource;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070028import android.content.IntentFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.content.pm.PackageManager;
30import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.database.Cursor;
32import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.net.Uri;
Joe Onorato36115782010-06-17 13:28:48 -040034import android.os.Handler;
35import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070036import android.os.Looper;
Joe Onorato0589f0f2010-02-08 13:44:00 -080037import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040039import android.os.SystemClock;
Sunny Goyale26d1002016-06-20 14:52:14 -070040import android.os.Trace;
Chris Wrenc3919c02013-09-18 09:48:33 -040041import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080042import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070043import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080044import android.util.LongSparseArray;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070045import android.util.MutableInt;
Winson Chungc9168342013-06-26 14:54:55 -070046import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010047
Sunny Goyalffe83f12014-08-14 17:39:34 -070048import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010049import com.android.launcher3.compat.LauncherActivityInfoCompat;
50import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070051import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070052import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010053import com.android.launcher3.compat.UserHandleCompat;
54import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070055import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -070056import com.android.launcher3.config.ProviderConfig;
Tony Wickham827cef22016-03-17 15:39:39 -070057import com.android.launcher3.dynamicui.ExtractionUtils;
Sunny Goyal26119432016-02-18 22:09:23 +000058import com.android.launcher3.folder.Folder;
59import com.android.launcher3.folder.FolderIcon;
Sunny Goyal1acb9e92016-05-16 12:41:09 -070060import com.android.launcher3.logging.FileLog;
Sunny Goyalf862a262015-12-14 14:27:38 -080061import com.android.launcher3.model.GridSizeMigrationTask;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070062import com.android.launcher3.model.WidgetsModel;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -070063import com.android.launcher3.provider.ImportDataTask;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070064import com.android.launcher3.provider.LauncherDbUtils;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070065import com.android.launcher3.shortcuts.DeepShortcutManager;
66import com.android.launcher3.shortcuts.ShortcutInfoCompat;
67import com.android.launcher3.shortcuts.ShortcutKey;
Robin Lee26ace122015-03-16 19:41:43 +000068import com.android.launcher3.util.ComponentKey;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070069import com.android.launcher3.util.CursorIconInfo;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -070070import com.android.launcher3.util.FlagOp;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070071import com.android.launcher3.util.GridOccupancy;
Sunny Goyale2df0622015-04-24 11:27:00 -070072import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070073import com.android.launcher3.util.ManagedProfileHeuristic;
Tony Wickhamd82a39d2016-07-01 15:44:13 -070074import com.android.launcher3.util.MultiHashMap;
Sunny Goyald09c3702016-04-06 16:18:20 -070075import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070076import com.android.launcher3.util.Preconditions;
Sunny Goyalda891c12016-03-18 18:29:24 -070077import com.android.launcher3.util.StringFilter;
Adam Cohen091440a2015-03-18 14:16:05 -070078import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070079import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080080
Michael Jurkac2f801e2011-07-12 14:19:46 -070081import java.lang.ref.WeakReference;
82import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070083import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070084import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070085import java.util.Arrays;
Michael Jurkac2f801e2011-07-12 14:19:46 -070086import java.util.Collections;
87import java.util.Comparator;
88import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070089import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070090import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070091import java.util.List;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070092import java.util.Map;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070093import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070094import java.util.Set;
Sunny Goyal527c7d32015-08-28 15:19:36 -070095import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -070096
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097/**
98 * Maintains in-memory state of the Launcher. It is expected that there should be only one
99 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700100 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101 */
Kenny Guyed131872014-04-30 03:02:21 +0100102public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +0100103 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800104 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -0400105 private static final boolean DEBUG_RECEIVER = false;
Chris Wrenb358f812014-04-16 13:37:00 -0400106
Joe Onorato9c1289c2009-08-17 11:03:03 -0400107 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700108
Joe Onorato36115782010-06-17 13:28:48 -0400109 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500110 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800111
Adam Cohen091440a2015-03-18 14:16:05 -0700112 @Thunk final LauncherAppState mApp;
113 @Thunk final Object mLock = new Object();
114 @Thunk DeferredHandler mHandler = new DeferredHandler();
115 @Thunk LoaderTask mLoaderTask;
116 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700117 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118
Adam Cohen091440a2015-03-18 14:16:05 -0700119 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700120 static {
121 sWorkerThread.start();
122 }
Adam Cohen091440a2015-03-18 14:16:05 -0700123 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700124
Joe Onoratocc67f472010-06-08 10:54:30 -0700125 // We start off with everything not loaded. After that, we assume that
126 // our monitoring of the package manager provides all updates and we never
127 // need to do a requery. These are only ever touched from the loader thread.
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700128 private boolean mWorkspaceLoaded;
129 private boolean mAllAppsLoaded;
130 private boolean mDeepShortcutsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700131
Sunny Goyal756a28a2015-04-23 17:07:55 -0700132 /**
133 * Set of runnables to be called on the background thread after the workspace binding
134 * is complete.
135 */
136 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
137
Adam Cohen091440a2015-03-18 14:16:05 -0700138 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700140 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800141 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700142 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800143 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800144
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700145 // Maps all launcher activities to the id's of their shortcuts (if they have any).
146 private final MultiHashMap<ComponentKey, String> mBgDeepShortcutMap = new MultiHashMap<>();
147
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700148 private boolean mHasShortcutHostPermission;
149 // Runnable to check if the shortcuts permission has changed.
150 private final Runnable mShortcutPermissionCheckRunnable = new Runnable() {
151 @Override
152 public void run() {
153 if (mDeepShortcutsLoaded) {
154 boolean hasShortcutHostPermission = mDeepShortcutManager.hasHostPermission();
155 if (hasShortcutHostPermission != mHasShortcutHostPermission) {
156 mApp.reloadWorkspace();
157 }
158 }
159 }
160 };
161
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700162 // The lock that must be acquired before referencing any static bg data structures. Unlike
163 // other locks, this one can generally be held long-term because we never expect any of these
164 // static data structures to be referenced outside of the worker thread except on the first
165 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700166 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700167
Adam Cohen487f7dd2012-06-28 18:12:10 -0700168 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700169 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700170 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700171
Adam Cohen487f7dd2012-06-28 18:12:10 -0700172 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
173 // created by LauncherModel that are directly on the home screen (however, no widgets or
174 // shortcuts within folders).
175 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700176
Adam Cohen487f7dd2012-06-28 18:12:10 -0700177 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
178 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700179 new ArrayList<LauncherAppWidgetInfo>();
180
Adam Cohen487f7dd2012-06-28 18:12:10 -0700181 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700182 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700183
Adam Cohendcd297f2013-06-18 13:13:40 -0700184 // sBgWorkspaceScreens is the ordered set of workspace screens.
185 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
186
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700187 // sBgPinnedShortcutCounts is the ComponentKey representing a pinned shortcut to the number of
188 // times it is pinned.
189 static final Map<ShortcutKey, MutableInt> sBgPinnedShortcutCounts = new HashMap<>();
190
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700191 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700192 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
193 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700194
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700195 // </ only access in worker thread >
196
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700197 private IconCache mIconCache;
198 private DeepShortcutManager mDeepShortcutManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700200 private final LauncherAppsCompat mLauncherApps;
201 private final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100202
Joe Onorato9c1289c2009-08-17 11:03:03 -0400203 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700204 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400205 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700206 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400207 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700208 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
209 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700210 public void bindScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700211 public void finishFirstPageBind(ViewOnDrawExecutor executor);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800212 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400213 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200214 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700215 public void bindAppsAdded(ArrayList<Long> newScreens,
216 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700217 ArrayList<ItemInfo> addAnimated,
218 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200219 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700220 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
221 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
222 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700223 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700224 public void bindWorkspaceComponentsRemoved(
225 HashSet<String> packageNames, HashSet<ComponentName> components,
226 UserHandleCompat user);
227 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800228 public void notifyWidgetProvidersChanged();
229 public void bindWidgetsModel(WidgetsModel model);
Adam Cohen1462de32012-07-24 22:34:36 -0700230 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700231 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700232 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMap);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400233 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800234
Winson Chung64359a52013-07-08 17:17:08 -0700235 public interface ItemInfoFilter {
236 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
237 }
238
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700239 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter,
240 DeepShortcutManager deepShortcutManager) {
Winson Chunga6945242014-01-08 14:04:34 -0800241 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400242 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100243 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700244 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800245 mIconCache = iconCache;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700246 mDeepShortcutManager = deepShortcutManager;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800247
Kenny Guyed131872014-04-30 03:02:21 +0100248 mLauncherApps = LauncherAppsCompat.getInstance(context);
249 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800250 }
251
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700252 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
253 * posted on the main thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700254 private void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700255 if (sWorkerThread.getThreadId() == Process.myTid()) {
256 // If we are on the worker thread, post onto the main handler
257 mHandler.post(r);
258 } else {
259 r.run();
260 }
261 }
262
263 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
264 * posted on the worker thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700265 private static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700266 if (sWorkerThread.getThreadId() == Process.myTid()) {
267 r.run();
268 } else {
269 // If we are not on the worker thread, then post to the worker handler
270 sWorker.post(r);
271 }
272 }
273
Sunny Goyal756adbc2015-04-16 15:20:51 -0700274 public void setPackageState(final PackageInstallInfo installInfo) {
275 Runnable updateRunnable = new Runnable() {
276
277 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500278 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700279 synchronized (sBgLock) {
280 final HashSet<ItemInfo> updates = new HashSet<>();
281
282 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
283 // Ignore install success events as they are handled by Package add events.
284 return;
285 }
286
Sunny Goyale2df0622015-04-24 11:27:00 -0700287 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700288 if (info instanceof ShortcutInfo) {
289 ShortcutInfo si = (ShortcutInfo) info;
290 ComponentName cn = si.getTargetComponent();
291 if (si.isPromise() && (cn != null)
292 && installInfo.packageName.equals(cn.getPackageName())) {
293 si.setInstallProgress(installInfo.progress);
294
295 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
296 // Mark this info as broken.
297 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
298 }
299 updates.add(si);
300 }
301 }
302 }
303
304 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
305 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
306 widget.installProgress = installInfo.progress;
307 updates.add(widget);
308 }
309 }
310
311 if (!updates.isEmpty()) {
312 // Push changes to the callback.
313 Runnable r = new Runnable() {
314 public void run() {
315 Callbacks callbacks = getCallback();
316 if (callbacks != null) {
317 callbacks.bindRestoreItemsChange(updates);
318 }
319 }
320 };
321 mHandler.post(r);
322 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500323 }
324 }
325 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700326 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500327 }
328
Sunny Goyal756adbc2015-04-16 15:20:51 -0700329 /**
330 * Updates the icons and label of all pending icons for the provided package name.
331 */
332 public void updateSessionDisplayInfo(final String packageName) {
333 Runnable updateRunnable = new Runnable() {
334
335 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700336 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700337 synchronized (sBgLock) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700338 ArrayList<ShortcutInfo> updates = new ArrayList<>();
339 UserHandleCompat user = UserHandleCompat.myUserHandle();
Sunny Goyal756adbc2015-04-16 15:20:51 -0700340
Sunny Goyale2df0622015-04-24 11:27:00 -0700341 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700342 if (info instanceof ShortcutInfo) {
343 ShortcutInfo si = (ShortcutInfo) info;
344 ComponentName cn = si.getTargetComponent();
345 if (si.isPromise() && (cn != null)
346 && packageName.equals(cn.getPackageName())) {
347 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
348 // For auto install apps update the icon as well as label.
349 mIconCache.getTitleAndIcon(si,
350 si.promisedIntent, user,
351 si.shouldUseLowResIcon());
352 } else {
353 // Only update the icon for restored apps.
354 si.updateIcon(mIconCache);
355 }
356 updates.add(si);
357 }
358 }
359 }
360
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700361 bindUpdatedShortcuts(updates, user);
Sunny Goyala22666f2014-09-18 13:25:15 -0700362 }
363 }
364 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700365 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700366 }
367
Adam Cohen76a47a12014-02-05 11:47:43 -0800368 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800369 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800370
371 if (allAppsApps == null) {
372 throw new RuntimeException("allAppsApps must not be null");
373 }
374 if (allAppsApps.isEmpty()) {
375 return;
376 }
377
378 // Process the newly added applications and add them to the database first
379 Runnable r = new Runnable() {
380 public void run() {
381 runOnMainThread(new Runnable() {
382 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800383 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800384 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500385 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800386 }
387 }
388 });
389 }
390 };
391 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700392 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800393
Sunny Goyala9116722015-04-29 13:55:58 -0700394 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800395 int[] xy, int spanX, int spanY) {
396 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700397 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700398
399 GridOccupancy occupied = new GridOccupancy(profile.numColumns, profile.numRows);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800400 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700401 for (ItemInfo r : occupiedPos) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700402 occupied.markCells(r, true);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800403 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800404 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700405 return occupied.findVacantCell(xy, spanX, spanY);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800406 }
407
408 /**
409 * Find a position on the screen for the given size or adds a new screen.
410 * @return screenId and the coordinates for the item.
411 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700412 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyalc3642d42015-10-30 10:27:08 -0700413 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800414 ArrayList<Long> workspaceScreens,
415 ArrayList<Long> addedWorkspaceScreensFinal,
416 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700417 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800418
Sunny Goyala9116722015-04-29 13:55:58 -0700419 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700420 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700421 synchronized (sBgLock) {
422 for (ItemInfo info : sBgItemsIdMap) {
423 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
424 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
425 if (items == null) {
426 items = new ArrayList<>();
427 screenItems.put(info.screenId, items);
428 }
429 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800430 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800431 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800432 }
433
434 // Find appropriate space for the item.
435 long screenId = 0;
436 int[] cordinates = new int[2];
437 boolean found = false;
438
439 int screenCount = workspaceScreens.size();
440 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700441 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800442 if (preferredScreenIndex < screenCount) {
443 screenId = workspaceScreens.get(preferredScreenIndex);
444 found = findNextAvailableIconSpaceInScreen(
445 screenItems.get(screenId), cordinates, spanX, spanY);
446 }
447
448 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700449 // Search on any of the screens starting from the first screen.
450 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800451 screenId = workspaceScreens.get(screen);
452 if (findNextAvailableIconSpaceInScreen(
453 screenItems.get(screenId), cordinates, spanX, spanY)) {
454 // We found a space for it
455 found = true;
456 break;
457 }
458 }
459 }
460
461 if (!found) {
462 // Still no position found. Add a new screen to the end.
Sunny Goyald2497482015-09-22 18:24:19 -0700463 screenId = LauncherSettings.Settings.call(context.getContentResolver(),
464 LauncherSettings.Settings.METHOD_NEW_SCREEN_ID)
465 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800466
467 // Save the screen id for binding in the workspace
468 workspaceScreens.add(screenId);
469 addedWorkspaceScreensFinal.add(screenId);
470
471 // If we still can't find an empty space, then God help us all!!!
472 if (!findNextAvailableIconSpaceInScreen(
473 screenItems.get(screenId), cordinates, spanX, spanY)) {
474 throw new RuntimeException("Can't find space to add the item");
475 }
476 }
477 return Pair.create(screenId, cordinates);
478 }
479
480 /**
481 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800482 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700483 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700484 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800485 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800486 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700487 return;
Winson Chung997a9232013-07-24 15:33:46 -0700488 }
Winson Chung64359a52013-07-08 17:17:08 -0700489 // Process the newly added applications and add them to the database first
490 Runnable r = new Runnable() {
491 public void run() {
492 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
493 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
494
Winson Chung76828c82013-08-19 15:43:29 -0700495 // Get the list of workspace screens. We need to append to this list and
496 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
497 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800498 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700499 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800500 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700501 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800502 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700503 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800504 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700505 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800506 }
Winson Chung76828c82013-08-19 15:43:29 -0700507
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800508 // Find appropriate space for the item.
Sunny Goyalc3642d42015-10-30 10:27:08 -0700509 Pair<Long, int[]> coords = findSpaceForItem(context,
Sunny Goyal9eba1fd2015-10-16 08:58:57 -0700510 workspaceScreens, addedWorkspaceScreensFinal, 1, 1);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800511 long screenId = coords.first;
512 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700513
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700514 ItemInfo itemInfo;
515 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
516 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800517 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700518 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700519 } else {
520 throw new RuntimeException("Unexpected info type");
521 }
Winson Chung94d67682013-09-25 16:29:40 -0700522
Winson Chung64359a52013-07-08 17:17:08 -0700523 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700524 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700525 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700526 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700527 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700528 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700529 }
530 }
531
Winson Chung76828c82013-08-19 15:43:29 -0700532 // Update the workspace screens
533 updateWorkspaceScreenOrder(context, workspaceScreens);
534
Adam Cohen76a47a12014-02-05 11:47:43 -0800535 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700536 runOnMainThread(new Runnable() {
537 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800538 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700539 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700540 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
541 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700542 if (!addedShortcutsFinal.isEmpty()) {
543 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
544 long lastScreenId = info.screenId;
545 for (ItemInfo i : addedShortcutsFinal) {
546 if (i.screenId == lastScreenId) {
547 addAnimated.add(i);
548 } else {
549 addNotAnimated.add(i);
550 }
Winson Chung997a9232013-07-24 15:33:46 -0700551 }
552 }
Winson Chungd64d1762013-08-20 14:37:16 -0700553 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800554 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700555 }
Winson Chung64359a52013-07-08 17:17:08 -0700556 }
Winson Chung997a9232013-07-24 15:33:46 -0700557 });
558 }
Winson Chung64359a52013-07-08 17:17:08 -0700559 }
560 };
561 runOnWorkerThread(r);
562 }
563
Joe Onorato9c1289c2009-08-17 11:03:03 -0400564 /**
565 * Adds an item to the DB if it was not created previously, or move it to a new
566 * <container, screen, cellX, cellY>
567 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800568 public static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700569 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400570 if (item.container == ItemInfo.NO_ID) {
571 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700572 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400573 } else {
574 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700575 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800576 }
577 }
578
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700579 static void checkItemInfoLocked(
580 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
581 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
582 if (modelItem != null && item != modelItem) {
583 // check all the data is consistent
584 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
585 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
586 ShortcutInfo shortcut = (ShortcutInfo) item;
587 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
588 modelShortcut.intent.filterEquals(shortcut.intent) &&
589 modelShortcut.id == shortcut.id &&
590 modelShortcut.itemType == shortcut.itemType &&
591 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700592 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700593 modelShortcut.cellX == shortcut.cellX &&
594 modelShortcut.cellY == shortcut.cellY &&
595 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700596 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700597 // For all intents and purposes, this is the same object
598 return;
599 }
600 }
601
602 // the modelItem needs to match up perfectly with item if our model is
603 // to be consistent with the database-- for now, just require
604 // modelItem == item or the equality check above
605 String msg = "item: " + ((item != null) ? item.toString() : "null") +
606 "modelItem: " +
607 ((modelItem != null) ? modelItem.toString() : "null") +
608 "Error: ItemInfo passed to checkItemInfo doesn't match original";
609 RuntimeException e = new RuntimeException(msg);
610 if (stackTrace != null) {
611 e.setStackTrace(stackTrace);
612 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800613 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700614 }
615 }
616
Michael Jurka816474f2012-06-25 14:49:02 -0700617 static void checkItemInfo(final ItemInfo item) {
618 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
619 final long itemId = item.id;
620 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700621 public void run() {
622 synchronized (sBgLock) {
623 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700624 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700625 }
626 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700627 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700628 }
629
Michael Jurkac9d95c52011-08-29 14:03:34 -0700630 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
631 final ItemInfo item, final String callingFunction) {
632 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700633 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700634 final ContentResolver cr = context.getContentResolver();
635
Adam Cohen487f7dd2012-06-28 18:12:10 -0700636 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700637 Runnable r = new Runnable() {
638 public void run() {
639 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700640 updateItemArrays(item, itemId, stackTrace);
641 }
642 };
643 runOnWorkerThread(r);
644 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700645
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700646 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
647 final ArrayList<ItemInfo> items, final String callingFunction) {
648 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700649
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700650 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
651 Runnable r = new Runnable() {
652 public void run() {
653 ArrayList<ContentProviderOperation> ops =
654 new ArrayList<ContentProviderOperation>();
655 int count = items.size();
656 for (int i = 0; i < count; i++) {
657 ItemInfo item = items.get(i);
658 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700659 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700660 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700661
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700662 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
663 updateItemArrays(item, itemId, stackTrace);
664
665 }
666 try {
667 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
668 } catch (Exception e) {
669 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700670 }
671 }
672 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700673 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700674 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700675
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700676 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
677 // Lock on mBgLock *after* the db operation
678 synchronized (sBgLock) {
679 checkItemInfoLocked(itemId, item, stackTrace);
680
681 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
682 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
683 // Item is in a folder, make sure this folder exists
684 if (!sBgFolders.containsKey(item.container)) {
685 // An items container is being set to a that of an item which is not in
686 // the list of Folders.
687 String msg = "item: " + item + " container being set to: " +
688 item.container + ", not in the list of folders";
689 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700690 }
691 }
692
693 // Items are added/removed from the corresponding FolderInfo elsewhere, such
694 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
695 // that are on the desktop, as appropriate
696 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800697 if (modelItem != null &&
698 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
699 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700700 switch (modelItem.itemType) {
701 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
702 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700703 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700704 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
705 if (!sBgWorkspaceItems.contains(modelItem)) {
706 sBgWorkspaceItems.add(modelItem);
707 }
708 break;
709 default:
710 break;
711 }
712 } else {
713 sBgWorkspaceItems.remove(modelItem);
714 }
715 }
716 }
717
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800718 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400719 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700720 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700721 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700722 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400723 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400724 item.cellX = cellX;
725 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700726
Winson Chung3d503fb2011-07-13 17:25:49 -0700727 // We store hotseat items in canonical form which is this orientation invariant position
728 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700729 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700730 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700731 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700732 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700733 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700734 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400735
736 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400737 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700738 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
739 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800740 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700741 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400742
Michael Jurkac9d95c52011-08-29 14:03:34 -0700743 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700744 }
745
746 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700747 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
748 * cellX, cellY have already been updated on the ItemInfos.
749 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800750 public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700751 final long container, final int screen) {
752
753 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
754 int count = items.size();
755
756 for (int i = 0; i < count; i++) {
757 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700758 item.container = container;
759
760 // We store hotseat items in canonical form which is this orientation invariant position
761 // in the hotseat
762 if (context instanceof Launcher && screen < 0 &&
763 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700764 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700765 item.cellY);
766 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700767 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700768 }
769
770 final ContentValues values = new ContentValues();
771 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
772 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
773 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800774 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700775 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700776
777 contentValues.add(values);
778 }
779 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
780 }
781
782 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700783 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800784 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700785 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700786 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700787 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800788 item.cellX = cellX;
789 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700790 item.spanX = spanX;
791 item.spanY = spanY;
792
793 // We store hotseat items in canonical form which is this orientation invariant position
794 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700795 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700796 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700797 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700798 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700799 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700800 }
Adam Cohend4844c32011-02-18 19:25:06 -0800801
Adam Cohend4844c32011-02-18 19:25:06 -0800802 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800803 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700804 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
805 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800806 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700807 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
808 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700809 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800810
Michael Jurka816474f2012-06-25 14:49:02 -0700811 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700812 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700813
814 /**
815 * Update an item to the database in a specified container.
816 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700817 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700818 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100819 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700820 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800821 }
822
Sunny Goyal756a28a2015-04-23 17:07:55 -0700823 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700824 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700825 synchronized (mLock) {
826 if (!mHasLoaderCompletedOnce ||
827 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
828 throw new RuntimeException("Trying to add shortcut while loader is running");
829 }
830 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700831 }
832 }
833
Adam Cohend4844c32011-02-18 19:25:06 -0800834 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700835 * Returns true if the shortcuts already exists on the workspace. This must be called after
836 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800837 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700838 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
839 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700840 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700841 if (intent.getComponent() != null) {
842 // If component is not null, an intent with null package will produce
843 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700844 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700845 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700846 intentWithPkg = intent.toUri(0);
847 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700848 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700849 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
850 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700851 }
852 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700853 intentWithPkg = intent.toUri(0);
854 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700855 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700856
857 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700858 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700859 if (item instanceof ShortcutInfo) {
860 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700861 Intent targetIntent = info.promisedIntent == null
862 ? info.intent : info.promisedIntent;
863 if (targetIntent != null && info.user.equals(user)) {
Sunny Goyalcbfe71d2016-08-23 13:25:58 -0700864 Intent copyIntent = new Intent(targetIntent);
865 copyIntent.setSourceBounds(intent.getSourceBounds());
866 String s = copyIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700867 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
868 return true;
869 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700870 }
871 }
872 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800873 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700874 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700875 }
876
Joe Onorato9c1289c2009-08-17 11:03:03 -0400877 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400878 * Add an item to the database in a specified container. Sets the container, screen, cellX and
879 * cellY fields of the item. Also assigns an ID to the item.
880 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700881 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700882 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400883 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400884 item.cellX = cellX;
885 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700886 // We store hotseat items in canonical form which is this orientation invariant position
887 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700888 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700889 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700890 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700891 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700892 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700893 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400894
895 final ContentValues values = new ContentValues();
896 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100897 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400898
Sunny Goyald2497482015-09-22 18:24:19 -0700899 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
900 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
901
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700902 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700903
Jason Monk8e19cf22014-03-20 15:06:57 -0400904 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700905 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700906 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700907 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400908
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700909 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700910 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400911 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700912 sBgItemsIdMap.put(item.id, item);
913 switch (item.itemType) {
914 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
915 sBgFolders.put(item.id, (FolderInfo) item);
916 // Fall through
917 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
918 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700919 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700920 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
921 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
922 sBgWorkspaceItems.add(item);
923 } else {
924 if (!sBgFolders.containsKey(item.container)) {
925 // Adding an item to a folder that doesn't exist.
926 String msg = "adding item: " + item + " to a folder that " +
927 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700928 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700929 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700930 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700931 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700932 incrementPinnedShortcutCount(
933 ShortcutKey.fromItemInfo(item), true /* shouldPin */);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700934 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700935 break;
936 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
937 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
938 break;
939 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700940 }
941 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700942 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700943 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700944 }
945
Sunny Goyal34942622014-08-29 17:20:55 -0700946 private static ArrayList<ItemInfo> getItemsByPackageName(
947 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700948 ItemInfoFilter filter = new ItemInfoFilter() {
949 @Override
950 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
951 return cn.getPackageName().equals(pn) && info.user.equals(user);
952 }
953 };
Sunny Goyale2df0622015-04-24 11:27:00 -0700954 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -0700955 }
956
957 /**
958 * Removes all the items from the database corresponding to the specified package.
959 */
960 static void deletePackageFromDatabase(Context context, final String pn,
961 final UserHandleCompat user) {
962 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700963 }
964
965 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700966 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -0400967 */
Sunny Goyalfa401a12015-04-10 13:45:42 -0700968 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700969 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
970 items.add(item);
971 deleteItemsFromDatabase(context, items);
972 }
973
974 /**
975 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700976 */
Sunny Goyal4390ace2014-10-13 11:33:11 -0700977 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400978 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -0700979 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700980 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700981 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700982 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700983 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700984
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700985 // Lock on mBgLock *after* the db operation
986 synchronized (sBgLock) {
987 switch (item.itemType) {
988 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
989 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -0700990 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700991 if (info.container == item.id) {
992 // We are deleting a folder which still contains items that
993 // think they are contained by that folder.
994 String msg = "deleting a folder (" + item + ") which still " +
995 "contains items (" + info + ")";
996 Log.e(TAG, msg);
997 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700998 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700999 sBgWorkspaceItems.remove(item);
1000 break;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001001 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001002 decrementPinnedShortcutCount(ShortcutKey.fromItemInfo(item));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001003 // Fall through.
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001004 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1005 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1006 sBgWorkspaceItems.remove(item);
1007 break;
1008 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1009 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1010 break;
1011 }
1012 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001013 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001014 }
1015 }
Michael Jurka83df1882011-08-31 20:59:26 -07001016 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001017 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001018 }
1019
1020 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001021 * Decrement the count for the given pinned shortcut, unpinning it if the count becomes 0.
1022 */
1023 private static void decrementPinnedShortcutCount(final ShortcutKey pinnedShortcut) {
1024 synchronized (sBgLock) {
1025 MutableInt count = sBgPinnedShortcutCounts.get(pinnedShortcut);
1026 if (count == null || --count.value == 0) {
1027 LauncherAppState.getInstance().getShortcutManager().unpinShortcut(pinnedShortcut);
1028 }
1029 }
1030 }
1031
1032 /**
1033 * Increment the count for the given shortcut, pinning it if the count becomes 1.
1034 *
1035 * As an optimization, the caller can pass shouldPin == false to avoid
1036 * unnecessary RPC's if the shortcut is already pinned.
1037 */
1038 private static void incrementPinnedShortcutCount(ShortcutKey pinnedShortcut, boolean shouldPin) {
1039 synchronized (sBgLock) {
1040 MutableInt count = sBgPinnedShortcutCounts.get(pinnedShortcut);
1041 if (count == null) {
1042 count = new MutableInt(1);
1043 sBgPinnedShortcutCounts.put(pinnedShortcut, count);
1044 } else {
1045 count.value++;
1046 }
1047 if (shouldPin && count.value == 1) {
1048 LauncherAppState.getInstance().getShortcutManager().pinShortcut(pinnedShortcut);
1049 }
1050 }
1051 }
1052
1053 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001054 * Update the order of the workspace screens in the database. The array list contains
1055 * a list of screen ids in the order that they should appear.
1056 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001057 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001058 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001059 final ContentResolver cr = context.getContentResolver();
1060 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1061
1062 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001063 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001064 while (iter.hasNext()) {
1065 long id = iter.next();
1066 if (id < 0) {
1067 iter.remove();
1068 }
1069 }
1070
1071 Runnable r = new Runnable() {
1072 @Override
1073 public void run() {
Yura085c8532014-02-11 15:15:29 +00001074 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001075 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001076 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001077 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001078 for (int i = 0; i < count; i++) {
1079 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001080 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001081 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1082 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001083 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001084 }
Yura085c8532014-02-11 15:15:29 +00001085
1086 try {
1087 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1088 } catch (Exception ex) {
1089 throw new RuntimeException(ex);
1090 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001091
Winson Chungba9c37f2013-08-30 14:11:37 -07001092 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001093 sBgWorkspaceScreens.clear();
1094 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001095 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001096 }
1097 };
1098 runOnWorkerThread(r);
1099 }
1100
1101 /**
Winsonc0b52fe2015-09-09 16:38:15 -07001102 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001103 */
Winsonc0b52fe2015-09-09 16:38:15 -07001104 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001105 final ContentResolver cr = context.getContentResolver();
1106
Michael Jurkac9d95c52011-08-29 14:03:34 -07001107 Runnable r = new Runnable() {
1108 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001109 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001110 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001111 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001112 sBgItemsIdMap.remove(info.id);
1113 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001114 sBgWorkspaceItems.remove(info);
1115 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001116
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001117 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001118 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001119 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001120 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001121 for (ItemInfo childInfo : info.contents) {
1122 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001123 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001124 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001125 }
1126 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001127 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001128 }
1129
1130 /**
1131 * Set this as the current Launcher activity object for the loader.
1132 */
1133 public void initialize(Callbacks callbacks) {
1134 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001135 Preconditions.assertUIThread();
1136 // Remove any queued UI runnables
1137 mHandler.cancelAll();
1138 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001139 }
1140 }
1141
Kenny Guyed131872014-04-30 03:02:21 +01001142 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001143 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001144 int op = PackageUpdatedTask.OP_UPDATE;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001145 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001146 user));
1147 }
1148
1149 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001150 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001151 int op = PackageUpdatedTask.OP_REMOVE;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001152 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001153 user));
1154 }
1155
1156 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001157 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001158 int op = PackageUpdatedTask.OP_ADD;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001159 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001160 user));
1161 }
1162
1163 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001164 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001165 boolean replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001166 enqueueItemUpdatedTask(
Sunny Goyal144154d2016-03-30 16:47:03 -07001167 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, packageNames, user));
Kenny Guyed131872014-04-30 03:02:21 +01001168 }
1169
1170 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001171 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001172 boolean replacing) {
1173 if (!replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001174 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guyed131872014-04-30 03:02:21 +01001175 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1176 user));
1177 }
Kenny Guyed131872014-04-30 03:02:21 +01001178 }
1179
Kenny Guy44cba692016-01-21 19:50:02 +00001180 @Override
1181 public void onPackagesSuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001182 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001183 PackageUpdatedTask.OP_SUSPEND, packageNames,
1184 user));
1185 }
1186
1187 @Override
1188 public void onPackagesUnsuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001189 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001190 PackageUpdatedTask.OP_UNSUSPEND, packageNames,
1191 user));
1192 }
1193
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001194 @Override
1195 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
1196 UserHandleCompat user) {
Sunny Goyal50941fb2016-08-04 12:03:52 -07001197 enqueueItemUpdatedTask(new ShortcutsChangedTask(packageName, shortcuts, user, true));
1198 }
1199
1200 public void updatePinnedShortcuts(String packageName, List<ShortcutInfoCompat> shortcuts,
1201 UserHandleCompat user) {
1202 enqueueItemUpdatedTask(new ShortcutsChangedTask(packageName, shortcuts, user, false));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001203 }
1204
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001205 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001206 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1207 * ACTION_PACKAGE_CHANGED.
1208 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001209 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001210 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001211 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001212
Joe Onorato36115782010-06-17 13:28:48 -04001213 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001214 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001215 // If we have changed locale we need to clear out the labels in all apps/workspace.
1216 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001217 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1218 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001219 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001220 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001221 } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1222 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
1223 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyalda891c12016-03-18 18:29:24 -07001224 UserHandleCompat user = UserHandleCompat.fromIntent(intent);
1225 if (user != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001226 if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1227 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
1228 enqueueItemUpdatedTask(new PackageUpdatedTask(
1229 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE,
1230 new String[0], user));
1231 }
1232
1233 // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so
1234 // we need to run the state change task again.
1235 if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
1236 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
1237 enqueueItemUpdatedTask(new UserLockStateChangedTask(user));
1238 }
Sunny Goyalda891c12016-03-18 18:29:24 -07001239 }
Tony Wickham827cef22016-03-17 15:39:39 -07001240 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
1241 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001242 }
1243 }
1244
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001245 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001246 resetLoadedState(true, true);
1247
Reena Lee93f824a2011-09-23 17:20:28 -07001248 // Do this here because if the launcher activity is running it will be restarted.
1249 // If it's not running startLoaderFromBackground will merely tell it that it needs
1250 // to reload.
1251 startLoaderFromBackground();
1252 }
1253
Winson Chungf0c6ae02012-03-21 16:10:31 -07001254 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1255 synchronized (mLock) {
1256 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1257 // mWorkspaceLoaded to true later
1258 stopLoaderLocked();
1259 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1260 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001261 // Always reset deep shortcuts loaded.
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001262 // TODO: why?
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001263 mDeepShortcutsLoaded = false;
Winson Chungf0c6ae02012-03-21 16:10:31 -07001264 }
1265 }
1266
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001267 /**
1268 * When the launcher is in the background, it's possible for it to miss paired
1269 * configuration changes. So whenever we trigger the loader from the background
1270 * tell the launcher that it needs to re-run the loader when it comes back instead
1271 * of doing it now.
1272 */
1273 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001274 Callbacks callbacks = getCallback();
1275 if (callbacks != null) {
1276 // Only actually run the loader if they're not paused.
1277 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -07001278 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001279 }
1280 }
Joe Onorato36115782010-06-17 13:28:48 -04001281 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001282
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001283 /**
1284 * If there is already a loader task running, tell it to stop.
1285 */
1286 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001287 LoaderTask oldTask = mLoaderTask;
1288 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001289 oldTask.stopLocked();
1290 }
Reena Lee93f824a2011-09-23 17:20:28 -07001291 }
1292
Adam Cohen1a85c582014-09-30 09:48:49 -07001293 public boolean isCurrentCallbacks(Callbacks callbacks) {
1294 return (mCallbacks != null && mCallbacks.get() == callbacks);
1295 }
1296
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001297 /**
1298 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
1299 * @return true if the page could be bound synchronously.
1300 */
1301 public boolean startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001302 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1303 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001304 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -04001305 // Don't bother to start the thread if we know it's not going to do anything
1306 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001307 final Callbacks oldCallbacks = mCallbacks.get();
1308 // Clear any pending bind-runnables from the synchronized load process.
1309 runOnMainThread(new Runnable() {
1310 public void run() {
1311 oldCallbacks.clearPendingBinds();
1312 }
1313 });
1314
Joe Onorato36115782010-06-17 13:28:48 -04001315 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001316 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001317 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001318 // TODO: mDeepShortcutsLoaded does not need to be true for synchronous bind.
Tony Wickham80f57872016-06-29 18:12:15 -07001319 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE && mAllAppsLoaded
1320 && mWorkspaceLoaded && mDeepShortcutsLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001321 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001322 return true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001323 } else {
1324 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1325 sWorker.post(mLoaderTask);
1326 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001327 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001328 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001329 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001330 }
1331
Joe Onorato36115782010-06-17 13:28:48 -04001332 public void stopLoader() {
1333 synchronized (mLock) {
1334 if (mLoaderTask != null) {
1335 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001336 }
1337 }
Joe Onorato36115782010-06-17 13:28:48 -04001338 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001339
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001340 /**
1341 * Loads the workspace screen ids in an ordered list.
1342 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001343 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001344 final ContentResolver contentResolver = context.getContentResolver();
1345 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001346
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001347 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001348 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
1349 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -07001350 }
1351
Joe Onorato36115782010-06-17 13:28:48 -04001352 /**
1353 * Runnable for the thread that loads the contents of the launcher:
1354 * - workspace icons
1355 * - widgets
1356 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001357 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -04001358 */
1359 private class LoaderTask implements Runnable {
1360 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001361 private int mPageToBindFirst;
1362
Adam Cohen091440a2015-03-18 14:16:05 -07001363 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001364 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001365 @Thunk boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001366
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001367 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -04001368 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001369 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001370 }
1371
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001372 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001373 mIsLoadingAndBindingWorkspace = true;
1374
Joe Onorato36115782010-06-17 13:28:48 -04001375 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001376 if (DEBUG_LOADERS) {
1377 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001378 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001379
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001380 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001381 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001382 synchronized (LoaderTask.this) {
1383 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001384 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001385 }
1386 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001387 }
1388 }
1389
Joe Onorato36115782010-06-17 13:28:48 -04001390 // Bind the workspace
Sunny Goyal93f878c2016-03-30 17:31:24 -07001391 bindWorkspace(mPageToBindFirst);
Joe Onorato36115782010-06-17 13:28:48 -04001392 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001393
Joe Onorato36115782010-06-17 13:28:48 -04001394 private void waitForIdle() {
1395 // Wait until the either we're stopped or the other threads are done.
1396 // This way we don't start loading all apps until the workspace has settled
1397 // down.
1398 synchronized (LoaderTask.this) {
1399 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001400
Joe Onorato36115782010-06-17 13:28:48 -04001401 mHandler.postIdle(new Runnable() {
1402 public void run() {
1403 synchronized (LoaderTask.this) {
1404 mLoadAndBindStepFinished = true;
1405 if (DEBUG_LOADERS) {
1406 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001407 }
Joe Onorato36115782010-06-17 13:28:48 -04001408 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001409 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001410 }
Joe Onorato36115782010-06-17 13:28:48 -04001411 });
1412
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001413 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001414 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001415 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1416 // wait no longer than 1sec at a time
1417 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001418 } catch (InterruptedException ex) {
1419 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001420 }
1421 }
Joe Onorato36115782010-06-17 13:28:48 -04001422 if (DEBUG_LOADERS) {
1423 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001424 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001425 + "ms for previous step to finish binding");
1426 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001427 }
Joe Onorato36115782010-06-17 13:28:48 -04001428 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001429
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001430 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001431 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001432 // Ensure that we have a valid page index to load synchronously
1433 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1434 "valid page index");
1435 }
1436 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1437 // Ensure that we don't try and bind a specified page when the pages have not been
1438 // loaded already (we should load everything asynchronously in that case)
1439 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1440 }
1441 synchronized (mLock) {
1442 if (mIsLoaderTaskRunning) {
1443 // Ensure that we are never running the background loading at this point since
1444 // we also touch the background collections
1445 throw new RuntimeException("Error! Background loading is already running");
1446 }
1447 }
1448
1449 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1450 // data structures, we can't allow any other thread to touch that data, but because
1451 // this call is synchronous, we can get away with not locking).
1452
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001453 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001454 // operations from the previous activity. We need to ensure that all queued operations
1455 // are executed before any synchronous binding work is done.
1456 mHandler.flush();
1457
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001458 // Divide the set of loaded items into those that we are binding synchronously, and
1459 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001460 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001461 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1462 // arise from that.
1463 onlyBindAllApps();
Tony Wickham80f57872016-06-29 18:12:15 -07001464
1465 bindDeepShortcuts();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001466 }
1467
Joe Onorato36115782010-06-17 13:28:48 -04001468 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001469 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001470 if (mStopped) {
1471 return;
1472 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001473 mIsLoaderTaskRunning = true;
1474 }
Joe Onorato36115782010-06-17 13:28:48 -04001475 // Optimize for end-user experience: if the Launcher is up and // running with the
1476 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1477 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001478 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001479 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001480 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001481
Joe Onorato36115782010-06-17 13:28:48 -04001482 if (mStopped) {
1483 break keep_running;
1484 }
1485
Joe Onorato36115782010-06-17 13:28:48 -04001486 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001487
1488 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001489 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1490 loadAndBindAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001491
1492 waitForIdle();
1493
1494 // third step
1495 if (DEBUG_LOADERS) Log.d(TAG, "step 3: loading deep shortcuts");
1496 loadAndBindDeepShortcuts();
Joe Onorato36115782010-06-17 13:28:48 -04001497 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001498
Joe Onorato36115782010-06-17 13:28:48 -04001499 // Clear out this reference, otherwise we end up holding it until all of the
1500 // callback runnables are done.
1501 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001502
Joe Onorato36115782010-06-17 13:28:48 -04001503 synchronized (mLock) {
1504 // If we are still the last one to be scheduled, remove ourselves.
1505 if (mLoaderTask == this) {
1506 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001507 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001508 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001509 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001510 }
Joe Onorato36115782010-06-17 13:28:48 -04001511 }
1512
1513 public void stopLocked() {
1514 synchronized (LoaderTask.this) {
1515 mStopped = true;
1516 this.notify();
1517 }
1518 }
1519
1520 /**
1521 * Gets the callbacks object. If we've been stopped, or if the launcher object
1522 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1523 * object that was around when the deferred message was scheduled, and if there's
1524 * a new Callbacks object around then also return null. This will save us from
1525 * calling onto it with data that will be ignored.
1526 */
1527 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1528 synchronized (mLock) {
1529 if (mStopped) {
1530 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001531 }
Joe Onorato36115782010-06-17 13:28:48 -04001532
1533 if (mCallbacks == null) {
1534 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001535 }
Joe Onorato36115782010-06-17 13:28:48 -04001536
1537 final Callbacks callbacks = mCallbacks.get();
1538 if (callbacks != oldCallbacks) {
1539 return null;
1540 }
1541 if (callbacks == null) {
1542 Log.w(TAG, "no mCallbacks");
1543 return null;
1544 }
1545
1546 return callbacks;
1547 }
1548 }
1549
1550 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001551 private boolean checkItemPlacement(LongArrayMap<GridOccupancy> occupied, ItemInfo item,
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001552 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001553 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001554 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung892c74d2013-08-22 16:15:50 -07001555
Adam Cohendcd297f2013-06-18 13:13:40 -07001556 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001557 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001558 // Return early if we detect that an item is under the hotseat button
Sunny Goyalbb011da2016-06-15 15:42:29 -07001559 if (!FeatureFlags.NO_ALL_APPS_ICON &&
1560 profile.isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001561 Log.e(TAG, "Error loading shortcut into hotseat " + item
1562 + " into position (" + item.screenId + ":" + item.cellX + ","
1563 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001564 return false;
1565 }
1566
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001567 final GridOccupancy hotseatOccupancy =
Dan Sandler295ae182013-12-10 16:05:47 -05001568 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1569
Adam Cohen2e6da152015-05-06 11:42:25 -07001570 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001571 Log.e(TAG, "Error loading shortcut " + item
1572 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001573 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001574 + ")");
1575 return false;
1576 }
1577
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001578 if (hotseatOccupancy != null) {
1579 if (hotseatOccupancy.cells[(int) item.screenId][0]) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001580 Log.e(TAG, "Error loading shortcut into hotseat " + item
1581 + " into position (" + item.screenId + ":" + item.cellX + ","
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001582 + item.cellY + ") already occupied");
Adam Cohendcd297f2013-06-18 13:13:40 -07001583 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001584 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001585 hotseatOccupancy.cells[(int) item.screenId][0] = true;
Dan Sandler295ae182013-12-10 16:05:47 -05001586 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001587 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001588 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001589 final GridOccupancy occupancy = new GridOccupancy(profile.numHotseatIcons, 1);
1590 occupancy.cells[(int) item.screenId][0] = true;
1591 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, occupancy);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001592 return true;
1593 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001594 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1595 if (!workspaceScreens.contains((Long) item.screenId)) {
1596 // The item has an invalid screen id.
1597 return false;
1598 }
1599 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001600 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001601 return true;
1602 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001603
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001604 final int countX = profile.numColumns;
1605 final int countY = profile.numRows;
Adam Cohenae4409d2013-11-26 10:34:59 -08001606 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1607 item.cellX < 0 || item.cellY < 0 ||
1608 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1609 Log.e(TAG, "Error loading shortcut " + item
1610 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1611 + item.cellX + "," + item.cellY
1612 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1613 return false;
1614 }
1615
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001616 if (!occupied.containsKey(item.screenId)) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001617 GridOccupancy screen = new GridOccupancy(countX + 1, countY + 1);
1618 if (item.screenId == Workspace.FIRST_SCREEN_ID) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001619 // Mark the first row as occupied (if the feature is enabled)
1620 // in order to account for the QSB.
1621 screen.markCells(0, 0, countX + 1, 1, FeatureFlags.QSB_ON_FIRST_SCREEN);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001622 }
1623 occupied.put(item.screenId, screen);
Joe Onorato36115782010-06-17 13:28:48 -04001624 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001625 final GridOccupancy occupancy = occupied.get(item.screenId);
Winson Chungf30ad5f2011-08-08 10:55:42 -07001626
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001627 // Check if any workspace icons overlap with each other
1628 if (occupancy.isRegionVacant(item.cellX, item.cellY, item.spanX, item.spanY)) {
1629 occupancy.markCells(item, true);
1630 return true;
1631 } else {
1632 Log.e(TAG, "Error loading shortcut " + item
1633 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1634 + item.cellX + "," + item.cellX + "," + item.spanX + "," + item.spanY
1635 + ") already occupied");
1636 return false;
1637 }
Joe Onorato36115782010-06-17 13:28:48 -04001638 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001639
Winson Chungba9c37f2013-08-30 14:11:37 -07001640 /** Clears all the sBg data structures */
1641 private void clearSBgDataStructures() {
1642 synchronized (sBgLock) {
1643 sBgWorkspaceItems.clear();
1644 sBgAppWidgets.clear();
1645 sBgFolders.clear();
1646 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001647 sBgWorkspaceScreens.clear();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001648 sBgPinnedShortcutCounts.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001649 }
1650 }
1651
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001652 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -07001653 if (LauncherAppState.PROFILE_STARTUP) {
1654 Trace.beginSection("Loading Workspace");
1655 }
Joe Onorato36115782010-06-17 13:28:48 -04001656 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001657
Joe Onorato36115782010-06-17 13:28:48 -04001658 final Context context = mContext;
1659 final ContentResolver contentResolver = context.getContentResolver();
1660 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001661 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001662 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001663 final boolean isSdCardReady = Utilities.isBootCompleted();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001664
Winson Chung892c74d2013-08-22 16:15:50 -07001665 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001666 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001667 int countX = profile.numColumns;
1668 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001669
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001670 boolean clearDb = false;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -07001671 try {
1672 ImportDataTask.performImportIfPossible(context);
1673 } catch (Exception e) {
1674 // Migration failed. Clear workspace.
1675 clearDb = true;
1676 }
1677
1678 if (!clearDb && GridSizeMigrationTask.ENABLED &&
Sunny Goyalf076eae2016-01-11 12:25:10 -08001679 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1680 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001681 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001682 }
1683
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001684 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -07001685 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001686 LauncherSettings.Settings.call(contentResolver,
1687 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001688 }
1689
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001690 Log.d(TAG, "loadWorkspace: loading default favorites");
1691 LauncherSettings.Settings.call(contentResolver,
1692 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -07001693
Winson Chung2abf94d2012-07-18 18:16:38 -07001694 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001695 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001696 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001697 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001698 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001699
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001700 final ArrayList<Long> itemsToRemove = new ArrayList<>();
1701 final ArrayList<Long> restoredRows = new ArrayList<>();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001702 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001703 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001704 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001705 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001706
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001707 // +1 for the hotseat (it can be larger than the workspace)
1708 // Load workspace in reverse order to ensure that latest items are loaded first (and
1709 // before any earlier duplicates)
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001710 final LongArrayMap<GridOccupancy> occupied = new LongArrayMap<>();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001711 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001712
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001713 try {
1714 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1715 final int intentIndex = c.getColumnIndexOrThrow
1716 (LauncherSettings.Favorites.INTENT);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001717 final int containerIndex = c.getColumnIndexOrThrow(
1718 LauncherSettings.Favorites.CONTAINER);
1719 final int itemTypeIndex = c.getColumnIndexOrThrow(
1720 LauncherSettings.Favorites.ITEM_TYPE);
1721 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1722 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001723 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1724 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001725 final int screenIndex = c.getColumnIndexOrThrow(
1726 LauncherSettings.Favorites.SCREEN);
1727 final int cellXIndex = c.getColumnIndexOrThrow
1728 (LauncherSettings.Favorites.CELLX);
1729 final int cellYIndex = c.getColumnIndexOrThrow
1730 (LauncherSettings.Favorites.CELLY);
1731 final int spanXIndex = c.getColumnIndexOrThrow
1732 (LauncherSettings.Favorites.SPANX);
1733 final int spanYIndex = c.getColumnIndexOrThrow(
1734 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001735 final int rankIndex = c.getColumnIndexOrThrow(
1736 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001737 final int restoredIndex = c.getColumnIndexOrThrow(
1738 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001739 final int profileIdIndex = c.getColumnIndexOrThrow(
1740 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001741 final int optionsIndex = c.getColumnIndexOrThrow(
1742 LauncherSettings.Favorites.OPTIONS);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001743 final CursorIconInfo cursorIconInfo = new CursorIconInfo(mContext, c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001744
Sunny Goyal7f834d22015-04-21 10:10:23 -07001745 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
Kenny Guyff05f432016-01-22 17:48:29 +00001746 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001747 final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>();
Sunny Goyal7f834d22015-04-21 10:10:23 -07001748 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001749 long serialNo = mUserManager.getSerialNumberForUser(user);
1750 allUsers.put(serialNo, user);
1751 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001752
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001753 boolean userUnlocked = mUserManager.isUserUnlocked(user);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001754
1755 // We can only query for shortcuts when the user is unlocked.
1756 if (userUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07001757 List<ShortcutInfoCompat> pinnedShortcuts =
1758 mDeepShortcutManager.queryForPinnedShortcuts(null, user);
1759 if (mDeepShortcutManager.wasLastCallSuccess()) {
1760 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
1761 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
1762 shortcut);
1763 }
1764 } else {
1765 // Shortcut manager can fail due to some race condition when the
1766 // lock state changes too frequently. For the purpose of the loading
1767 // shortcuts, consider the user is still locked.
1768 userUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001769 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001770 }
Sunny Goyal49f4f032016-08-01 15:45:49 -07001771 unlockedUsers.put(serialNo, userUnlocked);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001772 }
1773
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001774 ShortcutInfo info;
1775 String intentDescription;
1776 LauncherAppWidgetInfo appWidgetInfo;
1777 int container;
1778 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001779 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001780 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001781 UserHandleCompat user;
Sunny Goyald09c3702016-04-06 16:18:20 -07001782 String targetPackage;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001783
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001784 while (!mStopped && c.moveToNext()) {
1785 try {
1786 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001787 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001788 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001789 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001790
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001791 switch (itemType) {
1792 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1793 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001794 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001795 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001796 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001797 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001798 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001799 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001800 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001801 boolean itemReplaced = false;
Sunny Goyald09c3702016-04-06 16:18:20 -07001802 targetPackage = null;
Kenny Guyed131872014-04-30 03:02:21 +01001803 if (user == null) {
1804 // User has been deleted remove the item.
1805 itemsToRemove.add(id);
1806 continue;
1807 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001808 try {
1809 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001810 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001811 if (cn != null && cn.getPackageName() != null) {
1812 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1813 cn.getPackageName(), user);
1814 boolean validComponent = validPkg &&
1815 launcherApps.isActivityEnabledForProfile(cn, user);
Sunny Goyald09c3702016-04-06 16:18:20 -07001816 if (validPkg) {
1817 targetPackage = cn.getPackageName();
1818 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001819
1820 if (validComponent) {
1821 if (restored) {
1822 // no special handling necessary for this item
1823 restoredRows.add(id);
1824 restored = false;
1825 }
Kenny Guyff05f432016-01-22 17:48:29 +00001826 if (quietMode.get(serialNumber)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001827 disabledState = ShortcutInfo.FLAG_DISABLED_QUIET_USER;
Kenny Guyff05f432016-01-22 17:48:29 +00001828 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001829 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001830 intent = null;
1831 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1832 // We allow auto install apps to have their intent
1833 // updated after an install.
1834 intent = manager.getLaunchIntentForPackage(
1835 cn.getPackageName());
1836 if (intent != null) {
1837 ContentValues values = new ContentValues();
1838 values.put(LauncherSettings.Favorites.INTENT,
1839 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001840 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001841 }
1842 }
1843
1844 if (intent == null) {
1845 // The app is installed but the component is no
1846 // longer available.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001847 FileLog.d(TAG, "Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001848 itemsToRemove.add(id);
1849 continue;
1850 } else {
1851 // no special handling necessary for this item
1852 restoredRows.add(id);
1853 restored = false;
1854 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001855 } else if (restored) {
1856 // Package is not yet available but might be
1857 // installed later.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001858 FileLog.d(TAG, "package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001859
1860 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1861 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001862 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001863 // App restore has started. Update the flag
1864 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1865 ContentValues values = new ContentValues();
1866 values.put(LauncherSettings.Favorites.RESTORED,
1867 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001868 updateItem(id, values);
1869 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1870 // This is a common app. Try to replace this.
1871 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1872 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1873 if (parser.findDefaultApp()) {
1874 // Default app found. Replace it.
1875 intent = parser.parsedIntent;
1876 cn = intent.getComponent();
1877 ContentValues values = parser.parsedValues;
1878 values.put(LauncherSettings.Favorites.RESTORED, 0);
1879 updateItem(id, values);
1880 restored = false;
1881 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001882
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001883 } else {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001884 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001885 itemsToRemove.add(id);
1886 continue;
1887 }
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 Goyal94485362014-09-18 16:13:58 -07001890 itemsToRemove.add(id);
1891 continue;
1892 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001893 } else if (PackageManagerHelper.isAppOnSdcard(
1894 manager, cn.getPackageName())) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07001895 // Package is present but not available.
1896 allowMissingTarget = true;
1897 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1898 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001899 // SdCard is not ready yet. Package might get available,
1900 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001901 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001902 HashSet<String> pkgs = sPendingPackages.get(user);
1903 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001904 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001905 sPendingPackages.put(user, pkgs);
1906 }
1907 pkgs.add(cn.getPackageName());
1908 allowMissingTarget = true;
1909 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001910
1911 } else {
1912 // Do not wait for external media load anymore.
1913 // Log the invalid package, and remove it
Sunny Goyal713edfc2016-05-06 09:58:34 -07001914 FileLog.d(TAG, "Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001915 itemsToRemove.add(id);
1916 continue;
Winson Chungee055712013-07-30 14:46:24 -07001917 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001918 } else if (cn == null) {
1919 // For shortcuts with no component, keep them as they are
1920 restoredRows.add(id);
1921 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001922 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001923 } catch (URISyntaxException e) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001924 FileLog.d(TAG, "Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001925 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001926 continue;
1927 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001928
Sunny Goyal34b65272015-03-11 16:56:52 -07001929 boolean useLowResIcon = container >= 0 &&
1930 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1931
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001932 if (itemReplaced) {
1933 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001934 info = getAppShortcutInfo(intent, user, null,
1935 cursorIconInfo, false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001936 } else {
1937 // Don't replace items for other profiles.
1938 itemsToRemove.add(id);
1939 continue;
1940 }
1941 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001942 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001943 info = getRestoredItemInfo(c, intent,
1944 promiseType, itemType, cursorIconInfo);
Kenny Guyed131872014-04-30 03:02:21 +01001945 intent = getRestoredItemIntent(c, context, intent);
1946 } else {
1947 // Don't restore items for other profiles.
1948 itemsToRemove.add(id);
1949 continue;
1950 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001951 } else if (itemType ==
1952 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001953 info = getAppShortcutInfo(intent, user, c,
1954 cursorIconInfo, allowMissingTarget, useLowResIcon);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001955 } else if (itemType ==
1956 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001957
1958 ShortcutKey key = ShortcutKey.fromIntent(intent, user);
1959 if (unlockedUsers.get(serialNumber)) {
1960 ShortcutInfoCompat pinnedShortcut =
1961 shortcutKeyToPinnedShortcuts.get(key);
1962 if (pinnedShortcut == null) {
1963 // The shortcut is no longer valid.
1964 itemsToRemove.add(id);
1965 continue;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001966 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001967 info = new ShortcutInfo(pinnedShortcut, context);
1968 intent = info.intent;
1969 } else {
1970 // Create a shortcut info in disabled mode for now.
1971 info = new ShortcutInfo();
1972 info.user = user;
1973 info.itemType = itemType;
1974 loadInfoFromCursor(info, c, cursorIconInfo);
1975
1976 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001977 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001978 incrementPinnedShortcutCount(key, false /* shouldPin */);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001979 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001980 info = getShortcutInfo(c, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001981
Sunny Goyald09c3702016-04-06 16:18:20 -07001982 // Shortcuts are only available on the primary profile
1983 if (PackageManagerHelper.isAppSuspended(manager, targetPackage)) {
1984 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1985 }
1986
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001987 // App shortcuts that used to be automatically added to Launcher
1988 // didn't always have the correct intent flags set, so do that
1989 // here
1990 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001991 intent.getCategories() != null &&
1992 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001993 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001994 intent.addFlags(
1995 Intent.FLAG_ACTIVITY_NEW_TASK |
1996 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1997 }
Michael Jurka96879562012-03-22 05:54:33 -07001998 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001999
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002000 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07002001 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002002 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002003 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002004 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002005 info.cellX = c.getInt(cellXIndex);
2006 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08002007 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002008 info.spanX = 1;
2009 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002010 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002011 if (info.promisedIntent != null) {
2012 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
2013 }
Sunny Goyald09c3702016-04-06 16:18:20 -07002014 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002015 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2016 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2017 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002018
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002019 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002020 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002021 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002022 break;
2023 }
2024
Sunny Goyal756adbc2015-04-16 15:20:51 -07002025 if (restored) {
2026 ComponentName cn = info.getTargetComponent();
2027 if (cn != null) {
2028 Integer progress = installingPkgs.get(cn.getPackageName());
2029 if (progress != null) {
2030 info.setInstallProgress(progress);
2031 } else {
2032 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2033 }
2034 }
2035 }
2036
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002037 switch (container) {
2038 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2039 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2040 sBgWorkspaceItems.add(info);
2041 break;
2042 default:
2043 // Item is in a user folder
2044 FolderInfo folderInfo =
2045 findOrMakeFolder(sBgFolders, container);
Sunny Goyalc52ba712016-04-05 15:59:05 -07002046 folderInfo.add(info, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002047 break;
2048 }
2049 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002050 } else {
2051 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002052 }
2053 break;
2054
2055 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2056 id = c.getLong(idIndex);
2057 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2058
Sunny Goyala508e4f2015-05-21 09:33:57 -07002059 // Do not trim the folder label, as is was set by the user.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002060 folderInfo.title = c.getString(cursorIconInfo.titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002061 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002062 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002063 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002064 folderInfo.cellX = c.getInt(cellXIndex);
2065 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002066 folderInfo.spanX = 1;
2067 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002068 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002069
Daniel Sandler8802e962010-05-26 16:28:16 -04002070 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002071 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002072 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002073 break;
2074 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002075
Joe Onorato9c1289c2009-08-17 11:03:03 -04002076 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002077 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2078 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2079 sBgWorkspaceItems.add(folderInfo);
2080 break;
Joe Onorato36115782010-06-17 13:28:48 -04002081 }
Joe Onorato17a89222011-02-08 17:26:11 -08002082
Chris Wrenf4d08112014-01-16 18:13:56 -05002083 if (restored) {
2084 // no special handling required for restored folders
2085 restoredRows.add(id);
2086 }
2087
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002088 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2089 sBgFolders.put(folderInfo.id, folderInfo);
2090 break;
2091
2092 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002093 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002094 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002095 boolean customWidget = itemType ==
2096 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2097
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002098 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002099 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002100 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002101 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002102 user = allUsers.get(serialNumber);
2103 if (user == null) {
2104 itemsToRemove.add(id);
2105 continue;
2106 }
2107
Sunny Goyalff572272014-07-23 13:58:07 -07002108 final ComponentName component =
2109 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002110
Sunny Goyal651077b2014-06-30 14:15:31 -07002111 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002112 final boolean isIdValid = (restoreStatus &
2113 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002114 final boolean wasProviderReady = (restoreStatus &
2115 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002116
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002117 if (widgetProvidersMap == null) {
2118 widgetProvidersMap = AppWidgetManagerCompat
2119 .getInstance(mContext).getAllProvidersMap();
2120 }
2121 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
2122 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00002123 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002124 user));
Sunny Goyalff572272014-07-23 13:58:07 -07002125
2126 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002127 if (!isSafeMode && !customWidget &&
2128 wasProviderReady && !isProviderReady) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002129 FileLog.d(TAG, "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07002130 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002131 itemsToRemove.add(id);
2132 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002133 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002134 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2135 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002136
Sunny Goyal53f96722015-07-13 19:54:53 -07002137 // The provider is available. So the widget is either
2138 // available or not available. We do not need to track
2139 // any future restore updates.
2140 int status = restoreStatus &
2141 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002142 if (!wasProviderReady) {
2143 // If provider was not previously ready, update the
2144 // status and UI flag.
2145
2146 // Id would be valid only if the widget restore broadcast was received.
2147 if (isIdValid) {
Sunny Goyal86df1382016-08-10 15:03:22 -07002148 status |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002149 } else {
2150 status &= ~LauncherAppWidgetInfo
2151 .FLAG_PROVIDER_NOT_READY;
2152 }
2153 }
2154 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002155 } else {
2156 Log.v(TAG, "Widget restore pending id=" + id
2157 + " appWidgetId=" + appWidgetId
2158 + " status =" + restoreStatus);
2159 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002160 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002161 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002162 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002163
2164 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2165 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002166 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002167 // App restore has started. Update the flag
2168 appWidgetInfo.restoreStatus |=
2169 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalb05a00a2016-08-29 10:06:57 -07002170 } else if (!isSafeMode) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002171 FileLog.d(TAG, "Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07002172 itemsToRemove.add(id);
2173 continue;
2174 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002175
2176 appWidgetInfo.installProgress =
2177 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002178 }
Sunny Goyal86df1382016-08-10 15:03:22 -07002179 if (appWidgetInfo.hasRestoreFlag(
2180 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG)) {
2181 intentDescription = c.getString(intentIndex);
2182 if (!TextUtils.isEmpty(intentDescription)) {
2183 appWidgetInfo.bindOptions =
2184 Intent.parseUri(intentDescription, 0);
2185 }
2186 }
Sunny Goyalff572272014-07-23 13:58:07 -07002187
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002188 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002189 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002190 appWidgetInfo.cellX = c.getInt(cellXIndex);
2191 appWidgetInfo.cellY = c.getInt(cellYIndex);
2192 appWidgetInfo.spanX = c.getInt(spanXIndex);
2193 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002194 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002195
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002196 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2197 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2198 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002199 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2200 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002201 continue;
2202 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002203
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002204 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002205 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002206 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002207 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002208 break;
2209 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002210
Adam Cohen59400422014-03-05 18:07:04 -08002211 if (!customWidget) {
2212 String providerName =
2213 appWidgetInfo.providerName.flattenToString();
2214 if (!providerName.equals(savedProvider) ||
2215 (appWidgetInfo.restoreStatus != restoreStatus)) {
2216 ContentValues values = new ContentValues();
2217 values.put(
2218 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2219 providerName);
2220 values.put(LauncherSettings.Favorites.RESTORED,
2221 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002222 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002223 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002224 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002225 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2226 sBgAppWidgets.add(appWidgetInfo);
2227 }
Joe Onorato36115782010-06-17 13:28:48 -04002228 break;
2229 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002230 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002231 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002232 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002233 }
2234 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002235 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002236 }
2237
Winson Chungba9c37f2013-08-30 14:11:37 -07002238 // Break early if we've stopped loading
2239 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002240 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002241 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002242 }
2243
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002244 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002245 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002246 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2247 Utilities.createDbSelectionQuery(
2248 LauncherSettings.Favorites._ID, itemsToRemove), null);
2249 if (DEBUG_LOADERS) {
2250 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2251 LauncherSettings.Favorites._ID, itemsToRemove));
2252 }
2253
2254 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07002255 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
2256 .call(contentResolver,
2257 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
2258 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
2259 for (long folderId : deletedFolderIds) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002260 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2261 sBgFolders.remove(folderId);
2262 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002263 }
2264 }
2265
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002266 // Unpin shortcuts that don't exist on the workspace.
2267 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
2268 MutableInt numTimesPinned = sBgPinnedShortcutCounts.get(key);
2269 if (numTimesPinned == null || numTimesPinned.value == 0) {
2270 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
2271 mDeepShortcutManager.unpinShortcut(key);
2272 }
2273 }
2274
Sunny Goyal317698b2015-07-29 11:45:41 -07002275 // Sort all the folder items and make sure the first 3 items are high resolution.
2276 for (FolderInfo folder : sBgFolders) {
2277 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2278 int pos = 0;
2279 for (ShortcutInfo info : folder.contents) {
2280 if (info.usingLowResIcon) {
2281 info.updateIcon(mIconCache, false);
2282 }
2283 pos ++;
2284 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2285 break;
2286 }
2287 }
2288 }
2289
Chris Wrenf4d08112014-01-16 18:13:56 -05002290 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002291 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002292 ContentValues values = new ContentValues();
2293 values.put(LauncherSettings.Favorites.RESTORED, 0);
2294 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2295 Utilities.createDbSelectionQuery(
2296 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002297 }
2298
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002299 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2300 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002301 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002302 null, sWorker);
2303 }
2304
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002305 // Remove any empty screens
2306 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002307 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002308 long screenId = item.screenId;
2309 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2310 unusedScreens.contains(screenId)) {
2311 unusedScreens.remove(screenId);
2312 }
2313 }
2314
2315 // If there are any empty screens remove them, and update.
2316 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002317 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002318 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002319 }
2320
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002321 if (DEBUG_LOADERS) {
2322 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2323 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002324 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002325 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002326 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002327
Sunny Goyale2df0622015-04-24 11:27:00 -07002328 for (int i = 0; i < nScreens; i++) {
2329 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002330 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002331 line += " | ";
2332 }
Joe Onorato36115782010-06-17 13:28:48 -04002333 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002334 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002335 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002336 }
Joe Onorato36115782010-06-17 13:28:48 -04002337 }
Sunny Goyale26d1002016-06-20 14:52:14 -07002338 if (LauncherAppState.PROFILE_STARTUP) {
2339 Trace.endSection();
2340 }
Adam Cohene25af792013-06-06 23:08:25 -07002341 }
2342
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002343 /**
2344 * Partially updates the item without any notification. Must be called on the worker thread.
2345 */
2346 private void updateItem(long itemId, ContentValues update) {
2347 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002348 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002349 update,
2350 BaseColumns._ID + "= ?",
2351 new String[]{Long.toString(itemId)});
2352 }
2353
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002354 /** Filters the set of items who are directly or indirectly (via another container) on the
2355 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002356 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002357 ArrayList<ItemInfo> allWorkspaceItems,
2358 ArrayList<ItemInfo> currentScreenItems,
2359 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002360 // Purge any null ItemInfos
2361 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2362 while (iter.hasNext()) {
2363 ItemInfo i = iter.next();
2364 if (i == null) {
2365 iter.remove();
2366 }
2367 }
2368
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002369 // Order the set of items by their containers first, this allows use to walk through the
2370 // list sequentially, build up a list of containers that are in the specified screen,
2371 // as well as all items in those containers.
2372 Set<Long> itemsOnScreen = new HashSet<Long>();
2373 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2374 @Override
2375 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07002376 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002377 }
2378 });
2379 for (ItemInfo info : allWorkspaceItems) {
2380 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002381 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002382 currentScreenItems.add(info);
2383 itemsOnScreen.add(info.id);
2384 } else {
2385 otherScreenItems.add(info);
2386 }
2387 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2388 currentScreenItems.add(info);
2389 itemsOnScreen.add(info.id);
2390 } else {
2391 if (itemsOnScreen.contains(info.container)) {
2392 currentScreenItems.add(info);
2393 itemsOnScreen.add(info.id);
2394 } else {
2395 otherScreenItems.add(info);
2396 }
2397 }
2398 }
2399 }
2400
2401 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002402 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002403 ArrayList<LauncherAppWidgetInfo> appWidgets,
2404 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2405 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002406
2407 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002408 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002409 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002410 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002411 currentScreenWidgets.add(widget);
2412 } else {
2413 otherScreenWidgets.add(widget);
2414 }
2415 }
2416 }
2417
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002418 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2419 * right) */
2420 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002421 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002422 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002423 final int screenCols = profile.numColumns;
2424 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002425 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002426 @Override
2427 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002428 if (lhs.container == rhs.container) {
2429 // Within containers, order by their spatial position in that container
2430 switch ((int) lhs.container) {
2431 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2432 long lr = (lhs.screenId * screenCellCount +
2433 lhs.cellY * screenCols + lhs.cellX);
2434 long rr = (rhs.screenId * screenCellCount +
2435 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07002436 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07002437 }
2438 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2439 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07002440 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07002441 }
2442 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002443 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002444 throw new RuntimeException("Unexpected container type when " +
2445 "sorting workspace items.");
2446 }
2447 return 0;
2448 }
2449 } else {
2450 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07002451 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07002452 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002453 }
2454 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002455 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002456
Adam Cohendcd297f2013-06-18 13:13:40 -07002457 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2458 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002459 final Runnable r = new Runnable() {
2460 @Override
2461 public void run() {
2462 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2463 if (callbacks != null) {
2464 callbacks.bindScreens(orderedScreens);
2465 }
2466 }
2467 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002468 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002469 }
2470
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002471 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2472 final ArrayList<ItemInfo> workspaceItems,
2473 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002474 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002475
2476 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002477 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002478 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002479 final int start = i;
2480 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002481 final Runnable r = new Runnable() {
2482 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002483 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002484 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002485 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002486 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2487 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002488 }
2489 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002490 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002491 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002492 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002493
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002494 // Bind the widgets, one at a time
2495 N = appWidgets.size();
2496 for (int i = 0; i < N; i++) {
2497 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2498 final Runnable r = new Runnable() {
2499 public void run() {
2500 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2501 if (callbacks != null) {
2502 callbacks.bindAppWidget(widget);
2503 }
2504 }
2505 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002506 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002507 }
2508 }
2509
2510 /**
2511 * Binds all loaded data to actual views on the main thread.
2512 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002513 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002514 final long t = SystemClock.uptimeMillis();
2515 Runnable r;
2516
2517 // Don't use these two variables in any of the callback runnables.
2518 // Otherwise we hold a reference to them.
2519 final Callbacks oldCallbacks = mCallbacks.get();
2520 if (oldCallbacks == null) {
2521 // This launcher has exited and nobody bothered to tell us. Just bail.
2522 Log.w(TAG, "LoaderTask running with no launcher");
2523 return;
2524 }
2525
Winson Chung9b9fb962013-11-15 15:39:34 -08002526 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07002527 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
2528 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
2529 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002530
Winson Chung2abf94d2012-07-18 18:16:38 -07002531 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002532 workspaceItems.addAll(sBgWorkspaceItems);
2533 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002534 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002535 }
2536
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002537 final int currentScreen;
2538 {
2539 int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE
2540 ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen();
2541 if (currScreen >= orderedScreenIds.size()) {
2542 // There may be no workspace screens (just hotseat items and an empty page).
2543 currScreen = PagedView.INVALID_RESTORE_PAGE;
2544 }
2545 currentScreen = currScreen;
Winson Chung9b9fb962013-11-15 15:39:34 -08002546 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002547 final boolean validFirstPage = currentScreen >= 0;
2548 final long currentScreenId =
2549 validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID;
Winson Chung9b9fb962013-11-15 15:39:34 -08002550
Winson Chung9b9fb962013-11-15 15:39:34 -08002551 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07002552 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
2553 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
2554 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
2555 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002556
Winson Chung9b9fb962013-11-15 15:39:34 -08002557 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002558 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002559 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002560 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002561 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2562 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2563
2564 // Tell the workspace that we're about to start binding items
2565 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002566 public void run() {
2567 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2568 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002569 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002570 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002571 }
2572 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002573 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002574 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002575
Adam Cohendcd297f2013-06-18 13:13:40 -07002576 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2577
Sunny Goyal527c7d32015-08-28 15:19:36 -07002578 Executor mainExecutor = new DeferredMainThreadExecutor();
2579 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07002580 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002581
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002582 // In case of validFirstPage, only bind the first screen, and defer binding the
2583 // remaining screens after first onDraw (and an optional the fade animation whichever
2584 // happens later).
2585 // This ensures that the first screen is immediately visible (eg. during rotation)
2586 // In case of !validFirstPage, bind all pages one after other.
2587 final Executor deferredExecutor =
2588 validFirstPage ? new ViewOnDrawExecutor(mHandler) : mainExecutor;
2589
2590 mainExecutor.execute(new Runnable() {
2591 @Override
2592 public void run() {
2593 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2594 if (callbacks != null) {
2595 callbacks.finishFirstPageBind(
2596 validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null);
2597 }
2598 }
2599 });
Sunny Goyal527c7d32015-08-28 15:19:36 -07002600
Sunny Goyal44c06432016-04-02 10:56:02 -07002601 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002602
2603 // Tell the workspace that we're done binding items
2604 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002605 public void run() {
2606 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2607 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002608 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002609 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002610
Sunny Goyal639e9062015-08-19 19:17:06 -07002611 mIsLoadingAndBindingWorkspace = false;
2612
2613 // Run all the bind complete runnables after workspace is bound.
2614 if (!mBindCompleteRunnables.isEmpty()) {
2615 synchronized (mBindCompleteRunnables) {
2616 for (final Runnable r : mBindCompleteRunnables) {
2617 runOnWorkerThread(r);
2618 }
2619 mBindCompleteRunnables.clear();
2620 }
2621 }
2622
Winson Chung98e030b2012-05-07 16:01:11 -07002623 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002624 if (DEBUG_LOADERS) {
2625 Log.d(TAG, "bound workspace in "
2626 + (SystemClock.uptimeMillis()-t) + "ms");
2627 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002628
Joe Onorato36115782010-06-17 13:28:48 -04002629 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002630 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002631 deferredExecutor.execute(r);
2632
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002633 if (validFirstPage) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002634 r = new Runnable() {
2635 public void run() {
2636 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2637 if (callbacks != null) {
2638 // We are loading synchronously, which means, some of the pages will be
2639 // bound after first draw. Inform the callbacks that page binding is
2640 // not complete, and schedule the remaining pages.
2641 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2642 callbacks.onPageBoundSynchronously(currentScreen);
2643 }
2644 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2645 }
2646 }
2647 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002648 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002649 }
Joe Onorato36115782010-06-17 13:28:48 -04002650 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002651
Joe Onorato36115782010-06-17 13:28:48 -04002652 private void loadAndBindAllApps() {
2653 if (DEBUG_LOADERS) {
2654 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2655 }
2656 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002657 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002658 synchronized (LoaderTask.this) {
2659 if (mStopped) {
2660 return;
2661 }
2662 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002663 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002664 synchronized (LoaderTask.this) {
2665 if (mStopped) {
2666 return;
2667 }
2668 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002669 }
Joe Onorato36115782010-06-17 13:28:48 -04002670 } else {
2671 onlyBindAllApps();
2672 }
2673 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002674
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002675 private void updateIconCache() {
2676 // Ignore packages which have a promise icon.
2677 HashSet<String> packagesToIgnore = new HashSet<>();
2678 synchronized (sBgLock) {
2679 for (ItemInfo info : sBgItemsIdMap) {
2680 if (info instanceof ShortcutInfo) {
2681 ShortcutInfo si = (ShortcutInfo) info;
2682 if (si.isPromise() && si.getTargetComponent() != null) {
2683 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2684 }
2685 } else if (info instanceof LauncherAppWidgetInfo) {
2686 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2687 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2688 packagesToIgnore.add(lawi.providerName.getPackageName());
2689 }
2690 }
2691 }
2692 }
2693 mIconCache.updateDbIcons(packagesToIgnore);
2694 }
2695
Joe Onorato36115782010-06-17 13:28:48 -04002696 private void onlyBindAllApps() {
2697 final Callbacks oldCallbacks = mCallbacks.get();
2698 if (oldCallbacks == null) {
2699 // This launcher has exited and nobody bothered to tell us. Just bail.
2700 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2701 return;
2702 }
2703
2704 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002705 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002706 final ArrayList<AppInfo> list
2707 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002708 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002709 public void run() {
2710 final long t = SystemClock.uptimeMillis();
2711 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2712 if (callbacks != null) {
2713 callbacks.bindAllApplications(list);
2714 }
2715 if (DEBUG_LOADERS) {
2716 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002717 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04002718 }
2719 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002720 };
Tony Wickham80f57872016-06-29 18:12:15 -07002721 runOnMainThread(r);
Joe Onorato36115782010-06-17 13:28:48 -04002722 }
2723
Winson Chung64359a52013-07-08 17:17:08 -07002724 private void loadAllApps() {
2725 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002726
Joe Onorato36115782010-06-17 13:28:48 -04002727 final Callbacks oldCallbacks = mCallbacks.get();
2728 if (oldCallbacks == null) {
2729 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002730 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002731 return;
2732 }
2733
Kenny Guyed131872014-04-30 03:02:21 +01002734 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2735
Winson Chung64359a52013-07-08 17:17:08 -07002736 // Clear the list of apps
2737 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002738 for (UserHandleCompat user : profiles) {
2739 // Query for the set of apps
2740 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002741 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002742 if (DEBUG_LOADERS) {
2743 Log.d(TAG, "getActivityList took "
2744 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2745 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2746 }
2747 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002748 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002749 if (apps == null || apps.isEmpty()) {
2750 return;
2751 }
Kenny Guyff05f432016-01-22 17:48:29 +00002752 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002753 // Create the ApplicationInfos
2754 for (int i = 0; i < apps.size(); i++) {
2755 LauncherActivityInfoCompat app = apps.get(i);
2756 // This builds the icon bitmaps.
Kenny Guyff05f432016-01-22 17:48:29 +00002757 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode));
Kenny Guyed131872014-04-30 03:02:21 +01002758 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002759
Sunny Goyal756a28a2015-04-23 17:07:55 -07002760 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2761 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002762 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002763
2764 @Override
2765 public void run() {
2766 heuristic.processUserApps(apps);
2767 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002768 };
2769 runOnMainThread(new Runnable() {
2770
2771 @Override
2772 public void run() {
2773 // Check isLoadingWorkspace on the UI thread, as it is updated on
2774 // the UI thread.
2775 if (mIsLoadingAndBindingWorkspace) {
2776 synchronized (mBindCompleteRunnables) {
2777 mBindCompleteRunnables.add(r);
2778 }
2779 } else {
2780 runOnWorkerThread(r);
2781 }
2782 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002783 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002784 }
Winson Chung64359a52013-07-08 17:17:08 -07002785 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002786 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002787 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2788 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002789
2790 // Post callback on main thread
2791 mHandler.post(new Runnable() {
2792 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002793
Winson Chung64359a52013-07-08 17:17:08 -07002794 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002795 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002796 if (callbacks != null) {
2797 callbacks.bindAllApplications(added);
2798 if (DEBUG_LOADERS) {
2799 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002800 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07002801 }
2802 } else {
2803 Log.i(TAG, "not binding apps: no Launcher activity");
2804 }
2805 }
2806 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002807 // Cleanup any data stored for a deleted user.
2808 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04002809 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002810 Log.d(TAG, "Icons processed in "
2811 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002812 }
2813 }
2814
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002815 private void loadAndBindDeepShortcuts() {
2816 if (DEBUG_LOADERS) {
2817 Log.d(TAG, "loadAndBindDeepShortcuts mDeepShortcutsLoaded=" + mDeepShortcutsLoaded);
2818 }
2819 if (!mDeepShortcutsLoaded) {
2820 mBgDeepShortcutMap.clear();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002821 mHasShortcutHostPermission = mDeepShortcutManager.hasHostPermission();
2822 if (mHasShortcutHostPermission) {
2823 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
2824 if (mUserManager.isUserUnlocked(user)) {
2825 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager
2826 .queryForAllShortcuts(user);
2827 updateDeepShortcutMap(null, user, shortcuts);
2828 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002829 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002830 }
2831 synchronized (LoaderTask.this) {
2832 if (mStopped) {
2833 return;
2834 }
2835 mDeepShortcutsLoaded = true;
2836 }
2837 }
Tony Wickham80f57872016-06-29 18:12:15 -07002838 bindDeepShortcuts();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002839 }
2840
Joe Onoratobe386092009-11-17 17:32:16 -08002841 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002842 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002843 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002844 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2845 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2846 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2847 }
Joe Onorato36115782010-06-17 13:28:48 -04002848 }
2849 }
2850
Sunny Goyal860538d2016-07-20 12:35:16 -07002851 /**
2852 * Clear all the shortcuts for the given package, and re-add the new shortcuts.
2853 */
2854 private void updateDeepShortcutMap(
2855 String packageName, UserHandleCompat user, List<ShortcutInfoCompat> shortcuts) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002856 if (packageName != null) {
2857 Iterator<ComponentKey> keysIter = mBgDeepShortcutMap.keySet().iterator();
2858 while (keysIter.hasNext()) {
Sunny Goyal860538d2016-07-20 12:35:16 -07002859 ComponentKey next = keysIter.next();
2860 if (next.componentName.getPackageName().equals(packageName)
2861 && next.user.equals(user)) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002862 keysIter.remove();
2863 }
2864 }
2865 }
2866
2867 // Now add the new shortcuts to the map.
2868 for (ShortcutInfoCompat shortcut : shortcuts) {
Tony Wickhamca258b32016-07-28 12:31:28 -07002869 boolean shouldShowInContainer = shortcut.isEnabled()
2870 && (shortcut.isDeclaredInManifest() || shortcut.isDynamic());
2871 if (shouldShowInContainer) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002872 ComponentKey targetComponent
2873 = new ComponentKey(shortcut.getActivity(), shortcut.getUserHandle());
2874 mBgDeepShortcutMap.addToList(targetComponent, shortcut.getId());
2875 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002876 }
2877 }
2878
Tony Wickham80f57872016-06-29 18:12:15 -07002879 public void bindDeepShortcuts() {
Sunny Goyal5f064012016-08-08 10:10:01 -07002880 final MultiHashMap<ComponentKey, String> shortcutMapCopy = mBgDeepShortcutMap.clone();
Tony Wickham80f57872016-06-29 18:12:15 -07002881 Runnable r = new Runnable() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002882 @Override
2883 public void run() {
2884 Callbacks callbacks = getCallback();
2885 if (callbacks != null) {
2886 callbacks.bindDeepShortcutMap(shortcutMapCopy);
2887 }
2888 }
Tony Wickham80f57872016-06-29 18:12:15 -07002889 };
2890 runOnMainThread(r);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002891 }
2892
Sunny Goyal75b0f552015-05-20 21:57:06 -07002893 /**
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002894 * Refreshes the cached shortcuts if the shortcut permission has changed.
2895 * Current implementation simply reloads the workspace, but it can be optimized to
2896 * use partial updates similar to {@link UserManagerCompat}
2897 */
2898 public void refreshShortcutsIfRequired() {
2899 if (Utilities.isNycMR1OrAbove()) {
2900 sWorker.removeCallbacks(mShortcutPermissionCheckRunnable);
2901 sWorker.post(mShortcutPermissionCheckRunnable);
2902 }
2903 }
2904
2905 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -07002906 * Called when the icons for packages have been updated in the icon cache.
2907 */
2908 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2909 final Callbacks callbacks = getCallback();
2910 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2911 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2912
2913 // If any package icon has changed (app was updated while launcher was dead),
2914 // update the corresponding shortcuts.
2915 synchronized (sBgLock) {
2916 for (ItemInfo info : sBgItemsIdMap) {
2917 if (info instanceof ShortcutInfo && user.equals(info.user)
2918 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2919 ShortcutInfo si = (ShortcutInfo) info;
2920 ComponentName cn = si.getTargetComponent();
2921 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2922 si.updateIcon(mIconCache);
2923 updatedShortcuts.add(si);
2924 }
2925 }
2926 }
2927 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2928 }
2929
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002930 bindUpdatedShortcuts(updatedShortcuts, user);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002931
2932 if (!updatedApps.isEmpty()) {
2933 mHandler.post(new Runnable() {
2934
2935 public void run() {
2936 Callbacks cb = getCallback();
2937 if (cb != null && callbacks == cb) {
2938 cb.bindAppsUpdated(updatedApps);
2939 }
2940 }
2941 });
2942 }
2943 }
2944
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002945 private void bindUpdatedShortcuts(
2946 ArrayList<ShortcutInfo> updatedShortcuts, UserHandleCompat user) {
2947 bindUpdatedShortcuts(updatedShortcuts, new ArrayList<ShortcutInfo>(), user);
2948 }
2949
2950 private void bindUpdatedShortcuts(
2951 final ArrayList<ShortcutInfo> updatedShortcuts,
2952 final ArrayList<ShortcutInfo> removedShortcuts,
2953 final UserHandleCompat user) {
2954 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002955 final Callbacks callbacks = getCallback();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002956 mHandler.post(new Runnable() {
2957
2958 public void run() {
2959 Callbacks cb = getCallback();
2960 if (cb != null && callbacks == cb) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002961 cb.bindShortcutsChanged(updatedShortcuts, removedShortcuts, user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002962 }
2963 }
2964 });
2965 }
2966 }
2967
2968 void enqueueItemUpdatedTask(Runnable task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002969 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002970 }
2971
Adam Cohen091440a2015-03-18 14:16:05 -07002972 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002973
2974 @Override
2975 public void onReceive(Context context, Intent intent) {
2976 synchronized (sBgLock) {
2977 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2978 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002979 final PackageManager manager = context.getPackageManager();
2980 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2981 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002982 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2983 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002984 packagesRemoved.clear();
2985 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002986 for (String pkg : entry.getValue()) {
2987 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07002988 if (PackageManagerHelper.isAppOnSdcard(manager, pkg)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002989 packagesUnavailable.add(pkg);
2990 } else {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002991 packagesRemoved.add(pkg);
2992 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002993 }
2994 }
2995 if (!packagesRemoved.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002996 enqueueItemUpdatedTask(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002997 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2998 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002999 if (!packagesUnavailable.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003000 enqueueItemUpdatedTask(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
Sunny Goyal1a745e82014-10-02 15:58:31 -07003001 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
3002 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003003 }
Sunny Goyal34942622014-08-29 17:20:55 -07003004 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003005 }
3006 }
3007 }
3008
Joe Onorato36115782010-06-17 13:28:48 -04003009 private class PackageUpdatedTask implements Runnable {
3010 int mOp;
3011 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01003012 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04003013
3014 public static final int OP_NONE = 0;
3015 public static final int OP_ADD = 1;
3016 public static final int OP_UPDATE = 2;
3017 public static final int OP_REMOVE = 3; // uninstlled
3018 public static final int OP_UNAVAILABLE = 4; // external media unmounted
Kenny Guy44cba692016-01-21 19:50:02 +00003019 public static final int OP_SUSPEND = 5; // package suspended
3020 public static final int OP_UNSUSPEND = 6; // package unsuspended
Sunny Goyalda891c12016-03-18 18:29:24 -07003021 public static final int OP_USER_AVAILABILITY_CHANGE = 7; // user available/unavailable
Joe Onorato36115782010-06-17 13:28:48 -04003022
Kenny Guyed131872014-04-30 03:02:21 +01003023 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04003024 mOp = op;
3025 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01003026 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04003027 }
3028
3029 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07003030 if (!mHasLoaderCompletedOnce) {
3031 // Loader has not yet run.
3032 return;
3033 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04003034 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04003035
3036 final String[] packages = mPackages;
3037 final int N = packages.length;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003038 FlagOp flagOp = FlagOp.NO_OP;
Sunny Goyalda891c12016-03-18 18:29:24 -07003039 StringFilter pkgFilter = StringFilter.of(new HashSet<>(Arrays.asList(packages)));
Joe Onorato36115782010-06-17 13:28:48 -04003040 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003041 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04003042 for (int i=0; i<N; i++) {
3043 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003044 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003045 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003046 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003047
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003048 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3049 if (heuristic != null) {
3050 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003051 }
Joe Onorato36115782010-06-17 13:28:48 -04003052 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003053 }
Joe Onorato36115782010-06-17 13:28:48 -04003054 case OP_UPDATE:
3055 for (int i=0; i<N; i++) {
3056 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003057 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003058 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003059 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003060 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003061 // Since package was just updated, the target must be available now.
3062 flagOp = FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04003063 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003064 case OP_REMOVE: {
3065 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3066 if (heuristic != null) {
3067 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003068 }
Joe Onorato36115782010-06-17 13:28:48 -04003069 for (int i=0; i<N; i++) {
3070 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003071 mIconCache.removeIconsForPkg(packages[i], mUser);
3072 }
3073 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003074 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003075 case OP_UNAVAILABLE:
3076 for (int i=0; i<N; i++) {
3077 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3078 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003079 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003080 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003081 flagOp = FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04003082 break;
Kenny Guy44cba692016-01-21 19:50:02 +00003083 case OP_SUSPEND:
3084 case OP_UNSUSPEND:
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003085 flagOp = mOp == OP_SUSPEND ?
3086 FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED) :
3087 FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED);
Sunny Goyal17763cb2016-03-24 13:53:22 -07003088 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.(un)suspend " + N);
Sunny Goyalda891c12016-03-18 18:29:24 -07003089 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
3090 break;
3091 case OP_USER_AVAILABILITY_CHANGE:
3092 flagOp = UserManagerCompat.getInstance(context).isQuietModeEnabled(mUser)
3093 ? FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER)
3094 : FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER);
3095 // We want to update all packages for this user.
3096 pkgFilter = StringFilter.matchesAll();
3097 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
Kenny Guy44cba692016-01-21 19:50:02 +00003098 break;
Joe Onorato36115782010-06-17 13:28:48 -04003099 }
3100
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003101 ArrayList<AppInfo> added = null;
3102 ArrayList<AppInfo> modified = null;
3103 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003104
Adam Cohen487f7dd2012-06-28 18:12:10 -07003105 if (mBgAllAppsList.added.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003106 added = new ArrayList<>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003107 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003108 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003109 if (mBgAllAppsList.modified.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003110 modified = new ArrayList<>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003111 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003112 }
Winson Chung5d55f332012-07-16 20:45:03 -07003113 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003114 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003115 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003116 }
3117
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003118 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps = new HashMap<>();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003119
Joe Onorato36115782010-06-17 13:28:48 -04003120 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003121 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003122 for (AppInfo ai : added) {
3123 addedOrUpdatedApps.put(ai.componentName, ai);
3124 }
Joe Onorato36115782010-06-17 13:28:48 -04003125 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003126
Joe Onorato36115782010-06-17 13:28:48 -04003127 if (modified != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003128 final Callbacks callbacks = getCallback();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003129 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003130 for (AppInfo ai : modified) {
3131 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003132 }
3133
Joe Onorato36115782010-06-17 13:28:48 -04003134 mHandler.post(new Runnable() {
3135 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003136 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003137 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003138 callbacks.bindAppsUpdated(modifiedFinal);
3139 }
3140 }
3141 });
3142 }
Winson Chung83892cc2013-05-01 16:53:33 -07003143
Sunny Goyal4390ace2014-10-13 11:33:11 -07003144 // Update shortcut infos
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003145 if (mOp == OP_ADD || flagOp != FlagOp.NO_OP) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003146 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3147 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3148 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3149
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003150 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003151 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003152 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3153 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003154 boolean infoUpdated = false;
3155 boolean shortcutUpdated = false;
3156
3157 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003158 if ((si.iconResource != null)
Sunny Goyalda891c12016-03-18 18:29:24 -07003159 && pkgFilter.matches(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003160 Bitmap icon = Utilities.createIconBitmap(
3161 si.iconResource.packageName,
3162 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003163 if (icon != null) {
3164 si.setIcon(icon);
3165 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003166 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003167 }
3168 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003169
3170 ComponentName cn = si.getTargetComponent();
Sunny Goyalda891c12016-03-18 18:29:24 -07003171 if (cn != null && pkgFilter.matches(cn.getPackageName())) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003172 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3173
3174 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003175 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3176 // Auto install icon
3177 PackageManager pm = context.getPackageManager();
3178 ResolveInfo matched = pm.resolveActivity(
3179 new Intent(Intent.ACTION_MAIN)
3180 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3181 PackageManager.MATCH_DEFAULT_ONLY);
3182 if (matched == null) {
3183 // Try to find the best match activity.
3184 Intent intent = pm.getLaunchIntentForPackage(
3185 cn.getPackageName());
3186 if (intent != null) {
3187 cn = intent.getComponent();
3188 appInfo = addedOrUpdatedApps.get(cn);
3189 }
3190
3191 if ((intent == null) || (appInfo == null)) {
3192 removedShortcuts.add(si);
3193 continue;
3194 }
3195 si.promisedIntent = intent;
3196 }
3197 }
3198
3199 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003200 if (appInfo != null) {
3201 si.flags = appInfo.flags;
3202 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003203
Sunny Goyal756adbc2015-04-16 15:20:51 -07003204 si.intent = si.promisedIntent;
3205 si.promisedIntent = null;
3206 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003207 infoUpdated = true;
3208 si.updateIcon(mIconCache);
3209 }
3210
3211 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3212 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3213 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003214 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003215 si.contentDescription = appInfo.contentDescription;
3216 infoUpdated = true;
3217 }
3218
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003219 int oldDisabledFlags = si.isDisabled;
3220 si.isDisabled = flagOp.apply(si.isDisabled);
3221 if (si.isDisabled != oldDisabledFlags) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003222 shortcutUpdated = true;
3223 }
3224 }
3225
3226 if (infoUpdated || shortcutUpdated) {
3227 updatedShortcuts.add(si);
3228 }
3229 if (infoUpdated) {
3230 updateItemInDatabase(context, si);
3231 }
Sunny Goyalda891c12016-03-18 18:29:24 -07003232 } else if (info instanceof LauncherAppWidgetInfo && mOp == OP_ADD) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003233 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3234 if (mUser.equals(widgetInfo.user)
3235 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
Sunny Goyalda891c12016-03-18 18:29:24 -07003236 && pkgFilter.matches(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003237 widgetInfo.restoreStatus &=
3238 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3239 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003240
3241 // adding this flag ensures that launcher shows 'click to setup'
3242 // if the widget has a config activity. In case there is no config
3243 // activity, it will be marked as 'restored' during bind.
3244 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3245
Sunny Goyal4390ace2014-10-13 11:33:11 -07003246 widgets.add(widgetInfo);
3247 updateItemInDatabase(context, widgetInfo);
3248 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003249 }
3250 }
3251 }
3252
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003253 bindUpdatedShortcuts(updatedShortcuts, removedShortcuts, mUser);
3254 if (!removedShortcuts.isEmpty()) {
3255 deleteItemsFromDatabase(context, removedShortcuts);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003256 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003257
Sunny Goyal4390ace2014-10-13 11:33:11 -07003258 if (!widgets.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003259 final Callbacks callbacks = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003260 mHandler.post(new Runnable() {
3261 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003262 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003263 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003264 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003265 }
3266 }
3267 });
3268 }
3269 }
3270
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003271 final HashSet<String> removedPackages = new HashSet<>();
3272 final HashSet<ComponentName> removedComponents = new HashSet<>();
3273 if (mOp == OP_REMOVE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003274 // Mark all packages in the broadcast to be removed
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003275 Collections.addAll(removedPackages, packages);
3276
3277 // No need to update the removedComponents as
3278 // removedPackages is a super-set of removedComponents
Winson Chungdf95eb12013-10-16 14:57:07 -07003279 } else if (mOp == OP_UPDATE) {
3280 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003281 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003282 if (isPackageDisabled(context, packages[i], mUser)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003283 removedPackages.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003284 }
3285 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003286
3287 // Update removedComponents as some components can get removed during package update
3288 for (AppInfo info : removedApps) {
3289 removedComponents.add(info.componentName);
3290 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003291 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003292
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003293 if (!removedPackages.isEmpty() || !removedComponents.isEmpty()) {
3294 for (String pn : removedPackages) {
3295 deletePackageFromDatabase(context, pn, mUser);
3296 }
3297 for (ComponentName cn : removedComponents) {
3298 deleteItemsFromDatabase(context, getItemInfoForComponentName(cn, mUser));
Sunny Goyal1a745e82014-10-02 15:58:31 -07003299 }
3300
Winson Chungdf95eb12013-10-16 14:57:07 -07003301 // Remove any queued items from the install queue
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003302 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackages, mUser);
3303
Winson Chungdf95eb12013-10-16 14:57:07 -07003304 // Call the components-removed callback
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003305 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003306 mHandler.post(new Runnable() {
3307 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003308 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003309 if (callbacks == cb && cb != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003310 callbacks.bindWorkspaceComponentsRemoved(
3311 removedPackages, removedComponents, mUser);
3312 }
3313 }
3314 });
3315 }
3316
3317 if (!removedApps.isEmpty()) {
3318 // Remove corresponding apps from All-Apps
3319 final Callbacks callbacks = getCallback();
3320 mHandler.post(new Runnable() {
3321 public void run() {
3322 Callbacks cb = getCallback();
3323 if (callbacks == cb && cb != null) {
3324 callbacks.bindAppInfosRemoved(removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04003325 }
3326 }
3327 });
Joe Onoratobe386092009-11-17 17:32:16 -08003328 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003329
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003330 // Notify launcher of widget update. From marshmallow onwards we use AppWidgetHost to
3331 // get widget update signals.
3332 if (!Utilities.ATLEAST_MARSHMALLOW &&
3333 (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003334 final Callbacks callbacks = getCallback();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003335 mHandler.post(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003336 public void run() {
3337 Callbacks cb = getCallback();
3338 if (callbacks == cb && cb != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003339 callbacks.notifyWidgetProvidersChanged();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003340 }
3341 }
3342 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003343 }
3344 }
3345 }
3346
Sunny Goyal10923b32016-07-20 15:42:44 -07003347 /**
3348 * Repopulates the shortcut info, possibly updating any icon already on the workspace.
3349 */
3350 public void updateShortcutInfo(final ShortcutInfoCompat fullDetail, final ShortcutInfo info) {
3351 enqueueItemUpdatedTask(new Runnable() {
3352 @Override
3353 public void run() {
3354 info.updateFromDeepShortcutInfo(
3355 fullDetail, LauncherAppState.getInstance().getContext());
3356 ArrayList<ShortcutInfo> update = new ArrayList<ShortcutInfo>();
3357 update.add(info);
3358 bindUpdatedShortcuts(update, fullDetail.getUserHandle());
3359 }
3360 });
3361 }
3362
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003363 private class ShortcutsChangedTask implements Runnable {
Sunny Goyal50941fb2016-08-04 12:03:52 -07003364 private final String mPackageName;
3365 private final List<ShortcutInfoCompat> mShortcuts;
3366 private final UserHandleCompat mUser;
3367 private final boolean mUpdateIdMap;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003368
3369 public ShortcutsChangedTask(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal50941fb2016-08-04 12:03:52 -07003370 UserHandleCompat user, boolean updateIdMap) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003371 mPackageName = packageName;
3372 mShortcuts = shortcuts;
3373 mUser = user;
Sunny Goyal50941fb2016-08-04 12:03:52 -07003374 mUpdateIdMap = updateIdMap;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003375 }
3376
3377 @Override
3378 public void run() {
3379 mDeepShortcutManager.onShortcutsChanged(mShortcuts);
3380
3381 Map<String, ShortcutInfoCompat> idsToShortcuts = new HashMap<>();
3382 for (ShortcutInfoCompat shortcut : mShortcuts) {
3383 idsToShortcuts.put(shortcut.getId(), shortcut);
3384 }
3385
3386 // Find ShortcutInfo's that have changed on the workspace.
3387 MultiHashMap<String, ShortcutInfo> idsToWorkspaceShortcutInfos = new MultiHashMap<>();
3388 for (ItemInfo itemInfo : sBgItemsIdMap) {
3389 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
3390 ShortcutInfo si = (ShortcutInfo) itemInfo;
Tony Wickham672d07f2016-07-27 18:22:41 -07003391 if (si.getIntent().getPackage().equals(mPackageName) && si.user.equals(mUser)) {
3392 String shortcutId = si.getDeepShortcutId();
3393 if (idsToShortcuts.containsKey(shortcutId)) {
3394 idsToWorkspaceShortcutInfos.addToList(shortcutId, si);
3395 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003396 }
3397 }
3398 }
3399
3400 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3401 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager.queryForFullDetails(
3402 mPackageName, new ArrayList<>(idsToWorkspaceShortcutInfos.keySet()), mUser);
3403 ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3404 Context context = LauncherAppState.getInstance().getContext();
3405 for (ShortcutInfoCompat fullDetails : shortcuts) {
3406 List<ShortcutInfo> shortcutInfos = idsToWorkspaceShortcutInfos
3407 .get(fullDetails.getId());
3408 for (ShortcutInfo shortcutInfo : shortcutInfos) {
Sunny Goyal9994b2b2016-06-23 14:17:24 -07003409 shortcutInfo.updateFromDeepShortcutInfo(fullDetails, context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003410 updatedShortcutInfos.add(shortcutInfo);
3411 }
3412 }
3413 bindUpdatedShortcuts(updatedShortcutInfos, mUser);
3414
Sunny Goyal50941fb2016-08-04 12:03:52 -07003415 if (mUpdateIdMap) {
3416 // Update the deep shortcut map if the list of ids has changed for an activity.
3417 updateDeepShortcutMap(mPackageName, mUser, mShortcuts);
3418 bindDeepShortcuts();
3419 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003420 }
3421 }
3422
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003423 /**
3424 * Task to handle changing of lock state of the user
3425 */
3426 private class UserLockStateChangedTask implements Runnable {
3427
3428 private final UserHandleCompat mUser;
3429
3430 public UserLockStateChangedTask(UserHandleCompat user) {
3431 mUser = user;
3432 }
3433
3434 @Override
3435 public void run() {
3436 boolean isUserUnlocked = mUserManager.isUserUnlocked(mUser);
3437 Context context = mApp.getContext();
3438
3439 HashMap<ShortcutKey, ShortcutInfoCompat> pinnedShortcuts = new HashMap<>();
3440 if (isUserUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07003441 List<ShortcutInfoCompat> shortcuts =
3442 mDeepShortcutManager.queryForPinnedShortcuts(null, mUser);
3443 if (mDeepShortcutManager.wasLastCallSuccess()) {
3444 for (ShortcutInfoCompat shortcut : shortcuts) {
3445 pinnedShortcuts.put(ShortcutKey.fromInfo(shortcut), shortcut);
3446 }
3447 } else {
3448 // Shortcut manager can fail due to some race condition when the lock state
3449 // changes too frequently. For the purpose of the update,
3450 // consider it as still locked.
3451 isUserUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003452 }
3453 }
3454
3455 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3456 ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3457 ArrayList<ShortcutInfo> deletedShortcutInfos = new ArrayList<>();
3458 for (ItemInfo itemInfo : sBgItemsIdMap) {
3459 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT
3460 && mUser.equals(itemInfo.user)) {
3461 ShortcutInfo si = (ShortcutInfo) itemInfo;
3462 if (isUserUnlocked) {
3463 ShortcutInfoCompat shortcut =
3464 pinnedShortcuts.get(ShortcutKey.fromItemInfo(si));
3465 // We couldn't verify the shortcut during loader. If its no longer available
3466 // (probably due to clear data), delete the workspace item as well
3467 if (shortcut == null) {
3468 deletedShortcutInfos.add(si);
3469 continue;
3470 }
3471 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
3472 si.updateFromDeepShortcutInfo(shortcut, context);
3473 } else {
3474 si.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
3475 }
3476 updatedShortcutInfos.add(si);
3477 }
3478 }
3479 bindUpdatedShortcuts(updatedShortcutInfos, deletedShortcutInfos, mUser);
3480 if (!deletedShortcutInfos.isEmpty()) {
3481 deleteItemsFromDatabase(context, deletedShortcutInfos);
3482 }
3483
3484 // Remove shortcut id map for that user
3485 Iterator<ComponentKey> keysIter = mBgDeepShortcutMap.keySet().iterator();
3486 while (keysIter.hasNext()) {
3487 if (keysIter.next().user.equals(mUser)) {
3488 keysIter.remove();
3489 }
3490 }
3491
3492 if (isUserUnlocked) {
3493 updateDeepShortcutMap(null, mUser, mDeepShortcutManager.queryForAllShortcuts(mUser));
3494 }
3495 bindDeepShortcuts();
3496 }
3497 }
3498
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003499 private void bindWidgetsModel(final Callbacks callbacks, final WidgetsModel model) {
3500 mHandler.post(new Runnable() {
3501 @Override
3502 public void run() {
3503 Callbacks cb = getCallback();
3504 if (callbacks == cb && cb != null) {
3505 callbacks.bindWidgetsModel(model);
3506 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003507 }
3508 });
3509 }
3510
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003511 public void refreshAndBindWidgetsAndShortcuts(
3512 final Callbacks callbacks, final boolean bindFirst) {
3513 runOnWorkerThread(new Runnable() {
3514 @Override
3515 public void run() {
3516 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
3517 bindWidgetsModel(callbacks, mBgWidgetsModel.clone());
Sunny Goyal31860582015-09-18 08:38:57 -07003518 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003519 final WidgetsModel model = mBgWidgetsModel.updateAndClone(mApp.getContext());
3520 bindWidgetsModel(callbacks, model);
3521 // update the Widget entries inside DB on the worker thread.
3522 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3523 model.getRawList());
Sunny Goyal31860582015-09-18 08:38:57 -07003524 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003525 });
Michael Jurkac402cd92013-05-20 15:49:32 +02003526 }
3527
Adam Cohen091440a2015-03-18 14:16:05 -07003528 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003529 UserHandleCompat user) {
3530 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3531 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003532 }
Adam Cohen556f6132014-01-15 15:18:08 -08003533
Kenny Guyed131872014-04-30 03:02:21 +01003534 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3535 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003536 if (cn == null) {
3537 return false;
3538 }
Kenny Guyed131872014-04-30 03:02:21 +01003539 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3540 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003541 return false;
3542 }
Kenny Guyed131872014-04-30 03:02:21 +01003543 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003544 }
3545
Adam Cohena28b78e2014-05-20 17:03:04 -07003546 public static boolean isValidPackage(Context context, String packageName,
3547 UserHandleCompat user) {
3548 if (packageName == null) {
3549 return false;
3550 }
3551 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3552 return launcherApps.isPackageEnabledForProfile(packageName, user);
3553 }
3554
Joe Onorato9c1289c2009-08-17 11:03:03 -04003555 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003556 * Make an ShortcutInfo object for a restored application or shortcut item that points
3557 * to a package that is not yet installed on the system.
3558 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003559 public ShortcutInfo getRestoredItemInfo(Cursor c, Intent intent,
3560 int promiseType, int itemType, CursorIconInfo iconInfo) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003561 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003562 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003563
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003564 Bitmap icon = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003565 // the fallback icon
3566 if (icon == null) {
3567 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3568 } else {
3569 info.setIcon(icon);
3570 }
Sunny Goyal34942622014-08-29 17:20:55 -07003571
3572 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003573 String title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07003574 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003575 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003576 }
Sunny Goyal34942622014-08-29 17:20:55 -07003577 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3578 if (TextUtils.isEmpty(info.title)) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003579 info.title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07003580 }
Sunny Goyal34942622014-08-29 17:20:55 -07003581 } else {
3582 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3583 }
3584
Winson Chung82b016c2015-05-08 17:00:10 -07003585 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003586 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003587 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003588 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003589 return info;
3590 }
3591
3592 /**
3593 * Make an Intent object for a restored application or shortcut item that points
3594 * to the market page for the item.
3595 */
Adam Cohen091440a2015-03-18 14:16:05 -07003596 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003597 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003598 return getMarketIntent(componentName.getPackageName());
3599 }
3600
3601 static Intent getMarketIntent(String packageName) {
3602 return new Intent(Intent.ACTION_VIEW)
3603 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003604 .scheme("market")
3605 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003606 .appendQueryParameter("id", packageName)
3607 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003608 }
3609
3610 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003611 * Make an ShortcutInfo object for a shortcut that is an application.
3612 *
3613 * If c is not null, then it will be used to fill in missing data like the title and icon.
3614 */
Sunny Goyald09c3702016-04-06 16:18:20 -07003615 public ShortcutInfo getAppShortcutInfo(Intent intent,
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003616 UserHandleCompat user, Cursor c, CursorIconInfo iconInfo,
Sunny Goyal34b65272015-03-11 16:56:52 -07003617 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003618 if (user == null) {
3619 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003620 return null;
3621 }
3622
Kenny Guyed131872014-04-30 03:02:21 +01003623 ComponentName componentName = intent.getComponent();
3624 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08003625 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01003626 return null;
3627 }
3628
3629 Intent newIntent = new Intent(intent.getAction(), null);
3630 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3631 newIntent.setComponent(componentName);
3632 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003633 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003634 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3635 return null;
3636 }
3637
3638 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003639 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003640 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003641 Bitmap icon = iconInfo.loadIcon(c);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003642 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003643 }
3644
Sunny Goyald09c3702016-04-06 16:18:20 -07003645 if (lai != null && PackageManagerHelper.isAppSuspended(lai.getApplicationInfo())) {
3646 info.isDisabled = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
3647 }
3648
Joe Onorato56d82912010-03-07 14:32:10 -05003649 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003650 if (TextUtils.isEmpty(info.title) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003651 info.title = iconInfo.getTitle(c);
Joe Onorato56d82912010-03-07 14:32:10 -05003652 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003653
Joe Onorato56d82912010-03-07 14:32:10 -05003654 // fall back to the class name of the activity
3655 if (info.title == null) {
3656 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003657 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003658
Joe Onorato9c1289c2009-08-17 11:03:03 -04003659 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003660 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003661 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003662 if (lai != null) {
3663 info.flags = AppInfo.initFlags(lai);
3664 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003665 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003666 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003667
Sunny Goyale2df0622015-04-24 11:27:00 -07003668 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003669 ItemInfoFilter f) {
3670 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3671 for (ItemInfo i : infos) {
3672 if (i instanceof ShortcutInfo) {
3673 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003674 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003675 if (cn != null && f.filterItem(null, info, cn)) {
3676 filtered.add(info);
3677 }
3678 } else if (i instanceof FolderInfo) {
3679 FolderInfo info = (FolderInfo) i;
3680 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003681 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003682 if (cn != null && f.filterItem(info, s, cn)) {
3683 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003684 }
3685 }
Winson Chung64359a52013-07-08 17:17:08 -07003686 } else if (i instanceof LauncherAppWidgetInfo) {
3687 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3688 ComponentName cn = info.providerName;
3689 if (cn != null && f.filterItem(null, info, cn)) {
3690 filtered.add(info);
3691 }
Winson Chung8a435102012-08-30 17:16:53 -07003692 }
3693 }
Winson Chung64359a52013-07-08 17:17:08 -07003694 return new ArrayList<ItemInfo>(filtered);
3695 }
3696
Adam Cohen091440a2015-03-18 14:16:05 -07003697 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003698 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003699 ItemInfoFilter filter = new ItemInfoFilter() {
3700 @Override
3701 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003702 if (info.user == null) {
3703 return cn.equals(cname);
3704 } else {
3705 return cn.equals(cname) && info.user.equals(user);
3706 }
Winson Chung64359a52013-07-08 17:17:08 -07003707 }
3708 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003709 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003710 }
3711
Sunny Goyal1a745e82014-10-02 15:58:31 -07003712 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003713 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003714 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003715 @Thunk ShortcutInfo getShortcutInfo(Cursor c, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003716 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003717 // Non-app shortcuts are only supported for current user.
3718 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003719 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003720
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003721 // TODO: If there's an explicit component and we can't install that, delete it.
3722
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003723 loadInfoFromCursor(info, c, iconInfo);
3724 return info;
3725 }
Joe Onorato56d82912010-03-07 14:32:10 -05003726
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003727 /**
3728 * Make an ShortcutInfo object for a shortcut that isn't an application.
3729 */
3730 public void loadInfoFromCursor(ShortcutInfo info, Cursor c, CursorIconInfo iconInfo) {
3731 info.title = iconInfo.getTitle(c);
3732 Bitmap icon = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003733 // the fallback icon
3734 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003735 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003736 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003737 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003738 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003739 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003740
Sunny Goyal2350bc92014-10-14 16:42:54 -07003741 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003742 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3743 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3744 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3745
Adam Cohend9198822011-11-22 16:42:47 -08003746 if (intent == null) {
3747 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3748 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3749 return null;
3750 }
3751
Joe Onorato0589f0f2010-02-08 13:44:00 -08003752 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003753 boolean customIcon = false;
3754 ShortcutIconResource iconResource = null;
3755
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003756 if (bitmap instanceof Bitmap) {
3757 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003758 customIcon = true;
3759 } else {
3760 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003761 if (extra instanceof ShortcutIconResource) {
3762 iconResource = (ShortcutIconResource) extra;
3763 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003764 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003765 }
3766 }
3767
Michael Jurkac9d95c52011-08-29 14:03:34 -07003768 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003769
Kenny Guyed131872014-04-30 03:02:21 +01003770 // Only support intents for current user for now. Intents sent from other
3771 // users wouldn't get here without intent forwarding anyway.
3772 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003773 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003774 icon = mIconCache.getDefaultIcon(info.user);
3775 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003776 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003777 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003778
Winson Chung82b016c2015-05-08 17:00:10 -07003779 info.title = Utilities.trim(name);
3780 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003781 info.intent = intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003782 info.iconResource = iconResource;
3783
3784 return info;
3785 }
3786
Joe Onorato9c1289c2009-08-17 11:03:03 -04003787 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003788 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003789 * or make a new one.
3790 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003791 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003792 // See if a placeholder was created for us already
3793 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003794 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003795 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003796 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003797 folders.put(id, folderInfo);
3798 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003799 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003800 }
3801
Joe Onoratobe386092009-11-17 17:32:16 -08003802
Sunny Goyal651077b2014-06-30 14:15:31 -07003803 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3804 return (provider != null) && (provider.provider != null)
3805 && (provider.provider.getPackageName() != null);
3806 }
3807
Joe Onoratobe386092009-11-17 17:32:16 -08003808 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003809 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003810 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3811 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3812 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3813 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003814 if (mLoaderTask != null) {
3815 mLoaderTask.dumpState();
3816 } else {
3817 Log.d(TAG, "mLoaderTask=null");
3818 }
Joe Onoratobe386092009-11-17 17:32:16 -08003819 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003820
3821 public Callbacks getCallback() {
3822 return mCallbacks != null ? mCallbacks.get() : null;
3823 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003824
3825 /**
3826 * @return {@link FolderInfo} if its already loaded.
3827 */
3828 public FolderInfo findFolderById(Long folderId) {
3829 synchronized (sBgLock) {
3830 return sBgFolders.get(folderId);
3831 }
3832 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003833
Sunny Goyal527c7d32015-08-28 15:19:36 -07003834 @Thunk class DeferredMainThreadExecutor implements Executor {
3835
3836 @Override
3837 public void execute(Runnable command) {
3838 runOnMainThread(command);
3839 }
3840 }
3841
Sunny Goyal756adbc2015-04-16 15:20:51 -07003842 /**
3843 * @return the looper for the worker thread which can be used to start background tasks.
3844 */
3845 public static Looper getWorkerLooper() {
3846 return sWorkerThread.getLooper();
3847 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003848}