blob: 9e876600582eabc2d95db30c73d03786e89d9fd1 [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;
Chris Wrenc3919c02013-09-18 09:48:33 -040040import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080041import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070042import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080043import android.util.LongSparseArray;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070044import android.util.MutableInt;
Winson Chungc9168342013-06-26 14:54:55 -070045import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010046
Sunny Goyalffe83f12014-08-14 17:39:34 -070047import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010048import com.android.launcher3.compat.LauncherActivityInfoCompat;
49import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070050import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070051import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010052import com.android.launcher3.compat.UserHandleCompat;
53import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070054import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -070055import com.android.launcher3.config.ProviderConfig;
Tony Wickham827cef22016-03-17 15:39:39 -070056import com.android.launcher3.dynamicui.ExtractionUtils;
Sunny Goyal26119432016-02-18 22:09:23 +000057import com.android.launcher3.folder.Folder;
58import com.android.launcher3.folder.FolderIcon;
Sunny Goyal1acb9e92016-05-16 12:41:09 -070059import com.android.launcher3.logging.FileLog;
Sunny Goyalf862a262015-12-14 14:27:38 -080060import com.android.launcher3.model.GridSizeMigrationTask;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070061import com.android.launcher3.model.WidgetsModel;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070062import com.android.launcher3.provider.LauncherDbUtils;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070063import com.android.launcher3.shortcuts.DeepShortcutManager;
64import com.android.launcher3.shortcuts.ShortcutInfoCompat;
65import com.android.launcher3.shortcuts.ShortcutKey;
Robin Lee26ace122015-03-16 19:41:43 +000066import com.android.launcher3.util.ComponentKey;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070067import com.android.launcher3.util.CursorIconInfo;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -070068import com.android.launcher3.util.FlagOp;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070069import com.android.launcher3.util.GridOccupancy;
Sunny Goyale2df0622015-04-24 11:27:00 -070070import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070071import com.android.launcher3.util.ManagedProfileHeuristic;
Sunny Goyald09c3702016-04-06 16:18:20 -070072import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070073import com.android.launcher3.util.Preconditions;
Sunny Goyalda891c12016-03-18 18:29:24 -070074import com.android.launcher3.util.StringFilter;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070075import com.android.launcher3.util.MultiHashMap;
Adam Cohen091440a2015-03-18 14:16:05 -070076import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070077import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080078
Michael Jurkac2f801e2011-07-12 14:19:46 -070079import java.lang.ref.WeakReference;
80import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070081import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070082import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070083import java.util.Arrays;
Michael Jurkac2f801e2011-07-12 14:19:46 -070084import java.util.Collections;
85import java.util.Comparator;
86import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070087import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070088import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070089import java.util.List;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070090import java.util.Map;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070091import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070092import java.util.Set;
Sunny Goyal527c7d32015-08-28 15:19:36 -070093import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -070094
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095/**
96 * Maintains in-memory state of the Launcher. It is expected that there should be only one
97 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070098 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080099 */
Kenny Guyed131872014-04-30 03:02:21 +0100100public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +0100101 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800102 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -0400103 private static final boolean DEBUG_RECEIVER = false;
Sunny Goyal94485362014-09-18 16:13:58 -0700104 private static final boolean REMOVE_UNRESTORED_ICONS = true;
Chris Wrenb358f812014-04-16 13:37:00 -0400105
Joe Onorato9c1289c2009-08-17 11:03:03 -0400106 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700107
Joe Onorato36115782010-06-17 13:28:48 -0400108 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500109 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800110
Adam Cohen091440a2015-03-18 14:16:05 -0700111 @Thunk final LauncherAppState mApp;
112 @Thunk final Object mLock = new Object();
113 @Thunk DeferredHandler mHandler = new DeferredHandler();
114 @Thunk LoaderTask mLoaderTask;
115 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700116 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117
Adam Cohen091440a2015-03-18 14:16:05 -0700118 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700119 static {
120 sWorkerThread.start();
121 }
Adam Cohen091440a2015-03-18 14:16:05 -0700122 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700123
Joe Onoratocc67f472010-06-08 10:54:30 -0700124 // We start off with everything not loaded. After that, we assume that
125 // our monitoring of the package manager provides all updates and we never
126 // need to do a requery. These are only ever touched from the loader thread.
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700127 private boolean mWorkspaceLoaded;
128 private boolean mAllAppsLoaded;
129 private boolean mDeepShortcutsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700130
Sunny Goyal756a28a2015-04-23 17:07:55 -0700131 /**
132 * Set of runnables to be called on the background thread after the workspace binding
133 * is complete.
134 */
135 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
136
Adam Cohen091440a2015-03-18 14:16:05 -0700137 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700139 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800140 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700141 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800142 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800143
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700144 // Maps all launcher activities to the id's of their shortcuts (if they have any).
145 private final MultiHashMap<ComponentKey, String> mBgDeepShortcutMap = new MultiHashMap<>();
146
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700147 // The lock that must be acquired before referencing any static bg data structures. Unlike
148 // other locks, this one can generally be held long-term because we never expect any of these
149 // static data structures to be referenced outside of the worker thread except on the first
150 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700151 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700152
Adam Cohen487f7dd2012-06-28 18:12:10 -0700153 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700154 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700155 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700156
Adam Cohen487f7dd2012-06-28 18:12:10 -0700157 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
158 // created by LauncherModel that are directly on the home screen (however, no widgets or
159 // shortcuts within folders).
160 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700161
Adam Cohen487f7dd2012-06-28 18:12:10 -0700162 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
163 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700164 new ArrayList<LauncherAppWidgetInfo>();
165
Adam Cohen487f7dd2012-06-28 18:12:10 -0700166 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700167 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700168
Adam Cohendcd297f2013-06-18 13:13:40 -0700169 // sBgWorkspaceScreens is the ordered set of workspace screens.
170 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
171
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700172 // sBgPinnedShortcutCounts is the ComponentKey representing a pinned shortcut to the number of
173 // times it is pinned.
174 static final Map<ShortcutKey, MutableInt> sBgPinnedShortcutCounts = new HashMap<>();
175
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700176 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700177 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
178 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700179
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700180 // </ only access in worker thread >
181
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700182 private IconCache mIconCache;
183 private DeepShortcutManager mDeepShortcutManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700185 private final LauncherAppsCompat mLauncherApps;
186 private final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100187
Joe Onorato9c1289c2009-08-17 11:03:03 -0400188 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700189 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400190 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700191 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400192 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700193 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
194 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700195 public void bindScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800196 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400197 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200198 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700199 public void bindAppsAdded(ArrayList<Long> newScreens,
200 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700201 ArrayList<ItemInfo> addAnimated,
202 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200203 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700204 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
205 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
206 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700207 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700208 public void bindWorkspaceComponentsRemoved(
209 HashSet<String> packageNames, HashSet<ComponentName> components,
210 UserHandleCompat user);
211 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800212 public void notifyWidgetProvidersChanged();
213 public void bindWidgetsModel(WidgetsModel model);
Adam Cohen1462de32012-07-24 22:34:36 -0700214 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700215 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700216 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMap);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400217 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218
Winson Chung64359a52013-07-08 17:17:08 -0700219 public interface ItemInfoFilter {
220 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
221 }
222
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700223 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter,
224 DeepShortcutManager deepShortcutManager) {
Winson Chunga6945242014-01-08 14:04:34 -0800225 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400226 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100227 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700228 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800229 mIconCache = iconCache;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700230 mDeepShortcutManager = deepShortcutManager;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800231
Kenny Guyed131872014-04-30 03:02:21 +0100232 mLauncherApps = LauncherAppsCompat.getInstance(context);
233 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800234 }
235
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700236 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
237 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700238 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700239 if (sWorkerThread.getThreadId() == Process.myTid()) {
240 // If we are on the worker thread, post onto the main handler
241 mHandler.post(r);
242 } else {
243 r.run();
244 }
245 }
246
247 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
248 * posted on the worker thread handler. */
Sunny Goyal639e9062015-08-19 19:17:06 -0700249 @Thunk static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700250 if (sWorkerThread.getThreadId() == Process.myTid()) {
251 r.run();
252 } else {
253 // If we are not on the worker thread, then post to the worker handler
254 sWorker.post(r);
255 }
256 }
257
Sunny Goyal756adbc2015-04-16 15:20:51 -0700258 public void setPackageState(final PackageInstallInfo installInfo) {
259 Runnable updateRunnable = new Runnable() {
260
261 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500262 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700263 synchronized (sBgLock) {
264 final HashSet<ItemInfo> updates = new HashSet<>();
265
266 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
267 // Ignore install success events as they are handled by Package add events.
268 return;
269 }
270
Sunny Goyale2df0622015-04-24 11:27:00 -0700271 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700272 if (info instanceof ShortcutInfo) {
273 ShortcutInfo si = (ShortcutInfo) info;
274 ComponentName cn = si.getTargetComponent();
275 if (si.isPromise() && (cn != null)
276 && installInfo.packageName.equals(cn.getPackageName())) {
277 si.setInstallProgress(installInfo.progress);
278
279 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
280 // Mark this info as broken.
281 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
282 }
283 updates.add(si);
284 }
285 }
286 }
287
288 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
289 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
290 widget.installProgress = installInfo.progress;
291 updates.add(widget);
292 }
293 }
294
295 if (!updates.isEmpty()) {
296 // Push changes to the callback.
297 Runnable r = new Runnable() {
298 public void run() {
299 Callbacks callbacks = getCallback();
300 if (callbacks != null) {
301 callbacks.bindRestoreItemsChange(updates);
302 }
303 }
304 };
305 mHandler.post(r);
306 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500307 }
308 }
309 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700310 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500311 }
312
Sunny Goyal756adbc2015-04-16 15:20:51 -0700313 /**
314 * Updates the icons and label of all pending icons for the provided package name.
315 */
316 public void updateSessionDisplayInfo(final String packageName) {
317 Runnable updateRunnable = new Runnable() {
318
319 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700320 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700321 synchronized (sBgLock) {
322 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
323 final UserHandleCompat user = UserHandleCompat.myUserHandle();
324
Sunny Goyale2df0622015-04-24 11:27:00 -0700325 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700326 if (info instanceof ShortcutInfo) {
327 ShortcutInfo si = (ShortcutInfo) info;
328 ComponentName cn = si.getTargetComponent();
329 if (si.isPromise() && (cn != null)
330 && packageName.equals(cn.getPackageName())) {
331 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
332 // For auto install apps update the icon as well as label.
333 mIconCache.getTitleAndIcon(si,
334 si.promisedIntent, user,
335 si.shouldUseLowResIcon());
336 } else {
337 // Only update the icon for restored apps.
338 si.updateIcon(mIconCache);
339 }
340 updates.add(si);
341 }
342 }
343 }
344
345 if (!updates.isEmpty()) {
346 // Push changes to the callback.
347 Runnable r = new Runnable() {
348 public void run() {
349 Callbacks callbacks = getCallback();
350 if (callbacks != null) {
351 callbacks.bindShortcutsChanged(updates,
352 new ArrayList<ShortcutInfo>(), user);
353 }
354 }
355 };
356 mHandler.post(r);
357 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700358 }
359 }
360 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700361 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700362 }
363
Adam Cohen76a47a12014-02-05 11:47:43 -0800364 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800365 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800366
367 if (allAppsApps == null) {
368 throw new RuntimeException("allAppsApps must not be null");
369 }
370 if (allAppsApps.isEmpty()) {
371 return;
372 }
373
374 // Process the newly added applications and add them to the database first
375 Runnable r = new Runnable() {
376 public void run() {
377 runOnMainThread(new Runnable() {
378 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800379 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800380 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500381 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800382 }
383 }
384 });
385 }
386 };
387 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700388 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800389
Sunny Goyala9116722015-04-29 13:55:58 -0700390 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800391 int[] xy, int spanX, int spanY) {
392 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700393 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700394
395 GridOccupancy occupied = new GridOccupancy(profile.numColumns, profile.numRows);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800396 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700397 for (ItemInfo r : occupiedPos) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700398 occupied.markCells(r, true);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800399 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800400 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700401 return occupied.findVacantCell(xy, spanX, spanY);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800402 }
403
404 /**
405 * Find a position on the screen for the given size or adds a new screen.
406 * @return screenId and the coordinates for the item.
407 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700408 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyalc3642d42015-10-30 10:27:08 -0700409 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800410 ArrayList<Long> workspaceScreens,
411 ArrayList<Long> addedWorkspaceScreensFinal,
412 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700413 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800414
Sunny Goyala9116722015-04-29 13:55:58 -0700415 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700416 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700417 synchronized (sBgLock) {
418 for (ItemInfo info : sBgItemsIdMap) {
419 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
420 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
421 if (items == null) {
422 items = new ArrayList<>();
423 screenItems.put(info.screenId, items);
424 }
425 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800426 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800427 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800428 }
429
430 // Find appropriate space for the item.
431 long screenId = 0;
432 int[] cordinates = new int[2];
433 boolean found = false;
434
435 int screenCount = workspaceScreens.size();
436 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700437 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800438 if (preferredScreenIndex < screenCount) {
439 screenId = workspaceScreens.get(preferredScreenIndex);
440 found = findNextAvailableIconSpaceInScreen(
441 screenItems.get(screenId), cordinates, spanX, spanY);
442 }
443
444 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700445 // Search on any of the screens starting from the first screen.
446 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800447 screenId = workspaceScreens.get(screen);
448 if (findNextAvailableIconSpaceInScreen(
449 screenItems.get(screenId), cordinates, spanX, spanY)) {
450 // We found a space for it
451 found = true;
452 break;
453 }
454 }
455 }
456
457 if (!found) {
458 // Still no position found. Add a new screen to the end.
Sunny Goyald2497482015-09-22 18:24:19 -0700459 screenId = LauncherSettings.Settings.call(context.getContentResolver(),
460 LauncherSettings.Settings.METHOD_NEW_SCREEN_ID)
461 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800462
463 // Save the screen id for binding in the workspace
464 workspaceScreens.add(screenId);
465 addedWorkspaceScreensFinal.add(screenId);
466
467 // If we still can't find an empty space, then God help us all!!!
468 if (!findNextAvailableIconSpaceInScreen(
469 screenItems.get(screenId), cordinates, spanX, spanY)) {
470 throw new RuntimeException("Can't find space to add the item");
471 }
472 }
473 return Pair.create(screenId, cordinates);
474 }
475
476 /**
477 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800478 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700479 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700480 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800481 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800482 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700483 return;
Winson Chung997a9232013-07-24 15:33:46 -0700484 }
Winson Chung64359a52013-07-08 17:17:08 -0700485 // Process the newly added applications and add them to the database first
486 Runnable r = new Runnable() {
487 public void run() {
488 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
489 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
490
Winson Chung76828c82013-08-19 15:43:29 -0700491 // Get the list of workspace screens. We need to append to this list and
492 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
493 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800494 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700495 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800496 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700497 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800498 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700499 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800500 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700501 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800502 }
Winson Chung76828c82013-08-19 15:43:29 -0700503
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800504 // Find appropriate space for the item.
Sunny Goyalc3642d42015-10-30 10:27:08 -0700505 Pair<Long, int[]> coords = findSpaceForItem(context,
Sunny Goyal9eba1fd2015-10-16 08:58:57 -0700506 workspaceScreens, addedWorkspaceScreensFinal, 1, 1);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800507 long screenId = coords.first;
508 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700509
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700510 ItemInfo itemInfo;
511 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
512 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800513 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700514 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700515 } else {
516 throw new RuntimeException("Unexpected info type");
517 }
Winson Chung94d67682013-09-25 16:29:40 -0700518
Winson Chung64359a52013-07-08 17:17:08 -0700519 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700520 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700521 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700522 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700523 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700524 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700525 }
526 }
527
Winson Chung76828c82013-08-19 15:43:29 -0700528 // Update the workspace screens
529 updateWorkspaceScreenOrder(context, workspaceScreens);
530
Adam Cohen76a47a12014-02-05 11:47:43 -0800531 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700532 runOnMainThread(new Runnable() {
533 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800534 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700535 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700536 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
537 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700538 if (!addedShortcutsFinal.isEmpty()) {
539 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
540 long lastScreenId = info.screenId;
541 for (ItemInfo i : addedShortcutsFinal) {
542 if (i.screenId == lastScreenId) {
543 addAnimated.add(i);
544 } else {
545 addNotAnimated.add(i);
546 }
Winson Chung997a9232013-07-24 15:33:46 -0700547 }
548 }
Winson Chungd64d1762013-08-20 14:37:16 -0700549 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800550 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700551 }
Winson Chung64359a52013-07-08 17:17:08 -0700552 }
Winson Chung997a9232013-07-24 15:33:46 -0700553 });
554 }
Winson Chung64359a52013-07-08 17:17:08 -0700555 }
556 };
557 runOnWorkerThread(r);
558 }
559
Joe Onorato9c1289c2009-08-17 11:03:03 -0400560 /**
561 * Adds an item to the DB if it was not created previously, or move it to a new
562 * <container, screen, cellX, cellY>
563 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800564 public static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700565 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400566 if (item.container == ItemInfo.NO_ID) {
567 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700568 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400569 } else {
570 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700571 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800572 }
573 }
574
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700575 static void checkItemInfoLocked(
576 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
577 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
578 if (modelItem != null && item != modelItem) {
579 // check all the data is consistent
580 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
581 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
582 ShortcutInfo shortcut = (ShortcutInfo) item;
583 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
584 modelShortcut.intent.filterEquals(shortcut.intent) &&
585 modelShortcut.id == shortcut.id &&
586 modelShortcut.itemType == shortcut.itemType &&
587 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700588 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700589 modelShortcut.cellX == shortcut.cellX &&
590 modelShortcut.cellY == shortcut.cellY &&
591 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700592 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700593 // For all intents and purposes, this is the same object
594 return;
595 }
596 }
597
598 // the modelItem needs to match up perfectly with item if our model is
599 // to be consistent with the database-- for now, just require
600 // modelItem == item or the equality check above
601 String msg = "item: " + ((item != null) ? item.toString() : "null") +
602 "modelItem: " +
603 ((modelItem != null) ? modelItem.toString() : "null") +
604 "Error: ItemInfo passed to checkItemInfo doesn't match original";
605 RuntimeException e = new RuntimeException(msg);
606 if (stackTrace != null) {
607 e.setStackTrace(stackTrace);
608 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800609 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700610 }
611 }
612
Michael Jurka816474f2012-06-25 14:49:02 -0700613 static void checkItemInfo(final ItemInfo item) {
614 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
615 final long itemId = item.id;
616 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700617 public void run() {
618 synchronized (sBgLock) {
619 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700620 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700621 }
622 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700623 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700624 }
625
Michael Jurkac9d95c52011-08-29 14:03:34 -0700626 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
627 final ItemInfo item, final String callingFunction) {
628 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700629 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700630 final ContentResolver cr = context.getContentResolver();
631
Adam Cohen487f7dd2012-06-28 18:12:10 -0700632 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700633 Runnable r = new Runnable() {
634 public void run() {
635 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700636 updateItemArrays(item, itemId, stackTrace);
637 }
638 };
639 runOnWorkerThread(r);
640 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700641
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700642 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
643 final ArrayList<ItemInfo> items, final String callingFunction) {
644 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700645
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700646 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
647 Runnable r = new Runnable() {
648 public void run() {
649 ArrayList<ContentProviderOperation> ops =
650 new ArrayList<ContentProviderOperation>();
651 int count = items.size();
652 for (int i = 0; i < count; i++) {
653 ItemInfo item = items.get(i);
654 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700655 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700656 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700657
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700658 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
659 updateItemArrays(item, itemId, stackTrace);
660
661 }
662 try {
663 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
664 } catch (Exception e) {
665 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700666 }
667 }
668 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700669 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700670 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700671
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700672 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
673 // Lock on mBgLock *after* the db operation
674 synchronized (sBgLock) {
675 checkItemInfoLocked(itemId, item, stackTrace);
676
677 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
678 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
679 // Item is in a folder, make sure this folder exists
680 if (!sBgFolders.containsKey(item.container)) {
681 // An items container is being set to a that of an item which is not in
682 // the list of Folders.
683 String msg = "item: " + item + " container being set to: " +
684 item.container + ", not in the list of folders";
685 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700686 }
687 }
688
689 // Items are added/removed from the corresponding FolderInfo elsewhere, such
690 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
691 // that are on the desktop, as appropriate
692 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800693 if (modelItem != null &&
694 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
695 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700696 switch (modelItem.itemType) {
697 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
698 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700699 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700700 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
701 if (!sBgWorkspaceItems.contains(modelItem)) {
702 sBgWorkspaceItems.add(modelItem);
703 }
704 break;
705 default:
706 break;
707 }
708 } else {
709 sBgWorkspaceItems.remove(modelItem);
710 }
711 }
712 }
713
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400715 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700716 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700717 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700718 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400719 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400720 item.cellX = cellX;
721 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700722
Winson Chung3d503fb2011-07-13 17:25:49 -0700723 // We store hotseat items in canonical form which is this orientation invariant position
724 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700725 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700726 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700727 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700728 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700729 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700730 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400731
732 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400733 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700734 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
735 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800736 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700737 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400738
Michael Jurkac9d95c52011-08-29 14:03:34 -0700739 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700740 }
741
742 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700743 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
744 * cellX, cellY have already been updated on the ItemInfos.
745 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800746 public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700747 final long container, final int screen) {
748
749 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
750 int count = items.size();
751
752 for (int i = 0; i < count; i++) {
753 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700754 item.container = container;
755
756 // We store hotseat items in canonical form which is this orientation invariant position
757 // in the hotseat
758 if (context instanceof Launcher && screen < 0 &&
759 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700760 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700761 item.cellY);
762 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700763 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700764 }
765
766 final ContentValues values = new ContentValues();
767 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
768 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
769 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800770 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700771 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700772
773 contentValues.add(values);
774 }
775 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
776 }
777
778 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700779 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800780 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700781 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700782 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700783 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800784 item.cellX = cellX;
785 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700786 item.spanX = spanX;
787 item.spanY = spanY;
788
789 // We store hotseat items in canonical form which is this orientation invariant position
790 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700791 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700792 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700793 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700794 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700795 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700796 }
Adam Cohend4844c32011-02-18 19:25:06 -0800797
Adam Cohend4844c32011-02-18 19:25:06 -0800798 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800799 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700800 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
801 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800802 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700803 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
804 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700805 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800806
Michael Jurka816474f2012-06-25 14:49:02 -0700807 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700808 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700809
810 /**
811 * Update an item to the database in a specified container.
812 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700813 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700814 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100815 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700816 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800817 }
818
Sunny Goyal756a28a2015-04-23 17:07:55 -0700819 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700820 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700821 synchronized (mLock) {
822 if (!mHasLoaderCompletedOnce ||
823 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
824 throw new RuntimeException("Trying to add shortcut while loader is running");
825 }
826 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700827 }
828 }
829
Adam Cohend4844c32011-02-18 19:25:06 -0800830 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700831 * Returns true if the shortcuts already exists on the workspace. This must be called after
832 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700834 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
835 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700836 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700837 if (intent.getComponent() != null) {
838 // If component is not null, an intent with null package will produce
839 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700840 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700841 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700842 intentWithPkg = intent.toUri(0);
843 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700844 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700845 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
846 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700847 }
848 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700849 intentWithPkg = intent.toUri(0);
850 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700851 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700852
853 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700854 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700855 if (item instanceof ShortcutInfo) {
856 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700857 Intent targetIntent = info.promisedIntent == null
858 ? info.intent : info.promisedIntent;
859 if (targetIntent != null && info.user.equals(user)) {
860 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700861 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
862 return true;
863 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700864 }
865 }
866 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800867 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700868 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700869 }
870
Joe Onorato9c1289c2009-08-17 11:03:03 -0400871 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400872 * Add an item to the database in a specified container. Sets the container, screen, cellX and
873 * cellY fields of the item. Also assigns an ID to the item.
874 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700875 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700876 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400877 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400878 item.cellX = cellX;
879 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700880 // We store hotseat items in canonical form which is this orientation invariant position
881 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700882 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700883 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700884 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700885 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700886 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700887 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400888
889 final ContentValues values = new ContentValues();
890 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100891 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400892
Sunny Goyald2497482015-09-22 18:24:19 -0700893 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
894 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
895
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700896 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700897
Jason Monk8e19cf22014-03-20 15:06:57 -0400898 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700899 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700900 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700901 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400902
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700903 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700904 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400905 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700906 sBgItemsIdMap.put(item.id, item);
907 switch (item.itemType) {
908 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
909 sBgFolders.put(item.id, (FolderInfo) item);
910 // Fall through
911 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
912 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700913 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700914 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
915 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
916 sBgWorkspaceItems.add(item);
917 } else {
918 if (!sBgFolders.containsKey(item.container)) {
919 // Adding an item to a folder that doesn't exist.
920 String msg = "adding item: " + item + " to a folder that " +
921 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700922 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700923 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700924 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700925 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
926 ShortcutInfo shortcutInfo = (ShortcutInfo) item;
927 ShortcutKey shortcutToPin = new ShortcutKey(
928 shortcutInfo.intent.getPackage(),
929 shortcutInfo.user,
930 shortcutInfo.getDeepShortcutId());
931 incrementPinnedShortcutCount(shortcutToPin, true /* shouldPin */);
932 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700933 break;
934 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
935 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
936 break;
937 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700938 }
939 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700940 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700941 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700942 }
943
Sunny Goyal34942622014-08-29 17:20:55 -0700944 private static ArrayList<ItemInfo> getItemsByPackageName(
945 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700946 ItemInfoFilter filter = new ItemInfoFilter() {
947 @Override
948 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
949 return cn.getPackageName().equals(pn) && info.user.equals(user);
950 }
951 };
Sunny Goyale2df0622015-04-24 11:27:00 -0700952 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -0700953 }
954
955 /**
956 * Removes all the items from the database corresponding to the specified package.
957 */
958 static void deletePackageFromDatabase(Context context, final String pn,
959 final UserHandleCompat user) {
960 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700961 }
962
963 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700964 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -0400965 */
Sunny Goyalfa401a12015-04-10 13:45:42 -0700966 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700967 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
968 items.add(item);
969 deleteItemsFromDatabase(context, items);
970 }
971
972 /**
973 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700974 */
Sunny Goyal4390ace2014-10-13 11:33:11 -0700975 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400976 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -0700977 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700978 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700979 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700980 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700981 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700982
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700983 // Lock on mBgLock *after* the db operation
984 synchronized (sBgLock) {
985 switch (item.itemType) {
986 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
987 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -0700988 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700989 if (info.container == item.id) {
990 // We are deleting a folder which still contains items that
991 // think they are contained by that folder.
992 String msg = "deleting a folder (" + item + ") which still " +
993 "contains items (" + info + ")";
994 Log.e(TAG, msg);
995 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700996 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700997 sBgWorkspaceItems.remove(item);
998 break;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700999 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
1000 ShortcutInfo shortcutInfo = ((ShortcutInfo) item);
1001 ShortcutKey pinnedShortcut = new ShortcutKey(
1002 shortcutInfo.intent.getPackage(),
1003 shortcutInfo.user,
1004 shortcutInfo.getDeepShortcutId());
1005 decrementPinnedShortcutCount(pinnedShortcut);
1006 // Fall through.
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001007 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1008 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1009 sBgWorkspaceItems.remove(item);
1010 break;
1011 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1012 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1013 break;
1014 }
1015 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001016 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001017 }
1018 }
Michael Jurka83df1882011-08-31 20:59:26 -07001019 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001020 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001021 }
1022
1023 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001024 * Decrement the count for the given pinned shortcut, unpinning it if the count becomes 0.
1025 */
1026 private static void decrementPinnedShortcutCount(final ShortcutKey pinnedShortcut) {
1027 synchronized (sBgLock) {
1028 MutableInt count = sBgPinnedShortcutCounts.get(pinnedShortcut);
1029 if (count == null || --count.value == 0) {
1030 LauncherAppState.getInstance().getShortcutManager().unpinShortcut(pinnedShortcut);
1031 }
1032 }
1033 }
1034
1035 /**
1036 * Increment the count for the given shortcut, pinning it if the count becomes 1.
1037 *
1038 * As an optimization, the caller can pass shouldPin == false to avoid
1039 * unnecessary RPC's if the shortcut is already pinned.
1040 */
1041 private static void incrementPinnedShortcutCount(ShortcutKey pinnedShortcut, boolean shouldPin) {
1042 synchronized (sBgLock) {
1043 MutableInt count = sBgPinnedShortcutCounts.get(pinnedShortcut);
1044 if (count == null) {
1045 count = new MutableInt(1);
1046 sBgPinnedShortcutCounts.put(pinnedShortcut, count);
1047 } else {
1048 count.value++;
1049 }
1050 if (shouldPin && count.value == 1) {
1051 LauncherAppState.getInstance().getShortcutManager().pinShortcut(pinnedShortcut);
1052 }
1053 }
1054 }
1055
1056 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001057 * Update the order of the workspace screens in the database. The array list contains
1058 * a list of screen ids in the order that they should appear.
1059 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001060 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001061 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001062 final ContentResolver cr = context.getContentResolver();
1063 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1064
1065 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001066 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001067 while (iter.hasNext()) {
1068 long id = iter.next();
1069 if (id < 0) {
1070 iter.remove();
1071 }
1072 }
1073
1074 Runnable r = new Runnable() {
1075 @Override
1076 public void run() {
Yura085c8532014-02-11 15:15:29 +00001077 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001078 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001079 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001080 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001081 for (int i = 0; i < count; i++) {
1082 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001083 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001084 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1085 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001086 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001087 }
Yura085c8532014-02-11 15:15:29 +00001088
1089 try {
1090 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1091 } catch (Exception ex) {
1092 throw new RuntimeException(ex);
1093 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001094
Winson Chungba9c37f2013-08-30 14:11:37 -07001095 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001096 sBgWorkspaceScreens.clear();
1097 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001098 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001099 }
1100 };
1101 runOnWorkerThread(r);
1102 }
1103
1104 /**
Winsonc0b52fe2015-09-09 16:38:15 -07001105 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001106 */
Winsonc0b52fe2015-09-09 16:38:15 -07001107 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001108 final ContentResolver cr = context.getContentResolver();
1109
Michael Jurkac9d95c52011-08-29 14:03:34 -07001110 Runnable r = new Runnable() {
1111 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001112 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001113 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001114 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001115 sBgItemsIdMap.remove(info.id);
1116 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001117 sBgWorkspaceItems.remove(info);
1118 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001119
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001120 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001121 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001122 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001123 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001124 for (ItemInfo childInfo : info.contents) {
1125 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001126 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001127 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001128 }
1129 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001130 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001131 }
1132
1133 /**
1134 * Set this as the current Launcher activity object for the loader.
1135 */
1136 public void initialize(Callbacks callbacks) {
1137 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001138 Preconditions.assertUIThread();
1139 // Remove any queued UI runnables
1140 mHandler.cancelAll();
1141 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001142 }
1143 }
1144
Kenny Guyed131872014-04-30 03:02:21 +01001145 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001146 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001147 int op = PackageUpdatedTask.OP_UPDATE;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001148 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001149 user));
1150 }
1151
1152 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001153 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001154 int op = PackageUpdatedTask.OP_REMOVE;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001155 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001156 user));
1157 }
1158
1159 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001160 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001161 int op = PackageUpdatedTask.OP_ADD;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001162 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001163 user));
1164 }
1165
1166 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001167 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001168 boolean replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001169 enqueueItemUpdatedTask(
Sunny Goyal144154d2016-03-30 16:47:03 -07001170 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, packageNames, user));
Kenny Guyed131872014-04-30 03:02:21 +01001171 }
1172
1173 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001174 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001175 boolean replacing) {
1176 if (!replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001177 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guyed131872014-04-30 03:02:21 +01001178 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1179 user));
1180 }
Kenny Guyed131872014-04-30 03:02:21 +01001181 }
1182
Kenny Guy44cba692016-01-21 19:50:02 +00001183 @Override
1184 public void onPackagesSuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001185 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001186 PackageUpdatedTask.OP_SUSPEND, packageNames,
1187 user));
1188 }
1189
1190 @Override
1191 public void onPackagesUnsuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001192 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001193 PackageUpdatedTask.OP_UNSUSPEND, packageNames,
1194 user));
1195 }
1196
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001197 @Override
1198 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
1199 UserHandleCompat user) {
1200 enqueueItemUpdatedTask(new ShortcutsChangedTask(packageName, shortcuts, user));
1201 }
1202
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001203 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001204 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1205 * ACTION_PACKAGE_CHANGED.
1206 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001207 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001208 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001209 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001210
Joe Onorato36115782010-06-17 13:28:48 -04001211 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001212 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001213 // If we have changed locale we need to clear out the labels in all apps/workspace.
1214 forceReload();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001215 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
Sunny Goyalda891c12016-03-18 18:29:24 -07001216 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001217 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001218 forceReload();
Rubin Xuac6e5d72016-04-04 16:13:35 +01001219 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1220 LauncherAppsCompat.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
Sunny Goyalda891c12016-03-18 18:29:24 -07001221 UserHandleCompat user = UserHandleCompat.fromIntent(intent);
1222 if (user != null) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001223 enqueueItemUpdatedTask(new PackageUpdatedTask(
Sunny Goyalda891c12016-03-18 18:29:24 -07001224 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE,
1225 new String[0], user));
1226 }
Tony Wickham827cef22016-03-17 15:39:39 -07001227 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
1228 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001229 }
1230 }
1231
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001232 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001233 resetLoadedState(true, true);
1234
Reena Lee93f824a2011-09-23 17:20:28 -07001235 // Do this here because if the launcher activity is running it will be restarted.
1236 // If it's not running startLoaderFromBackground will merely tell it that it needs
1237 // to reload.
1238 startLoaderFromBackground();
1239 }
1240
Winson Chungf0c6ae02012-03-21 16:10:31 -07001241 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1242 synchronized (mLock) {
1243 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1244 // mWorkspaceLoaded to true later
1245 stopLoaderLocked();
1246 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1247 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001248 // Always reset deep shortcuts loaded.
1249 mDeepShortcutsLoaded = false;
Winson Chungf0c6ae02012-03-21 16:10:31 -07001250 }
1251 }
1252
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001253 /**
1254 * When the launcher is in the background, it's possible for it to miss paired
1255 * configuration changes. So whenever we trigger the loader from the background
1256 * tell the launcher that it needs to re-run the loader when it comes back instead
1257 * of doing it now.
1258 */
1259 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001260 Callbacks callbacks = getCallback();
1261 if (callbacks != null) {
1262 // Only actually run the loader if they're not paused.
1263 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -07001264 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001265 }
1266 }
Joe Onorato36115782010-06-17 13:28:48 -04001267 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001268
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001269 /**
1270 * If there is already a loader task running, tell it to stop.
1271 */
1272 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001273 LoaderTask oldTask = mLoaderTask;
1274 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001275 oldTask.stopLocked();
1276 }
Reena Lee93f824a2011-09-23 17:20:28 -07001277 }
1278
Adam Cohen1a85c582014-09-30 09:48:49 -07001279 public boolean isCurrentCallbacks(Callbacks callbacks) {
1280 return (mCallbacks != null && mCallbacks.get() == callbacks);
1281 }
1282
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001283 public void startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001284 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1285 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001286 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -04001287 // Don't bother to start the thread if we know it's not going to do anything
1288 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001289 final Callbacks oldCallbacks = mCallbacks.get();
1290 // Clear any pending bind-runnables from the synchronized load process.
1291 runOnMainThread(new Runnable() {
1292 public void run() {
1293 oldCallbacks.clearPendingBinds();
1294 }
1295 });
1296
Joe Onorato36115782010-06-17 13:28:48 -04001297 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001298 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001299 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Derek Prothro7aff3992013-12-10 14:00:37 -05001300 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001301 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001302 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1303 } else {
1304 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1305 sWorker.post(mLoaderTask);
1306 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001307 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001308 }
1309 }
1310
Joe Onorato36115782010-06-17 13:28:48 -04001311 public void stopLoader() {
1312 synchronized (mLock) {
1313 if (mLoaderTask != null) {
1314 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001315 }
1316 }
Joe Onorato36115782010-06-17 13:28:48 -04001317 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001318
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001319 /**
1320 * Loads the workspace screen ids in an ordered list.
1321 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001322 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001323 final ContentResolver contentResolver = context.getContentResolver();
1324 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001325
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001326 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001327 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
1328 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -07001329 }
1330
Joe Onorato36115782010-06-17 13:28:48 -04001331 /**
1332 * Runnable for the thread that loads the contents of the launcher:
1333 * - workspace icons
1334 * - widgets
1335 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001336 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -04001337 */
1338 private class LoaderTask implements Runnable {
1339 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001340 private int mPageToBindFirst;
1341
Adam Cohen091440a2015-03-18 14:16:05 -07001342 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001343 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001344 @Thunk boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001345
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001346 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -04001347 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001348 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001349 }
1350
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001351 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001352 mIsLoadingAndBindingWorkspace = true;
1353
Joe Onorato36115782010-06-17 13:28:48 -04001354 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001355 if (DEBUG_LOADERS) {
1356 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001357 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001358
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001359 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001360 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001361 synchronized (LoaderTask.this) {
1362 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001363 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001364 }
1365 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001366 }
1367 }
1368
Joe Onorato36115782010-06-17 13:28:48 -04001369 // Bind the workspace
Sunny Goyal93f878c2016-03-30 17:31:24 -07001370 bindWorkspace(mPageToBindFirst);
Joe Onorato36115782010-06-17 13:28:48 -04001371 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001372
Joe Onorato36115782010-06-17 13:28:48 -04001373 private void waitForIdle() {
1374 // Wait until the either we're stopped or the other threads are done.
1375 // This way we don't start loading all apps until the workspace has settled
1376 // down.
1377 synchronized (LoaderTask.this) {
1378 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001379
Joe Onorato36115782010-06-17 13:28:48 -04001380 mHandler.postIdle(new Runnable() {
1381 public void run() {
1382 synchronized (LoaderTask.this) {
1383 mLoadAndBindStepFinished = true;
1384 if (DEBUG_LOADERS) {
1385 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001386 }
Joe Onorato36115782010-06-17 13:28:48 -04001387 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001388 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001389 }
Joe Onorato36115782010-06-17 13:28:48 -04001390 });
1391
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001392 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001393 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001394 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1395 // wait no longer than 1sec at a time
1396 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001397 } catch (InterruptedException ex) {
1398 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001399 }
1400 }
Joe Onorato36115782010-06-17 13:28:48 -04001401 if (DEBUG_LOADERS) {
1402 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001403 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001404 + "ms for previous step to finish binding");
1405 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001406 }
Joe Onorato36115782010-06-17 13:28:48 -04001407 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001408
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001409 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001410 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001411 // Ensure that we have a valid page index to load synchronously
1412 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1413 "valid page index");
1414 }
1415 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1416 // Ensure that we don't try and bind a specified page when the pages have not been
1417 // loaded already (we should load everything asynchronously in that case)
1418 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1419 }
1420 synchronized (mLock) {
1421 if (mIsLoaderTaskRunning) {
1422 // Ensure that we are never running the background loading at this point since
1423 // we also touch the background collections
1424 throw new RuntimeException("Error! Background loading is already running");
1425 }
1426 }
1427
1428 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1429 // data structures, we can't allow any other thread to touch that data, but because
1430 // this call is synchronous, we can get away with not locking).
1431
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001432 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001433 // operations from the previous activity. We need to ensure that all queued operations
1434 // are executed before any synchronous binding work is done.
1435 mHandler.flush();
1436
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001437 // Divide the set of loaded items into those that we are binding synchronously, and
1438 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001439 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001440 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1441 // arise from that.
1442 onlyBindAllApps();
1443 }
1444
Joe Onorato36115782010-06-17 13:28:48 -04001445 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001446 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001447 if (mStopped) {
1448 return;
1449 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001450 mIsLoaderTaskRunning = true;
1451 }
Joe Onorato36115782010-06-17 13:28:48 -04001452 // Optimize for end-user experience: if the Launcher is up and // running with the
1453 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1454 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001455 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001456 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001457 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001458
Joe Onorato36115782010-06-17 13:28:48 -04001459 if (mStopped) {
1460 break keep_running;
1461 }
1462
Joe Onorato36115782010-06-17 13:28:48 -04001463 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001464
1465 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001466 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1467 loadAndBindAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001468
1469 waitForIdle();
1470
1471 // third step
1472 if (DEBUG_LOADERS) Log.d(TAG, "step 3: loading deep shortcuts");
1473 loadAndBindDeepShortcuts();
Joe Onorato36115782010-06-17 13:28:48 -04001474 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001475
Joe Onorato36115782010-06-17 13:28:48 -04001476 // Clear out this reference, otherwise we end up holding it until all of the
1477 // callback runnables are done.
1478 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001479
Joe Onorato36115782010-06-17 13:28:48 -04001480 synchronized (mLock) {
1481 // If we are still the last one to be scheduled, remove ourselves.
1482 if (mLoaderTask == this) {
1483 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001484 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001485 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001486 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001487 }
Joe Onorato36115782010-06-17 13:28:48 -04001488 }
1489
1490 public void stopLocked() {
1491 synchronized (LoaderTask.this) {
1492 mStopped = true;
1493 this.notify();
1494 }
1495 }
1496
1497 /**
1498 * Gets the callbacks object. If we've been stopped, or if the launcher object
1499 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1500 * object that was around when the deferred message was scheduled, and if there's
1501 * a new Callbacks object around then also return null. This will save us from
1502 * calling onto it with data that will be ignored.
1503 */
1504 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1505 synchronized (mLock) {
1506 if (mStopped) {
1507 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001508 }
Joe Onorato36115782010-06-17 13:28:48 -04001509
1510 if (mCallbacks == null) {
1511 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001512 }
Joe Onorato36115782010-06-17 13:28:48 -04001513
1514 final Callbacks callbacks = mCallbacks.get();
1515 if (callbacks != oldCallbacks) {
1516 return null;
1517 }
1518 if (callbacks == null) {
1519 Log.w(TAG, "no mCallbacks");
1520 return null;
1521 }
1522
1523 return callbacks;
1524 }
1525 }
1526
1527 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001528 private boolean checkItemPlacement(LongArrayMap<GridOccupancy> occupied, ItemInfo item,
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001529 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001530 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001531 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung892c74d2013-08-22 16:15:50 -07001532
Adam Cohendcd297f2013-06-18 13:13:40 -07001533 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001534 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001535 // Return early if we detect that an item is under the hotseat button
Sunny Goyalbb011da2016-06-15 15:42:29 -07001536 if (!FeatureFlags.NO_ALL_APPS_ICON &&
1537 profile.isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001538 Log.e(TAG, "Error loading shortcut into hotseat " + item
1539 + " into position (" + item.screenId + ":" + item.cellX + ","
1540 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001541 return false;
1542 }
1543
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001544 final GridOccupancy hotseatOccupancy =
Dan Sandler295ae182013-12-10 16:05:47 -05001545 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1546
Adam Cohen2e6da152015-05-06 11:42:25 -07001547 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001548 Log.e(TAG, "Error loading shortcut " + item
1549 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001550 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001551 + ")");
1552 return false;
1553 }
1554
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001555 if (hotseatOccupancy != null) {
1556 if (hotseatOccupancy.cells[(int) item.screenId][0]) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001557 Log.e(TAG, "Error loading shortcut into hotseat " + item
1558 + " into position (" + item.screenId + ":" + item.cellX + ","
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001559 + item.cellY + ") already occupied");
Adam Cohendcd297f2013-06-18 13:13:40 -07001560 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001561 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001562 hotseatOccupancy.cells[(int) item.screenId][0] = true;
Dan Sandler295ae182013-12-10 16:05:47 -05001563 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001564 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001565 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001566 final GridOccupancy occupancy = new GridOccupancy(profile.numHotseatIcons, 1);
1567 occupancy.cells[(int) item.screenId][0] = true;
1568 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, occupancy);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001569 return true;
1570 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001571 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1572 if (!workspaceScreens.contains((Long) item.screenId)) {
1573 // The item has an invalid screen id.
1574 return false;
1575 }
1576 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001577 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001578 return true;
1579 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001580
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001581 final int countX = profile.numColumns;
1582 final int countY = profile.numRows;
Adam Cohenae4409d2013-11-26 10:34:59 -08001583 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1584 item.cellX < 0 || item.cellY < 0 ||
1585 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1586 Log.e(TAG, "Error loading shortcut " + item
1587 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1588 + item.cellX + "," + item.cellY
1589 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1590 return false;
1591 }
1592
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001593 if (!occupied.containsKey(item.screenId)) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001594 GridOccupancy screen = new GridOccupancy(countX + 1, countY + 1);
1595 if (item.screenId == Workspace.FIRST_SCREEN_ID) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001596 // Mark the first row as occupied (if the feature is enabled)
1597 // in order to account for the QSB.
1598 screen.markCells(0, 0, countX + 1, 1, FeatureFlags.QSB_ON_FIRST_SCREEN);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001599 }
1600 occupied.put(item.screenId, screen);
Joe Onorato36115782010-06-17 13:28:48 -04001601 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001602 final GridOccupancy occupancy = occupied.get(item.screenId);
Winson Chungf30ad5f2011-08-08 10:55:42 -07001603
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001604 // Check if any workspace icons overlap with each other
1605 if (occupancy.isRegionVacant(item.cellX, item.cellY, item.spanX, item.spanY)) {
1606 occupancy.markCells(item, true);
1607 return true;
1608 } else {
1609 Log.e(TAG, "Error loading shortcut " + item
1610 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1611 + item.cellX + "," + item.cellX + "," + item.spanX + "," + item.spanY
1612 + ") already occupied");
1613 return false;
1614 }
Joe Onorato36115782010-06-17 13:28:48 -04001615 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001616
Winson Chungba9c37f2013-08-30 14:11:37 -07001617 /** Clears all the sBg data structures */
1618 private void clearSBgDataStructures() {
1619 synchronized (sBgLock) {
1620 sBgWorkspaceItems.clear();
1621 sBgAppWidgets.clear();
1622 sBgFolders.clear();
1623 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001624 sBgWorkspaceScreens.clear();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001625 sBgPinnedShortcutCounts.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001626 }
1627 }
1628
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001629 private void loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001630 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001631
Joe Onorato36115782010-06-17 13:28:48 -04001632 final Context context = mContext;
1633 final ContentResolver contentResolver = context.getContentResolver();
1634 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001635 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001636 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001637 final boolean isSdCardReady = Utilities.isBootCompleted();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001638
Winson Chung892c74d2013-08-22 16:15:50 -07001639 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001640 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001641 int countX = profile.numColumns;
1642 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001643
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001644 boolean clearDb = false;
Sunny Goyalf076eae2016-01-11 12:25:10 -08001645 if (GridSizeMigrationTask.ENABLED &&
1646 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1647 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001648 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001649 }
1650
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001651 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -07001652 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001653 LauncherSettings.Settings.call(contentResolver,
1654 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001655 }
1656
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001657 Log.d(TAG, "loadWorkspace: loading default favorites");
1658 LauncherSettings.Settings.call(contentResolver,
1659 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -07001660
Winson Chung2abf94d2012-07-18 18:16:38 -07001661 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001662 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001663 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001664 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001665 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001666
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001667 final ArrayList<Long> itemsToRemove = new ArrayList<>();
1668 final ArrayList<Long> restoredRows = new ArrayList<>();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001669 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001670 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001671 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001672 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001673
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001674 // +1 for the hotseat (it can be larger than the workspace)
1675 // Load workspace in reverse order to ensure that latest items are loaded first (and
1676 // before any earlier duplicates)
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001677 final LongArrayMap<GridOccupancy> occupied = new LongArrayMap<>();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001678 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001679
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001680 try {
1681 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1682 final int intentIndex = c.getColumnIndexOrThrow
1683 (LauncherSettings.Favorites.INTENT);
1684 final int titleIndex = c.getColumnIndexOrThrow
1685 (LauncherSettings.Favorites.TITLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001686 final int containerIndex = c.getColumnIndexOrThrow(
1687 LauncherSettings.Favorites.CONTAINER);
1688 final int itemTypeIndex = c.getColumnIndexOrThrow(
1689 LauncherSettings.Favorites.ITEM_TYPE);
1690 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1691 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001692 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1693 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001694 final int screenIndex = c.getColumnIndexOrThrow(
1695 LauncherSettings.Favorites.SCREEN);
1696 final int cellXIndex = c.getColumnIndexOrThrow
1697 (LauncherSettings.Favorites.CELLX);
1698 final int cellYIndex = c.getColumnIndexOrThrow
1699 (LauncherSettings.Favorites.CELLY);
1700 final int spanXIndex = c.getColumnIndexOrThrow
1701 (LauncherSettings.Favorites.SPANX);
1702 final int spanYIndex = c.getColumnIndexOrThrow(
1703 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001704 final int rankIndex = c.getColumnIndexOrThrow(
1705 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001706 final int restoredIndex = c.getColumnIndexOrThrow(
1707 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001708 final int profileIdIndex = c.getColumnIndexOrThrow(
1709 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001710 final int optionsIndex = c.getColumnIndexOrThrow(
1711 LauncherSettings.Favorites.OPTIONS);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001712 final CursorIconInfo cursorIconInfo = new CursorIconInfo(c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001713
Sunny Goyal7f834d22015-04-21 10:10:23 -07001714 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
Kenny Guyff05f432016-01-22 17:48:29 +00001715 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyal7f834d22015-04-21 10:10:23 -07001716 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001717 long serialNo = mUserManager.getSerialNumberForUser(user);
1718 allUsers.put(serialNo, user);
1719 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001720
1721 List<ShortcutInfoCompat> pinnedShortcuts = mDeepShortcutManager
1722 .queryForPinnedShortcuts(null, user);
1723 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
1724 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
1725 shortcut);
1726 }
Sunny Goyal7f834d22015-04-21 10:10:23 -07001727 }
1728
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001729 ShortcutInfo info;
1730 String intentDescription;
1731 LauncherAppWidgetInfo appWidgetInfo;
1732 int container;
1733 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001734 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001735 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001736 UserHandleCompat user;
Sunny Goyald09c3702016-04-06 16:18:20 -07001737 String targetPackage;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001738
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001739 while (!mStopped && c.moveToNext()) {
1740 try {
1741 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001742 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001743 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001744 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001745
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001746 switch (itemType) {
1747 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1748 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001749 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001750 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001751 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001752 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001753 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001754 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001755 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001756 boolean itemReplaced = false;
Sunny Goyald09c3702016-04-06 16:18:20 -07001757 targetPackage = null;
Kenny Guyed131872014-04-30 03:02:21 +01001758 if (user == null) {
1759 // User has been deleted remove the item.
1760 itemsToRemove.add(id);
1761 continue;
1762 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001763 try {
1764 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001765 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001766 if (cn != null && cn.getPackageName() != null) {
1767 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1768 cn.getPackageName(), user);
1769 boolean validComponent = validPkg &&
1770 launcherApps.isActivityEnabledForProfile(cn, user);
Sunny Goyald09c3702016-04-06 16:18:20 -07001771 if (validPkg) {
1772 targetPackage = cn.getPackageName();
1773 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001774
1775 if (validComponent) {
1776 if (restored) {
1777 // no special handling necessary for this item
1778 restoredRows.add(id);
1779 restored = false;
1780 }
Kenny Guyff05f432016-01-22 17:48:29 +00001781 if (quietMode.get(serialNumber)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001782 disabledState = ShortcutInfo.FLAG_DISABLED_QUIET_USER;
Kenny Guyff05f432016-01-22 17:48:29 +00001783 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001784 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001785 intent = null;
1786 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1787 // We allow auto install apps to have their intent
1788 // updated after an install.
1789 intent = manager.getLaunchIntentForPackage(
1790 cn.getPackageName());
1791 if (intent != null) {
1792 ContentValues values = new ContentValues();
1793 values.put(LauncherSettings.Favorites.INTENT,
1794 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001795 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001796 }
1797 }
1798
1799 if (intent == null) {
1800 // The app is installed but the component is no
1801 // longer available.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001802 FileLog.d(TAG, "Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001803 itemsToRemove.add(id);
1804 continue;
1805 } else {
1806 // no special handling necessary for this item
1807 restoredRows.add(id);
1808 restored = false;
1809 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001810 } else if (restored) {
1811 // Package is not yet available but might be
1812 // installed later.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001813 FileLog.d(TAG, "package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001814
1815 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1816 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001817 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001818 // App restore has started. Update the flag
1819 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1820 ContentValues values = new ContentValues();
1821 values.put(LauncherSettings.Favorites.RESTORED,
1822 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001823 updateItem(id, values);
1824 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1825 // This is a common app. Try to replace this.
1826 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1827 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1828 if (parser.findDefaultApp()) {
1829 // Default app found. Replace it.
1830 intent = parser.parsedIntent;
1831 cn = intent.getComponent();
1832 ContentValues values = parser.parsedValues;
1833 values.put(LauncherSettings.Favorites.RESTORED, 0);
1834 updateItem(id, values);
1835 restored = false;
1836 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001837
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001838 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001839 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001840 itemsToRemove.add(id);
1841 continue;
1842 }
Sunny Goyal94485362014-09-18 16:13:58 -07001843 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001844 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001845 itemsToRemove.add(id);
1846 continue;
1847 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001848 } else if (PackageManagerHelper.isAppOnSdcard(
1849 manager, cn.getPackageName())) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07001850 // Package is present but not available.
1851 allowMissingTarget = true;
1852 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1853 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001854 // SdCard is not ready yet. Package might get available,
1855 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001856 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001857 HashSet<String> pkgs = sPendingPackages.get(user);
1858 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001859 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001860 sPendingPackages.put(user, pkgs);
1861 }
1862 pkgs.add(cn.getPackageName());
1863 allowMissingTarget = true;
1864 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001865
1866 } else {
1867 // Do not wait for external media load anymore.
1868 // Log the invalid package, and remove it
Sunny Goyal713edfc2016-05-06 09:58:34 -07001869 FileLog.d(TAG, "Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001870 itemsToRemove.add(id);
1871 continue;
Winson Chungee055712013-07-30 14:46:24 -07001872 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001873 } else if (cn == null) {
1874 // For shortcuts with no component, keep them as they are
1875 restoredRows.add(id);
1876 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001877 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001878 } catch (URISyntaxException e) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001879 FileLog.d(TAG, "Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001880 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001881 continue;
1882 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001883
Sunny Goyal34b65272015-03-11 16:56:52 -07001884 boolean useLowResIcon = container >= 0 &&
1885 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1886
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001887 if (itemReplaced) {
1888 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001889 info = getAppShortcutInfo(intent, user, context, null,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001890 cursorIconInfo.iconIndex, titleIndex,
1891 false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001892 } else {
1893 // Don't replace items for other profiles.
1894 itemsToRemove.add(id);
1895 continue;
1896 }
1897 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001898 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal34b65272015-03-11 16:56:52 -07001899 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07001900 promiseType, itemType, cursorIconInfo, context);
Kenny Guyed131872014-04-30 03:02:21 +01001901 intent = getRestoredItemIntent(c, context, intent);
1902 } else {
1903 // Don't restore items for other profiles.
1904 itemsToRemove.add(id);
1905 continue;
1906 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001907 } else if (itemType ==
1908 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001909 info = getAppShortcutInfo(intent, user, context, c,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001910 cursorIconInfo.iconIndex, titleIndex,
1911 allowMissingTarget, useLowResIcon);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001912 } else if (itemType ==
1913 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
1914 String shortcutId = intent.getStringExtra(
1915 ShortcutInfoCompat.EXTRA_SHORTCUT_ID);
1916 String packageName = intent.getPackage();
1917 ShortcutKey key = new ShortcutKey(intent.getPackage(),
1918 user, shortcutId);
1919 ShortcutInfoCompat pinnedShortcut =
1920 shortcutKeyToPinnedShortcuts.get(key);
1921 boolean shouldPin = false; // It's already pinned.
1922 if (pinnedShortcut == null) {
1923 // It shouldn't be possible for a shortcut to be on the
1924 // workspace without being pinned, but if one somehow is,
1925 // we should pin it now to get back to a good state.
1926 Log.w(TAG, "Shortcut was on workspace but wasn't pinned");
1927 // Get full details; incrementing the count will pin it.
1928 List<ShortcutInfoCompat> fullDetails = mDeepShortcutManager
1929 .queryForFullDetails(packageName,
1930 Collections.singletonList(shortcutId), user);
1931 if (fullDetails == null || fullDetails.isEmpty()) {
1932 itemsToRemove.add(id);
1933 continue;
1934 } else {
1935 pinnedShortcut = fullDetails.get(0);
1936 shouldPin = true;
1937 }
1938 }
1939 incrementPinnedShortcutCount(key, shouldPin);
1940 info = ShortcutInfo.fromDeepShortcutInfo(pinnedShortcut,
1941 context, launcherApps);
1942 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001943 info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001944
Sunny Goyald09c3702016-04-06 16:18:20 -07001945 // Shortcuts are only available on the primary profile
1946 if (PackageManagerHelper.isAppSuspended(manager, targetPackage)) {
1947 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1948 }
1949
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001950 // App shortcuts that used to be automatically added to Launcher
1951 // didn't always have the correct intent flags set, so do that
1952 // here
1953 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001954 intent.getCategories() != null &&
1955 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001956 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001957 intent.addFlags(
1958 Intent.FLAG_ACTIVITY_NEW_TASK |
1959 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1960 }
Michael Jurka96879562012-03-22 05:54:33 -07001961 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001962
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001963 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001964 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001965 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001966 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001967 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001968 info.cellX = c.getInt(cellXIndex);
1969 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08001970 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001971 info.spanX = 1;
1972 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01001973 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001974 if (info.promisedIntent != null) {
1975 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
1976 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001977 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001978 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1979 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1980 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001981
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001982 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001983 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001984 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001985 break;
1986 }
1987
Sunny Goyal756adbc2015-04-16 15:20:51 -07001988 if (restored) {
1989 ComponentName cn = info.getTargetComponent();
1990 if (cn != null) {
1991 Integer progress = installingPkgs.get(cn.getPackageName());
1992 if (progress != null) {
1993 info.setInstallProgress(progress);
1994 } else {
1995 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
1996 }
1997 }
1998 }
1999
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002000 switch (container) {
2001 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2002 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2003 sBgWorkspaceItems.add(info);
2004 break;
2005 default:
2006 // Item is in a user folder
2007 FolderInfo folderInfo =
2008 findOrMakeFolder(sBgFolders, container);
Sunny Goyalc52ba712016-04-05 15:59:05 -07002009 folderInfo.add(info, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002010 break;
2011 }
2012 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002013 } else {
2014 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002015 }
2016 break;
2017
2018 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2019 id = c.getLong(idIndex);
2020 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2021
Sunny Goyala508e4f2015-05-21 09:33:57 -07002022 // Do not trim the folder label, as is was set by the user.
2023 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002024 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002025 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002026 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002027 folderInfo.cellX = c.getInt(cellXIndex);
2028 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002029 folderInfo.spanX = 1;
2030 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002031 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002032
Daniel Sandler8802e962010-05-26 16:28:16 -04002033 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002034 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002035 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002036 break;
2037 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002038
Joe Onorato9c1289c2009-08-17 11:03:03 -04002039 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002040 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2041 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2042 sBgWorkspaceItems.add(folderInfo);
2043 break;
Joe Onorato36115782010-06-17 13:28:48 -04002044 }
Joe Onorato17a89222011-02-08 17:26:11 -08002045
Chris Wrenf4d08112014-01-16 18:13:56 -05002046 if (restored) {
2047 // no special handling required for restored folders
2048 restoredRows.add(id);
2049 }
2050
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002051 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2052 sBgFolders.put(folderInfo.id, folderInfo);
2053 break;
2054
2055 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002056 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002057 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002058 boolean customWidget = itemType ==
2059 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2060
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002061 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002062 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002063 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002064 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002065 user = allUsers.get(serialNumber);
2066 if (user == null) {
2067 itemsToRemove.add(id);
2068 continue;
2069 }
2070
Sunny Goyalff572272014-07-23 13:58:07 -07002071 final ComponentName component =
2072 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002073
Sunny Goyal651077b2014-06-30 14:15:31 -07002074 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002075 final boolean isIdValid = (restoreStatus &
2076 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002077 final boolean wasProviderReady = (restoreStatus &
2078 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002079
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002080 if (widgetProvidersMap == null) {
2081 widgetProvidersMap = AppWidgetManagerCompat
2082 .getInstance(mContext).getAllProvidersMap();
2083 }
2084 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
2085 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00002086 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002087 user));
Sunny Goyalff572272014-07-23 13:58:07 -07002088
2089 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002090 if (!isSafeMode && !customWidget &&
2091 wasProviderReady && !isProviderReady) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002092 FileLog.d(TAG, "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07002093 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002094 itemsToRemove.add(id);
2095 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002096 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002097 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2098 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002099
Sunny Goyal53f96722015-07-13 19:54:53 -07002100 // The provider is available. So the widget is either
2101 // available or not available. We do not need to track
2102 // any future restore updates.
2103 int status = restoreStatus &
2104 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002105 if (!wasProviderReady) {
2106 // If provider was not previously ready, update the
2107 // status and UI flag.
2108
2109 // Id would be valid only if the widget restore broadcast was received.
2110 if (isIdValid) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002111 status = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002112 } else {
2113 status &= ~LauncherAppWidgetInfo
2114 .FLAG_PROVIDER_NOT_READY;
2115 }
2116 }
2117 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002118 } else {
2119 Log.v(TAG, "Widget restore pending id=" + id
2120 + " appWidgetId=" + appWidgetId
2121 + " status =" + restoreStatus);
2122 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002123 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002124 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002125 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002126
2127 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2128 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002129 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002130 // App restore has started. Update the flag
2131 appWidgetInfo.restoreStatus |=
2132 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002133 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002134 FileLog.d(TAG, "Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07002135 itemsToRemove.add(id);
2136 continue;
2137 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002138
2139 appWidgetInfo.installProgress =
2140 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002141 }
Sunny Goyalff572272014-07-23 13:58:07 -07002142
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002143 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002144 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002145 appWidgetInfo.cellX = c.getInt(cellXIndex);
2146 appWidgetInfo.cellY = c.getInt(cellYIndex);
2147 appWidgetInfo.spanX = c.getInt(spanXIndex);
2148 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002149 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002150
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002151 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2152 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2153 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002154 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2155 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002156 continue;
2157 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002158
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002159 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002160 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002161 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002162 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002163 break;
2164 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002165
Adam Cohen59400422014-03-05 18:07:04 -08002166 if (!customWidget) {
2167 String providerName =
2168 appWidgetInfo.providerName.flattenToString();
2169 if (!providerName.equals(savedProvider) ||
2170 (appWidgetInfo.restoreStatus != restoreStatus)) {
2171 ContentValues values = new ContentValues();
2172 values.put(
2173 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2174 providerName);
2175 values.put(LauncherSettings.Favorites.RESTORED,
2176 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002177 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002178 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002179 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002180 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2181 sBgAppWidgets.add(appWidgetInfo);
2182 }
Joe Onorato36115782010-06-17 13:28:48 -04002183 break;
2184 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002185 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002186 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002187 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002188 }
2189 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002190 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002191 }
2192
Winson Chungba9c37f2013-08-30 14:11:37 -07002193 // Break early if we've stopped loading
2194 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002195 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002196 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002197 }
2198
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002199 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002200 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002201 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2202 Utilities.createDbSelectionQuery(
2203 LauncherSettings.Favorites._ID, itemsToRemove), null);
2204 if (DEBUG_LOADERS) {
2205 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2206 LauncherSettings.Favorites._ID, itemsToRemove));
2207 }
2208
2209 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07002210 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
2211 .call(contentResolver,
2212 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
2213 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
2214 for (long folderId : deletedFolderIds) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002215 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2216 sBgFolders.remove(folderId);
2217 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002218 }
2219 }
2220
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002221 // Unpin shortcuts that don't exist on the workspace.
2222 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
2223 MutableInt numTimesPinned = sBgPinnedShortcutCounts.get(key);
2224 if (numTimesPinned == null || numTimesPinned.value == 0) {
2225 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
2226 mDeepShortcutManager.unpinShortcut(key);
2227 }
2228 }
2229
Sunny Goyal317698b2015-07-29 11:45:41 -07002230 // Sort all the folder items and make sure the first 3 items are high resolution.
2231 for (FolderInfo folder : sBgFolders) {
2232 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2233 int pos = 0;
2234 for (ShortcutInfo info : folder.contents) {
2235 if (info.usingLowResIcon) {
2236 info.updateIcon(mIconCache, false);
2237 }
2238 pos ++;
2239 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2240 break;
2241 }
2242 }
2243 }
2244
Chris Wrenf4d08112014-01-16 18:13:56 -05002245 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002246 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002247 ContentValues values = new ContentValues();
2248 values.put(LauncherSettings.Favorites.RESTORED, 0);
2249 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2250 Utilities.createDbSelectionQuery(
2251 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002252 }
2253
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002254 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2255 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002256 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002257 null, sWorker);
2258 }
2259
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002260 // Remove any empty screens
2261 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002262 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002263 long screenId = item.screenId;
2264 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2265 unusedScreens.contains(screenId)) {
2266 unusedScreens.remove(screenId);
2267 }
2268 }
2269
2270 // If there are any empty screens remove them, and update.
2271 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002272 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002273 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002274 }
2275
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002276 if (DEBUG_LOADERS) {
2277 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2278 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002279 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002280 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002281 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002282
Sunny Goyale2df0622015-04-24 11:27:00 -07002283 for (int i = 0; i < nScreens; i++) {
2284 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002285 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002286 line += " | ";
2287 }
Joe Onorato36115782010-06-17 13:28:48 -04002288 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002289 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002290 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002291 }
Joe Onorato36115782010-06-17 13:28:48 -04002292 }
Adam Cohene25af792013-06-06 23:08:25 -07002293 }
2294
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002295 /**
2296 * Partially updates the item without any notification. Must be called on the worker thread.
2297 */
2298 private void updateItem(long itemId, ContentValues update) {
2299 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002300 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002301 update,
2302 BaseColumns._ID + "= ?",
2303 new String[]{Long.toString(itemId)});
2304 }
2305
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002306 /** Filters the set of items who are directly or indirectly (via another container) on the
2307 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002308 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002309 ArrayList<ItemInfo> allWorkspaceItems,
2310 ArrayList<ItemInfo> currentScreenItems,
2311 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002312 // Purge any null ItemInfos
2313 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2314 while (iter.hasNext()) {
2315 ItemInfo i = iter.next();
2316 if (i == null) {
2317 iter.remove();
2318 }
2319 }
2320
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002321 // Order the set of items by their containers first, this allows use to walk through the
2322 // list sequentially, build up a list of containers that are in the specified screen,
2323 // as well as all items in those containers.
2324 Set<Long> itemsOnScreen = new HashSet<Long>();
2325 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2326 @Override
2327 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07002328 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002329 }
2330 });
2331 for (ItemInfo info : allWorkspaceItems) {
2332 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002333 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002334 currentScreenItems.add(info);
2335 itemsOnScreen.add(info.id);
2336 } else {
2337 otherScreenItems.add(info);
2338 }
2339 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2340 currentScreenItems.add(info);
2341 itemsOnScreen.add(info.id);
2342 } else {
2343 if (itemsOnScreen.contains(info.container)) {
2344 currentScreenItems.add(info);
2345 itemsOnScreen.add(info.id);
2346 } else {
2347 otherScreenItems.add(info);
2348 }
2349 }
2350 }
2351 }
2352
2353 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002354 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002355 ArrayList<LauncherAppWidgetInfo> appWidgets,
2356 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2357 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002358
2359 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002360 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002361 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002362 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002363 currentScreenWidgets.add(widget);
2364 } else {
2365 otherScreenWidgets.add(widget);
2366 }
2367 }
2368 }
2369
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002370 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2371 * right) */
2372 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002373 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002374 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002375 final int screenCols = profile.numColumns;
2376 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002377 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002378 @Override
2379 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002380 if (lhs.container == rhs.container) {
2381 // Within containers, order by their spatial position in that container
2382 switch ((int) lhs.container) {
2383 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2384 long lr = (lhs.screenId * screenCellCount +
2385 lhs.cellY * screenCols + lhs.cellX);
2386 long rr = (rhs.screenId * screenCellCount +
2387 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07002388 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07002389 }
2390 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2391 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07002392 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07002393 }
2394 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002395 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002396 throw new RuntimeException("Unexpected container type when " +
2397 "sorting workspace items.");
2398 }
2399 return 0;
2400 }
2401 } else {
2402 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07002403 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07002404 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002405 }
2406 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002407 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002408
Adam Cohendcd297f2013-06-18 13:13:40 -07002409 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2410 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002411 final Runnable r = new Runnable() {
2412 @Override
2413 public void run() {
2414 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2415 if (callbacks != null) {
2416 callbacks.bindScreens(orderedScreens);
2417 }
2418 }
2419 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002420 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002421 }
2422
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002423 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2424 final ArrayList<ItemInfo> workspaceItems,
2425 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002426 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002427
2428 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002429 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002430 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002431 final int start = i;
2432 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002433 final Runnable r = new Runnable() {
2434 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002435 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002436 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002437 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002438 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2439 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002440 }
2441 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002442 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002443 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002444 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002445
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002446 // Bind the widgets, one at a time
2447 N = appWidgets.size();
2448 for (int i = 0; i < N; i++) {
2449 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2450 final Runnable r = new Runnable() {
2451 public void run() {
2452 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2453 if (callbacks != null) {
2454 callbacks.bindAppWidget(widget);
2455 }
2456 }
2457 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002458 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002459 }
2460 }
2461
2462 /**
2463 * Binds all loaded data to actual views on the main thread.
2464 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002465 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002466 final long t = SystemClock.uptimeMillis();
2467 Runnable r;
2468
2469 // Don't use these two variables in any of the callback runnables.
2470 // Otherwise we hold a reference to them.
2471 final Callbacks oldCallbacks = mCallbacks.get();
2472 if (oldCallbacks == null) {
2473 // This launcher has exited and nobody bothered to tell us. Just bail.
2474 Log.w(TAG, "LoaderTask running with no launcher");
2475 return;
2476 }
2477
Winson Chung9b9fb962013-11-15 15:39:34 -08002478 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07002479 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
2480 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
2481 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002482
Winson Chung2abf94d2012-07-18 18:16:38 -07002483 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002484 workspaceItems.addAll(sBgWorkspaceItems);
2485 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002486 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002487 }
2488
Derek Prothro7aff3992013-12-10 14:00:37 -05002489 final boolean isLoadingSynchronously =
2490 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002491 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002492 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002493 if (currScreen >= orderedScreenIds.size()) {
2494 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002495 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002496 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002497 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002498 final long currentScreenId = currentScreen < 0
2499 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002500
Winson Chung9b9fb962013-11-15 15:39:34 -08002501 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07002502 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
2503 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
2504 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
2505 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002506
Winson Chung9b9fb962013-11-15 15:39:34 -08002507 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002508 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002509 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002510 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002511 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2512 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2513
2514 // Tell the workspace that we're about to start binding items
2515 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002516 public void run() {
2517 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2518 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002519 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002520 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002521 }
2522 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002523 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002524 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002525
Adam Cohendcd297f2013-06-18 13:13:40 -07002526 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2527
Sunny Goyal527c7d32015-08-28 15:19:36 -07002528 Executor mainExecutor = new DeferredMainThreadExecutor();
2529 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07002530 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002531
Sunny Goyal527c7d32015-08-28 15:19:36 -07002532 // In case of isLoadingSynchronously, only bind the first screen, and defer binding the
2533 // remaining screens after first onDraw is called. This ensures that the first screen
2534 // is immediately visible (eg. during rotation)
2535 // In case of !isLoadingSynchronously, bind all pages one after other.
2536 final Executor deferredExecutor = isLoadingSynchronously ?
2537 new ViewOnDrawExecutor(mHandler) : mainExecutor;
2538
Sunny Goyal44c06432016-04-02 10:56:02 -07002539 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002540
2541 // Tell the workspace that we're done binding items
2542 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002543 public void run() {
2544 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2545 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002546 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002547 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002548
Sunny Goyal639e9062015-08-19 19:17:06 -07002549 mIsLoadingAndBindingWorkspace = false;
2550
2551 // Run all the bind complete runnables after workspace is bound.
2552 if (!mBindCompleteRunnables.isEmpty()) {
2553 synchronized (mBindCompleteRunnables) {
2554 for (final Runnable r : mBindCompleteRunnables) {
2555 runOnWorkerThread(r);
2556 }
2557 mBindCompleteRunnables.clear();
2558 }
2559 }
2560
Winson Chung98e030b2012-05-07 16:01:11 -07002561 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002562 if (DEBUG_LOADERS) {
2563 Log.d(TAG, "bound workspace in "
2564 + (SystemClock.uptimeMillis()-t) + "ms");
2565 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002566
Joe Onorato36115782010-06-17 13:28:48 -04002567 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002568 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002569 deferredExecutor.execute(r);
2570
Winson Chung4a2afa32012-07-19 14:53:05 -07002571 if (isLoadingSynchronously) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002572 r = new Runnable() {
2573 public void run() {
2574 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2575 if (callbacks != null) {
2576 // We are loading synchronously, which means, some of the pages will be
2577 // bound after first draw. Inform the callbacks that page binding is
2578 // not complete, and schedule the remaining pages.
2579 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2580 callbacks.onPageBoundSynchronously(currentScreen);
2581 }
2582 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2583 }
2584 }
2585 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002586 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002587 }
Joe Onorato36115782010-06-17 13:28:48 -04002588 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002589
Joe Onorato36115782010-06-17 13:28:48 -04002590 private void loadAndBindAllApps() {
2591 if (DEBUG_LOADERS) {
2592 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2593 }
2594 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002595 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002596 synchronized (LoaderTask.this) {
2597 if (mStopped) {
2598 return;
2599 }
2600 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002601 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002602 synchronized (LoaderTask.this) {
2603 if (mStopped) {
2604 return;
2605 }
2606 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002607 }
Joe Onorato36115782010-06-17 13:28:48 -04002608 } else {
2609 onlyBindAllApps();
2610 }
2611 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002612
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002613 private void updateIconCache() {
2614 // Ignore packages which have a promise icon.
2615 HashSet<String> packagesToIgnore = new HashSet<>();
2616 synchronized (sBgLock) {
2617 for (ItemInfo info : sBgItemsIdMap) {
2618 if (info instanceof ShortcutInfo) {
2619 ShortcutInfo si = (ShortcutInfo) info;
2620 if (si.isPromise() && si.getTargetComponent() != null) {
2621 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2622 }
2623 } else if (info instanceof LauncherAppWidgetInfo) {
2624 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2625 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2626 packagesToIgnore.add(lawi.providerName.getPackageName());
2627 }
2628 }
2629 }
2630 }
2631 mIconCache.updateDbIcons(packagesToIgnore);
2632 }
2633
Joe Onorato36115782010-06-17 13:28:48 -04002634 private void onlyBindAllApps() {
2635 final Callbacks oldCallbacks = mCallbacks.get();
2636 if (oldCallbacks == null) {
2637 // This launcher has exited and nobody bothered to tell us. Just bail.
2638 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2639 return;
2640 }
2641
2642 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002643 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002644 final ArrayList<AppInfo> list
2645 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002646 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002647 public void run() {
2648 final long t = SystemClock.uptimeMillis();
2649 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2650 if (callbacks != null) {
2651 callbacks.bindAllApplications(list);
2652 }
2653 if (DEBUG_LOADERS) {
2654 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002655 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04002656 }
2657 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002658 };
2659 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002660 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002661 r.run();
2662 } else {
2663 mHandler.post(r);
2664 }
Joe Onorato36115782010-06-17 13:28:48 -04002665 }
2666
Winson Chung64359a52013-07-08 17:17:08 -07002667 private void loadAllApps() {
2668 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002669
Joe Onorato36115782010-06-17 13:28:48 -04002670 final Callbacks oldCallbacks = mCallbacks.get();
2671 if (oldCallbacks == null) {
2672 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002673 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002674 return;
2675 }
2676
Kenny Guyed131872014-04-30 03:02:21 +01002677 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2678
Winson Chung64359a52013-07-08 17:17:08 -07002679 // Clear the list of apps
2680 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002681 for (UserHandleCompat user : profiles) {
2682 // Query for the set of apps
2683 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002684 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002685 if (DEBUG_LOADERS) {
2686 Log.d(TAG, "getActivityList took "
2687 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2688 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2689 }
2690 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002691 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002692 if (apps == null || apps.isEmpty()) {
2693 return;
2694 }
Kenny Guyff05f432016-01-22 17:48:29 +00002695 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002696 // Create the ApplicationInfos
2697 for (int i = 0; i < apps.size(); i++) {
2698 LauncherActivityInfoCompat app = apps.get(i);
2699 // This builds the icon bitmaps.
Kenny Guyff05f432016-01-22 17:48:29 +00002700 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode));
Kenny Guyed131872014-04-30 03:02:21 +01002701 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002702
Sunny Goyal756a28a2015-04-23 17:07:55 -07002703 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2704 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002705 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002706
2707 @Override
2708 public void run() {
2709 heuristic.processUserApps(apps);
2710 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002711 };
2712 runOnMainThread(new Runnable() {
2713
2714 @Override
2715 public void run() {
2716 // Check isLoadingWorkspace on the UI thread, as it is updated on
2717 // the UI thread.
2718 if (mIsLoadingAndBindingWorkspace) {
2719 synchronized (mBindCompleteRunnables) {
2720 mBindCompleteRunnables.add(r);
2721 }
2722 } else {
2723 runOnWorkerThread(r);
2724 }
2725 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002726 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002727 }
Winson Chung64359a52013-07-08 17:17:08 -07002728 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002729 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002730 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2731 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002732
2733 // Post callback on main thread
2734 mHandler.post(new Runnable() {
2735 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002736
Winson Chung64359a52013-07-08 17:17:08 -07002737 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002738 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002739 if (callbacks != null) {
2740 callbacks.bindAllApplications(added);
2741 if (DEBUG_LOADERS) {
2742 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002743 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07002744 }
2745 } else {
2746 Log.i(TAG, "not binding apps: no Launcher activity");
2747 }
2748 }
2749 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002750 // Cleanup any data stored for a deleted user.
2751 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04002752 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002753 Log.d(TAG, "Icons processed in "
2754 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002755 }
2756 }
2757
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002758 private void loadAndBindDeepShortcuts() {
2759 if (DEBUG_LOADERS) {
2760 Log.d(TAG, "loadAndBindDeepShortcuts mDeepShortcutsLoaded=" + mDeepShortcutsLoaded);
2761 }
2762 if (!mDeepShortcutsLoaded) {
2763 mBgDeepShortcutMap.clear();
2764 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
2765 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager
2766 .queryForAllShortcuts(user);
2767 updateDeepShortcutMap(null, shortcuts);
2768 }
2769 synchronized (LoaderTask.this) {
2770 if (mStopped) {
2771 return;
2772 }
2773 mDeepShortcutsLoaded = true;
2774 }
2775 }
2776 bindDeepShortcutMapOnMainThread();
2777 }
2778
Joe Onoratobe386092009-11-17 17:32:16 -08002779 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002780 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002781 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002782 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2783 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2784 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2785 }
Joe Onorato36115782010-06-17 13:28:48 -04002786 }
2787 }
2788
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002789 // Clear all the shortcuts for the given package, and re-add the new shortcuts.
2790 private void updateDeepShortcutMap(String packageName, List<ShortcutInfoCompat> shortcuts) {
2791 // Remove all keys associated with the given package.
2792 if (packageName != null) {
2793 Iterator<ComponentKey> keysIter = mBgDeepShortcutMap.keySet().iterator();
2794 while (keysIter.hasNext()) {
2795 if (keysIter.next().componentName.getPackageName().equals(packageName)) {
2796 keysIter.remove();
2797 }
2798 }
2799 }
2800
2801 // Now add the new shortcuts to the map.
2802 for (ShortcutInfoCompat shortcut : shortcuts) {
2803 ComponentKey targetComponent
2804 = new ComponentKey(shortcut.getActivity(), shortcut.getUserHandle());
2805 mBgDeepShortcutMap.addToList(targetComponent, shortcut.getId());
2806 }
2807 }
2808
2809 private void bindDeepShortcutMapOnMainThread() {
2810 final MultiHashMap<ComponentKey, String> shortcutMapCopy = new MultiHashMap<>();
2811 shortcutMapCopy.putAll(mBgDeepShortcutMap);
2812 mHandler.post(new Runnable() {
2813 @Override
2814 public void run() {
2815 Callbacks callbacks = getCallback();
2816 if (callbacks != null) {
2817 callbacks.bindDeepShortcutMap(shortcutMapCopy);
2818 }
2819 }
2820 });
2821 }
2822
Sunny Goyal75b0f552015-05-20 21:57:06 -07002823 /**
2824 * Called when the icons for packages have been updated in the icon cache.
2825 */
2826 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2827 final Callbacks callbacks = getCallback();
2828 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2829 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2830
2831 // If any package icon has changed (app was updated while launcher was dead),
2832 // update the corresponding shortcuts.
2833 synchronized (sBgLock) {
2834 for (ItemInfo info : sBgItemsIdMap) {
2835 if (info instanceof ShortcutInfo && user.equals(info.user)
2836 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2837 ShortcutInfo si = (ShortcutInfo) info;
2838 ComponentName cn = si.getTargetComponent();
2839 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2840 si.updateIcon(mIconCache);
2841 updatedShortcuts.add(si);
2842 }
2843 }
2844 }
2845 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2846 }
2847
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002848 bindUpdatedShortcuts(updatedShortcuts, user);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002849
2850 if (!updatedApps.isEmpty()) {
2851 mHandler.post(new Runnable() {
2852
2853 public void run() {
2854 Callbacks cb = getCallback();
2855 if (cb != null && callbacks == cb) {
2856 cb.bindAppsUpdated(updatedApps);
2857 }
2858 }
2859 });
2860 }
2861 }
2862
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002863 private void bindUpdatedShortcuts(final ArrayList<ShortcutInfo> updatedShortcuts,
2864 UserHandleCompat user) {
2865 if (!updatedShortcuts.isEmpty()) {
2866 final Callbacks callbacks = getCallback();
2867 final UserHandleCompat userFinal = user;
2868 mHandler.post(new Runnable() {
2869
2870 public void run() {
2871 Callbacks cb = getCallback();
2872 if (cb != null && callbacks == cb) {
2873 cb.bindShortcutsChanged(updatedShortcuts,
2874 new ArrayList<ShortcutInfo>(), userFinal);
2875 }
2876 }
2877 });
2878 }
2879 }
2880
2881 void enqueueItemUpdatedTask(Runnable task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002882 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002883 }
2884
Adam Cohen091440a2015-03-18 14:16:05 -07002885 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002886
2887 @Override
2888 public void onReceive(Context context, Intent intent) {
2889 synchronized (sBgLock) {
2890 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2891 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002892 final PackageManager manager = context.getPackageManager();
2893 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2894 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002895 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2896 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002897 packagesRemoved.clear();
2898 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002899 for (String pkg : entry.getValue()) {
2900 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07002901 if (PackageManagerHelper.isAppOnSdcard(manager, pkg)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002902 packagesUnavailable.add(pkg);
2903 } else {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002904 packagesRemoved.add(pkg);
2905 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002906 }
2907 }
2908 if (!packagesRemoved.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002909 enqueueItemUpdatedTask(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002910 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2911 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002912 if (!packagesUnavailable.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002913 enqueueItemUpdatedTask(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
Sunny Goyal1a745e82014-10-02 15:58:31 -07002914 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2915 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002916 }
Sunny Goyal34942622014-08-29 17:20:55 -07002917 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002918 }
2919 }
2920 }
2921
Joe Onorato36115782010-06-17 13:28:48 -04002922 private class PackageUpdatedTask implements Runnable {
2923 int mOp;
2924 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002925 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002926
2927 public static final int OP_NONE = 0;
2928 public static final int OP_ADD = 1;
2929 public static final int OP_UPDATE = 2;
2930 public static final int OP_REMOVE = 3; // uninstlled
2931 public static final int OP_UNAVAILABLE = 4; // external media unmounted
Kenny Guy44cba692016-01-21 19:50:02 +00002932 public static final int OP_SUSPEND = 5; // package suspended
2933 public static final int OP_UNSUSPEND = 6; // package unsuspended
Sunny Goyalda891c12016-03-18 18:29:24 -07002934 public static final int OP_USER_AVAILABILITY_CHANGE = 7; // user available/unavailable
Joe Onorato36115782010-06-17 13:28:48 -04002935
Kenny Guyed131872014-04-30 03:02:21 +01002936 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002937 mOp = op;
2938 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002939 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002940 }
2941
2942 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002943 if (!mHasLoaderCompletedOnce) {
2944 // Loader has not yet run.
2945 return;
2946 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002947 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002948
2949 final String[] packages = mPackages;
2950 final int N = packages.length;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002951 FlagOp flagOp = FlagOp.NO_OP;
Sunny Goyalda891c12016-03-18 18:29:24 -07002952 StringFilter pkgFilter = StringFilter.of(new HashSet<>(Arrays.asList(packages)));
Joe Onorato36115782010-06-17 13:28:48 -04002953 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002954 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002955 for (int i=0; i<N; i++) {
2956 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002957 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002958 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002959 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002960
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002961 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2962 if (heuristic != null) {
2963 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002964 }
Joe Onorato36115782010-06-17 13:28:48 -04002965 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002966 }
Joe Onorato36115782010-06-17 13:28:48 -04002967 case OP_UPDATE:
2968 for (int i=0; i<N; i++) {
2969 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002970 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002971 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002972 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002973 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002974 // Since package was just updated, the target must be available now.
2975 flagOp = FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04002976 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002977 case OP_REMOVE: {
2978 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2979 if (heuristic != null) {
2980 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002981 }
Joe Onorato36115782010-06-17 13:28:48 -04002982 for (int i=0; i<N; i++) {
2983 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002984 mIconCache.removeIconsForPkg(packages[i], mUser);
2985 }
2986 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002987 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002988 case OP_UNAVAILABLE:
2989 for (int i=0; i<N; i++) {
2990 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
2991 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002992 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002993 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002994 flagOp = FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04002995 break;
Kenny Guy44cba692016-01-21 19:50:02 +00002996 case OP_SUSPEND:
2997 case OP_UNSUSPEND:
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002998 flagOp = mOp == OP_SUSPEND ?
2999 FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED) :
3000 FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED);
Sunny Goyal17763cb2016-03-24 13:53:22 -07003001 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.(un)suspend " + N);
Sunny Goyalda891c12016-03-18 18:29:24 -07003002 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
3003 break;
3004 case OP_USER_AVAILABILITY_CHANGE:
3005 flagOp = UserManagerCompat.getInstance(context).isQuietModeEnabled(mUser)
3006 ? FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER)
3007 : FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER);
3008 // We want to update all packages for this user.
3009 pkgFilter = StringFilter.matchesAll();
3010 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
Kenny Guy44cba692016-01-21 19:50:02 +00003011 break;
Joe Onorato36115782010-06-17 13:28:48 -04003012 }
3013
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003014 ArrayList<AppInfo> added = null;
3015 ArrayList<AppInfo> modified = null;
3016 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003017
Adam Cohen487f7dd2012-06-28 18:12:10 -07003018 if (mBgAllAppsList.added.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003019 added = new ArrayList<>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003020 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003021 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003022 if (mBgAllAppsList.modified.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003023 modified = new ArrayList<>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003024 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003025 }
Winson Chung5d55f332012-07-16 20:45:03 -07003026 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003027 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003028 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003029 }
3030
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003031 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps = new HashMap<>();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003032
Joe Onorato36115782010-06-17 13:28:48 -04003033 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003034 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003035 for (AppInfo ai : added) {
3036 addedOrUpdatedApps.put(ai.componentName, ai);
3037 }
Joe Onorato36115782010-06-17 13:28:48 -04003038 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003039
Joe Onorato36115782010-06-17 13:28:48 -04003040 if (modified != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003041 final Callbacks callbacks = getCallback();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003042 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003043 for (AppInfo ai : modified) {
3044 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003045 }
3046
Joe Onorato36115782010-06-17 13:28:48 -04003047 mHandler.post(new Runnable() {
3048 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003049 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003050 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003051 callbacks.bindAppsUpdated(modifiedFinal);
3052 }
3053 }
3054 });
3055 }
Winson Chung83892cc2013-05-01 16:53:33 -07003056
Sunny Goyal4390ace2014-10-13 11:33:11 -07003057 // Update shortcut infos
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003058 if (mOp == OP_ADD || flagOp != FlagOp.NO_OP) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003059 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3060 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3061 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3062
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003063 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003064 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003065 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3066 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003067 boolean infoUpdated = false;
3068 boolean shortcutUpdated = false;
3069
3070 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003071 if ((si.iconResource != null)
Sunny Goyalda891c12016-03-18 18:29:24 -07003072 && pkgFilter.matches(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003073 Bitmap icon = Utilities.createIconBitmap(
3074 si.iconResource.packageName,
3075 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003076 if (icon != null) {
3077 si.setIcon(icon);
3078 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003079 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003080 }
3081 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003082
3083 ComponentName cn = si.getTargetComponent();
Sunny Goyalda891c12016-03-18 18:29:24 -07003084 if (cn != null && pkgFilter.matches(cn.getPackageName())) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003085 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3086
3087 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003088 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3089 // Auto install icon
3090 PackageManager pm = context.getPackageManager();
3091 ResolveInfo matched = pm.resolveActivity(
3092 new Intent(Intent.ACTION_MAIN)
3093 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3094 PackageManager.MATCH_DEFAULT_ONLY);
3095 if (matched == null) {
3096 // Try to find the best match activity.
3097 Intent intent = pm.getLaunchIntentForPackage(
3098 cn.getPackageName());
3099 if (intent != null) {
3100 cn = intent.getComponent();
3101 appInfo = addedOrUpdatedApps.get(cn);
3102 }
3103
3104 if ((intent == null) || (appInfo == null)) {
3105 removedShortcuts.add(si);
3106 continue;
3107 }
3108 si.promisedIntent = intent;
3109 }
3110 }
3111
3112 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003113 if (appInfo != null) {
3114 si.flags = appInfo.flags;
3115 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003116
Sunny Goyal756adbc2015-04-16 15:20:51 -07003117 si.intent = si.promisedIntent;
3118 si.promisedIntent = null;
3119 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003120 infoUpdated = true;
3121 si.updateIcon(mIconCache);
3122 }
3123
3124 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3125 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3126 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003127 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003128 si.contentDescription = appInfo.contentDescription;
3129 infoUpdated = true;
3130 }
3131
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003132 int oldDisabledFlags = si.isDisabled;
3133 si.isDisabled = flagOp.apply(si.isDisabled);
3134 if (si.isDisabled != oldDisabledFlags) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003135 shortcutUpdated = true;
3136 }
3137 }
3138
3139 if (infoUpdated || shortcutUpdated) {
3140 updatedShortcuts.add(si);
3141 }
3142 if (infoUpdated) {
3143 updateItemInDatabase(context, si);
3144 }
Sunny Goyalda891c12016-03-18 18:29:24 -07003145 } else if (info instanceof LauncherAppWidgetInfo && mOp == OP_ADD) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003146 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3147 if (mUser.equals(widgetInfo.user)
3148 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
Sunny Goyalda891c12016-03-18 18:29:24 -07003149 && pkgFilter.matches(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003150 widgetInfo.restoreStatus &=
3151 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3152 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003153
3154 // adding this flag ensures that launcher shows 'click to setup'
3155 // if the widget has a config activity. In case there is no config
3156 // activity, it will be marked as 'restored' during bind.
3157 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3158
Sunny Goyal4390ace2014-10-13 11:33:11 -07003159 widgets.add(widgetInfo);
3160 updateItemInDatabase(context, widgetInfo);
3161 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003162 }
3163 }
3164 }
3165
Sunny Goyal4390ace2014-10-13 11:33:11 -07003166 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003167 final Callbacks callbacks = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003168 mHandler.post(new Runnable() {
3169
3170 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003171 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003172 if (callbacks == cb && cb != null) {
3173 callbacks.bindShortcutsChanged(
3174 updatedShortcuts, removedShortcuts, mUser);
3175 }
3176 }
3177 });
3178 if (!removedShortcuts.isEmpty()) {
3179 deleteItemsFromDatabase(context, removedShortcuts);
3180 }
3181 }
3182 if (!widgets.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003183 final Callbacks callbacks = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003184 mHandler.post(new Runnable() {
3185 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003186 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003187 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003188 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003189 }
3190 }
3191 });
3192 }
3193 }
3194
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003195 final HashSet<String> removedPackages = new HashSet<>();
3196 final HashSet<ComponentName> removedComponents = new HashSet<>();
3197 if (mOp == OP_REMOVE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003198 // Mark all packages in the broadcast to be removed
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003199 Collections.addAll(removedPackages, packages);
3200
3201 // No need to update the removedComponents as
3202 // removedPackages is a super-set of removedComponents
Winson Chungdf95eb12013-10-16 14:57:07 -07003203 } else if (mOp == OP_UPDATE) {
3204 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003205 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003206 if (isPackageDisabled(context, packages[i], mUser)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003207 removedPackages.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003208 }
3209 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003210
3211 // Update removedComponents as some components can get removed during package update
3212 for (AppInfo info : removedApps) {
3213 removedComponents.add(info.componentName);
3214 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003215 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003216
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003217 if (!removedPackages.isEmpty() || !removedComponents.isEmpty()) {
3218 for (String pn : removedPackages) {
3219 deletePackageFromDatabase(context, pn, mUser);
3220 }
3221 for (ComponentName cn : removedComponents) {
3222 deleteItemsFromDatabase(context, getItemInfoForComponentName(cn, mUser));
Sunny Goyal1a745e82014-10-02 15:58:31 -07003223 }
3224
Winson Chungdf95eb12013-10-16 14:57:07 -07003225 // Remove any queued items from the install queue
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003226 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackages, mUser);
3227
Winson Chungdf95eb12013-10-16 14:57:07 -07003228 // Call the components-removed callback
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003229 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003230 mHandler.post(new Runnable() {
3231 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003232 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003233 if (callbacks == cb && cb != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003234 callbacks.bindWorkspaceComponentsRemoved(
3235 removedPackages, removedComponents, mUser);
3236 }
3237 }
3238 });
3239 }
3240
3241 if (!removedApps.isEmpty()) {
3242 // Remove corresponding apps from All-Apps
3243 final Callbacks callbacks = getCallback();
3244 mHandler.post(new Runnable() {
3245 public void run() {
3246 Callbacks cb = getCallback();
3247 if (callbacks == cb && cb != null) {
3248 callbacks.bindAppInfosRemoved(removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04003249 }
3250 }
3251 });
Joe Onoratobe386092009-11-17 17:32:16 -08003252 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003253
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003254 // Notify launcher of widget update. From marshmallow onwards we use AppWidgetHost to
3255 // get widget update signals.
3256 if (!Utilities.ATLEAST_MARSHMALLOW &&
3257 (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003258 final Callbacks callbacks = getCallback();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003259 mHandler.post(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003260 public void run() {
3261 Callbacks cb = getCallback();
3262 if (callbacks == cb && cb != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003263 callbacks.notifyWidgetProvidersChanged();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003264 }
3265 }
3266 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003267 }
3268 }
3269 }
3270
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003271 private class ShortcutsChangedTask implements Runnable {
3272 private String mPackageName;
3273 private List<ShortcutInfoCompat> mShortcuts;
3274 private UserHandleCompat mUser;
3275
3276 public ShortcutsChangedTask(String packageName, List<ShortcutInfoCompat> shortcuts,
3277 UserHandleCompat user) {
3278 mPackageName = packageName;
3279 mShortcuts = shortcuts;
3280 mUser = user;
3281 }
3282
3283 @Override
3284 public void run() {
3285 mDeepShortcutManager.onShortcutsChanged(mShortcuts);
3286
3287 Map<String, ShortcutInfoCompat> idsToShortcuts = new HashMap<>();
3288 for (ShortcutInfoCompat shortcut : mShortcuts) {
3289 idsToShortcuts.put(shortcut.getId(), shortcut);
3290 }
3291
3292 // Find ShortcutInfo's that have changed on the workspace.
3293 MultiHashMap<String, ShortcutInfo> idsToWorkspaceShortcutInfos = new MultiHashMap<>();
3294 for (ItemInfo itemInfo : sBgItemsIdMap) {
3295 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
3296 ShortcutInfo si = (ShortcutInfo) itemInfo;
3297 String shortcutId = si.getDeepShortcutId();
3298 if (idsToShortcuts.containsKey(shortcutId)) {
3299 idsToWorkspaceShortcutInfos.addToList(shortcutId, si);
3300 }
3301 }
3302 }
3303
3304 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3305 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager.queryForFullDetails(
3306 mPackageName, new ArrayList<>(idsToWorkspaceShortcutInfos.keySet()), mUser);
3307 ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3308 Context context = LauncherAppState.getInstance().getContext();
3309 for (ShortcutInfoCompat fullDetails : shortcuts) {
3310 List<ShortcutInfo> shortcutInfos = idsToWorkspaceShortcutInfos
3311 .get(fullDetails.getId());
3312 for (ShortcutInfo shortcutInfo : shortcutInfos) {
3313 shortcutInfo.updateFromDeepShortcutInfo(fullDetails, context, mLauncherApps);
3314 updatedShortcutInfos.add(shortcutInfo);
3315 }
3316 }
3317 bindUpdatedShortcuts(updatedShortcutInfos, mUser);
3318
3319 // Update the deep shortcut map, in case the list of ids has changed for an activity.
3320 updateDeepShortcutMap(mPackageName, mShortcuts);
3321 bindDeepShortcutMapOnMainThread();
3322 }
3323 }
3324
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003325 private void bindWidgetsModel(final Callbacks callbacks, final WidgetsModel model) {
3326 mHandler.post(new Runnable() {
3327 @Override
3328 public void run() {
3329 Callbacks cb = getCallback();
3330 if (callbacks == cb && cb != null) {
3331 callbacks.bindWidgetsModel(model);
3332 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003333 }
3334 });
3335 }
3336
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003337 public void refreshAndBindWidgetsAndShortcuts(
3338 final Callbacks callbacks, final boolean bindFirst) {
3339 runOnWorkerThread(new Runnable() {
3340 @Override
3341 public void run() {
3342 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
3343 bindWidgetsModel(callbacks, mBgWidgetsModel.clone());
Sunny Goyal31860582015-09-18 08:38:57 -07003344 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003345 final WidgetsModel model = mBgWidgetsModel.updateAndClone(mApp.getContext());
3346 bindWidgetsModel(callbacks, model);
3347 // update the Widget entries inside DB on the worker thread.
3348 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3349 model.getRawList());
Sunny Goyal31860582015-09-18 08:38:57 -07003350 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003351 });
Michael Jurkac402cd92013-05-20 15:49:32 +02003352 }
3353
Adam Cohen091440a2015-03-18 14:16:05 -07003354 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003355 UserHandleCompat user) {
3356 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3357 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003358 }
Adam Cohen556f6132014-01-15 15:18:08 -08003359
Kenny Guyed131872014-04-30 03:02:21 +01003360 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3361 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003362 if (cn == null) {
3363 return false;
3364 }
Kenny Guyed131872014-04-30 03:02:21 +01003365 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3366 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003367 return false;
3368 }
Kenny Guyed131872014-04-30 03:02:21 +01003369 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003370 }
3371
Adam Cohena28b78e2014-05-20 17:03:04 -07003372 public static boolean isValidPackage(Context context, String packageName,
3373 UserHandleCompat user) {
3374 if (packageName == null) {
3375 return false;
3376 }
3377 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3378 return launcherApps.isPackageEnabledForProfile(packageName, user);
3379 }
3380
Joe Onorato9c1289c2009-08-17 11:03:03 -04003381 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003382 * Make an ShortcutInfo object for a restored application or shortcut item that points
3383 * to a package that is not yet installed on the system.
3384 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003385 public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07003386 int promiseType, int itemType, CursorIconInfo iconInfo, Context context) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003387 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003388 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003389
3390 Bitmap icon = iconInfo.loadIcon(c, info, context);
3391 // the fallback icon
3392 if (icon == null) {
3393 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3394 } else {
3395 info.setIcon(icon);
3396 }
Sunny Goyal34942622014-08-29 17:20:55 -07003397
3398 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003399 String title = (c != null) ? c.getString(titleIndex) : null;
Sunny Goyal34942622014-08-29 17:20:55 -07003400 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003401 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003402 }
Sunny Goyal34942622014-08-29 17:20:55 -07003403 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3404 if (TextUtils.isEmpty(info.title)) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003405 info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003406 }
Sunny Goyal34942622014-08-29 17:20:55 -07003407 } else {
3408 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3409 }
3410
Winson Chung82b016c2015-05-08 17:00:10 -07003411 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003412 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003413 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003414 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003415 return info;
3416 }
3417
3418 /**
3419 * Make an Intent object for a restored application or shortcut item that points
3420 * to the market page for the item.
3421 */
Adam Cohen091440a2015-03-18 14:16:05 -07003422 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003423 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003424 return getMarketIntent(componentName.getPackageName());
3425 }
3426
3427 static Intent getMarketIntent(String packageName) {
3428 return new Intent(Intent.ACTION_VIEW)
3429 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003430 .scheme("market")
3431 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003432 .appendQueryParameter("id", packageName)
3433 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003434 }
3435
3436 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003437 * Make an ShortcutInfo object for a shortcut that is an application.
3438 *
3439 * If c is not null, then it will be used to fill in missing data like the title and icon.
3440 */
Sunny Goyald09c3702016-04-06 16:18:20 -07003441 public ShortcutInfo getAppShortcutInfo(Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003442 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003443 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003444 if (user == null) {
3445 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003446 return null;
3447 }
3448
Kenny Guyed131872014-04-30 03:02:21 +01003449 ComponentName componentName = intent.getComponent();
3450 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08003451 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01003452 return null;
3453 }
3454
3455 Intent newIntent = new Intent(intent.getAction(), null);
3456 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3457 newIntent.setComponent(componentName);
3458 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003459 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003460 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3461 return null;
3462 }
3463
3464 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003465 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003466 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3467 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3468 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003469 }
3470
Sunny Goyald09c3702016-04-06 16:18:20 -07003471 if (lai != null && PackageManagerHelper.isAppSuspended(lai.getApplicationInfo())) {
3472 info.isDisabled = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
3473 }
3474
Joe Onorato56d82912010-03-07 14:32:10 -05003475 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003476 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003477 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003478 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003479
Joe Onorato56d82912010-03-07 14:32:10 -05003480 // fall back to the class name of the activity
3481 if (info.title == null) {
3482 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003483 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003484
Joe Onorato9c1289c2009-08-17 11:03:03 -04003485 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003486 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003487 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003488 if (lai != null) {
3489 info.flags = AppInfo.initFlags(lai);
3490 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003491 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003492 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003493
Sunny Goyale2df0622015-04-24 11:27:00 -07003494 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003495 ItemInfoFilter f) {
3496 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3497 for (ItemInfo i : infos) {
3498 if (i instanceof ShortcutInfo) {
3499 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003500 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003501 if (cn != null && f.filterItem(null, info, cn)) {
3502 filtered.add(info);
3503 }
3504 } else if (i instanceof FolderInfo) {
3505 FolderInfo info = (FolderInfo) i;
3506 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003507 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003508 if (cn != null && f.filterItem(info, s, cn)) {
3509 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003510 }
3511 }
Winson Chung64359a52013-07-08 17:17:08 -07003512 } else if (i instanceof LauncherAppWidgetInfo) {
3513 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3514 ComponentName cn = info.providerName;
3515 if (cn != null && f.filterItem(null, info, cn)) {
3516 filtered.add(info);
3517 }
Winson Chung8a435102012-08-30 17:16:53 -07003518 }
3519 }
Winson Chung64359a52013-07-08 17:17:08 -07003520 return new ArrayList<ItemInfo>(filtered);
3521 }
3522
Adam Cohen091440a2015-03-18 14:16:05 -07003523 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003524 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003525 ItemInfoFilter filter = new ItemInfoFilter() {
3526 @Override
3527 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003528 if (info.user == null) {
3529 return cn.equals(cname);
3530 } else {
3531 return cn.equals(cname) && info.user.equals(user);
3532 }
Winson Chung64359a52013-07-08 17:17:08 -07003533 }
3534 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003535 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003536 }
3537
Sunny Goyal1a745e82014-10-02 15:58:31 -07003538 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003539 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003540 */
Adam Cohen091440a2015-03-18 14:16:05 -07003541 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003542 int titleIndex, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003543 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003544 // Non-app shortcuts are only supported for current user.
3545 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003546 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003547
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003548 // TODO: If there's an explicit component and we can't install that, delete it.
3549
Winson Chung82b016c2015-05-08 17:00:10 -07003550 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003551
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003552 Bitmap icon = iconInfo.loadIcon(c, info, context);
3553 // the fallback icon
3554 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003555 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003556 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003557 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003558 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003559 return info;
3560 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003561
Sunny Goyal2350bc92014-10-14 16:42:54 -07003562 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003563 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3564 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3565 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3566
Adam Cohend9198822011-11-22 16:42:47 -08003567 if (intent == null) {
3568 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3569 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3570 return null;
3571 }
3572
Joe Onorato0589f0f2010-02-08 13:44:00 -08003573 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003574 boolean customIcon = false;
3575 ShortcutIconResource iconResource = null;
3576
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003577 if (bitmap instanceof Bitmap) {
3578 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003579 customIcon = true;
3580 } else {
3581 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003582 if (extra instanceof ShortcutIconResource) {
3583 iconResource = (ShortcutIconResource) extra;
3584 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003585 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003586 }
3587 }
3588
Michael Jurkac9d95c52011-08-29 14:03:34 -07003589 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003590
Kenny Guyed131872014-04-30 03:02:21 +01003591 // Only support intents for current user for now. Intents sent from other
3592 // users wouldn't get here without intent forwarding anyway.
3593 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003594 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003595 icon = mIconCache.getDefaultIcon(info.user);
3596 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003597 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003598 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003599
Winson Chung82b016c2015-05-08 17:00:10 -07003600 info.title = Utilities.trim(name);
3601 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003602 info.intent = intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003603 info.iconResource = iconResource;
3604
3605 return info;
3606 }
3607
Joe Onorato9c1289c2009-08-17 11:03:03 -04003608 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003609 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003610 * or make a new one.
3611 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003612 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003613 // See if a placeholder was created for us already
3614 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003615 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003616 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003617 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003618 folders.put(id, folderInfo);
3619 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003620 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003621 }
3622
Joe Onoratobe386092009-11-17 17:32:16 -08003623
Sunny Goyal651077b2014-06-30 14:15:31 -07003624 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3625 return (provider != null) && (provider.provider != null)
3626 && (provider.provider.getPackageName() != null);
3627 }
3628
Joe Onoratobe386092009-11-17 17:32:16 -08003629 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003630 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003631 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3632 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3633 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3634 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003635 if (mLoaderTask != null) {
3636 mLoaderTask.dumpState();
3637 } else {
3638 Log.d(TAG, "mLoaderTask=null");
3639 }
Joe Onoratobe386092009-11-17 17:32:16 -08003640 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003641
3642 public Callbacks getCallback() {
3643 return mCallbacks != null ? mCallbacks.get() : null;
3644 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003645
3646 /**
3647 * @return {@link FolderInfo} if its already loaded.
3648 */
3649 public FolderInfo findFolderById(Long folderId) {
3650 synchronized (sBgLock) {
3651 return sBgFolders.get(folderId);
3652 }
3653 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003654
Sunny Goyal527c7d32015-08-28 15:19:36 -07003655 @Thunk class DeferredMainThreadExecutor implements Executor {
3656
3657 @Override
3658 public void execute(Runnable command) {
3659 runOnMainThread(command);
3660 }
3661 }
3662
Sunny Goyal756adbc2015-04-16 15:20:51 -07003663 /**
3664 * @return the looper for the worker thread which can be used to start background tasks.
3665 */
3666 public static Looper getWorkerLooper() {
3667 return sWorkerThread.getLooper();
3668 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003669}