blob: 39e28c0b0cde83123addd6a695cafdbbb5d35ed6 [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;
Sunny Goyald09c3702016-04-06 16:18:20 -070074import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070075import com.android.launcher3.util.Preconditions;
Sunny Goyalda891c12016-03-18 18:29:24 -070076import com.android.launcher3.util.StringFilter;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070077import com.android.launcher3.util.MultiHashMap;
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);
Tonyacd86ff2016-06-26 22:16:46 -07001945 if (fullDetails != null && !fullDetails.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001946 pinnedShortcut = fullDetails.get(0);
1947 shouldPin = true;
1948 }
1949 }
1950 incrementPinnedShortcutCount(key, shouldPin);
Sunny Goyal9994b2b2016-06-23 14:17:24 -07001951 info = ShortcutInfo.fromDeepShortcutInfo(
1952 pinnedShortcut, context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001953 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001954 info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001955
Sunny Goyald09c3702016-04-06 16:18:20 -07001956 // Shortcuts are only available on the primary profile
1957 if (PackageManagerHelper.isAppSuspended(manager, targetPackage)) {
1958 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1959 }
1960
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001961 // App shortcuts that used to be automatically added to Launcher
1962 // didn't always have the correct intent flags set, so do that
1963 // here
1964 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001965 intent.getCategories() != null &&
1966 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001967 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001968 intent.addFlags(
1969 Intent.FLAG_ACTIVITY_NEW_TASK |
1970 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1971 }
Michael Jurka96879562012-03-22 05:54:33 -07001972 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001973
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001974 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001975 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001976 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001977 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001978 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001979 info.cellX = c.getInt(cellXIndex);
1980 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08001981 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001982 info.spanX = 1;
1983 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01001984 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001985 if (info.promisedIntent != null) {
1986 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
1987 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001988 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001989 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1990 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1991 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001992
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001993 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001994 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001995 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001996 break;
1997 }
1998
Sunny Goyal756adbc2015-04-16 15:20:51 -07001999 if (restored) {
2000 ComponentName cn = info.getTargetComponent();
2001 if (cn != null) {
2002 Integer progress = installingPkgs.get(cn.getPackageName());
2003 if (progress != null) {
2004 info.setInstallProgress(progress);
2005 } else {
2006 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2007 }
2008 }
2009 }
2010
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002011 switch (container) {
2012 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2013 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2014 sBgWorkspaceItems.add(info);
2015 break;
2016 default:
2017 // Item is in a user folder
2018 FolderInfo folderInfo =
2019 findOrMakeFolder(sBgFolders, container);
Sunny Goyalc52ba712016-04-05 15:59:05 -07002020 folderInfo.add(info, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002021 break;
2022 }
2023 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002024 } else {
2025 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002026 }
2027 break;
2028
2029 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2030 id = c.getLong(idIndex);
2031 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2032
Sunny Goyala508e4f2015-05-21 09:33:57 -07002033 // Do not trim the folder label, as is was set by the user.
2034 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002035 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002036 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002037 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002038 folderInfo.cellX = c.getInt(cellXIndex);
2039 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002040 folderInfo.spanX = 1;
2041 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002042 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002043
Daniel Sandler8802e962010-05-26 16:28:16 -04002044 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002045 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002046 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002047 break;
2048 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002049
Joe Onorato9c1289c2009-08-17 11:03:03 -04002050 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002051 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2052 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2053 sBgWorkspaceItems.add(folderInfo);
2054 break;
Joe Onorato36115782010-06-17 13:28:48 -04002055 }
Joe Onorato17a89222011-02-08 17:26:11 -08002056
Chris Wrenf4d08112014-01-16 18:13:56 -05002057 if (restored) {
2058 // no special handling required for restored folders
2059 restoredRows.add(id);
2060 }
2061
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002062 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2063 sBgFolders.put(folderInfo.id, folderInfo);
2064 break;
2065
2066 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002067 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002068 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002069 boolean customWidget = itemType ==
2070 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2071
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002072 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002073 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002074 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002075 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002076 user = allUsers.get(serialNumber);
2077 if (user == null) {
2078 itemsToRemove.add(id);
2079 continue;
2080 }
2081
Sunny Goyalff572272014-07-23 13:58:07 -07002082 final ComponentName component =
2083 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002084
Sunny Goyal651077b2014-06-30 14:15:31 -07002085 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002086 final boolean isIdValid = (restoreStatus &
2087 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002088 final boolean wasProviderReady = (restoreStatus &
2089 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002090
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002091 if (widgetProvidersMap == null) {
2092 widgetProvidersMap = AppWidgetManagerCompat
2093 .getInstance(mContext).getAllProvidersMap();
2094 }
2095 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
2096 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00002097 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002098 user));
Sunny Goyalff572272014-07-23 13:58:07 -07002099
2100 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002101 if (!isSafeMode && !customWidget &&
2102 wasProviderReady && !isProviderReady) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002103 FileLog.d(TAG, "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07002104 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002105 itemsToRemove.add(id);
2106 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002107 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002108 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2109 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002110
Sunny Goyal53f96722015-07-13 19:54:53 -07002111 // The provider is available. So the widget is either
2112 // available or not available. We do not need to track
2113 // any future restore updates.
2114 int status = restoreStatus &
2115 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002116 if (!wasProviderReady) {
2117 // If provider was not previously ready, update the
2118 // status and UI flag.
2119
2120 // Id would be valid only if the widget restore broadcast was received.
2121 if (isIdValid) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002122 status = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002123 } else {
2124 status &= ~LauncherAppWidgetInfo
2125 .FLAG_PROVIDER_NOT_READY;
2126 }
2127 }
2128 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002129 } else {
2130 Log.v(TAG, "Widget restore pending id=" + id
2131 + " appWidgetId=" + appWidgetId
2132 + " status =" + restoreStatus);
2133 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002134 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002135 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002136 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002137
2138 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2139 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002140 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002141 // App restore has started. Update the flag
2142 appWidgetInfo.restoreStatus |=
2143 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002144 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002145 FileLog.d(TAG, "Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07002146 itemsToRemove.add(id);
2147 continue;
2148 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002149
2150 appWidgetInfo.installProgress =
2151 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002152 }
Sunny Goyalff572272014-07-23 13:58:07 -07002153
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002154 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002155 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002156 appWidgetInfo.cellX = c.getInt(cellXIndex);
2157 appWidgetInfo.cellY = c.getInt(cellYIndex);
2158 appWidgetInfo.spanX = c.getInt(spanXIndex);
2159 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002160 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002161
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002162 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2163 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2164 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002165 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2166 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002167 continue;
2168 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002169
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002170 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002171 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002172 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002173 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002174 break;
2175 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002176
Adam Cohen59400422014-03-05 18:07:04 -08002177 if (!customWidget) {
2178 String providerName =
2179 appWidgetInfo.providerName.flattenToString();
2180 if (!providerName.equals(savedProvider) ||
2181 (appWidgetInfo.restoreStatus != restoreStatus)) {
2182 ContentValues values = new ContentValues();
2183 values.put(
2184 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2185 providerName);
2186 values.put(LauncherSettings.Favorites.RESTORED,
2187 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002188 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002189 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002190 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002191 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2192 sBgAppWidgets.add(appWidgetInfo);
2193 }
Joe Onorato36115782010-06-17 13:28:48 -04002194 break;
2195 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002196 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002197 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002198 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002199 }
2200 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002201 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002202 }
2203
Winson Chungba9c37f2013-08-30 14:11:37 -07002204 // Break early if we've stopped loading
2205 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002206 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002207 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002208 }
2209
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002210 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002211 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002212 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2213 Utilities.createDbSelectionQuery(
2214 LauncherSettings.Favorites._ID, itemsToRemove), null);
2215 if (DEBUG_LOADERS) {
2216 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2217 LauncherSettings.Favorites._ID, itemsToRemove));
2218 }
2219
2220 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07002221 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
2222 .call(contentResolver,
2223 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
2224 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
2225 for (long folderId : deletedFolderIds) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002226 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2227 sBgFolders.remove(folderId);
2228 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002229 }
2230 }
2231
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002232 // Unpin shortcuts that don't exist on the workspace.
2233 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
2234 MutableInt numTimesPinned = sBgPinnedShortcutCounts.get(key);
2235 if (numTimesPinned == null || numTimesPinned.value == 0) {
2236 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
2237 mDeepShortcutManager.unpinShortcut(key);
2238 }
2239 }
2240
Sunny Goyal317698b2015-07-29 11:45:41 -07002241 // Sort all the folder items and make sure the first 3 items are high resolution.
2242 for (FolderInfo folder : sBgFolders) {
2243 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2244 int pos = 0;
2245 for (ShortcutInfo info : folder.contents) {
2246 if (info.usingLowResIcon) {
2247 info.updateIcon(mIconCache, false);
2248 }
2249 pos ++;
2250 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2251 break;
2252 }
2253 }
2254 }
2255
Chris Wrenf4d08112014-01-16 18:13:56 -05002256 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002257 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002258 ContentValues values = new ContentValues();
2259 values.put(LauncherSettings.Favorites.RESTORED, 0);
2260 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2261 Utilities.createDbSelectionQuery(
2262 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002263 }
2264
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002265 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2266 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002267 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002268 null, sWorker);
2269 }
2270
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002271 // Remove any empty screens
2272 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002273 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002274 long screenId = item.screenId;
2275 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2276 unusedScreens.contains(screenId)) {
2277 unusedScreens.remove(screenId);
2278 }
2279 }
2280
2281 // If there are any empty screens remove them, and update.
2282 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002283 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002284 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002285 }
2286
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002287 if (DEBUG_LOADERS) {
2288 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2289 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002290 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002291 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002292 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002293
Sunny Goyale2df0622015-04-24 11:27:00 -07002294 for (int i = 0; i < nScreens; i++) {
2295 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002296 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002297 line += " | ";
2298 }
Joe Onorato36115782010-06-17 13:28:48 -04002299 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002300 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002301 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002302 }
Joe Onorato36115782010-06-17 13:28:48 -04002303 }
Sunny Goyale26d1002016-06-20 14:52:14 -07002304 if (LauncherAppState.PROFILE_STARTUP) {
2305 Trace.endSection();
2306 }
Adam Cohene25af792013-06-06 23:08:25 -07002307 }
2308
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002309 /**
2310 * Partially updates the item without any notification. Must be called on the worker thread.
2311 */
2312 private void updateItem(long itemId, ContentValues update) {
2313 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002314 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002315 update,
2316 BaseColumns._ID + "= ?",
2317 new String[]{Long.toString(itemId)});
2318 }
2319
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002320 /** Filters the set of items who are directly or indirectly (via another container) on the
2321 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002322 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002323 ArrayList<ItemInfo> allWorkspaceItems,
2324 ArrayList<ItemInfo> currentScreenItems,
2325 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002326 // Purge any null ItemInfos
2327 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2328 while (iter.hasNext()) {
2329 ItemInfo i = iter.next();
2330 if (i == null) {
2331 iter.remove();
2332 }
2333 }
2334
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002335 // Order the set of items by their containers first, this allows use to walk through the
2336 // list sequentially, build up a list of containers that are in the specified screen,
2337 // as well as all items in those containers.
2338 Set<Long> itemsOnScreen = new HashSet<Long>();
2339 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2340 @Override
2341 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07002342 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002343 }
2344 });
2345 for (ItemInfo info : allWorkspaceItems) {
2346 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002347 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002348 currentScreenItems.add(info);
2349 itemsOnScreen.add(info.id);
2350 } else {
2351 otherScreenItems.add(info);
2352 }
2353 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2354 currentScreenItems.add(info);
2355 itemsOnScreen.add(info.id);
2356 } else {
2357 if (itemsOnScreen.contains(info.container)) {
2358 currentScreenItems.add(info);
2359 itemsOnScreen.add(info.id);
2360 } else {
2361 otherScreenItems.add(info);
2362 }
2363 }
2364 }
2365 }
2366
2367 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002368 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002369 ArrayList<LauncherAppWidgetInfo> appWidgets,
2370 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2371 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002372
2373 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002374 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002375 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002376 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002377 currentScreenWidgets.add(widget);
2378 } else {
2379 otherScreenWidgets.add(widget);
2380 }
2381 }
2382 }
2383
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002384 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2385 * right) */
2386 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002387 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002388 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002389 final int screenCols = profile.numColumns;
2390 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002391 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002392 @Override
2393 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002394 if (lhs.container == rhs.container) {
2395 // Within containers, order by their spatial position in that container
2396 switch ((int) lhs.container) {
2397 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2398 long lr = (lhs.screenId * screenCellCount +
2399 lhs.cellY * screenCols + lhs.cellX);
2400 long rr = (rhs.screenId * screenCellCount +
2401 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07002402 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07002403 }
2404 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2405 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07002406 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07002407 }
2408 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002409 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002410 throw new RuntimeException("Unexpected container type when " +
2411 "sorting workspace items.");
2412 }
2413 return 0;
2414 }
2415 } else {
2416 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07002417 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07002418 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002419 }
2420 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002421 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002422
Adam Cohendcd297f2013-06-18 13:13:40 -07002423 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2424 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002425 final Runnable r = new Runnable() {
2426 @Override
2427 public void run() {
2428 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2429 if (callbacks != null) {
2430 callbacks.bindScreens(orderedScreens);
2431 }
2432 }
2433 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002434 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002435 }
2436
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002437 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2438 final ArrayList<ItemInfo> workspaceItems,
2439 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002440 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002441
2442 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002443 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002444 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002445 final int start = i;
2446 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002447 final Runnable r = new Runnable() {
2448 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002449 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002450 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002451 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002452 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2453 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002454 }
2455 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002456 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002457 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002458 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002459
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002460 // Bind the widgets, one at a time
2461 N = appWidgets.size();
2462 for (int i = 0; i < N; i++) {
2463 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2464 final Runnable r = new Runnable() {
2465 public void run() {
2466 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2467 if (callbacks != null) {
2468 callbacks.bindAppWidget(widget);
2469 }
2470 }
2471 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002472 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002473 }
2474 }
2475
2476 /**
2477 * Binds all loaded data to actual views on the main thread.
2478 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002479 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002480 final long t = SystemClock.uptimeMillis();
2481 Runnable r;
2482
2483 // Don't use these two variables in any of the callback runnables.
2484 // Otherwise we hold a reference to them.
2485 final Callbacks oldCallbacks = mCallbacks.get();
2486 if (oldCallbacks == null) {
2487 // This launcher has exited and nobody bothered to tell us. Just bail.
2488 Log.w(TAG, "LoaderTask running with no launcher");
2489 return;
2490 }
2491
Winson Chung9b9fb962013-11-15 15:39:34 -08002492 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07002493 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
2494 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
2495 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002496
Winson Chung2abf94d2012-07-18 18:16:38 -07002497 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002498 workspaceItems.addAll(sBgWorkspaceItems);
2499 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002500 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002501 }
2502
Derek Prothro7aff3992013-12-10 14:00:37 -05002503 final boolean isLoadingSynchronously =
2504 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002505 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002506 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002507 if (currScreen >= orderedScreenIds.size()) {
2508 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002509 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002510 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002511 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002512 final long currentScreenId = currentScreen < 0
2513 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002514
Winson Chung9b9fb962013-11-15 15:39:34 -08002515 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07002516 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
2517 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
2518 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
2519 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002520
Winson Chung9b9fb962013-11-15 15:39:34 -08002521 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002522 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002523 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002524 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002525 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2526 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2527
2528 // Tell the workspace that we're about to start binding items
2529 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002530 public void run() {
2531 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2532 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002533 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002534 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002535 }
2536 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002537 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002538 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002539
Adam Cohendcd297f2013-06-18 13:13:40 -07002540 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2541
Sunny Goyal527c7d32015-08-28 15:19:36 -07002542 Executor mainExecutor = new DeferredMainThreadExecutor();
2543 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07002544 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002545
Sunny Goyal527c7d32015-08-28 15:19:36 -07002546 // In case of isLoadingSynchronously, only bind the first screen, and defer binding the
2547 // remaining screens after first onDraw is called. This ensures that the first screen
2548 // is immediately visible (eg. during rotation)
2549 // In case of !isLoadingSynchronously, bind all pages one after other.
2550 final Executor deferredExecutor = isLoadingSynchronously ?
2551 new ViewOnDrawExecutor(mHandler) : mainExecutor;
2552
Sunny Goyal44c06432016-04-02 10:56:02 -07002553 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002554
2555 // Tell the workspace that we're done binding items
2556 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002557 public void run() {
2558 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2559 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002560 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002561 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002562
Sunny Goyal639e9062015-08-19 19:17:06 -07002563 mIsLoadingAndBindingWorkspace = false;
2564
2565 // Run all the bind complete runnables after workspace is bound.
2566 if (!mBindCompleteRunnables.isEmpty()) {
2567 synchronized (mBindCompleteRunnables) {
2568 for (final Runnable r : mBindCompleteRunnables) {
2569 runOnWorkerThread(r);
2570 }
2571 mBindCompleteRunnables.clear();
2572 }
2573 }
2574
Winson Chung98e030b2012-05-07 16:01:11 -07002575 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002576 if (DEBUG_LOADERS) {
2577 Log.d(TAG, "bound workspace in "
2578 + (SystemClock.uptimeMillis()-t) + "ms");
2579 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002580
Joe Onorato36115782010-06-17 13:28:48 -04002581 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002582 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002583 deferredExecutor.execute(r);
2584
Winson Chung4a2afa32012-07-19 14:53:05 -07002585 if (isLoadingSynchronously) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002586 r = new Runnable() {
2587 public void run() {
2588 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2589 if (callbacks != null) {
2590 // We are loading synchronously, which means, some of the pages will be
2591 // bound after first draw. Inform the callbacks that page binding is
2592 // not complete, and schedule the remaining pages.
2593 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2594 callbacks.onPageBoundSynchronously(currentScreen);
2595 }
2596 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2597 }
2598 }
2599 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002600 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002601 }
Joe Onorato36115782010-06-17 13:28:48 -04002602 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002603
Joe Onorato36115782010-06-17 13:28:48 -04002604 private void loadAndBindAllApps() {
2605 if (DEBUG_LOADERS) {
2606 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2607 }
2608 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002609 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002610 synchronized (LoaderTask.this) {
2611 if (mStopped) {
2612 return;
2613 }
2614 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002615 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002616 synchronized (LoaderTask.this) {
2617 if (mStopped) {
2618 return;
2619 }
2620 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002621 }
Joe Onorato36115782010-06-17 13:28:48 -04002622 } else {
2623 onlyBindAllApps();
2624 }
2625 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002626
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002627 private void updateIconCache() {
2628 // Ignore packages which have a promise icon.
2629 HashSet<String> packagesToIgnore = new HashSet<>();
2630 synchronized (sBgLock) {
2631 for (ItemInfo info : sBgItemsIdMap) {
2632 if (info instanceof ShortcutInfo) {
2633 ShortcutInfo si = (ShortcutInfo) info;
2634 if (si.isPromise() && si.getTargetComponent() != null) {
2635 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2636 }
2637 } else if (info instanceof LauncherAppWidgetInfo) {
2638 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2639 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2640 packagesToIgnore.add(lawi.providerName.getPackageName());
2641 }
2642 }
2643 }
2644 }
2645 mIconCache.updateDbIcons(packagesToIgnore);
2646 }
2647
Joe Onorato36115782010-06-17 13:28:48 -04002648 private void onlyBindAllApps() {
2649 final Callbacks oldCallbacks = mCallbacks.get();
2650 if (oldCallbacks == null) {
2651 // This launcher has exited and nobody bothered to tell us. Just bail.
2652 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2653 return;
2654 }
2655
2656 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002657 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002658 final ArrayList<AppInfo> list
2659 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002660 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002661 public void run() {
2662 final long t = SystemClock.uptimeMillis();
2663 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2664 if (callbacks != null) {
2665 callbacks.bindAllApplications(list);
2666 }
2667 if (DEBUG_LOADERS) {
2668 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002669 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04002670 }
2671 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002672 };
Tony Wickham80f57872016-06-29 18:12:15 -07002673 runOnMainThread(r);
Joe Onorato36115782010-06-17 13:28:48 -04002674 }
2675
Winson Chung64359a52013-07-08 17:17:08 -07002676 private void loadAllApps() {
2677 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002678
Joe Onorato36115782010-06-17 13:28:48 -04002679 final Callbacks oldCallbacks = mCallbacks.get();
2680 if (oldCallbacks == null) {
2681 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002682 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002683 return;
2684 }
2685
Kenny Guyed131872014-04-30 03:02:21 +01002686 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2687
Winson Chung64359a52013-07-08 17:17:08 -07002688 // Clear the list of apps
2689 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002690 for (UserHandleCompat user : profiles) {
2691 // Query for the set of apps
2692 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002693 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002694 if (DEBUG_LOADERS) {
2695 Log.d(TAG, "getActivityList took "
2696 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2697 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2698 }
2699 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002700 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002701 if (apps == null || apps.isEmpty()) {
2702 return;
2703 }
Kenny Guyff05f432016-01-22 17:48:29 +00002704 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002705 // Create the ApplicationInfos
2706 for (int i = 0; i < apps.size(); i++) {
2707 LauncherActivityInfoCompat app = apps.get(i);
2708 // This builds the icon bitmaps.
Kenny Guyff05f432016-01-22 17:48:29 +00002709 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode));
Kenny Guyed131872014-04-30 03:02:21 +01002710 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002711
Sunny Goyal756a28a2015-04-23 17:07:55 -07002712 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2713 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002714 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002715
2716 @Override
2717 public void run() {
2718 heuristic.processUserApps(apps);
2719 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002720 };
2721 runOnMainThread(new Runnable() {
2722
2723 @Override
2724 public void run() {
2725 // Check isLoadingWorkspace on the UI thread, as it is updated on
2726 // the UI thread.
2727 if (mIsLoadingAndBindingWorkspace) {
2728 synchronized (mBindCompleteRunnables) {
2729 mBindCompleteRunnables.add(r);
2730 }
2731 } else {
2732 runOnWorkerThread(r);
2733 }
2734 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002735 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002736 }
Winson Chung64359a52013-07-08 17:17:08 -07002737 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002738 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002739 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2740 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002741
2742 // Post callback on main thread
2743 mHandler.post(new Runnable() {
2744 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002745
Winson Chung64359a52013-07-08 17:17:08 -07002746 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002747 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002748 if (callbacks != null) {
2749 callbacks.bindAllApplications(added);
2750 if (DEBUG_LOADERS) {
2751 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002752 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07002753 }
2754 } else {
2755 Log.i(TAG, "not binding apps: no Launcher activity");
2756 }
2757 }
2758 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002759 // Cleanup any data stored for a deleted user.
2760 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04002761 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002762 Log.d(TAG, "Icons processed in "
2763 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002764 }
2765 }
2766
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002767 private void loadAndBindDeepShortcuts() {
2768 if (DEBUG_LOADERS) {
2769 Log.d(TAG, "loadAndBindDeepShortcuts mDeepShortcutsLoaded=" + mDeepShortcutsLoaded);
2770 }
2771 if (!mDeepShortcutsLoaded) {
2772 mBgDeepShortcutMap.clear();
2773 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
2774 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager
2775 .queryForAllShortcuts(user);
2776 updateDeepShortcutMap(null, shortcuts);
2777 }
2778 synchronized (LoaderTask.this) {
2779 if (mStopped) {
2780 return;
2781 }
2782 mDeepShortcutsLoaded = true;
2783 }
2784 }
Tony Wickham80f57872016-06-29 18:12:15 -07002785 bindDeepShortcuts();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002786 }
2787
Joe Onoratobe386092009-11-17 17:32:16 -08002788 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002789 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002790 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002791 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2792 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2793 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2794 }
Joe Onorato36115782010-06-17 13:28:48 -04002795 }
2796 }
2797
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002798 // Clear all the shortcuts for the given package, and re-add the new shortcuts.
2799 private void updateDeepShortcutMap(String packageName, List<ShortcutInfoCompat> shortcuts) {
2800 // Remove all keys associated with the given package.
2801 if (packageName != null) {
2802 Iterator<ComponentKey> keysIter = mBgDeepShortcutMap.keySet().iterator();
2803 while (keysIter.hasNext()) {
2804 if (keysIter.next().componentName.getPackageName().equals(packageName)) {
2805 keysIter.remove();
2806 }
2807 }
2808 }
2809
2810 // Now add the new shortcuts to the map.
2811 for (ShortcutInfoCompat shortcut : shortcuts) {
2812 ComponentKey targetComponent
2813 = new ComponentKey(shortcut.getActivity(), shortcut.getUserHandle());
2814 mBgDeepShortcutMap.addToList(targetComponent, shortcut.getId());
2815 }
2816 }
2817
Tony Wickham80f57872016-06-29 18:12:15 -07002818 public void bindDeepShortcuts() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002819 final MultiHashMap<ComponentKey, String> shortcutMapCopy = new MultiHashMap<>();
2820 shortcutMapCopy.putAll(mBgDeepShortcutMap);
Tony Wickham80f57872016-06-29 18:12:15 -07002821 Runnable r = new Runnable() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002822 @Override
2823 public void run() {
2824 Callbacks callbacks = getCallback();
2825 if (callbacks != null) {
2826 callbacks.bindDeepShortcutMap(shortcutMapCopy);
2827 }
2828 }
Tony Wickham80f57872016-06-29 18:12:15 -07002829 };
2830 runOnMainThread(r);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002831 }
2832
Sunny Goyal75b0f552015-05-20 21:57:06 -07002833 /**
2834 * Called when the icons for packages have been updated in the icon cache.
2835 */
2836 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2837 final Callbacks callbacks = getCallback();
2838 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2839 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2840
2841 // If any package icon has changed (app was updated while launcher was dead),
2842 // update the corresponding shortcuts.
2843 synchronized (sBgLock) {
2844 for (ItemInfo info : sBgItemsIdMap) {
2845 if (info instanceof ShortcutInfo && user.equals(info.user)
2846 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2847 ShortcutInfo si = (ShortcutInfo) info;
2848 ComponentName cn = si.getTargetComponent();
2849 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2850 si.updateIcon(mIconCache);
2851 updatedShortcuts.add(si);
2852 }
2853 }
2854 }
2855 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2856 }
2857
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002858 bindUpdatedShortcuts(updatedShortcuts, user);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002859
2860 if (!updatedApps.isEmpty()) {
2861 mHandler.post(new Runnable() {
2862
2863 public void run() {
2864 Callbacks cb = getCallback();
2865 if (cb != null && callbacks == cb) {
2866 cb.bindAppsUpdated(updatedApps);
2867 }
2868 }
2869 });
2870 }
2871 }
2872
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002873 private void bindUpdatedShortcuts(final ArrayList<ShortcutInfo> updatedShortcuts,
2874 UserHandleCompat user) {
2875 if (!updatedShortcuts.isEmpty()) {
2876 final Callbacks callbacks = getCallback();
2877 final UserHandleCompat userFinal = user;
2878 mHandler.post(new Runnable() {
2879
2880 public void run() {
2881 Callbacks cb = getCallback();
2882 if (cb != null && callbacks == cb) {
2883 cb.bindShortcutsChanged(updatedShortcuts,
2884 new ArrayList<ShortcutInfo>(), userFinal);
2885 }
2886 }
2887 });
2888 }
2889 }
2890
2891 void enqueueItemUpdatedTask(Runnable task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002892 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002893 }
2894
Adam Cohen091440a2015-03-18 14:16:05 -07002895 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002896
2897 @Override
2898 public void onReceive(Context context, Intent intent) {
2899 synchronized (sBgLock) {
2900 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2901 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002902 final PackageManager manager = context.getPackageManager();
2903 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2904 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002905 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2906 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002907 packagesRemoved.clear();
2908 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002909 for (String pkg : entry.getValue()) {
2910 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07002911 if (PackageManagerHelper.isAppOnSdcard(manager, pkg)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002912 packagesUnavailable.add(pkg);
2913 } else {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002914 packagesRemoved.add(pkg);
2915 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002916 }
2917 }
2918 if (!packagesRemoved.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002919 enqueueItemUpdatedTask(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002920 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2921 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002922 if (!packagesUnavailable.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002923 enqueueItemUpdatedTask(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
Sunny Goyal1a745e82014-10-02 15:58:31 -07002924 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2925 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002926 }
Sunny Goyal34942622014-08-29 17:20:55 -07002927 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002928 }
2929 }
2930 }
2931
Joe Onorato36115782010-06-17 13:28:48 -04002932 private class PackageUpdatedTask implements Runnable {
2933 int mOp;
2934 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002935 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002936
2937 public static final int OP_NONE = 0;
2938 public static final int OP_ADD = 1;
2939 public static final int OP_UPDATE = 2;
2940 public static final int OP_REMOVE = 3; // uninstlled
2941 public static final int OP_UNAVAILABLE = 4; // external media unmounted
Kenny Guy44cba692016-01-21 19:50:02 +00002942 public static final int OP_SUSPEND = 5; // package suspended
2943 public static final int OP_UNSUSPEND = 6; // package unsuspended
Sunny Goyalda891c12016-03-18 18:29:24 -07002944 public static final int OP_USER_AVAILABILITY_CHANGE = 7; // user available/unavailable
Joe Onorato36115782010-06-17 13:28:48 -04002945
Kenny Guyed131872014-04-30 03:02:21 +01002946 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002947 mOp = op;
2948 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002949 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002950 }
2951
2952 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002953 if (!mHasLoaderCompletedOnce) {
2954 // Loader has not yet run.
2955 return;
2956 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002957 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002958
2959 final String[] packages = mPackages;
2960 final int N = packages.length;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002961 FlagOp flagOp = FlagOp.NO_OP;
Sunny Goyalda891c12016-03-18 18:29:24 -07002962 StringFilter pkgFilter = StringFilter.of(new HashSet<>(Arrays.asList(packages)));
Joe Onorato36115782010-06-17 13:28:48 -04002963 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002964 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002965 for (int i=0; i<N; i++) {
2966 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002967 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002968 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002969 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002970
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002971 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2972 if (heuristic != null) {
2973 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002974 }
Joe Onorato36115782010-06-17 13:28:48 -04002975 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002976 }
Joe Onorato36115782010-06-17 13:28:48 -04002977 case OP_UPDATE:
2978 for (int i=0; i<N; i++) {
2979 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002980 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002981 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002982 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002983 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002984 // Since package was just updated, the target must be available now.
2985 flagOp = FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04002986 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002987 case OP_REMOVE: {
2988 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2989 if (heuristic != null) {
2990 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002991 }
Joe Onorato36115782010-06-17 13:28:48 -04002992 for (int i=0; i<N; i++) {
2993 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002994 mIconCache.removeIconsForPkg(packages[i], mUser);
2995 }
2996 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002997 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002998 case OP_UNAVAILABLE:
2999 for (int i=0; i<N; i++) {
3000 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3001 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003002 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003003 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003004 flagOp = FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04003005 break;
Kenny Guy44cba692016-01-21 19:50:02 +00003006 case OP_SUSPEND:
3007 case OP_UNSUSPEND:
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003008 flagOp = mOp == OP_SUSPEND ?
3009 FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED) :
3010 FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED);
Sunny Goyal17763cb2016-03-24 13:53:22 -07003011 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.(un)suspend " + N);
Sunny Goyalda891c12016-03-18 18:29:24 -07003012 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
3013 break;
3014 case OP_USER_AVAILABILITY_CHANGE:
3015 flagOp = UserManagerCompat.getInstance(context).isQuietModeEnabled(mUser)
3016 ? FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER)
3017 : FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER);
3018 // We want to update all packages for this user.
3019 pkgFilter = StringFilter.matchesAll();
3020 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
Kenny Guy44cba692016-01-21 19:50:02 +00003021 break;
Joe Onorato36115782010-06-17 13:28:48 -04003022 }
3023
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003024 ArrayList<AppInfo> added = null;
3025 ArrayList<AppInfo> modified = null;
3026 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003027
Adam Cohen487f7dd2012-06-28 18:12:10 -07003028 if (mBgAllAppsList.added.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003029 added = new ArrayList<>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003030 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003031 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003032 if (mBgAllAppsList.modified.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003033 modified = new ArrayList<>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003034 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003035 }
Winson Chung5d55f332012-07-16 20:45:03 -07003036 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003037 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003038 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003039 }
3040
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003041 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps = new HashMap<>();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003042
Joe Onorato36115782010-06-17 13:28:48 -04003043 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003044 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003045 for (AppInfo ai : added) {
3046 addedOrUpdatedApps.put(ai.componentName, ai);
3047 }
Joe Onorato36115782010-06-17 13:28:48 -04003048 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003049
Joe Onorato36115782010-06-17 13:28:48 -04003050 if (modified != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003051 final Callbacks callbacks = getCallback();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003052 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003053 for (AppInfo ai : modified) {
3054 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003055 }
3056
Joe Onorato36115782010-06-17 13:28:48 -04003057 mHandler.post(new Runnable() {
3058 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003059 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003060 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003061 callbacks.bindAppsUpdated(modifiedFinal);
3062 }
3063 }
3064 });
3065 }
Winson Chung83892cc2013-05-01 16:53:33 -07003066
Sunny Goyal4390ace2014-10-13 11:33:11 -07003067 // Update shortcut infos
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003068 if (mOp == OP_ADD || flagOp != FlagOp.NO_OP) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003069 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3070 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3071 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3072
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003073 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003074 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003075 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3076 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003077 boolean infoUpdated = false;
3078 boolean shortcutUpdated = false;
3079
3080 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003081 if ((si.iconResource != null)
Sunny Goyalda891c12016-03-18 18:29:24 -07003082 && pkgFilter.matches(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003083 Bitmap icon = Utilities.createIconBitmap(
3084 si.iconResource.packageName,
3085 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003086 if (icon != null) {
3087 si.setIcon(icon);
3088 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003089 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003090 }
3091 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003092
3093 ComponentName cn = si.getTargetComponent();
Sunny Goyalda891c12016-03-18 18:29:24 -07003094 if (cn != null && pkgFilter.matches(cn.getPackageName())) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003095 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3096
3097 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003098 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3099 // Auto install icon
3100 PackageManager pm = context.getPackageManager();
3101 ResolveInfo matched = pm.resolveActivity(
3102 new Intent(Intent.ACTION_MAIN)
3103 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3104 PackageManager.MATCH_DEFAULT_ONLY);
3105 if (matched == null) {
3106 // Try to find the best match activity.
3107 Intent intent = pm.getLaunchIntentForPackage(
3108 cn.getPackageName());
3109 if (intent != null) {
3110 cn = intent.getComponent();
3111 appInfo = addedOrUpdatedApps.get(cn);
3112 }
3113
3114 if ((intent == null) || (appInfo == null)) {
3115 removedShortcuts.add(si);
3116 continue;
3117 }
3118 si.promisedIntent = intent;
3119 }
3120 }
3121
3122 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003123 if (appInfo != null) {
3124 si.flags = appInfo.flags;
3125 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003126
Sunny Goyal756adbc2015-04-16 15:20:51 -07003127 si.intent = si.promisedIntent;
3128 si.promisedIntent = null;
3129 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003130 infoUpdated = true;
3131 si.updateIcon(mIconCache);
3132 }
3133
3134 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3135 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3136 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003137 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003138 si.contentDescription = appInfo.contentDescription;
3139 infoUpdated = true;
3140 }
3141
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003142 int oldDisabledFlags = si.isDisabled;
3143 si.isDisabled = flagOp.apply(si.isDisabled);
3144 if (si.isDisabled != oldDisabledFlags) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003145 shortcutUpdated = true;
3146 }
3147 }
3148
3149 if (infoUpdated || shortcutUpdated) {
3150 updatedShortcuts.add(si);
3151 }
3152 if (infoUpdated) {
3153 updateItemInDatabase(context, si);
3154 }
Sunny Goyalda891c12016-03-18 18:29:24 -07003155 } else if (info instanceof LauncherAppWidgetInfo && mOp == OP_ADD) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003156 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3157 if (mUser.equals(widgetInfo.user)
3158 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
Sunny Goyalda891c12016-03-18 18:29:24 -07003159 && pkgFilter.matches(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003160 widgetInfo.restoreStatus &=
3161 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3162 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003163
3164 // adding this flag ensures that launcher shows 'click to setup'
3165 // if the widget has a config activity. In case there is no config
3166 // activity, it will be marked as 'restored' during bind.
3167 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3168
Sunny Goyal4390ace2014-10-13 11:33:11 -07003169 widgets.add(widgetInfo);
3170 updateItemInDatabase(context, widgetInfo);
3171 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003172 }
3173 }
3174 }
3175
Sunny Goyal4390ace2014-10-13 11:33:11 -07003176 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003177 final Callbacks callbacks = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003178 mHandler.post(new Runnable() {
3179
3180 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003181 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003182 if (callbacks == cb && cb != null) {
3183 callbacks.bindShortcutsChanged(
3184 updatedShortcuts, removedShortcuts, mUser);
3185 }
3186 }
3187 });
3188 if (!removedShortcuts.isEmpty()) {
3189 deleteItemsFromDatabase(context, removedShortcuts);
3190 }
3191 }
3192 if (!widgets.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003193 final Callbacks callbacks = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003194 mHandler.post(new Runnable() {
3195 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003196 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003197 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003198 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003199 }
3200 }
3201 });
3202 }
3203 }
3204
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003205 final HashSet<String> removedPackages = new HashSet<>();
3206 final HashSet<ComponentName> removedComponents = new HashSet<>();
3207 if (mOp == OP_REMOVE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003208 // Mark all packages in the broadcast to be removed
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003209 Collections.addAll(removedPackages, packages);
3210
3211 // No need to update the removedComponents as
3212 // removedPackages is a super-set of removedComponents
Winson Chungdf95eb12013-10-16 14:57:07 -07003213 } else if (mOp == OP_UPDATE) {
3214 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003215 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003216 if (isPackageDisabled(context, packages[i], mUser)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003217 removedPackages.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003218 }
3219 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003220
3221 // Update removedComponents as some components can get removed during package update
3222 for (AppInfo info : removedApps) {
3223 removedComponents.add(info.componentName);
3224 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003225 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003226
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003227 if (!removedPackages.isEmpty() || !removedComponents.isEmpty()) {
3228 for (String pn : removedPackages) {
3229 deletePackageFromDatabase(context, pn, mUser);
3230 }
3231 for (ComponentName cn : removedComponents) {
3232 deleteItemsFromDatabase(context, getItemInfoForComponentName(cn, mUser));
Sunny Goyal1a745e82014-10-02 15:58:31 -07003233 }
3234
Winson Chungdf95eb12013-10-16 14:57:07 -07003235 // Remove any queued items from the install queue
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003236 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackages, mUser);
3237
Winson Chungdf95eb12013-10-16 14:57:07 -07003238 // Call the components-removed callback
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003239 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003240 mHandler.post(new Runnable() {
3241 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003242 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003243 if (callbacks == cb && cb != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003244 callbacks.bindWorkspaceComponentsRemoved(
3245 removedPackages, removedComponents, mUser);
3246 }
3247 }
3248 });
3249 }
3250
3251 if (!removedApps.isEmpty()) {
3252 // Remove corresponding apps from All-Apps
3253 final Callbacks callbacks = getCallback();
3254 mHandler.post(new Runnable() {
3255 public void run() {
3256 Callbacks cb = getCallback();
3257 if (callbacks == cb && cb != null) {
3258 callbacks.bindAppInfosRemoved(removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04003259 }
3260 }
3261 });
Joe Onoratobe386092009-11-17 17:32:16 -08003262 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003263
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003264 // Notify launcher of widget update. From marshmallow onwards we use AppWidgetHost to
3265 // get widget update signals.
3266 if (!Utilities.ATLEAST_MARSHMALLOW &&
3267 (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003268 final Callbacks callbacks = getCallback();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003269 mHandler.post(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003270 public void run() {
3271 Callbacks cb = getCallback();
3272 if (callbacks == cb && cb != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003273 callbacks.notifyWidgetProvidersChanged();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003274 }
3275 }
3276 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003277 }
3278 }
3279 }
3280
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003281 private class ShortcutsChangedTask implements Runnable {
3282 private String mPackageName;
3283 private List<ShortcutInfoCompat> mShortcuts;
3284 private UserHandleCompat mUser;
3285
3286 public ShortcutsChangedTask(String packageName, List<ShortcutInfoCompat> shortcuts,
3287 UserHandleCompat user) {
3288 mPackageName = packageName;
3289 mShortcuts = shortcuts;
3290 mUser = user;
3291 }
3292
3293 @Override
3294 public void run() {
3295 mDeepShortcutManager.onShortcutsChanged(mShortcuts);
3296
3297 Map<String, ShortcutInfoCompat> idsToShortcuts = new HashMap<>();
3298 for (ShortcutInfoCompat shortcut : mShortcuts) {
3299 idsToShortcuts.put(shortcut.getId(), shortcut);
3300 }
3301
3302 // Find ShortcutInfo's that have changed on the workspace.
3303 MultiHashMap<String, ShortcutInfo> idsToWorkspaceShortcutInfos = new MultiHashMap<>();
3304 for (ItemInfo itemInfo : sBgItemsIdMap) {
3305 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
3306 ShortcutInfo si = (ShortcutInfo) itemInfo;
3307 String shortcutId = si.getDeepShortcutId();
3308 if (idsToShortcuts.containsKey(shortcutId)) {
3309 idsToWorkspaceShortcutInfos.addToList(shortcutId, si);
3310 }
3311 }
3312 }
3313
3314 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3315 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager.queryForFullDetails(
3316 mPackageName, new ArrayList<>(idsToWorkspaceShortcutInfos.keySet()), mUser);
3317 ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3318 Context context = LauncherAppState.getInstance().getContext();
3319 for (ShortcutInfoCompat fullDetails : shortcuts) {
3320 List<ShortcutInfo> shortcutInfos = idsToWorkspaceShortcutInfos
3321 .get(fullDetails.getId());
3322 for (ShortcutInfo shortcutInfo : shortcutInfos) {
Sunny Goyal9994b2b2016-06-23 14:17:24 -07003323 shortcutInfo.updateFromDeepShortcutInfo(fullDetails, context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003324 updatedShortcutInfos.add(shortcutInfo);
3325 }
3326 }
3327 bindUpdatedShortcuts(updatedShortcutInfos, mUser);
3328
3329 // Update the deep shortcut map, in case the list of ids has changed for an activity.
3330 updateDeepShortcutMap(mPackageName, mShortcuts);
Tony Wickham80f57872016-06-29 18:12:15 -07003331 bindDeepShortcuts();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003332 }
3333 }
3334
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003335 private void bindWidgetsModel(final Callbacks callbacks, final WidgetsModel model) {
3336 mHandler.post(new Runnable() {
3337 @Override
3338 public void run() {
3339 Callbacks cb = getCallback();
3340 if (callbacks == cb && cb != null) {
3341 callbacks.bindWidgetsModel(model);
3342 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003343 }
3344 });
3345 }
3346
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003347 public void refreshAndBindWidgetsAndShortcuts(
3348 final Callbacks callbacks, final boolean bindFirst) {
3349 runOnWorkerThread(new Runnable() {
3350 @Override
3351 public void run() {
3352 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
3353 bindWidgetsModel(callbacks, mBgWidgetsModel.clone());
Sunny Goyal31860582015-09-18 08:38:57 -07003354 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003355 final WidgetsModel model = mBgWidgetsModel.updateAndClone(mApp.getContext());
3356 bindWidgetsModel(callbacks, model);
3357 // update the Widget entries inside DB on the worker thread.
3358 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3359 model.getRawList());
Sunny Goyal31860582015-09-18 08:38:57 -07003360 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003361 });
Michael Jurkac402cd92013-05-20 15:49:32 +02003362 }
3363
Adam Cohen091440a2015-03-18 14:16:05 -07003364 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003365 UserHandleCompat user) {
3366 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3367 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003368 }
Adam Cohen556f6132014-01-15 15:18:08 -08003369
Kenny Guyed131872014-04-30 03:02:21 +01003370 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3371 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003372 if (cn == null) {
3373 return false;
3374 }
Kenny Guyed131872014-04-30 03:02:21 +01003375 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3376 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003377 return false;
3378 }
Kenny Guyed131872014-04-30 03:02:21 +01003379 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003380 }
3381
Adam Cohena28b78e2014-05-20 17:03:04 -07003382 public static boolean isValidPackage(Context context, String packageName,
3383 UserHandleCompat user) {
3384 if (packageName == null) {
3385 return false;
3386 }
3387 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3388 return launcherApps.isPackageEnabledForProfile(packageName, user);
3389 }
3390
Joe Onorato9c1289c2009-08-17 11:03:03 -04003391 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003392 * Make an ShortcutInfo object for a restored application or shortcut item that points
3393 * to a package that is not yet installed on the system.
3394 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003395 public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07003396 int promiseType, int itemType, CursorIconInfo iconInfo, Context context) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003397 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003398 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003399
3400 Bitmap icon = iconInfo.loadIcon(c, info, context);
3401 // the fallback icon
3402 if (icon == null) {
3403 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3404 } else {
3405 info.setIcon(icon);
3406 }
Sunny Goyal34942622014-08-29 17:20:55 -07003407
3408 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003409 String title = (c != null) ? c.getString(titleIndex) : null;
Sunny Goyal34942622014-08-29 17:20:55 -07003410 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003411 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003412 }
Sunny Goyal34942622014-08-29 17:20:55 -07003413 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3414 if (TextUtils.isEmpty(info.title)) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003415 info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003416 }
Sunny Goyal34942622014-08-29 17:20:55 -07003417 } else {
3418 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3419 }
3420
Winson Chung82b016c2015-05-08 17:00:10 -07003421 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003422 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003423 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003424 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003425 return info;
3426 }
3427
3428 /**
3429 * Make an Intent object for a restored application or shortcut item that points
3430 * to the market page for the item.
3431 */
Adam Cohen091440a2015-03-18 14:16:05 -07003432 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003433 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003434 return getMarketIntent(componentName.getPackageName());
3435 }
3436
3437 static Intent getMarketIntent(String packageName) {
3438 return new Intent(Intent.ACTION_VIEW)
3439 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003440 .scheme("market")
3441 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003442 .appendQueryParameter("id", packageName)
3443 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003444 }
3445
3446 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003447 * Make an ShortcutInfo object for a shortcut that is an application.
3448 *
3449 * If c is not null, then it will be used to fill in missing data like the title and icon.
3450 */
Sunny Goyald09c3702016-04-06 16:18:20 -07003451 public ShortcutInfo getAppShortcutInfo(Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003452 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003453 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003454 if (user == null) {
3455 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003456 return null;
3457 }
3458
Kenny Guyed131872014-04-30 03:02:21 +01003459 ComponentName componentName = intent.getComponent();
3460 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08003461 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01003462 return null;
3463 }
3464
3465 Intent newIntent = new Intent(intent.getAction(), null);
3466 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3467 newIntent.setComponent(componentName);
3468 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003469 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003470 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3471 return null;
3472 }
3473
3474 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003475 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003476 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3477 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3478 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003479 }
3480
Sunny Goyald09c3702016-04-06 16:18:20 -07003481 if (lai != null && PackageManagerHelper.isAppSuspended(lai.getApplicationInfo())) {
3482 info.isDisabled = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
3483 }
3484
Joe Onorato56d82912010-03-07 14:32:10 -05003485 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003486 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003487 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003488 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003489
Joe Onorato56d82912010-03-07 14:32:10 -05003490 // fall back to the class name of the activity
3491 if (info.title == null) {
3492 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003493 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003494
Joe Onorato9c1289c2009-08-17 11:03:03 -04003495 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003496 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003497 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003498 if (lai != null) {
3499 info.flags = AppInfo.initFlags(lai);
3500 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003501 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003502 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003503
Sunny Goyale2df0622015-04-24 11:27:00 -07003504 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003505 ItemInfoFilter f) {
3506 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3507 for (ItemInfo i : infos) {
3508 if (i instanceof ShortcutInfo) {
3509 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003510 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003511 if (cn != null && f.filterItem(null, info, cn)) {
3512 filtered.add(info);
3513 }
3514 } else if (i instanceof FolderInfo) {
3515 FolderInfo info = (FolderInfo) i;
3516 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003517 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003518 if (cn != null && f.filterItem(info, s, cn)) {
3519 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003520 }
3521 }
Winson Chung64359a52013-07-08 17:17:08 -07003522 } else if (i instanceof LauncherAppWidgetInfo) {
3523 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3524 ComponentName cn = info.providerName;
3525 if (cn != null && f.filterItem(null, info, cn)) {
3526 filtered.add(info);
3527 }
Winson Chung8a435102012-08-30 17:16:53 -07003528 }
3529 }
Winson Chung64359a52013-07-08 17:17:08 -07003530 return new ArrayList<ItemInfo>(filtered);
3531 }
3532
Adam Cohen091440a2015-03-18 14:16:05 -07003533 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003534 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003535 ItemInfoFilter filter = new ItemInfoFilter() {
3536 @Override
3537 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003538 if (info.user == null) {
3539 return cn.equals(cname);
3540 } else {
3541 return cn.equals(cname) && info.user.equals(user);
3542 }
Winson Chung64359a52013-07-08 17:17:08 -07003543 }
3544 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003545 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003546 }
3547
Sunny Goyal1a745e82014-10-02 15:58:31 -07003548 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003549 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003550 */
Adam Cohen091440a2015-03-18 14:16:05 -07003551 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003552 int titleIndex, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003553 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003554 // Non-app shortcuts are only supported for current user.
3555 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003556 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003557
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003558 // TODO: If there's an explicit component and we can't install that, delete it.
3559
Winson Chung82b016c2015-05-08 17:00:10 -07003560 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003561
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003562 Bitmap icon = iconInfo.loadIcon(c, info, context);
3563 // the fallback icon
3564 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003565 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003566 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003567 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003568 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003569 return info;
3570 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003571
Sunny Goyal2350bc92014-10-14 16:42:54 -07003572 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003573 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3574 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3575 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3576
Adam Cohend9198822011-11-22 16:42:47 -08003577 if (intent == null) {
3578 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3579 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3580 return null;
3581 }
3582
Joe Onorato0589f0f2010-02-08 13:44:00 -08003583 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003584 boolean customIcon = false;
3585 ShortcutIconResource iconResource = null;
3586
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003587 if (bitmap instanceof Bitmap) {
3588 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003589 customIcon = true;
3590 } else {
3591 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003592 if (extra instanceof ShortcutIconResource) {
3593 iconResource = (ShortcutIconResource) extra;
3594 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003595 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003596 }
3597 }
3598
Michael Jurkac9d95c52011-08-29 14:03:34 -07003599 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003600
Kenny Guyed131872014-04-30 03:02:21 +01003601 // Only support intents for current user for now. Intents sent from other
3602 // users wouldn't get here without intent forwarding anyway.
3603 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003604 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003605 icon = mIconCache.getDefaultIcon(info.user);
3606 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003607 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003608 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003609
Winson Chung82b016c2015-05-08 17:00:10 -07003610 info.title = Utilities.trim(name);
3611 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003612 info.intent = intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003613 info.iconResource = iconResource;
3614
3615 return info;
3616 }
3617
Joe Onorato9c1289c2009-08-17 11:03:03 -04003618 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003619 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003620 * or make a new one.
3621 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003622 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003623 // See if a placeholder was created for us already
3624 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003625 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003626 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003627 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003628 folders.put(id, folderInfo);
3629 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003630 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003631 }
3632
Joe Onoratobe386092009-11-17 17:32:16 -08003633
Sunny Goyal651077b2014-06-30 14:15:31 -07003634 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3635 return (provider != null) && (provider.provider != null)
3636 && (provider.provider.getPackageName() != null);
3637 }
3638
Joe Onoratobe386092009-11-17 17:32:16 -08003639 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003640 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003641 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3642 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3643 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3644 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003645 if (mLoaderTask != null) {
3646 mLoaderTask.dumpState();
3647 } else {
3648 Log.d(TAG, "mLoaderTask=null");
3649 }
Joe Onoratobe386092009-11-17 17:32:16 -08003650 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003651
3652 public Callbacks getCallback() {
3653 return mCallbacks != null ? mCallbacks.get() : null;
3654 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003655
3656 /**
3657 * @return {@link FolderInfo} if its already loaded.
3658 */
3659 public FolderInfo findFolderById(Long folderId) {
3660 synchronized (sBgLock) {
3661 return sBgFolders.get(folderId);
3662 }
3663 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003664
Sunny Goyal527c7d32015-08-28 15:19:36 -07003665 @Thunk class DeferredMainThreadExecutor implements Executor {
3666
3667 @Override
3668 public void execute(Runnable command) {
3669 runOnMainThread(command);
3670 }
3671 }
3672
Sunny Goyal756adbc2015-04-16 15:20:51 -07003673 /**
3674 * @return the looper for the worker thread which can be used to start background tasks.
3675 */
3676 public static Looper getWorkerLooper() {
3677 return sWorkerThread.getLooper();
3678 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003679}