blob: ddd60c89c10f35da49b4be5e6d44a6ed4838bb18 [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;
Sunny Goyal94485362014-09-18 16:13:58 -0700106 private static final boolean REMOVE_UNRESTORED_ICONS = true;
Chris Wrenb358f812014-04-16 13:37:00 -0400107
Joe Onorato9c1289c2009-08-17 11:03:03 -0400108 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700109
Joe Onorato36115782010-06-17 13:28:48 -0400110 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500111 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800112
Adam Cohen091440a2015-03-18 14:16:05 -0700113 @Thunk final LauncherAppState mApp;
114 @Thunk final Object mLock = new Object();
115 @Thunk DeferredHandler mHandler = new DeferredHandler();
116 @Thunk LoaderTask mLoaderTask;
117 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700118 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119
Adam Cohen091440a2015-03-18 14:16:05 -0700120 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700121 static {
122 sWorkerThread.start();
123 }
Adam Cohen091440a2015-03-18 14:16:05 -0700124 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700125
Joe Onoratocc67f472010-06-08 10:54:30 -0700126 // We start off with everything not loaded. After that, we assume that
127 // our monitoring of the package manager provides all updates and we never
128 // need to do a requery. These are only ever touched from the loader thread.
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700129 private boolean mWorkspaceLoaded;
130 private boolean mAllAppsLoaded;
131 private boolean mDeepShortcutsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700132
Sunny Goyal756a28a2015-04-23 17:07:55 -0700133 /**
134 * Set of runnables to be called on the background thread after the workspace binding
135 * is complete.
136 */
137 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
138
Adam Cohen091440a2015-03-18 14:16:05 -0700139 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700141 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800142 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700143 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800144 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800145
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700146 // Maps all launcher activities to the id's of their shortcuts (if they have any).
147 private final MultiHashMap<ComponentKey, String> mBgDeepShortcutMap = new MultiHashMap<>();
148
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700149 // The lock that must be acquired before referencing any static bg data structures. Unlike
150 // other locks, this one can generally be held long-term because we never expect any of these
151 // static data structures to be referenced outside of the worker thread except on the first
152 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700153 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700154
Adam Cohen487f7dd2012-06-28 18:12:10 -0700155 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700156 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700157 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700158
Adam Cohen487f7dd2012-06-28 18:12:10 -0700159 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
160 // created by LauncherModel that are directly on the home screen (however, no widgets or
161 // shortcuts within folders).
162 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700163
Adam Cohen487f7dd2012-06-28 18:12:10 -0700164 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
165 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700166 new ArrayList<LauncherAppWidgetInfo>();
167
Adam Cohen487f7dd2012-06-28 18:12:10 -0700168 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700169 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700170
Adam Cohendcd297f2013-06-18 13:13:40 -0700171 // sBgWorkspaceScreens is the ordered set of workspace screens.
172 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
173
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700174 // sBgPinnedShortcutCounts is the ComponentKey representing a pinned shortcut to the number of
175 // times it is pinned.
176 static final Map<ShortcutKey, MutableInt> sBgPinnedShortcutCounts = new HashMap<>();
177
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700178 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700179 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
180 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700181
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700182 // </ only access in worker thread >
183
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700184 private IconCache mIconCache;
185 private DeepShortcutManager mDeepShortcutManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700187 private final LauncherAppsCompat mLauncherApps;
188 private final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100189
Joe Onorato9c1289c2009-08-17 11:03:03 -0400190 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700191 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400192 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700193 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400194 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700195 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
196 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700197 public void bindScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800198 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400199 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200200 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700201 public void bindAppsAdded(ArrayList<Long> newScreens,
202 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700203 ArrayList<ItemInfo> addAnimated,
204 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200205 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700206 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
207 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
208 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700209 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700210 public void bindWorkspaceComponentsRemoved(
211 HashSet<String> packageNames, HashSet<ComponentName> components,
212 UserHandleCompat user);
213 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800214 public void notifyWidgetProvidersChanged();
215 public void bindWidgetsModel(WidgetsModel model);
Adam Cohen1462de32012-07-24 22:34:36 -0700216 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700217 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700218 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMap);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400219 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800220
Winson Chung64359a52013-07-08 17:17:08 -0700221 public interface ItemInfoFilter {
222 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
223 }
224
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700225 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter,
226 DeepShortcutManager deepShortcutManager) {
Winson Chunga6945242014-01-08 14:04:34 -0800227 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400228 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100229 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700230 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800231 mIconCache = iconCache;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700232 mDeepShortcutManager = deepShortcutManager;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800233
Kenny Guyed131872014-04-30 03:02:21 +0100234 mLauncherApps = LauncherAppsCompat.getInstance(context);
235 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800236 }
237
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700238 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
239 * posted on the main thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700240 private void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700241 if (sWorkerThread.getThreadId() == Process.myTid()) {
242 // If we are on the worker thread, post onto the main handler
243 mHandler.post(r);
244 } else {
245 r.run();
246 }
247 }
248
249 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
250 * posted on the worker thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700251 private static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700252 if (sWorkerThread.getThreadId() == Process.myTid()) {
253 r.run();
254 } else {
255 // If we are not on the worker thread, then post to the worker handler
256 sWorker.post(r);
257 }
258 }
259
Sunny Goyal756adbc2015-04-16 15:20:51 -0700260 public void setPackageState(final PackageInstallInfo installInfo) {
261 Runnable updateRunnable = new Runnable() {
262
263 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500264 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700265 synchronized (sBgLock) {
266 final HashSet<ItemInfo> updates = new HashSet<>();
267
268 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
269 // Ignore install success events as they are handled by Package add events.
270 return;
271 }
272
Sunny Goyale2df0622015-04-24 11:27:00 -0700273 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700274 if (info instanceof ShortcutInfo) {
275 ShortcutInfo si = (ShortcutInfo) info;
276 ComponentName cn = si.getTargetComponent();
277 if (si.isPromise() && (cn != null)
278 && installInfo.packageName.equals(cn.getPackageName())) {
279 si.setInstallProgress(installInfo.progress);
280
281 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
282 // Mark this info as broken.
283 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
284 }
285 updates.add(si);
286 }
287 }
288 }
289
290 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
291 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
292 widget.installProgress = installInfo.progress;
293 updates.add(widget);
294 }
295 }
296
297 if (!updates.isEmpty()) {
298 // Push changes to the callback.
299 Runnable r = new Runnable() {
300 public void run() {
301 Callbacks callbacks = getCallback();
302 if (callbacks != null) {
303 callbacks.bindRestoreItemsChange(updates);
304 }
305 }
306 };
307 mHandler.post(r);
308 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500309 }
310 }
311 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700312 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500313 }
314
Sunny Goyal756adbc2015-04-16 15:20:51 -0700315 /**
316 * Updates the icons and label of all pending icons for the provided package name.
317 */
318 public void updateSessionDisplayInfo(final String packageName) {
319 Runnable updateRunnable = new Runnable() {
320
321 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700322 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700323 synchronized (sBgLock) {
324 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
325 final UserHandleCompat user = UserHandleCompat.myUserHandle();
326
Sunny Goyale2df0622015-04-24 11:27:00 -0700327 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700328 if (info instanceof ShortcutInfo) {
329 ShortcutInfo si = (ShortcutInfo) info;
330 ComponentName cn = si.getTargetComponent();
331 if (si.isPromise() && (cn != null)
332 && packageName.equals(cn.getPackageName())) {
333 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
334 // For auto install apps update the icon as well as label.
335 mIconCache.getTitleAndIcon(si,
336 si.promisedIntent, user,
337 si.shouldUseLowResIcon());
338 } else {
339 // Only update the icon for restored apps.
340 si.updateIcon(mIconCache);
341 }
342 updates.add(si);
343 }
344 }
345 }
346
347 if (!updates.isEmpty()) {
348 // Push changes to the callback.
349 Runnable r = new Runnable() {
350 public void run() {
351 Callbacks callbacks = getCallback();
352 if (callbacks != null) {
353 callbacks.bindShortcutsChanged(updates,
354 new ArrayList<ShortcutInfo>(), user);
355 }
356 }
357 };
358 mHandler.post(r);
359 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700360 }
361 }
362 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700363 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700364 }
365
Adam Cohen76a47a12014-02-05 11:47:43 -0800366 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800367 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800368
369 if (allAppsApps == null) {
370 throw new RuntimeException("allAppsApps must not be null");
371 }
372 if (allAppsApps.isEmpty()) {
373 return;
374 }
375
376 // Process the newly added applications and add them to the database first
377 Runnable r = new Runnable() {
378 public void run() {
379 runOnMainThread(new Runnable() {
380 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800381 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800382 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500383 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800384 }
385 }
386 });
387 }
388 };
389 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700390 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800391
Sunny Goyala9116722015-04-29 13:55:58 -0700392 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800393 int[] xy, int spanX, int spanY) {
394 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700395 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700396
397 GridOccupancy occupied = new GridOccupancy(profile.numColumns, profile.numRows);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800398 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700399 for (ItemInfo r : occupiedPos) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700400 occupied.markCells(r, true);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800401 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800402 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700403 return occupied.findVacantCell(xy, spanX, spanY);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800404 }
405
406 /**
407 * Find a position on the screen for the given size or adds a new screen.
408 * @return screenId and the coordinates for the item.
409 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700410 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyalc3642d42015-10-30 10:27:08 -0700411 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800412 ArrayList<Long> workspaceScreens,
413 ArrayList<Long> addedWorkspaceScreensFinal,
414 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700415 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800416
Sunny Goyala9116722015-04-29 13:55:58 -0700417 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700418 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700419 synchronized (sBgLock) {
420 for (ItemInfo info : sBgItemsIdMap) {
421 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
422 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
423 if (items == null) {
424 items = new ArrayList<>();
425 screenItems.put(info.screenId, items);
426 }
427 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800428 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800429 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800430 }
431
432 // Find appropriate space for the item.
433 long screenId = 0;
434 int[] cordinates = new int[2];
435 boolean found = false;
436
437 int screenCount = workspaceScreens.size();
438 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700439 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800440 if (preferredScreenIndex < screenCount) {
441 screenId = workspaceScreens.get(preferredScreenIndex);
442 found = findNextAvailableIconSpaceInScreen(
443 screenItems.get(screenId), cordinates, spanX, spanY);
444 }
445
446 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700447 // Search on any of the screens starting from the first screen.
448 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800449 screenId = workspaceScreens.get(screen);
450 if (findNextAvailableIconSpaceInScreen(
451 screenItems.get(screenId), cordinates, spanX, spanY)) {
452 // We found a space for it
453 found = true;
454 break;
455 }
456 }
457 }
458
459 if (!found) {
460 // Still no position found. Add a new screen to the end.
Sunny Goyald2497482015-09-22 18:24:19 -0700461 screenId = LauncherSettings.Settings.call(context.getContentResolver(),
462 LauncherSettings.Settings.METHOD_NEW_SCREEN_ID)
463 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800464
465 // Save the screen id for binding in the workspace
466 workspaceScreens.add(screenId);
467 addedWorkspaceScreensFinal.add(screenId);
468
469 // If we still can't find an empty space, then God help us all!!!
470 if (!findNextAvailableIconSpaceInScreen(
471 screenItems.get(screenId), cordinates, spanX, spanY)) {
472 throw new RuntimeException("Can't find space to add the item");
473 }
474 }
475 return Pair.create(screenId, cordinates);
476 }
477
478 /**
479 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800480 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700481 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700482 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800483 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800484 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700485 return;
Winson Chung997a9232013-07-24 15:33:46 -0700486 }
Winson Chung64359a52013-07-08 17:17:08 -0700487 // Process the newly added applications and add them to the database first
488 Runnable r = new Runnable() {
489 public void run() {
490 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
491 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
492
Winson Chung76828c82013-08-19 15:43:29 -0700493 // Get the list of workspace screens. We need to append to this list and
494 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
495 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800496 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700497 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800498 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700499 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800500 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700501 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800502 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700503 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800504 }
Winson Chung76828c82013-08-19 15:43:29 -0700505
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800506 // Find appropriate space for the item.
Sunny Goyalc3642d42015-10-30 10:27:08 -0700507 Pair<Long, int[]> coords = findSpaceForItem(context,
Sunny Goyal9eba1fd2015-10-16 08:58:57 -0700508 workspaceScreens, addedWorkspaceScreensFinal, 1, 1);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800509 long screenId = coords.first;
510 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700511
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700512 ItemInfo itemInfo;
513 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
514 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800515 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700516 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700517 } else {
518 throw new RuntimeException("Unexpected info type");
519 }
Winson Chung94d67682013-09-25 16:29:40 -0700520
Winson Chung64359a52013-07-08 17:17:08 -0700521 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700522 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700523 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700524 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700525 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700526 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700527 }
528 }
529
Winson Chung76828c82013-08-19 15:43:29 -0700530 // Update the workspace screens
531 updateWorkspaceScreenOrder(context, workspaceScreens);
532
Adam Cohen76a47a12014-02-05 11:47:43 -0800533 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700534 runOnMainThread(new Runnable() {
535 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800536 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700537 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700538 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
539 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700540 if (!addedShortcutsFinal.isEmpty()) {
541 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
542 long lastScreenId = info.screenId;
543 for (ItemInfo i : addedShortcutsFinal) {
544 if (i.screenId == lastScreenId) {
545 addAnimated.add(i);
546 } else {
547 addNotAnimated.add(i);
548 }
Winson Chung997a9232013-07-24 15:33:46 -0700549 }
550 }
Winson Chungd64d1762013-08-20 14:37:16 -0700551 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800552 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700553 }
Winson Chung64359a52013-07-08 17:17:08 -0700554 }
Winson Chung997a9232013-07-24 15:33:46 -0700555 });
556 }
Winson Chung64359a52013-07-08 17:17:08 -0700557 }
558 };
559 runOnWorkerThread(r);
560 }
561
Joe Onorato9c1289c2009-08-17 11:03:03 -0400562 /**
563 * Adds an item to the DB if it was not created previously, or move it to a new
564 * <container, screen, cellX, cellY>
565 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800566 public static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700567 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400568 if (item.container == ItemInfo.NO_ID) {
569 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700570 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400571 } else {
572 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700573 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800574 }
575 }
576
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700577 static void checkItemInfoLocked(
578 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
579 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
580 if (modelItem != null && item != modelItem) {
581 // check all the data is consistent
582 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
583 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
584 ShortcutInfo shortcut = (ShortcutInfo) item;
585 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
586 modelShortcut.intent.filterEquals(shortcut.intent) &&
587 modelShortcut.id == shortcut.id &&
588 modelShortcut.itemType == shortcut.itemType &&
589 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700590 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700591 modelShortcut.cellX == shortcut.cellX &&
592 modelShortcut.cellY == shortcut.cellY &&
593 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700594 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700595 // For all intents and purposes, this is the same object
596 return;
597 }
598 }
599
600 // the modelItem needs to match up perfectly with item if our model is
601 // to be consistent with the database-- for now, just require
602 // modelItem == item or the equality check above
603 String msg = "item: " + ((item != null) ? item.toString() : "null") +
604 "modelItem: " +
605 ((modelItem != null) ? modelItem.toString() : "null") +
606 "Error: ItemInfo passed to checkItemInfo doesn't match original";
607 RuntimeException e = new RuntimeException(msg);
608 if (stackTrace != null) {
609 e.setStackTrace(stackTrace);
610 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800611 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700612 }
613 }
614
Michael Jurka816474f2012-06-25 14:49:02 -0700615 static void checkItemInfo(final ItemInfo item) {
616 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
617 final long itemId = item.id;
618 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700619 public void run() {
620 synchronized (sBgLock) {
621 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700622 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700623 }
624 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700625 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700626 }
627
Michael Jurkac9d95c52011-08-29 14:03:34 -0700628 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
629 final ItemInfo item, final String callingFunction) {
630 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700631 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700632 final ContentResolver cr = context.getContentResolver();
633
Adam Cohen487f7dd2012-06-28 18:12:10 -0700634 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700635 Runnable r = new Runnable() {
636 public void run() {
637 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700638 updateItemArrays(item, itemId, stackTrace);
639 }
640 };
641 runOnWorkerThread(r);
642 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700643
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700644 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
645 final ArrayList<ItemInfo> items, final String callingFunction) {
646 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700647
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700648 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
649 Runnable r = new Runnable() {
650 public void run() {
651 ArrayList<ContentProviderOperation> ops =
652 new ArrayList<ContentProviderOperation>();
653 int count = items.size();
654 for (int i = 0; i < count; i++) {
655 ItemInfo item = items.get(i);
656 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700657 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700658 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700659
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700660 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
661 updateItemArrays(item, itemId, stackTrace);
662
663 }
664 try {
665 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
666 } catch (Exception e) {
667 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700668 }
669 }
670 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700671 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700672 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700673
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700674 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
675 // Lock on mBgLock *after* the db operation
676 synchronized (sBgLock) {
677 checkItemInfoLocked(itemId, item, stackTrace);
678
679 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
680 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
681 // Item is in a folder, make sure this folder exists
682 if (!sBgFolders.containsKey(item.container)) {
683 // An items container is being set to a that of an item which is not in
684 // the list of Folders.
685 String msg = "item: " + item + " container being set to: " +
686 item.container + ", not in the list of folders";
687 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700688 }
689 }
690
691 // Items are added/removed from the corresponding FolderInfo elsewhere, such
692 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
693 // that are on the desktop, as appropriate
694 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800695 if (modelItem != null &&
696 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
697 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700698 switch (modelItem.itemType) {
699 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
700 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700701 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700702 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
703 if (!sBgWorkspaceItems.contains(modelItem)) {
704 sBgWorkspaceItems.add(modelItem);
705 }
706 break;
707 default:
708 break;
709 }
710 } else {
711 sBgWorkspaceItems.remove(modelItem);
712 }
713 }
714 }
715
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800716 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400717 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700718 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700719 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700720 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400721 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400722 item.cellX = cellX;
723 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700724
Winson Chung3d503fb2011-07-13 17:25:49 -0700725 // We store hotseat items in canonical form which is this orientation invariant position
726 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700727 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700728 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700729 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700730 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700731 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700732 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400733
734 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400735 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700736 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
737 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800738 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700739 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400740
Michael Jurkac9d95c52011-08-29 14:03:34 -0700741 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700742 }
743
744 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700745 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
746 * cellX, cellY have already been updated on the ItemInfos.
747 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800748 public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700749 final long container, final int screen) {
750
751 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
752 int count = items.size();
753
754 for (int i = 0; i < count; i++) {
755 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700756 item.container = container;
757
758 // We store hotseat items in canonical form which is this orientation invariant position
759 // in the hotseat
760 if (context instanceof Launcher && screen < 0 &&
761 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700762 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700763 item.cellY);
764 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700765 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700766 }
767
768 final ContentValues values = new ContentValues();
769 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
770 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
771 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800772 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700773 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700774
775 contentValues.add(values);
776 }
777 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
778 }
779
780 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700781 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800782 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700783 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700784 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700785 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800786 item.cellX = cellX;
787 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700788 item.spanX = spanX;
789 item.spanY = spanY;
790
791 // We store hotseat items in canonical form which is this orientation invariant position
792 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700793 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700794 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700795 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700796 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700797 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700798 }
Adam Cohend4844c32011-02-18 19:25:06 -0800799
Adam Cohend4844c32011-02-18 19:25:06 -0800800 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800801 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700802 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
803 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800804 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700805 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
806 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700807 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800808
Michael Jurka816474f2012-06-25 14:49:02 -0700809 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700810 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700811
812 /**
813 * Update an item to the database in a specified container.
814 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700815 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700816 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100817 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700818 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800819 }
820
Sunny Goyal756a28a2015-04-23 17:07:55 -0700821 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700822 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700823 synchronized (mLock) {
824 if (!mHasLoaderCompletedOnce ||
825 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
826 throw new RuntimeException("Trying to add shortcut while loader is running");
827 }
828 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700829 }
830 }
831
Adam Cohend4844c32011-02-18 19:25:06 -0800832 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700833 * Returns true if the shortcuts already exists on the workspace. This must be called after
834 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700836 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
837 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700838 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700839 if (intent.getComponent() != null) {
840 // If component is not null, an intent with null package will produce
841 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700842 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700843 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700844 intentWithPkg = intent.toUri(0);
845 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700846 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700847 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
848 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700849 }
850 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700851 intentWithPkg = intent.toUri(0);
852 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700853 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700854
855 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700856 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700857 if (item instanceof ShortcutInfo) {
858 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700859 Intent targetIntent = info.promisedIntent == null
860 ? info.intent : info.promisedIntent;
861 if (targetIntent != null && info.user.equals(user)) {
862 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700863 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
864 return true;
865 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700866 }
867 }
868 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800869 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700870 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700871 }
872
Joe Onorato9c1289c2009-08-17 11:03:03 -0400873 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400874 * Add an item to the database in a specified container. Sets the container, screen, cellX and
875 * cellY fields of the item. Also assigns an ID to the item.
876 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700877 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700878 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400879 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400880 item.cellX = cellX;
881 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700882 // We store hotseat items in canonical form which is this orientation invariant position
883 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700884 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700885 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700886 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700887 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700888 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700889 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400890
891 final ContentValues values = new ContentValues();
892 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100893 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400894
Sunny Goyald2497482015-09-22 18:24:19 -0700895 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
896 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
897
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700898 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700899
Jason Monk8e19cf22014-03-20 15:06:57 -0400900 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700901 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700902 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700903 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400904
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700905 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700906 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400907 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700908 sBgItemsIdMap.put(item.id, item);
909 switch (item.itemType) {
910 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
911 sBgFolders.put(item.id, (FolderInfo) item);
912 // Fall through
913 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
914 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700915 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700916 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
917 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
918 sBgWorkspaceItems.add(item);
919 } else {
920 if (!sBgFolders.containsKey(item.container)) {
921 // Adding an item to a folder that doesn't exist.
922 String msg = "adding item: " + item + " to a folder that " +
923 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700924 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700925 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700926 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700927 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
928 ShortcutInfo shortcutInfo = (ShortcutInfo) item;
929 ShortcutKey shortcutToPin = new ShortcutKey(
930 shortcutInfo.intent.getPackage(),
931 shortcutInfo.user,
932 shortcutInfo.getDeepShortcutId());
933 incrementPinnedShortcutCount(shortcutToPin, true /* shouldPin */);
934 }
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:
1002 ShortcutInfo shortcutInfo = ((ShortcutInfo) item);
1003 ShortcutKey pinnedShortcut = new ShortcutKey(
1004 shortcutInfo.intent.getPackage(),
1005 shortcutInfo.user,
1006 shortcutInfo.getDeepShortcutId());
1007 decrementPinnedShortcutCount(pinnedShortcut);
1008 // Fall through.
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001009 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1010 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1011 sBgWorkspaceItems.remove(item);
1012 break;
1013 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1014 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1015 break;
1016 }
1017 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001018 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001019 }
1020 }
Michael Jurka83df1882011-08-31 20:59:26 -07001021 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001022 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001023 }
1024
1025 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001026 * Decrement the count for the given pinned shortcut, unpinning it if the count becomes 0.
1027 */
1028 private static void decrementPinnedShortcutCount(final ShortcutKey pinnedShortcut) {
1029 synchronized (sBgLock) {
1030 MutableInt count = sBgPinnedShortcutCounts.get(pinnedShortcut);
1031 if (count == null || --count.value == 0) {
1032 LauncherAppState.getInstance().getShortcutManager().unpinShortcut(pinnedShortcut);
1033 }
1034 }
1035 }
1036
1037 /**
1038 * Increment the count for the given shortcut, pinning it if the count becomes 1.
1039 *
1040 * As an optimization, the caller can pass shouldPin == false to avoid
1041 * unnecessary RPC's if the shortcut is already pinned.
1042 */
1043 private static void incrementPinnedShortcutCount(ShortcutKey pinnedShortcut, boolean shouldPin) {
1044 synchronized (sBgLock) {
1045 MutableInt count = sBgPinnedShortcutCounts.get(pinnedShortcut);
1046 if (count == null) {
1047 count = new MutableInt(1);
1048 sBgPinnedShortcutCounts.put(pinnedShortcut, count);
1049 } else {
1050 count.value++;
1051 }
1052 if (shouldPin && count.value == 1) {
1053 LauncherAppState.getInstance().getShortcutManager().pinShortcut(pinnedShortcut);
1054 }
1055 }
1056 }
1057
1058 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001059 * Update the order of the workspace screens in the database. The array list contains
1060 * a list of screen ids in the order that they should appear.
1061 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001062 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001063 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001064 final ContentResolver cr = context.getContentResolver();
1065 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1066
1067 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001068 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001069 while (iter.hasNext()) {
1070 long id = iter.next();
1071 if (id < 0) {
1072 iter.remove();
1073 }
1074 }
1075
1076 Runnable r = new Runnable() {
1077 @Override
1078 public void run() {
Yura085c8532014-02-11 15:15:29 +00001079 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001080 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001081 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001082 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001083 for (int i = 0; i < count; i++) {
1084 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001085 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001086 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1087 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001088 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001089 }
Yura085c8532014-02-11 15:15:29 +00001090
1091 try {
1092 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1093 } catch (Exception ex) {
1094 throw new RuntimeException(ex);
1095 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001096
Winson Chungba9c37f2013-08-30 14:11:37 -07001097 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001098 sBgWorkspaceScreens.clear();
1099 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001100 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001101 }
1102 };
1103 runOnWorkerThread(r);
1104 }
1105
1106 /**
Winsonc0b52fe2015-09-09 16:38:15 -07001107 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001108 */
Winsonc0b52fe2015-09-09 16:38:15 -07001109 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001110 final ContentResolver cr = context.getContentResolver();
1111
Michael Jurkac9d95c52011-08-29 14:03:34 -07001112 Runnable r = new Runnable() {
1113 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001114 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001115 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001116 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001117 sBgItemsIdMap.remove(info.id);
1118 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001119 sBgWorkspaceItems.remove(info);
1120 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001121
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001122 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001123 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001124 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001125 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001126 for (ItemInfo childInfo : info.contents) {
1127 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001128 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001129 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001130 }
1131 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001132 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001133 }
1134
1135 /**
1136 * Set this as the current Launcher activity object for the loader.
1137 */
1138 public void initialize(Callbacks callbacks) {
1139 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001140 Preconditions.assertUIThread();
1141 // Remove any queued UI runnables
1142 mHandler.cancelAll();
1143 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001144 }
1145 }
1146
Kenny Guyed131872014-04-30 03:02:21 +01001147 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001148 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001149 int op = PackageUpdatedTask.OP_UPDATE;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001150 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001151 user));
1152 }
1153
1154 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001155 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001156 int op = PackageUpdatedTask.OP_REMOVE;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001157 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001158 user));
1159 }
1160
1161 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001162 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001163 int op = PackageUpdatedTask.OP_ADD;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001164 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001165 user));
1166 }
1167
1168 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001169 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001170 boolean replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001171 enqueueItemUpdatedTask(
Sunny Goyal144154d2016-03-30 16:47:03 -07001172 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, packageNames, user));
Kenny Guyed131872014-04-30 03:02:21 +01001173 }
1174
1175 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001176 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001177 boolean replacing) {
1178 if (!replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001179 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guyed131872014-04-30 03:02:21 +01001180 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1181 user));
1182 }
Kenny Guyed131872014-04-30 03:02:21 +01001183 }
1184
Kenny Guy44cba692016-01-21 19:50:02 +00001185 @Override
1186 public void onPackagesSuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001187 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001188 PackageUpdatedTask.OP_SUSPEND, packageNames,
1189 user));
1190 }
1191
1192 @Override
1193 public void onPackagesUnsuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001194 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001195 PackageUpdatedTask.OP_UNSUSPEND, packageNames,
1196 user));
1197 }
1198
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001199 @Override
1200 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
1201 UserHandleCompat user) {
1202 enqueueItemUpdatedTask(new ShortcutsChangedTask(packageName, shortcuts, user));
1203 }
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 Goyal957c13f2015-05-01 13:02:20 -07001217 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
Sunny Goyalda891c12016-03-18 18:29:24 -07001218 || LauncherAppsCompat.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();
Rubin Xuac6e5d72016-04-04 16:13:35 +01001221 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1222 LauncherAppsCompat.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
Sunny Goyalda891c12016-03-18 18:29:24 -07001223 UserHandleCompat user = UserHandleCompat.fromIntent(intent);
1224 if (user != null) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001225 enqueueItemUpdatedTask(new PackageUpdatedTask(
Sunny Goyalda891c12016-03-18 18:29:24 -07001226 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE,
1227 new String[0], user));
1228 }
Tony Wickham827cef22016-03-17 15:39:39 -07001229 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
1230 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001231 }
1232 }
1233
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001234 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001235 resetLoadedState(true, true);
1236
Reena Lee93f824a2011-09-23 17:20:28 -07001237 // Do this here because if the launcher activity is running it will be restarted.
1238 // If it's not running startLoaderFromBackground will merely tell it that it needs
1239 // to reload.
1240 startLoaderFromBackground();
1241 }
1242
Winson Chungf0c6ae02012-03-21 16:10:31 -07001243 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1244 synchronized (mLock) {
1245 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1246 // mWorkspaceLoaded to true later
1247 stopLoaderLocked();
1248 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1249 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001250 // Always reset deep shortcuts loaded.
1251 mDeepShortcutsLoaded = false;
Winson Chungf0c6ae02012-03-21 16:10:31 -07001252 }
1253 }
1254
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001255 /**
1256 * When the launcher is in the background, it's possible for it to miss paired
1257 * configuration changes. So whenever we trigger the loader from the background
1258 * tell the launcher that it needs to re-run the loader when it comes back instead
1259 * of doing it now.
1260 */
1261 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001262 Callbacks callbacks = getCallback();
1263 if (callbacks != null) {
1264 // Only actually run the loader if they're not paused.
1265 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -07001266 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001267 }
1268 }
Joe Onorato36115782010-06-17 13:28:48 -04001269 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001270
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001271 /**
1272 * If there is already a loader task running, tell it to stop.
1273 */
1274 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001275 LoaderTask oldTask = mLoaderTask;
1276 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001277 oldTask.stopLocked();
1278 }
Reena Lee93f824a2011-09-23 17:20:28 -07001279 }
1280
Adam Cohen1a85c582014-09-30 09:48:49 -07001281 public boolean isCurrentCallbacks(Callbacks callbacks) {
1282 return (mCallbacks != null && mCallbacks.get() == callbacks);
1283 }
1284
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001285 public void startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001286 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1287 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001288 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -04001289 // Don't bother to start the thread if we know it's not going to do anything
1290 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001291 final Callbacks oldCallbacks = mCallbacks.get();
1292 // Clear any pending bind-runnables from the synchronized load process.
1293 runOnMainThread(new Runnable() {
1294 public void run() {
1295 oldCallbacks.clearPendingBinds();
1296 }
1297 });
1298
Joe Onorato36115782010-06-17 13:28:48 -04001299 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001300 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001301 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Tony Wickham80f57872016-06-29 18:12:15 -07001302 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE && mAllAppsLoaded
1303 && mWorkspaceLoaded && mDeepShortcutsLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001304 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1305 } else {
1306 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1307 sWorker.post(mLoaderTask);
1308 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001309 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001310 }
1311 }
1312
Joe Onorato36115782010-06-17 13:28:48 -04001313 public void stopLoader() {
1314 synchronized (mLock) {
1315 if (mLoaderTask != null) {
1316 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001317 }
1318 }
Joe Onorato36115782010-06-17 13:28:48 -04001319 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001320
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001321 /**
1322 * Loads the workspace screen ids in an ordered list.
1323 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001324 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001325 final ContentResolver contentResolver = context.getContentResolver();
1326 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001327
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001328 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001329 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
1330 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -07001331 }
1332
Joe Onorato36115782010-06-17 13:28:48 -04001333 /**
1334 * Runnable for the thread that loads the contents of the launcher:
1335 * - workspace icons
1336 * - widgets
1337 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001338 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -04001339 */
1340 private class LoaderTask implements Runnable {
1341 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001342 private int mPageToBindFirst;
1343
Adam Cohen091440a2015-03-18 14:16:05 -07001344 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001345 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001346 @Thunk boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001347
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001348 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -04001349 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001350 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001351 }
1352
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001353 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001354 mIsLoadingAndBindingWorkspace = true;
1355
Joe Onorato36115782010-06-17 13:28:48 -04001356 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001357 if (DEBUG_LOADERS) {
1358 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001359 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001360
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001361 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001362 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001363 synchronized (LoaderTask.this) {
1364 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001365 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001366 }
1367 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001368 }
1369 }
1370
Joe Onorato36115782010-06-17 13:28:48 -04001371 // Bind the workspace
Sunny Goyal93f878c2016-03-30 17:31:24 -07001372 bindWorkspace(mPageToBindFirst);
Joe Onorato36115782010-06-17 13:28:48 -04001373 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001374
Joe Onorato36115782010-06-17 13:28:48 -04001375 private void waitForIdle() {
1376 // Wait until the either we're stopped or the other threads are done.
1377 // This way we don't start loading all apps until the workspace has settled
1378 // down.
1379 synchronized (LoaderTask.this) {
1380 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001381
Joe Onorato36115782010-06-17 13:28:48 -04001382 mHandler.postIdle(new Runnable() {
1383 public void run() {
1384 synchronized (LoaderTask.this) {
1385 mLoadAndBindStepFinished = true;
1386 if (DEBUG_LOADERS) {
1387 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001388 }
Joe Onorato36115782010-06-17 13:28:48 -04001389 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001390 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001391 }
Joe Onorato36115782010-06-17 13:28:48 -04001392 });
1393
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001394 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001395 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001396 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1397 // wait no longer than 1sec at a time
1398 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001399 } catch (InterruptedException ex) {
1400 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001401 }
1402 }
Joe Onorato36115782010-06-17 13:28:48 -04001403 if (DEBUG_LOADERS) {
1404 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001405 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001406 + "ms for previous step to finish binding");
1407 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001408 }
Joe Onorato36115782010-06-17 13:28:48 -04001409 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001410
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001411 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001412 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001413 // Ensure that we have a valid page index to load synchronously
1414 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1415 "valid page index");
1416 }
1417 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1418 // Ensure that we don't try and bind a specified page when the pages have not been
1419 // loaded already (we should load everything asynchronously in that case)
1420 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1421 }
1422 synchronized (mLock) {
1423 if (mIsLoaderTaskRunning) {
1424 // Ensure that we are never running the background loading at this point since
1425 // we also touch the background collections
1426 throw new RuntimeException("Error! Background loading is already running");
1427 }
1428 }
1429
1430 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1431 // data structures, we can't allow any other thread to touch that data, but because
1432 // this call is synchronous, we can get away with not locking).
1433
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001434 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001435 // operations from the previous activity. We need to ensure that all queued operations
1436 // are executed before any synchronous binding work is done.
1437 mHandler.flush();
1438
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001439 // Divide the set of loaded items into those that we are binding synchronously, and
1440 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001441 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001442 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1443 // arise from that.
1444 onlyBindAllApps();
Tony Wickham80f57872016-06-29 18:12:15 -07001445
1446 bindDeepShortcuts();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001447 }
1448
Joe Onorato36115782010-06-17 13:28:48 -04001449 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001450 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001451 if (mStopped) {
1452 return;
1453 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001454 mIsLoaderTaskRunning = true;
1455 }
Joe Onorato36115782010-06-17 13:28:48 -04001456 // Optimize for end-user experience: if the Launcher is up and // running with the
1457 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1458 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001459 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001460 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001461 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001462
Joe Onorato36115782010-06-17 13:28:48 -04001463 if (mStopped) {
1464 break keep_running;
1465 }
1466
Joe Onorato36115782010-06-17 13:28:48 -04001467 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001468
1469 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001470 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1471 loadAndBindAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001472
1473 waitForIdle();
1474
1475 // third step
1476 if (DEBUG_LOADERS) Log.d(TAG, "step 3: loading deep shortcuts");
1477 loadAndBindDeepShortcuts();
Joe Onorato36115782010-06-17 13:28:48 -04001478 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001479
Joe Onorato36115782010-06-17 13:28:48 -04001480 // Clear out this reference, otherwise we end up holding it until all of the
1481 // callback runnables are done.
1482 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001483
Joe Onorato36115782010-06-17 13:28:48 -04001484 synchronized (mLock) {
1485 // If we are still the last one to be scheduled, remove ourselves.
1486 if (mLoaderTask == this) {
1487 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001488 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001489 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001490 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001491 }
Joe Onorato36115782010-06-17 13:28:48 -04001492 }
1493
1494 public void stopLocked() {
1495 synchronized (LoaderTask.this) {
1496 mStopped = true;
1497 this.notify();
1498 }
1499 }
1500
1501 /**
1502 * Gets the callbacks object. If we've been stopped, or if the launcher object
1503 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1504 * object that was around when the deferred message was scheduled, and if there's
1505 * a new Callbacks object around then also return null. This will save us from
1506 * calling onto it with data that will be ignored.
1507 */
1508 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1509 synchronized (mLock) {
1510 if (mStopped) {
1511 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001512 }
Joe Onorato36115782010-06-17 13:28:48 -04001513
1514 if (mCallbacks == null) {
1515 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001516 }
Joe Onorato36115782010-06-17 13:28:48 -04001517
1518 final Callbacks callbacks = mCallbacks.get();
1519 if (callbacks != oldCallbacks) {
1520 return null;
1521 }
1522 if (callbacks == null) {
1523 Log.w(TAG, "no mCallbacks");
1524 return null;
1525 }
1526
1527 return callbacks;
1528 }
1529 }
1530
1531 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001532 private boolean checkItemPlacement(LongArrayMap<GridOccupancy> occupied, ItemInfo item,
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001533 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001534 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001535 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung892c74d2013-08-22 16:15:50 -07001536
Adam Cohendcd297f2013-06-18 13:13:40 -07001537 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001538 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001539 // Return early if we detect that an item is under the hotseat button
Sunny Goyalbb011da2016-06-15 15:42:29 -07001540 if (!FeatureFlags.NO_ALL_APPS_ICON &&
1541 profile.isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001542 Log.e(TAG, "Error loading shortcut into hotseat " + item
1543 + " into position (" + item.screenId + ":" + item.cellX + ","
1544 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001545 return false;
1546 }
1547
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001548 final GridOccupancy hotseatOccupancy =
Dan Sandler295ae182013-12-10 16:05:47 -05001549 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1550
Adam Cohen2e6da152015-05-06 11:42:25 -07001551 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001552 Log.e(TAG, "Error loading shortcut " + item
1553 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001554 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001555 + ")");
1556 return false;
1557 }
1558
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001559 if (hotseatOccupancy != null) {
1560 if (hotseatOccupancy.cells[(int) item.screenId][0]) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001561 Log.e(TAG, "Error loading shortcut into hotseat " + item
1562 + " into position (" + item.screenId + ":" + item.cellX + ","
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001563 + item.cellY + ") already occupied");
Adam Cohendcd297f2013-06-18 13:13:40 -07001564 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001565 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001566 hotseatOccupancy.cells[(int) item.screenId][0] = true;
Dan Sandler295ae182013-12-10 16:05:47 -05001567 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001568 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001569 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001570 final GridOccupancy occupancy = new GridOccupancy(profile.numHotseatIcons, 1);
1571 occupancy.cells[(int) item.screenId][0] = true;
1572 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, occupancy);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001573 return true;
1574 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001575 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1576 if (!workspaceScreens.contains((Long) item.screenId)) {
1577 // The item has an invalid screen id.
1578 return false;
1579 }
1580 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001581 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001582 return true;
1583 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001584
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001585 final int countX = profile.numColumns;
1586 final int countY = profile.numRows;
Adam Cohenae4409d2013-11-26 10:34:59 -08001587 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1588 item.cellX < 0 || item.cellY < 0 ||
1589 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1590 Log.e(TAG, "Error loading shortcut " + item
1591 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1592 + item.cellX + "," + item.cellY
1593 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1594 return false;
1595 }
1596
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001597 if (!occupied.containsKey(item.screenId)) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001598 GridOccupancy screen = new GridOccupancy(countX + 1, countY + 1);
1599 if (item.screenId == Workspace.FIRST_SCREEN_ID) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001600 // Mark the first row as occupied (if the feature is enabled)
1601 // in order to account for the QSB.
1602 screen.markCells(0, 0, countX + 1, 1, FeatureFlags.QSB_ON_FIRST_SCREEN);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001603 }
1604 occupied.put(item.screenId, screen);
Joe Onorato36115782010-06-17 13:28:48 -04001605 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001606 final GridOccupancy occupancy = occupied.get(item.screenId);
Winson Chungf30ad5f2011-08-08 10:55:42 -07001607
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001608 // Check if any workspace icons overlap with each other
1609 if (occupancy.isRegionVacant(item.cellX, item.cellY, item.spanX, item.spanY)) {
1610 occupancy.markCells(item, true);
1611 return true;
1612 } else {
1613 Log.e(TAG, "Error loading shortcut " + item
1614 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1615 + item.cellX + "," + item.cellX + "," + item.spanX + "," + item.spanY
1616 + ") already occupied");
1617 return false;
1618 }
Joe Onorato36115782010-06-17 13:28:48 -04001619 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001620
Winson Chungba9c37f2013-08-30 14:11:37 -07001621 /** Clears all the sBg data structures */
1622 private void clearSBgDataStructures() {
1623 synchronized (sBgLock) {
1624 sBgWorkspaceItems.clear();
1625 sBgAppWidgets.clear();
1626 sBgFolders.clear();
1627 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001628 sBgWorkspaceScreens.clear();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001629 sBgPinnedShortcutCounts.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001630 }
1631 }
1632
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001633 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -07001634 if (LauncherAppState.PROFILE_STARTUP) {
1635 Trace.beginSection("Loading Workspace");
1636 }
Joe Onorato36115782010-06-17 13:28:48 -04001637 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001638
Joe Onorato36115782010-06-17 13:28:48 -04001639 final Context context = mContext;
1640 final ContentResolver contentResolver = context.getContentResolver();
1641 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001642 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001643 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001644 final boolean isSdCardReady = Utilities.isBootCompleted();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001645
Winson Chung892c74d2013-08-22 16:15:50 -07001646 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001647 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001648 int countX = profile.numColumns;
1649 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001650
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001651 boolean clearDb = false;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -07001652 try {
1653 ImportDataTask.performImportIfPossible(context);
1654 } catch (Exception e) {
1655 // Migration failed. Clear workspace.
1656 clearDb = true;
1657 }
1658
1659 if (!clearDb && GridSizeMigrationTask.ENABLED &&
Sunny Goyalf076eae2016-01-11 12:25:10 -08001660 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1661 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001662 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001663 }
1664
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001665 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -07001666 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001667 LauncherSettings.Settings.call(contentResolver,
1668 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001669 }
1670
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001671 Log.d(TAG, "loadWorkspace: loading default favorites");
1672 LauncherSettings.Settings.call(contentResolver,
1673 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -07001674
Winson Chung2abf94d2012-07-18 18:16:38 -07001675 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001676 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001677 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001678 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001679 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001680
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001681 final ArrayList<Long> itemsToRemove = new ArrayList<>();
1682 final ArrayList<Long> restoredRows = new ArrayList<>();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001683 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001684 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001685 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001686 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001687
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001688 // +1 for the hotseat (it can be larger than the workspace)
1689 // Load workspace in reverse order to ensure that latest items are loaded first (and
1690 // before any earlier duplicates)
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001691 final LongArrayMap<GridOccupancy> occupied = new LongArrayMap<>();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001692 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001693
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001694 try {
1695 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1696 final int intentIndex = c.getColumnIndexOrThrow
1697 (LauncherSettings.Favorites.INTENT);
1698 final int titleIndex = c.getColumnIndexOrThrow
1699 (LauncherSettings.Favorites.TITLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001700 final int containerIndex = c.getColumnIndexOrThrow(
1701 LauncherSettings.Favorites.CONTAINER);
1702 final int itemTypeIndex = c.getColumnIndexOrThrow(
1703 LauncherSettings.Favorites.ITEM_TYPE);
1704 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1705 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001706 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1707 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001708 final int screenIndex = c.getColumnIndexOrThrow(
1709 LauncherSettings.Favorites.SCREEN);
1710 final int cellXIndex = c.getColumnIndexOrThrow
1711 (LauncherSettings.Favorites.CELLX);
1712 final int cellYIndex = c.getColumnIndexOrThrow
1713 (LauncherSettings.Favorites.CELLY);
1714 final int spanXIndex = c.getColumnIndexOrThrow
1715 (LauncherSettings.Favorites.SPANX);
1716 final int spanYIndex = c.getColumnIndexOrThrow(
1717 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001718 final int rankIndex = c.getColumnIndexOrThrow(
1719 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001720 final int restoredIndex = c.getColumnIndexOrThrow(
1721 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001722 final int profileIdIndex = c.getColumnIndexOrThrow(
1723 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001724 final int optionsIndex = c.getColumnIndexOrThrow(
1725 LauncherSettings.Favorites.OPTIONS);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001726 final CursorIconInfo cursorIconInfo = new CursorIconInfo(c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001727
Sunny Goyal7f834d22015-04-21 10:10:23 -07001728 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
Kenny Guyff05f432016-01-22 17:48:29 +00001729 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyal7f834d22015-04-21 10:10:23 -07001730 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001731 long serialNo = mUserManager.getSerialNumberForUser(user);
1732 allUsers.put(serialNo, user);
1733 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001734
1735 List<ShortcutInfoCompat> pinnedShortcuts = mDeepShortcutManager
1736 .queryForPinnedShortcuts(null, user);
1737 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
1738 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
1739 shortcut);
1740 }
Sunny Goyal7f834d22015-04-21 10:10:23 -07001741 }
1742
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001743 ShortcutInfo info;
1744 String intentDescription;
1745 LauncherAppWidgetInfo appWidgetInfo;
1746 int container;
1747 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001748 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001749 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001750 UserHandleCompat user;
Sunny Goyald09c3702016-04-06 16:18:20 -07001751 String targetPackage;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001752
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001753 while (!mStopped && c.moveToNext()) {
1754 try {
1755 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001756 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001757 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001758 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001759
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001760 switch (itemType) {
1761 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1762 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001763 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001764 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001765 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001766 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001767 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001768 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001769 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001770 boolean itemReplaced = false;
Sunny Goyald09c3702016-04-06 16:18:20 -07001771 targetPackage = null;
Kenny Guyed131872014-04-30 03:02:21 +01001772 if (user == null) {
1773 // User has been deleted remove the item.
1774 itemsToRemove.add(id);
1775 continue;
1776 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001777 try {
1778 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001779 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001780 if (cn != null && cn.getPackageName() != null) {
1781 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1782 cn.getPackageName(), user);
1783 boolean validComponent = validPkg &&
1784 launcherApps.isActivityEnabledForProfile(cn, user);
Sunny Goyald09c3702016-04-06 16:18:20 -07001785 if (validPkg) {
1786 targetPackage = cn.getPackageName();
1787 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001788
1789 if (validComponent) {
1790 if (restored) {
1791 // no special handling necessary for this item
1792 restoredRows.add(id);
1793 restored = false;
1794 }
Kenny Guyff05f432016-01-22 17:48:29 +00001795 if (quietMode.get(serialNumber)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001796 disabledState = ShortcutInfo.FLAG_DISABLED_QUIET_USER;
Kenny Guyff05f432016-01-22 17:48:29 +00001797 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001798 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001799 intent = null;
1800 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1801 // We allow auto install apps to have their intent
1802 // updated after an install.
1803 intent = manager.getLaunchIntentForPackage(
1804 cn.getPackageName());
1805 if (intent != null) {
1806 ContentValues values = new ContentValues();
1807 values.put(LauncherSettings.Favorites.INTENT,
1808 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001809 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001810 }
1811 }
1812
1813 if (intent == null) {
1814 // The app is installed but the component is no
1815 // longer available.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001816 FileLog.d(TAG, "Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001817 itemsToRemove.add(id);
1818 continue;
1819 } else {
1820 // no special handling necessary for this item
1821 restoredRows.add(id);
1822 restored = false;
1823 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001824 } else if (restored) {
1825 // Package is not yet available but might be
1826 // installed later.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001827 FileLog.d(TAG, "package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001828
1829 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1830 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001831 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001832 // App restore has started. Update the flag
1833 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1834 ContentValues values = new ContentValues();
1835 values.put(LauncherSettings.Favorites.RESTORED,
1836 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001837 updateItem(id, values);
1838 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1839 // This is a common app. Try to replace this.
1840 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1841 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1842 if (parser.findDefaultApp()) {
1843 // Default app found. Replace it.
1844 intent = parser.parsedIntent;
1845 cn = intent.getComponent();
1846 ContentValues values = parser.parsedValues;
1847 values.put(LauncherSettings.Favorites.RESTORED, 0);
1848 updateItem(id, values);
1849 restored = false;
1850 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001851
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001852 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001853 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001854 itemsToRemove.add(id);
1855 continue;
1856 }
Sunny Goyal94485362014-09-18 16:13:58 -07001857 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001858 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001859 itemsToRemove.add(id);
1860 continue;
1861 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001862 } else if (PackageManagerHelper.isAppOnSdcard(
1863 manager, cn.getPackageName())) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07001864 // Package is present but not available.
1865 allowMissingTarget = true;
1866 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1867 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001868 // SdCard is not ready yet. Package might get available,
1869 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001870 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001871 HashSet<String> pkgs = sPendingPackages.get(user);
1872 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001873 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001874 sPendingPackages.put(user, pkgs);
1875 }
1876 pkgs.add(cn.getPackageName());
1877 allowMissingTarget = true;
1878 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001879
1880 } else {
1881 // Do not wait for external media load anymore.
1882 // Log the invalid package, and remove it
Sunny Goyal713edfc2016-05-06 09:58:34 -07001883 FileLog.d(TAG, "Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001884 itemsToRemove.add(id);
1885 continue;
Winson Chungee055712013-07-30 14:46:24 -07001886 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001887 } else if (cn == null) {
1888 // For shortcuts with no component, keep them as they are
1889 restoredRows.add(id);
1890 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001891 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001892 } catch (URISyntaxException e) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001893 FileLog.d(TAG, "Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001894 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001895 continue;
1896 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001897
Sunny Goyal34b65272015-03-11 16:56:52 -07001898 boolean useLowResIcon = container >= 0 &&
1899 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1900
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001901 if (itemReplaced) {
1902 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001903 info = getAppShortcutInfo(intent, user, context, null,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001904 cursorIconInfo.iconIndex, titleIndex,
1905 false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001906 } else {
1907 // Don't replace items for other profiles.
1908 itemsToRemove.add(id);
1909 continue;
1910 }
1911 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001912 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal34b65272015-03-11 16:56:52 -07001913 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07001914 promiseType, itemType, cursorIconInfo, context);
Kenny Guyed131872014-04-30 03:02:21 +01001915 intent = getRestoredItemIntent(c, context, intent);
1916 } else {
1917 // Don't restore items for other profiles.
1918 itemsToRemove.add(id);
1919 continue;
1920 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001921 } else if (itemType ==
1922 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001923 info = getAppShortcutInfo(intent, user, context, c,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001924 cursorIconInfo.iconIndex, titleIndex,
1925 allowMissingTarget, useLowResIcon);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001926 } else if (itemType ==
1927 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
1928 String shortcutId = intent.getStringExtra(
1929 ShortcutInfoCompat.EXTRA_SHORTCUT_ID);
1930 String packageName = intent.getPackage();
1931 ShortcutKey key = new ShortcutKey(intent.getPackage(),
1932 user, shortcutId);
1933 ShortcutInfoCompat pinnedShortcut =
1934 shortcutKeyToPinnedShortcuts.get(key);
1935 boolean shouldPin = false; // It's already pinned.
1936 if (pinnedShortcut == null) {
1937 // It shouldn't be possible for a shortcut to be on the
1938 // workspace without being pinned, but if one somehow is,
1939 // we should pin it now to get back to a good state.
1940 Log.w(TAG, "Shortcut was on workspace but wasn't pinned");
1941 // Get full details; incrementing the count will pin it.
1942 List<ShortcutInfoCompat> fullDetails = mDeepShortcutManager
1943 .queryForFullDetails(packageName,
1944 Collections.singletonList(shortcutId), user);
Tony Wickhamd82a39d2016-07-01 15:44:13 -07001945 if (fullDetails == null || fullDetails.isEmpty()) {
1946 // There are no details for the shortcut. If this is due
1947 // to a SecurityException, keep it in the database so
1948 // we can restore the icon when the launcher regains
1949 // permission. Otherwise remove the icon from the db.
1950 if (!mDeepShortcutManager.wasLastCallSuccess()) {
1951 itemsToRemove.add(id);
1952 continue;
1953 }
1954 } else {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001955 pinnedShortcut = fullDetails.get(0);
1956 shouldPin = true;
1957 }
1958 }
1959 incrementPinnedShortcutCount(key, shouldPin);
Sunny Goyal9994b2b2016-06-23 14:17:24 -07001960 info = ShortcutInfo.fromDeepShortcutInfo(
1961 pinnedShortcut, context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001962 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001963 info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001964
Sunny Goyald09c3702016-04-06 16:18:20 -07001965 // Shortcuts are only available on the primary profile
1966 if (PackageManagerHelper.isAppSuspended(manager, targetPackage)) {
1967 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1968 }
1969
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001970 // App shortcuts that used to be automatically added to Launcher
1971 // didn't always have the correct intent flags set, so do that
1972 // here
1973 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001974 intent.getCategories() != null &&
1975 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001976 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001977 intent.addFlags(
1978 Intent.FLAG_ACTIVITY_NEW_TASK |
1979 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1980 }
Michael Jurka96879562012-03-22 05:54:33 -07001981 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001982
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001983 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001984 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001985 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001986 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001987 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001988 info.cellX = c.getInt(cellXIndex);
1989 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08001990 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001991 info.spanX = 1;
1992 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01001993 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001994 if (info.promisedIntent != null) {
1995 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
1996 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001997 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001998 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1999 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2000 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002001
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002002 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002003 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002004 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002005 break;
2006 }
2007
Sunny Goyal756adbc2015-04-16 15:20:51 -07002008 if (restored) {
2009 ComponentName cn = info.getTargetComponent();
2010 if (cn != null) {
2011 Integer progress = installingPkgs.get(cn.getPackageName());
2012 if (progress != null) {
2013 info.setInstallProgress(progress);
2014 } else {
2015 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2016 }
2017 }
2018 }
2019
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002020 switch (container) {
2021 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2022 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2023 sBgWorkspaceItems.add(info);
2024 break;
2025 default:
2026 // Item is in a user folder
2027 FolderInfo folderInfo =
2028 findOrMakeFolder(sBgFolders, container);
Sunny Goyalc52ba712016-04-05 15:59:05 -07002029 folderInfo.add(info, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002030 break;
2031 }
2032 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002033 } else {
2034 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002035 }
2036 break;
2037
2038 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2039 id = c.getLong(idIndex);
2040 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2041
Sunny Goyala508e4f2015-05-21 09:33:57 -07002042 // Do not trim the folder label, as is was set by the user.
2043 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002044 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002045 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002046 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002047 folderInfo.cellX = c.getInt(cellXIndex);
2048 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002049 folderInfo.spanX = 1;
2050 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002051 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002052
Daniel Sandler8802e962010-05-26 16:28:16 -04002053 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002054 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002055 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002056 break;
2057 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002058
Joe Onorato9c1289c2009-08-17 11:03:03 -04002059 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002060 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2061 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2062 sBgWorkspaceItems.add(folderInfo);
2063 break;
Joe Onorato36115782010-06-17 13:28:48 -04002064 }
Joe Onorato17a89222011-02-08 17:26:11 -08002065
Chris Wrenf4d08112014-01-16 18:13:56 -05002066 if (restored) {
2067 // no special handling required for restored folders
2068 restoredRows.add(id);
2069 }
2070
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002071 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2072 sBgFolders.put(folderInfo.id, folderInfo);
2073 break;
2074
2075 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002076 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002077 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002078 boolean customWidget = itemType ==
2079 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2080
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002081 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002082 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002083 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002084 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002085 user = allUsers.get(serialNumber);
2086 if (user == null) {
2087 itemsToRemove.add(id);
2088 continue;
2089 }
2090
Sunny Goyalff572272014-07-23 13:58:07 -07002091 final ComponentName component =
2092 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002093
Sunny Goyal651077b2014-06-30 14:15:31 -07002094 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002095 final boolean isIdValid = (restoreStatus &
2096 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002097 final boolean wasProviderReady = (restoreStatus &
2098 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002099
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002100 if (widgetProvidersMap == null) {
2101 widgetProvidersMap = AppWidgetManagerCompat
2102 .getInstance(mContext).getAllProvidersMap();
2103 }
2104 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
2105 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00002106 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002107 user));
Sunny Goyalff572272014-07-23 13:58:07 -07002108
2109 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002110 if (!isSafeMode && !customWidget &&
2111 wasProviderReady && !isProviderReady) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002112 FileLog.d(TAG, "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07002113 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002114 itemsToRemove.add(id);
2115 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002116 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002117 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2118 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002119
Sunny Goyal53f96722015-07-13 19:54:53 -07002120 // The provider is available. So the widget is either
2121 // available or not available. We do not need to track
2122 // any future restore updates.
2123 int status = restoreStatus &
2124 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002125 if (!wasProviderReady) {
2126 // If provider was not previously ready, update the
2127 // status and UI flag.
2128
2129 // Id would be valid only if the widget restore broadcast was received.
2130 if (isIdValid) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002131 status = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002132 } else {
2133 status &= ~LauncherAppWidgetInfo
2134 .FLAG_PROVIDER_NOT_READY;
2135 }
2136 }
2137 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002138 } else {
2139 Log.v(TAG, "Widget restore pending id=" + id
2140 + " appWidgetId=" + appWidgetId
2141 + " status =" + restoreStatus);
2142 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002143 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002144 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002145 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002146
2147 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2148 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002149 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002150 // App restore has started. Update the flag
2151 appWidgetInfo.restoreStatus |=
2152 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002153 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002154 FileLog.d(TAG, "Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07002155 itemsToRemove.add(id);
2156 continue;
2157 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002158
2159 appWidgetInfo.installProgress =
2160 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002161 }
Sunny Goyalff572272014-07-23 13:58:07 -07002162
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002163 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002164 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002165 appWidgetInfo.cellX = c.getInt(cellXIndex);
2166 appWidgetInfo.cellY = c.getInt(cellYIndex);
2167 appWidgetInfo.spanX = c.getInt(spanXIndex);
2168 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002169 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002170
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002171 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2172 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2173 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002174 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2175 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002176 continue;
2177 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002178
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002179 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002180 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002181 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002182 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002183 break;
2184 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002185
Adam Cohen59400422014-03-05 18:07:04 -08002186 if (!customWidget) {
2187 String providerName =
2188 appWidgetInfo.providerName.flattenToString();
2189 if (!providerName.equals(savedProvider) ||
2190 (appWidgetInfo.restoreStatus != restoreStatus)) {
2191 ContentValues values = new ContentValues();
2192 values.put(
2193 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2194 providerName);
2195 values.put(LauncherSettings.Favorites.RESTORED,
2196 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002197 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002198 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002199 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002200 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2201 sBgAppWidgets.add(appWidgetInfo);
2202 }
Joe Onorato36115782010-06-17 13:28:48 -04002203 break;
2204 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002205 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002206 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002207 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002208 }
2209 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002210 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002211 }
2212
Winson Chungba9c37f2013-08-30 14:11:37 -07002213 // Break early if we've stopped loading
2214 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002215 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002216 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002217 }
2218
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002219 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002220 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002221 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2222 Utilities.createDbSelectionQuery(
2223 LauncherSettings.Favorites._ID, itemsToRemove), null);
2224 if (DEBUG_LOADERS) {
2225 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2226 LauncherSettings.Favorites._ID, itemsToRemove));
2227 }
2228
2229 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07002230 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
2231 .call(contentResolver,
2232 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
2233 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
2234 for (long folderId : deletedFolderIds) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002235 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2236 sBgFolders.remove(folderId);
2237 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002238 }
2239 }
2240
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002241 // Unpin shortcuts that don't exist on the workspace.
2242 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
2243 MutableInt numTimesPinned = sBgPinnedShortcutCounts.get(key);
2244 if (numTimesPinned == null || numTimesPinned.value == 0) {
2245 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
2246 mDeepShortcutManager.unpinShortcut(key);
2247 }
2248 }
2249
Sunny Goyal317698b2015-07-29 11:45:41 -07002250 // Sort all the folder items and make sure the first 3 items are high resolution.
2251 for (FolderInfo folder : sBgFolders) {
2252 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2253 int pos = 0;
2254 for (ShortcutInfo info : folder.contents) {
2255 if (info.usingLowResIcon) {
2256 info.updateIcon(mIconCache, false);
2257 }
2258 pos ++;
2259 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2260 break;
2261 }
2262 }
2263 }
2264
Chris Wrenf4d08112014-01-16 18:13:56 -05002265 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002266 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002267 ContentValues values = new ContentValues();
2268 values.put(LauncherSettings.Favorites.RESTORED, 0);
2269 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2270 Utilities.createDbSelectionQuery(
2271 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002272 }
2273
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002274 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2275 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002276 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002277 null, sWorker);
2278 }
2279
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002280 // Remove any empty screens
2281 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002282 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002283 long screenId = item.screenId;
2284 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2285 unusedScreens.contains(screenId)) {
2286 unusedScreens.remove(screenId);
2287 }
2288 }
2289
2290 // If there are any empty screens remove them, and update.
2291 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002292 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002293 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002294 }
2295
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002296 if (DEBUG_LOADERS) {
2297 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2298 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002299 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002300 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002301 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002302
Sunny Goyale2df0622015-04-24 11:27:00 -07002303 for (int i = 0; i < nScreens; i++) {
2304 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002305 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002306 line += " | ";
2307 }
Joe Onorato36115782010-06-17 13:28:48 -04002308 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002309 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002310 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002311 }
Joe Onorato36115782010-06-17 13:28:48 -04002312 }
Sunny Goyale26d1002016-06-20 14:52:14 -07002313 if (LauncherAppState.PROFILE_STARTUP) {
2314 Trace.endSection();
2315 }
Adam Cohene25af792013-06-06 23:08:25 -07002316 }
2317
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002318 /**
2319 * Partially updates the item without any notification. Must be called on the worker thread.
2320 */
2321 private void updateItem(long itemId, ContentValues update) {
2322 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002323 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002324 update,
2325 BaseColumns._ID + "= ?",
2326 new String[]{Long.toString(itemId)});
2327 }
2328
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002329 /** Filters the set of items who are directly or indirectly (via another container) on the
2330 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002331 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002332 ArrayList<ItemInfo> allWorkspaceItems,
2333 ArrayList<ItemInfo> currentScreenItems,
2334 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002335 // Purge any null ItemInfos
2336 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2337 while (iter.hasNext()) {
2338 ItemInfo i = iter.next();
2339 if (i == null) {
2340 iter.remove();
2341 }
2342 }
2343
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002344 // Order the set of items by their containers first, this allows use to walk through the
2345 // list sequentially, build up a list of containers that are in the specified screen,
2346 // as well as all items in those containers.
2347 Set<Long> itemsOnScreen = new HashSet<Long>();
2348 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2349 @Override
2350 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07002351 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002352 }
2353 });
2354 for (ItemInfo info : allWorkspaceItems) {
2355 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002356 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002357 currentScreenItems.add(info);
2358 itemsOnScreen.add(info.id);
2359 } else {
2360 otherScreenItems.add(info);
2361 }
2362 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2363 currentScreenItems.add(info);
2364 itemsOnScreen.add(info.id);
2365 } else {
2366 if (itemsOnScreen.contains(info.container)) {
2367 currentScreenItems.add(info);
2368 itemsOnScreen.add(info.id);
2369 } else {
2370 otherScreenItems.add(info);
2371 }
2372 }
2373 }
2374 }
2375
2376 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002377 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002378 ArrayList<LauncherAppWidgetInfo> appWidgets,
2379 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2380 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002381
2382 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002383 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002384 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002385 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002386 currentScreenWidgets.add(widget);
2387 } else {
2388 otherScreenWidgets.add(widget);
2389 }
2390 }
2391 }
2392
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002393 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2394 * right) */
2395 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002396 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002397 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002398 final int screenCols = profile.numColumns;
2399 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002400 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002401 @Override
2402 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002403 if (lhs.container == rhs.container) {
2404 // Within containers, order by their spatial position in that container
2405 switch ((int) lhs.container) {
2406 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2407 long lr = (lhs.screenId * screenCellCount +
2408 lhs.cellY * screenCols + lhs.cellX);
2409 long rr = (rhs.screenId * screenCellCount +
2410 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07002411 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07002412 }
2413 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2414 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07002415 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07002416 }
2417 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002418 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002419 throw new RuntimeException("Unexpected container type when " +
2420 "sorting workspace items.");
2421 }
2422 return 0;
2423 }
2424 } else {
2425 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07002426 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07002427 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002428 }
2429 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002430 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002431
Adam Cohendcd297f2013-06-18 13:13:40 -07002432 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2433 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002434 final Runnable r = new Runnable() {
2435 @Override
2436 public void run() {
2437 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2438 if (callbacks != null) {
2439 callbacks.bindScreens(orderedScreens);
2440 }
2441 }
2442 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002443 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002444 }
2445
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002446 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2447 final ArrayList<ItemInfo> workspaceItems,
2448 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002449 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002450
2451 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002452 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002453 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002454 final int start = i;
2455 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002456 final Runnable r = new Runnable() {
2457 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002458 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002459 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002460 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002461 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2462 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002463 }
2464 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002465 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002466 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002467 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002468
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002469 // Bind the widgets, one at a time
2470 N = appWidgets.size();
2471 for (int i = 0; i < N; i++) {
2472 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2473 final Runnable r = new Runnable() {
2474 public void run() {
2475 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2476 if (callbacks != null) {
2477 callbacks.bindAppWidget(widget);
2478 }
2479 }
2480 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002481 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002482 }
2483 }
2484
2485 /**
2486 * Binds all loaded data to actual views on the main thread.
2487 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002488 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002489 final long t = SystemClock.uptimeMillis();
2490 Runnable r;
2491
2492 // Don't use these two variables in any of the callback runnables.
2493 // Otherwise we hold a reference to them.
2494 final Callbacks oldCallbacks = mCallbacks.get();
2495 if (oldCallbacks == null) {
2496 // This launcher has exited and nobody bothered to tell us. Just bail.
2497 Log.w(TAG, "LoaderTask running with no launcher");
2498 return;
2499 }
2500
Winson Chung9b9fb962013-11-15 15:39:34 -08002501 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07002502 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
2503 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
2504 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002505
Winson Chung2abf94d2012-07-18 18:16:38 -07002506 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002507 workspaceItems.addAll(sBgWorkspaceItems);
2508 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002509 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002510 }
2511
Derek Prothro7aff3992013-12-10 14:00:37 -05002512 final boolean isLoadingSynchronously =
2513 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002514 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002515 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002516 if (currScreen >= orderedScreenIds.size()) {
2517 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002518 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002519 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002520 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002521 final long currentScreenId = currentScreen < 0
2522 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002523
Winson Chung9b9fb962013-11-15 15:39:34 -08002524 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07002525 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
2526 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
2527 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
2528 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002529
Winson Chung9b9fb962013-11-15 15:39:34 -08002530 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002531 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002532 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002533 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002534 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2535 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2536
2537 // Tell the workspace that we're about to start binding items
2538 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002539 public void run() {
2540 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2541 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002542 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002543 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002544 }
2545 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002546 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002547 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002548
Adam Cohendcd297f2013-06-18 13:13:40 -07002549 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2550
Sunny Goyal527c7d32015-08-28 15:19:36 -07002551 Executor mainExecutor = new DeferredMainThreadExecutor();
2552 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07002553 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002554
Sunny Goyal527c7d32015-08-28 15:19:36 -07002555 // In case of isLoadingSynchronously, only bind the first screen, and defer binding the
2556 // remaining screens after first onDraw is called. This ensures that the first screen
2557 // is immediately visible (eg. during rotation)
2558 // In case of !isLoadingSynchronously, bind all pages one after other.
2559 final Executor deferredExecutor = isLoadingSynchronously ?
2560 new ViewOnDrawExecutor(mHandler) : mainExecutor;
2561
Sunny Goyal44c06432016-04-02 10:56:02 -07002562 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002563
2564 // Tell the workspace that we're done 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 Goyal66cfdc22015-02-02 13:01:51 -08002569 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002570 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002571
Sunny Goyal639e9062015-08-19 19:17:06 -07002572 mIsLoadingAndBindingWorkspace = false;
2573
2574 // Run all the bind complete runnables after workspace is bound.
2575 if (!mBindCompleteRunnables.isEmpty()) {
2576 synchronized (mBindCompleteRunnables) {
2577 for (final Runnable r : mBindCompleteRunnables) {
2578 runOnWorkerThread(r);
2579 }
2580 mBindCompleteRunnables.clear();
2581 }
2582 }
2583
Winson Chung98e030b2012-05-07 16:01:11 -07002584 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002585 if (DEBUG_LOADERS) {
2586 Log.d(TAG, "bound workspace in "
2587 + (SystemClock.uptimeMillis()-t) + "ms");
2588 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002589
Joe Onorato36115782010-06-17 13:28:48 -04002590 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002591 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002592 deferredExecutor.execute(r);
2593
Winson Chung4a2afa32012-07-19 14:53:05 -07002594 if (isLoadingSynchronously) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002595 r = new Runnable() {
2596 public void run() {
2597 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2598 if (callbacks != null) {
2599 // We are loading synchronously, which means, some of the pages will be
2600 // bound after first draw. Inform the callbacks that page binding is
2601 // not complete, and schedule the remaining pages.
2602 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2603 callbacks.onPageBoundSynchronously(currentScreen);
2604 }
2605 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2606 }
2607 }
2608 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002609 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002610 }
Joe Onorato36115782010-06-17 13:28:48 -04002611 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002612
Joe Onorato36115782010-06-17 13:28:48 -04002613 private void loadAndBindAllApps() {
2614 if (DEBUG_LOADERS) {
2615 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2616 }
2617 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002618 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002619 synchronized (LoaderTask.this) {
2620 if (mStopped) {
2621 return;
2622 }
2623 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002624 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002625 synchronized (LoaderTask.this) {
2626 if (mStopped) {
2627 return;
2628 }
2629 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002630 }
Joe Onorato36115782010-06-17 13:28:48 -04002631 } else {
2632 onlyBindAllApps();
2633 }
2634 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002635
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002636 private void updateIconCache() {
2637 // Ignore packages which have a promise icon.
2638 HashSet<String> packagesToIgnore = new HashSet<>();
2639 synchronized (sBgLock) {
2640 for (ItemInfo info : sBgItemsIdMap) {
2641 if (info instanceof ShortcutInfo) {
2642 ShortcutInfo si = (ShortcutInfo) info;
2643 if (si.isPromise() && si.getTargetComponent() != null) {
2644 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2645 }
2646 } else if (info instanceof LauncherAppWidgetInfo) {
2647 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2648 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2649 packagesToIgnore.add(lawi.providerName.getPackageName());
2650 }
2651 }
2652 }
2653 }
2654 mIconCache.updateDbIcons(packagesToIgnore);
2655 }
2656
Joe Onorato36115782010-06-17 13:28:48 -04002657 private void onlyBindAllApps() {
2658 final Callbacks oldCallbacks = mCallbacks.get();
2659 if (oldCallbacks == null) {
2660 // This launcher has exited and nobody bothered to tell us. Just bail.
2661 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2662 return;
2663 }
2664
2665 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002666 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002667 final ArrayList<AppInfo> list
2668 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002669 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002670 public void run() {
2671 final long t = SystemClock.uptimeMillis();
2672 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2673 if (callbacks != null) {
2674 callbacks.bindAllApplications(list);
2675 }
2676 if (DEBUG_LOADERS) {
2677 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002678 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04002679 }
2680 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002681 };
Tony Wickham80f57872016-06-29 18:12:15 -07002682 runOnMainThread(r);
Joe Onorato36115782010-06-17 13:28:48 -04002683 }
2684
Winson Chung64359a52013-07-08 17:17:08 -07002685 private void loadAllApps() {
2686 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002687
Joe Onorato36115782010-06-17 13:28:48 -04002688 final Callbacks oldCallbacks = mCallbacks.get();
2689 if (oldCallbacks == null) {
2690 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002691 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002692 return;
2693 }
2694
Kenny Guyed131872014-04-30 03:02:21 +01002695 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2696
Winson Chung64359a52013-07-08 17:17:08 -07002697 // Clear the list of apps
2698 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002699 for (UserHandleCompat user : profiles) {
2700 // Query for the set of apps
2701 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002702 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002703 if (DEBUG_LOADERS) {
2704 Log.d(TAG, "getActivityList took "
2705 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2706 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2707 }
2708 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002709 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002710 if (apps == null || apps.isEmpty()) {
2711 return;
2712 }
Kenny Guyff05f432016-01-22 17:48:29 +00002713 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002714 // Create the ApplicationInfos
2715 for (int i = 0; i < apps.size(); i++) {
2716 LauncherActivityInfoCompat app = apps.get(i);
2717 // This builds the icon bitmaps.
Kenny Guyff05f432016-01-22 17:48:29 +00002718 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode));
Kenny Guyed131872014-04-30 03:02:21 +01002719 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002720
Sunny Goyal756a28a2015-04-23 17:07:55 -07002721 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2722 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002723 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002724
2725 @Override
2726 public void run() {
2727 heuristic.processUserApps(apps);
2728 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002729 };
2730 runOnMainThread(new Runnable() {
2731
2732 @Override
2733 public void run() {
2734 // Check isLoadingWorkspace on the UI thread, as it is updated on
2735 // the UI thread.
2736 if (mIsLoadingAndBindingWorkspace) {
2737 synchronized (mBindCompleteRunnables) {
2738 mBindCompleteRunnables.add(r);
2739 }
2740 } else {
2741 runOnWorkerThread(r);
2742 }
2743 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002744 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002745 }
Winson Chung64359a52013-07-08 17:17:08 -07002746 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002747 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002748 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2749 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002750
2751 // Post callback on main thread
2752 mHandler.post(new Runnable() {
2753 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002754
Winson Chung64359a52013-07-08 17:17:08 -07002755 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002756 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002757 if (callbacks != null) {
2758 callbacks.bindAllApplications(added);
2759 if (DEBUG_LOADERS) {
2760 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002761 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07002762 }
2763 } else {
2764 Log.i(TAG, "not binding apps: no Launcher activity");
2765 }
2766 }
2767 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002768 // Cleanup any data stored for a deleted user.
2769 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04002770 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002771 Log.d(TAG, "Icons processed in "
2772 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002773 }
2774 }
2775
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002776 private void loadAndBindDeepShortcuts() {
2777 if (DEBUG_LOADERS) {
2778 Log.d(TAG, "loadAndBindDeepShortcuts mDeepShortcutsLoaded=" + mDeepShortcutsLoaded);
2779 }
2780 if (!mDeepShortcutsLoaded) {
2781 mBgDeepShortcutMap.clear();
2782 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
2783 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager
2784 .queryForAllShortcuts(user);
Sunny Goyal860538d2016-07-20 12:35:16 -07002785 updateDeepShortcutMap(null, user, shortcuts);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002786 }
2787 synchronized (LoaderTask.this) {
2788 if (mStopped) {
2789 return;
2790 }
2791 mDeepShortcutsLoaded = true;
2792 }
2793 }
Tony Wickham80f57872016-06-29 18:12:15 -07002794 bindDeepShortcuts();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002795 }
2796
Joe Onoratobe386092009-11-17 17:32:16 -08002797 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002798 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002799 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002800 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2801 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2802 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2803 }
Joe Onorato36115782010-06-17 13:28:48 -04002804 }
2805 }
2806
Sunny Goyal860538d2016-07-20 12:35:16 -07002807 /**
2808 * Clear all the shortcuts for the given package, and re-add the new shortcuts.
2809 */
2810 private void updateDeepShortcutMap(
2811 String packageName, UserHandleCompat user, List<ShortcutInfoCompat> shortcuts) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002812 if (packageName != null) {
2813 Iterator<ComponentKey> keysIter = mBgDeepShortcutMap.keySet().iterator();
2814 while (keysIter.hasNext()) {
Sunny Goyal860538d2016-07-20 12:35:16 -07002815 ComponentKey next = keysIter.next();
2816 if (next.componentName.getPackageName().equals(packageName)
2817 && next.user.equals(user)) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002818 keysIter.remove();
2819 }
2820 }
2821 }
2822
2823 // Now add the new shortcuts to the map.
2824 for (ShortcutInfoCompat shortcut : shortcuts) {
2825 ComponentKey targetComponent
2826 = new ComponentKey(shortcut.getActivity(), shortcut.getUserHandle());
2827 mBgDeepShortcutMap.addToList(targetComponent, shortcut.getId());
2828 }
2829 }
2830
Tony Wickham80f57872016-06-29 18:12:15 -07002831 public void bindDeepShortcuts() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002832 final MultiHashMap<ComponentKey, String> shortcutMapCopy = new MultiHashMap<>();
2833 shortcutMapCopy.putAll(mBgDeepShortcutMap);
Tony Wickham80f57872016-06-29 18:12:15 -07002834 Runnable r = new Runnable() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002835 @Override
2836 public void run() {
2837 Callbacks callbacks = getCallback();
2838 if (callbacks != null) {
2839 callbacks.bindDeepShortcutMap(shortcutMapCopy);
2840 }
2841 }
Tony Wickham80f57872016-06-29 18:12:15 -07002842 };
2843 runOnMainThread(r);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002844 }
2845
Sunny Goyal75b0f552015-05-20 21:57:06 -07002846 /**
2847 * Called when the icons for packages have been updated in the icon cache.
2848 */
2849 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2850 final Callbacks callbacks = getCallback();
2851 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2852 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2853
2854 // If any package icon has changed (app was updated while launcher was dead),
2855 // update the corresponding shortcuts.
2856 synchronized (sBgLock) {
2857 for (ItemInfo info : sBgItemsIdMap) {
2858 if (info instanceof ShortcutInfo && user.equals(info.user)
2859 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2860 ShortcutInfo si = (ShortcutInfo) info;
2861 ComponentName cn = si.getTargetComponent();
2862 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2863 si.updateIcon(mIconCache);
2864 updatedShortcuts.add(si);
2865 }
2866 }
2867 }
2868 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2869 }
2870
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002871 bindUpdatedShortcuts(updatedShortcuts, user);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002872
2873 if (!updatedApps.isEmpty()) {
2874 mHandler.post(new Runnable() {
2875
2876 public void run() {
2877 Callbacks cb = getCallback();
2878 if (cb != null && callbacks == cb) {
2879 cb.bindAppsUpdated(updatedApps);
2880 }
2881 }
2882 });
2883 }
2884 }
2885
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002886 private void bindUpdatedShortcuts(final ArrayList<ShortcutInfo> updatedShortcuts,
2887 UserHandleCompat user) {
2888 if (!updatedShortcuts.isEmpty()) {
2889 final Callbacks callbacks = getCallback();
2890 final UserHandleCompat userFinal = user;
2891 mHandler.post(new Runnable() {
2892
2893 public void run() {
2894 Callbacks cb = getCallback();
2895 if (cb != null && callbacks == cb) {
2896 cb.bindShortcutsChanged(updatedShortcuts,
2897 new ArrayList<ShortcutInfo>(), userFinal);
2898 }
2899 }
2900 });
2901 }
2902 }
2903
2904 void enqueueItemUpdatedTask(Runnable task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002905 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002906 }
2907
Adam Cohen091440a2015-03-18 14:16:05 -07002908 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002909
2910 @Override
2911 public void onReceive(Context context, Intent intent) {
2912 synchronized (sBgLock) {
2913 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2914 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002915 final PackageManager manager = context.getPackageManager();
2916 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2917 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002918 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2919 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002920 packagesRemoved.clear();
2921 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002922 for (String pkg : entry.getValue()) {
2923 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07002924 if (PackageManagerHelper.isAppOnSdcard(manager, pkg)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002925 packagesUnavailable.add(pkg);
2926 } else {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002927 packagesRemoved.add(pkg);
2928 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002929 }
2930 }
2931 if (!packagesRemoved.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002932 enqueueItemUpdatedTask(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002933 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2934 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002935 if (!packagesUnavailable.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002936 enqueueItemUpdatedTask(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
Sunny Goyal1a745e82014-10-02 15:58:31 -07002937 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2938 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002939 }
Sunny Goyal34942622014-08-29 17:20:55 -07002940 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002941 }
2942 }
2943 }
2944
Joe Onorato36115782010-06-17 13:28:48 -04002945 private class PackageUpdatedTask implements Runnable {
2946 int mOp;
2947 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002948 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002949
2950 public static final int OP_NONE = 0;
2951 public static final int OP_ADD = 1;
2952 public static final int OP_UPDATE = 2;
2953 public static final int OP_REMOVE = 3; // uninstlled
2954 public static final int OP_UNAVAILABLE = 4; // external media unmounted
Kenny Guy44cba692016-01-21 19:50:02 +00002955 public static final int OP_SUSPEND = 5; // package suspended
2956 public static final int OP_UNSUSPEND = 6; // package unsuspended
Sunny Goyalda891c12016-03-18 18:29:24 -07002957 public static final int OP_USER_AVAILABILITY_CHANGE = 7; // user available/unavailable
Joe Onorato36115782010-06-17 13:28:48 -04002958
Kenny Guyed131872014-04-30 03:02:21 +01002959 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002960 mOp = op;
2961 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002962 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002963 }
2964
2965 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002966 if (!mHasLoaderCompletedOnce) {
2967 // Loader has not yet run.
2968 return;
2969 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002970 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002971
2972 final String[] packages = mPackages;
2973 final int N = packages.length;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002974 FlagOp flagOp = FlagOp.NO_OP;
Sunny Goyalda891c12016-03-18 18:29:24 -07002975 StringFilter pkgFilter = StringFilter.of(new HashSet<>(Arrays.asList(packages)));
Joe Onorato36115782010-06-17 13:28:48 -04002976 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002977 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002978 for (int i=0; i<N; i++) {
2979 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002980 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002981 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002982 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002983
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002984 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2985 if (heuristic != null) {
2986 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002987 }
Joe Onorato36115782010-06-17 13:28:48 -04002988 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002989 }
Joe Onorato36115782010-06-17 13:28:48 -04002990 case OP_UPDATE:
2991 for (int i=0; i<N; i++) {
2992 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002993 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002994 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002995 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002996 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002997 // Since package was just updated, the target must be available now.
2998 flagOp = FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04002999 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003000 case OP_REMOVE: {
3001 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3002 if (heuristic != null) {
3003 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003004 }
Joe Onorato36115782010-06-17 13:28:48 -04003005 for (int i=0; i<N; i++) {
3006 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003007 mIconCache.removeIconsForPkg(packages[i], mUser);
3008 }
3009 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003010 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003011 case OP_UNAVAILABLE:
3012 for (int i=0; i<N; i++) {
3013 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3014 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003015 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003016 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003017 flagOp = FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04003018 break;
Kenny Guy44cba692016-01-21 19:50:02 +00003019 case OP_SUSPEND:
3020 case OP_UNSUSPEND:
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003021 flagOp = mOp == OP_SUSPEND ?
3022 FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED) :
3023 FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED);
Sunny Goyal17763cb2016-03-24 13:53:22 -07003024 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.(un)suspend " + N);
Sunny Goyalda891c12016-03-18 18:29:24 -07003025 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
3026 break;
3027 case OP_USER_AVAILABILITY_CHANGE:
3028 flagOp = UserManagerCompat.getInstance(context).isQuietModeEnabled(mUser)
3029 ? FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER)
3030 : FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER);
3031 // We want to update all packages for this user.
3032 pkgFilter = StringFilter.matchesAll();
3033 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
Kenny Guy44cba692016-01-21 19:50:02 +00003034 break;
Joe Onorato36115782010-06-17 13:28:48 -04003035 }
3036
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003037 ArrayList<AppInfo> added = null;
3038 ArrayList<AppInfo> modified = null;
3039 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003040
Adam Cohen487f7dd2012-06-28 18:12:10 -07003041 if (mBgAllAppsList.added.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003042 added = new ArrayList<>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003043 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003044 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003045 if (mBgAllAppsList.modified.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003046 modified = new ArrayList<>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003047 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003048 }
Winson Chung5d55f332012-07-16 20:45:03 -07003049 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003050 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003051 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003052 }
3053
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003054 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps = new HashMap<>();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003055
Joe Onorato36115782010-06-17 13:28:48 -04003056 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003057 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003058 for (AppInfo ai : added) {
3059 addedOrUpdatedApps.put(ai.componentName, ai);
3060 }
Joe Onorato36115782010-06-17 13:28:48 -04003061 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003062
Joe Onorato36115782010-06-17 13:28:48 -04003063 if (modified != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003064 final Callbacks callbacks = getCallback();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003065 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003066 for (AppInfo ai : modified) {
3067 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003068 }
3069
Joe Onorato36115782010-06-17 13:28:48 -04003070 mHandler.post(new Runnable() {
3071 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003072 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003073 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003074 callbacks.bindAppsUpdated(modifiedFinal);
3075 }
3076 }
3077 });
3078 }
Winson Chung83892cc2013-05-01 16:53:33 -07003079
Sunny Goyal4390ace2014-10-13 11:33:11 -07003080 // Update shortcut infos
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003081 if (mOp == OP_ADD || flagOp != FlagOp.NO_OP) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003082 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3083 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3084 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3085
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003086 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003087 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003088 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3089 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003090 boolean infoUpdated = false;
3091 boolean shortcutUpdated = false;
3092
3093 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003094 if ((si.iconResource != null)
Sunny Goyalda891c12016-03-18 18:29:24 -07003095 && pkgFilter.matches(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003096 Bitmap icon = Utilities.createIconBitmap(
3097 si.iconResource.packageName,
3098 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003099 if (icon != null) {
3100 si.setIcon(icon);
3101 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003102 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003103 }
3104 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003105
3106 ComponentName cn = si.getTargetComponent();
Sunny Goyalda891c12016-03-18 18:29:24 -07003107 if (cn != null && pkgFilter.matches(cn.getPackageName())) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003108 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3109
3110 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003111 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3112 // Auto install icon
3113 PackageManager pm = context.getPackageManager();
3114 ResolveInfo matched = pm.resolveActivity(
3115 new Intent(Intent.ACTION_MAIN)
3116 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3117 PackageManager.MATCH_DEFAULT_ONLY);
3118 if (matched == null) {
3119 // Try to find the best match activity.
3120 Intent intent = pm.getLaunchIntentForPackage(
3121 cn.getPackageName());
3122 if (intent != null) {
3123 cn = intent.getComponent();
3124 appInfo = addedOrUpdatedApps.get(cn);
3125 }
3126
3127 if ((intent == null) || (appInfo == null)) {
3128 removedShortcuts.add(si);
3129 continue;
3130 }
3131 si.promisedIntent = intent;
3132 }
3133 }
3134
3135 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003136 if (appInfo != null) {
3137 si.flags = appInfo.flags;
3138 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003139
Sunny Goyal756adbc2015-04-16 15:20:51 -07003140 si.intent = si.promisedIntent;
3141 si.promisedIntent = null;
3142 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003143 infoUpdated = true;
3144 si.updateIcon(mIconCache);
3145 }
3146
3147 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3148 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3149 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003150 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003151 si.contentDescription = appInfo.contentDescription;
3152 infoUpdated = true;
3153 }
3154
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003155 int oldDisabledFlags = si.isDisabled;
3156 si.isDisabled = flagOp.apply(si.isDisabled);
3157 if (si.isDisabled != oldDisabledFlags) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003158 shortcutUpdated = true;
3159 }
3160 }
3161
3162 if (infoUpdated || shortcutUpdated) {
3163 updatedShortcuts.add(si);
3164 }
3165 if (infoUpdated) {
3166 updateItemInDatabase(context, si);
3167 }
Sunny Goyalda891c12016-03-18 18:29:24 -07003168 } else if (info instanceof LauncherAppWidgetInfo && mOp == OP_ADD) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003169 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3170 if (mUser.equals(widgetInfo.user)
3171 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
Sunny Goyalda891c12016-03-18 18:29:24 -07003172 && pkgFilter.matches(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003173 widgetInfo.restoreStatus &=
3174 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3175 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003176
3177 // adding this flag ensures that launcher shows 'click to setup'
3178 // if the widget has a config activity. In case there is no config
3179 // activity, it will be marked as 'restored' during bind.
3180 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3181
Sunny Goyal4390ace2014-10-13 11:33:11 -07003182 widgets.add(widgetInfo);
3183 updateItemInDatabase(context, widgetInfo);
3184 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003185 }
3186 }
3187 }
3188
Sunny Goyal4390ace2014-10-13 11:33:11 -07003189 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003190 final Callbacks callbacks = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003191 mHandler.post(new Runnable() {
3192
3193 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003194 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003195 if (callbacks == cb && cb != null) {
3196 callbacks.bindShortcutsChanged(
3197 updatedShortcuts, removedShortcuts, mUser);
3198 }
3199 }
3200 });
3201 if (!removedShortcuts.isEmpty()) {
3202 deleteItemsFromDatabase(context, removedShortcuts);
3203 }
3204 }
3205 if (!widgets.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003206 final Callbacks callbacks = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003207 mHandler.post(new Runnable() {
3208 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003209 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003210 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003211 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003212 }
3213 }
3214 });
3215 }
3216 }
3217
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003218 final HashSet<String> removedPackages = new HashSet<>();
3219 final HashSet<ComponentName> removedComponents = new HashSet<>();
3220 if (mOp == OP_REMOVE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003221 // Mark all packages in the broadcast to be removed
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003222 Collections.addAll(removedPackages, packages);
3223
3224 // No need to update the removedComponents as
3225 // removedPackages is a super-set of removedComponents
Winson Chungdf95eb12013-10-16 14:57:07 -07003226 } else if (mOp == OP_UPDATE) {
3227 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003228 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003229 if (isPackageDisabled(context, packages[i], mUser)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003230 removedPackages.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003231 }
3232 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003233
3234 // Update removedComponents as some components can get removed during package update
3235 for (AppInfo info : removedApps) {
3236 removedComponents.add(info.componentName);
3237 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003238 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003239
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003240 if (!removedPackages.isEmpty() || !removedComponents.isEmpty()) {
3241 for (String pn : removedPackages) {
3242 deletePackageFromDatabase(context, pn, mUser);
3243 }
3244 for (ComponentName cn : removedComponents) {
3245 deleteItemsFromDatabase(context, getItemInfoForComponentName(cn, mUser));
Sunny Goyal1a745e82014-10-02 15:58:31 -07003246 }
3247
Winson Chungdf95eb12013-10-16 14:57:07 -07003248 // Remove any queued items from the install queue
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003249 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackages, mUser);
3250
Winson Chungdf95eb12013-10-16 14:57:07 -07003251 // Call the components-removed callback
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003252 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003253 mHandler.post(new Runnable() {
3254 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003255 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003256 if (callbacks == cb && cb != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003257 callbacks.bindWorkspaceComponentsRemoved(
3258 removedPackages, removedComponents, mUser);
3259 }
3260 }
3261 });
3262 }
3263
3264 if (!removedApps.isEmpty()) {
3265 // Remove corresponding apps from All-Apps
3266 final Callbacks callbacks = getCallback();
3267 mHandler.post(new Runnable() {
3268 public void run() {
3269 Callbacks cb = getCallback();
3270 if (callbacks == cb && cb != null) {
3271 callbacks.bindAppInfosRemoved(removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04003272 }
3273 }
3274 });
Joe Onoratobe386092009-11-17 17:32:16 -08003275 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003276
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003277 // Notify launcher of widget update. From marshmallow onwards we use AppWidgetHost to
3278 // get widget update signals.
3279 if (!Utilities.ATLEAST_MARSHMALLOW &&
3280 (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003281 final Callbacks callbacks = getCallback();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003282 mHandler.post(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003283 public void run() {
3284 Callbacks cb = getCallback();
3285 if (callbacks == cb && cb != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003286 callbacks.notifyWidgetProvidersChanged();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003287 }
3288 }
3289 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003290 }
3291 }
3292 }
3293
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003294 private class ShortcutsChangedTask implements Runnable {
3295 private String mPackageName;
3296 private List<ShortcutInfoCompat> mShortcuts;
3297 private UserHandleCompat mUser;
3298
3299 public ShortcutsChangedTask(String packageName, List<ShortcutInfoCompat> shortcuts,
3300 UserHandleCompat user) {
3301 mPackageName = packageName;
3302 mShortcuts = shortcuts;
3303 mUser = user;
3304 }
3305
3306 @Override
3307 public void run() {
3308 mDeepShortcutManager.onShortcutsChanged(mShortcuts);
3309
3310 Map<String, ShortcutInfoCompat> idsToShortcuts = new HashMap<>();
3311 for (ShortcutInfoCompat shortcut : mShortcuts) {
3312 idsToShortcuts.put(shortcut.getId(), shortcut);
3313 }
3314
3315 // Find ShortcutInfo's that have changed on the workspace.
3316 MultiHashMap<String, ShortcutInfo> idsToWorkspaceShortcutInfos = new MultiHashMap<>();
3317 for (ItemInfo itemInfo : sBgItemsIdMap) {
3318 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
3319 ShortcutInfo si = (ShortcutInfo) itemInfo;
3320 String shortcutId = si.getDeepShortcutId();
3321 if (idsToShortcuts.containsKey(shortcutId)) {
3322 idsToWorkspaceShortcutInfos.addToList(shortcutId, si);
3323 }
3324 }
3325 }
3326
3327 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3328 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager.queryForFullDetails(
3329 mPackageName, new ArrayList<>(idsToWorkspaceShortcutInfos.keySet()), mUser);
3330 ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3331 Context context = LauncherAppState.getInstance().getContext();
3332 for (ShortcutInfoCompat fullDetails : shortcuts) {
3333 List<ShortcutInfo> shortcutInfos = idsToWorkspaceShortcutInfos
3334 .get(fullDetails.getId());
3335 for (ShortcutInfo shortcutInfo : shortcutInfos) {
Sunny Goyal9994b2b2016-06-23 14:17:24 -07003336 shortcutInfo.updateFromDeepShortcutInfo(fullDetails, context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003337 updatedShortcutInfos.add(shortcutInfo);
3338 }
3339 }
3340 bindUpdatedShortcuts(updatedShortcutInfos, mUser);
3341
3342 // Update the deep shortcut map, in case the list of ids has changed for an activity.
Sunny Goyal860538d2016-07-20 12:35:16 -07003343 updateDeepShortcutMap(mPackageName, mUser, mShortcuts);
Tony Wickham80f57872016-06-29 18:12:15 -07003344 bindDeepShortcuts();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003345 }
3346 }
3347
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003348 private void bindWidgetsModel(final Callbacks callbacks, final WidgetsModel model) {
3349 mHandler.post(new Runnable() {
3350 @Override
3351 public void run() {
3352 Callbacks cb = getCallback();
3353 if (callbacks == cb && cb != null) {
3354 callbacks.bindWidgetsModel(model);
3355 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003356 }
3357 });
3358 }
3359
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003360 public void refreshAndBindWidgetsAndShortcuts(
3361 final Callbacks callbacks, final boolean bindFirst) {
3362 runOnWorkerThread(new Runnable() {
3363 @Override
3364 public void run() {
3365 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
3366 bindWidgetsModel(callbacks, mBgWidgetsModel.clone());
Sunny Goyal31860582015-09-18 08:38:57 -07003367 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003368 final WidgetsModel model = mBgWidgetsModel.updateAndClone(mApp.getContext());
3369 bindWidgetsModel(callbacks, model);
3370 // update the Widget entries inside DB on the worker thread.
3371 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3372 model.getRawList());
Sunny Goyal31860582015-09-18 08:38:57 -07003373 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003374 });
Michael Jurkac402cd92013-05-20 15:49:32 +02003375 }
3376
Adam Cohen091440a2015-03-18 14:16:05 -07003377 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003378 UserHandleCompat user) {
3379 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3380 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003381 }
Adam Cohen556f6132014-01-15 15:18:08 -08003382
Kenny Guyed131872014-04-30 03:02:21 +01003383 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3384 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003385 if (cn == null) {
3386 return false;
3387 }
Kenny Guyed131872014-04-30 03:02:21 +01003388 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3389 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003390 return false;
3391 }
Kenny Guyed131872014-04-30 03:02:21 +01003392 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003393 }
3394
Adam Cohena28b78e2014-05-20 17:03:04 -07003395 public static boolean isValidPackage(Context context, String packageName,
3396 UserHandleCompat user) {
3397 if (packageName == null) {
3398 return false;
3399 }
3400 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3401 return launcherApps.isPackageEnabledForProfile(packageName, user);
3402 }
3403
Joe Onorato9c1289c2009-08-17 11:03:03 -04003404 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003405 * Make an ShortcutInfo object for a restored application or shortcut item that points
3406 * to a package that is not yet installed on the system.
3407 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003408 public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07003409 int promiseType, int itemType, CursorIconInfo iconInfo, Context context) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003410 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003411 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003412
3413 Bitmap icon = iconInfo.loadIcon(c, info, context);
3414 // the fallback icon
3415 if (icon == null) {
3416 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3417 } else {
3418 info.setIcon(icon);
3419 }
Sunny Goyal34942622014-08-29 17:20:55 -07003420
3421 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003422 String title = (c != null) ? c.getString(titleIndex) : null;
Sunny Goyal34942622014-08-29 17:20:55 -07003423 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003424 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003425 }
Sunny Goyal34942622014-08-29 17:20:55 -07003426 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3427 if (TextUtils.isEmpty(info.title)) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003428 info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003429 }
Sunny Goyal34942622014-08-29 17:20:55 -07003430 } else {
3431 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3432 }
3433
Winson Chung82b016c2015-05-08 17:00:10 -07003434 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003435 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003436 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003437 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003438 return info;
3439 }
3440
3441 /**
3442 * Make an Intent object for a restored application or shortcut item that points
3443 * to the market page for the item.
3444 */
Adam Cohen091440a2015-03-18 14:16:05 -07003445 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003446 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003447 return getMarketIntent(componentName.getPackageName());
3448 }
3449
3450 static Intent getMarketIntent(String packageName) {
3451 return new Intent(Intent.ACTION_VIEW)
3452 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003453 .scheme("market")
3454 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003455 .appendQueryParameter("id", packageName)
3456 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003457 }
3458
3459 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003460 * Make an ShortcutInfo object for a shortcut that is an application.
3461 *
3462 * If c is not null, then it will be used to fill in missing data like the title and icon.
3463 */
Sunny Goyald09c3702016-04-06 16:18:20 -07003464 public ShortcutInfo getAppShortcutInfo(Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003465 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003466 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003467 if (user == null) {
3468 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003469 return null;
3470 }
3471
Kenny Guyed131872014-04-30 03:02:21 +01003472 ComponentName componentName = intent.getComponent();
3473 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08003474 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01003475 return null;
3476 }
3477
3478 Intent newIntent = new Intent(intent.getAction(), null);
3479 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3480 newIntent.setComponent(componentName);
3481 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003482 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003483 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3484 return null;
3485 }
3486
3487 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003488 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003489 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3490 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3491 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003492 }
3493
Sunny Goyald09c3702016-04-06 16:18:20 -07003494 if (lai != null && PackageManagerHelper.isAppSuspended(lai.getApplicationInfo())) {
3495 info.isDisabled = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
3496 }
3497
Joe Onorato56d82912010-03-07 14:32:10 -05003498 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003499 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003500 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003501 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003502
Joe Onorato56d82912010-03-07 14:32:10 -05003503 // fall back to the class name of the activity
3504 if (info.title == null) {
3505 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003506 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003507
Joe Onorato9c1289c2009-08-17 11:03:03 -04003508 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003509 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003510 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003511 if (lai != null) {
3512 info.flags = AppInfo.initFlags(lai);
3513 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003514 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003515 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003516
Sunny Goyale2df0622015-04-24 11:27:00 -07003517 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003518 ItemInfoFilter f) {
3519 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3520 for (ItemInfo i : infos) {
3521 if (i instanceof ShortcutInfo) {
3522 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003523 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003524 if (cn != null && f.filterItem(null, info, cn)) {
3525 filtered.add(info);
3526 }
3527 } else if (i instanceof FolderInfo) {
3528 FolderInfo info = (FolderInfo) i;
3529 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003530 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003531 if (cn != null && f.filterItem(info, s, cn)) {
3532 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003533 }
3534 }
Winson Chung64359a52013-07-08 17:17:08 -07003535 } else if (i instanceof LauncherAppWidgetInfo) {
3536 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3537 ComponentName cn = info.providerName;
3538 if (cn != null && f.filterItem(null, info, cn)) {
3539 filtered.add(info);
3540 }
Winson Chung8a435102012-08-30 17:16:53 -07003541 }
3542 }
Winson Chung64359a52013-07-08 17:17:08 -07003543 return new ArrayList<ItemInfo>(filtered);
3544 }
3545
Adam Cohen091440a2015-03-18 14:16:05 -07003546 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003547 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003548 ItemInfoFilter filter = new ItemInfoFilter() {
3549 @Override
3550 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003551 if (info.user == null) {
3552 return cn.equals(cname);
3553 } else {
3554 return cn.equals(cname) && info.user.equals(user);
3555 }
Winson Chung64359a52013-07-08 17:17:08 -07003556 }
3557 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003558 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003559 }
3560
Sunny Goyal1a745e82014-10-02 15:58:31 -07003561 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003562 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003563 */
Adam Cohen091440a2015-03-18 14:16:05 -07003564 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003565 int titleIndex, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003566 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003567 // Non-app shortcuts are only supported for current user.
3568 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003569 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003570
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003571 // TODO: If there's an explicit component and we can't install that, delete it.
3572
Winson Chung82b016c2015-05-08 17:00:10 -07003573 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003574
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003575 Bitmap icon = iconInfo.loadIcon(c, info, context);
3576 // the fallback icon
3577 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003578 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003579 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003580 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003581 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003582 return info;
3583 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003584
Sunny Goyal2350bc92014-10-14 16:42:54 -07003585 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003586 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3587 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3588 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3589
Adam Cohend9198822011-11-22 16:42:47 -08003590 if (intent == null) {
3591 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3592 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3593 return null;
3594 }
3595
Joe Onorato0589f0f2010-02-08 13:44:00 -08003596 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003597 boolean customIcon = false;
3598 ShortcutIconResource iconResource = null;
3599
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003600 if (bitmap instanceof Bitmap) {
3601 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003602 customIcon = true;
3603 } else {
3604 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003605 if (extra instanceof ShortcutIconResource) {
3606 iconResource = (ShortcutIconResource) extra;
3607 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003608 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003609 }
3610 }
3611
Michael Jurkac9d95c52011-08-29 14:03:34 -07003612 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003613
Kenny Guyed131872014-04-30 03:02:21 +01003614 // Only support intents for current user for now. Intents sent from other
3615 // users wouldn't get here without intent forwarding anyway.
3616 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003617 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003618 icon = mIconCache.getDefaultIcon(info.user);
3619 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003620 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003621 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003622
Winson Chung82b016c2015-05-08 17:00:10 -07003623 info.title = Utilities.trim(name);
3624 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003625 info.intent = intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003626 info.iconResource = iconResource;
3627
3628 return info;
3629 }
3630
Joe Onorato9c1289c2009-08-17 11:03:03 -04003631 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003632 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003633 * or make a new one.
3634 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003635 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003636 // See if a placeholder was created for us already
3637 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003638 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003639 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003640 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003641 folders.put(id, folderInfo);
3642 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003643 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003644 }
3645
Joe Onoratobe386092009-11-17 17:32:16 -08003646
Sunny Goyal651077b2014-06-30 14:15:31 -07003647 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3648 return (provider != null) && (provider.provider != null)
3649 && (provider.provider.getPackageName() != null);
3650 }
3651
Joe Onoratobe386092009-11-17 17:32:16 -08003652 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003653 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003654 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3655 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3656 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3657 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003658 if (mLoaderTask != null) {
3659 mLoaderTask.dumpState();
3660 } else {
3661 Log.d(TAG, "mLoaderTask=null");
3662 }
Joe Onoratobe386092009-11-17 17:32:16 -08003663 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003664
3665 public Callbacks getCallback() {
3666 return mCallbacks != null ? mCallbacks.get() : null;
3667 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003668
3669 /**
3670 * @return {@link FolderInfo} if its already loaded.
3671 */
3672 public FolderInfo findFolderById(Long folderId) {
3673 synchronized (sBgLock) {
3674 return sBgFolders.get(folderId);
3675 }
3676 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003677
Sunny Goyal527c7d32015-08-28 15:19:36 -07003678 @Thunk class DeferredMainThreadExecutor implements Executor {
3679
3680 @Override
3681 public void execute(Runnable command) {
3682 runOnMainThread(command);
3683 }
3684 }
3685
Sunny Goyal756adbc2015-04-16 15:20:51 -07003686 /**
3687 * @return the looper for the worker thread which can be used to start background tasks.
3688 */
3689 public static Looper getWorkerLooper() {
3690 return sWorkerThread.getLooper();
3691 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003692}