blob: 3e666541e94becc377e67b93b17bda038378d039 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Romain Guy629de3e2010-01-13 12:20:59 -080019import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070020import android.content.BroadcastReceiver;
21import android.content.ComponentName;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070022import android.content.ContentProviderOperation;
23import android.content.ContentResolver;
24import android.content.ContentValues;
25import android.content.Context;
26import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080027import android.content.Intent.ShortcutIconResource;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070028import android.content.IntentFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.content.pm.PackageManager;
30import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.database.Cursor;
32import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.net.Uri;
Joe Onorato36115782010-06-17 13:28:48 -040034import android.os.Handler;
35import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070036import android.os.Looper;
Joe Onorato0589f0f2010-02-08 13:44:00 -080037import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040039import android.os.SystemClock;
Sunny Goyale26d1002016-06-20 14:52:14 -070040import android.os.Trace;
Chris Wrenc3919c02013-09-18 09:48:33 -040041import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080042import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070043import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080044import android.util.LongSparseArray;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070045import android.util.MutableInt;
Winson Chungc9168342013-06-26 14:54:55 -070046import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010047
Sunny Goyalffe83f12014-08-14 17:39:34 -070048import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010049import com.android.launcher3.compat.LauncherActivityInfoCompat;
50import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070051import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070052import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010053import com.android.launcher3.compat.UserHandleCompat;
54import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070055import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -070056import com.android.launcher3.config.ProviderConfig;
Tony Wickham827cef22016-03-17 15:39:39 -070057import com.android.launcher3.dynamicui.ExtractionUtils;
Sunny Goyal26119432016-02-18 22:09:23 +000058import com.android.launcher3.folder.Folder;
59import com.android.launcher3.folder.FolderIcon;
Sunny Goyal1acb9e92016-05-16 12:41:09 -070060import com.android.launcher3.logging.FileLog;
Sunny Goyalf862a262015-12-14 14:27:38 -080061import com.android.launcher3.model.GridSizeMigrationTask;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070062import com.android.launcher3.model.WidgetsModel;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070063import com.android.launcher3.provider.LauncherDbUtils;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070064import com.android.launcher3.shortcuts.DeepShortcutManager;
65import com.android.launcher3.shortcuts.ShortcutInfoCompat;
66import com.android.launcher3.shortcuts.ShortcutKey;
Robin Lee26ace122015-03-16 19:41:43 +000067import com.android.launcher3.util.ComponentKey;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070068import com.android.launcher3.util.CursorIconInfo;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -070069import com.android.launcher3.util.FlagOp;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070070import com.android.launcher3.util.GridOccupancy;
Sunny Goyale2df0622015-04-24 11:27:00 -070071import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070072import com.android.launcher3.util.ManagedProfileHeuristic;
Sunny Goyald09c3702016-04-06 16:18:20 -070073import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070074import com.android.launcher3.util.Preconditions;
Sunny Goyalda891c12016-03-18 18:29:24 -070075import com.android.launcher3.util.StringFilter;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070076import com.android.launcher3.util.MultiHashMap;
Adam Cohen091440a2015-03-18 14:16:05 -070077import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070078import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080079
Michael Jurkac2f801e2011-07-12 14:19:46 -070080import java.lang.ref.WeakReference;
81import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070082import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070083import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070084import java.util.Arrays;
Michael Jurkac2f801e2011-07-12 14:19:46 -070085import java.util.Collections;
86import java.util.Comparator;
87import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070088import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070089import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070090import java.util.List;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070091import java.util.Map;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070092import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070093import java.util.Set;
Sunny Goyal527c7d32015-08-28 15:19:36 -070094import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -070095
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096/**
97 * Maintains in-memory state of the Launcher. It is expected that there should be only one
98 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070099 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800100 */
Kenny Guyed131872014-04-30 03:02:21 +0100101public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +0100102 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800103 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -0400104 private static final boolean DEBUG_RECEIVER = false;
Sunny Goyal94485362014-09-18 16:13:58 -0700105 private static final boolean REMOVE_UNRESTORED_ICONS = true;
Chris Wrenb358f812014-04-16 13:37:00 -0400106
Joe Onorato9c1289c2009-08-17 11:03:03 -0400107 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700108
Joe Onorato36115782010-06-17 13:28:48 -0400109 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500110 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800111
Adam Cohen091440a2015-03-18 14:16:05 -0700112 @Thunk final LauncherAppState mApp;
113 @Thunk final Object mLock = new Object();
114 @Thunk DeferredHandler mHandler = new DeferredHandler();
115 @Thunk LoaderTask mLoaderTask;
116 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700117 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118
Adam Cohen091440a2015-03-18 14:16:05 -0700119 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700120 static {
121 sWorkerThread.start();
122 }
Adam Cohen091440a2015-03-18 14:16:05 -0700123 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700124
Joe Onoratocc67f472010-06-08 10:54:30 -0700125 // We start off with everything not loaded. After that, we assume that
126 // our monitoring of the package manager provides all updates and we never
127 // need to do a requery. These are only ever touched from the loader thread.
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700128 private boolean mWorkspaceLoaded;
129 private boolean mAllAppsLoaded;
130 private boolean mDeepShortcutsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700131
Sunny Goyal756a28a2015-04-23 17:07:55 -0700132 /**
133 * Set of runnables to be called on the background thread after the workspace binding
134 * is complete.
135 */
136 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
137
Adam Cohen091440a2015-03-18 14:16:05 -0700138 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700140 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800141 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700142 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800143 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800144
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700145 // Maps all launcher activities to the id's of their shortcuts (if they have any).
146 private final MultiHashMap<ComponentKey, String> mBgDeepShortcutMap = new MultiHashMap<>();
147
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700148 // The lock that must be acquired before referencing any static bg data structures. Unlike
149 // other locks, this one can generally be held long-term because we never expect any of these
150 // static data structures to be referenced outside of the worker thread except on the first
151 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700152 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700153
Adam Cohen487f7dd2012-06-28 18:12:10 -0700154 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700155 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700156 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700157
Adam Cohen487f7dd2012-06-28 18:12:10 -0700158 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
159 // created by LauncherModel that are directly on the home screen (however, no widgets or
160 // shortcuts within folders).
161 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700162
Adam Cohen487f7dd2012-06-28 18:12:10 -0700163 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
164 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700165 new ArrayList<LauncherAppWidgetInfo>();
166
Adam Cohen487f7dd2012-06-28 18:12:10 -0700167 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700168 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700169
Adam Cohendcd297f2013-06-18 13:13:40 -0700170 // sBgWorkspaceScreens is the ordered set of workspace screens.
171 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
172
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700173 // sBgPinnedShortcutCounts is the ComponentKey representing a pinned shortcut to the number of
174 // times it is pinned.
175 static final Map<ShortcutKey, MutableInt> sBgPinnedShortcutCounts = new HashMap<>();
176
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700177 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700178 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
179 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700180
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700181 // </ only access in worker thread >
182
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700183 private IconCache mIconCache;
184 private DeepShortcutManager mDeepShortcutManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700186 private final LauncherAppsCompat mLauncherApps;
187 private final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100188
Joe Onorato9c1289c2009-08-17 11:03:03 -0400189 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700190 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400191 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700192 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400193 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700194 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
195 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700196 public void bindScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800197 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400198 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200199 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700200 public void bindAppsAdded(ArrayList<Long> newScreens,
201 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700202 ArrayList<ItemInfo> addAnimated,
203 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200204 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700205 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
206 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
207 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700208 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700209 public void bindWorkspaceComponentsRemoved(
210 HashSet<String> packageNames, HashSet<ComponentName> components,
211 UserHandleCompat user);
212 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800213 public void notifyWidgetProvidersChanged();
214 public void bindWidgetsModel(WidgetsModel model);
Adam Cohen1462de32012-07-24 22:34:36 -0700215 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700216 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700217 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMap);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400218 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219
Winson Chung64359a52013-07-08 17:17:08 -0700220 public interface ItemInfoFilter {
221 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
222 }
223
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700224 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter,
225 DeepShortcutManager deepShortcutManager) {
Winson Chunga6945242014-01-08 14:04:34 -0800226 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400227 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100228 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700229 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800230 mIconCache = iconCache;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700231 mDeepShortcutManager = deepShortcutManager;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800232
Kenny Guyed131872014-04-30 03:02:21 +0100233 mLauncherApps = LauncherAppsCompat.getInstance(context);
234 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800235 }
236
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700237 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
238 * posted on the main thread handler. */
Adam Cohen091440a2015-03-18 14:16:05 -0700239 @Thunk void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700240 if (sWorkerThread.getThreadId() == Process.myTid()) {
241 // If we are on the worker thread, post onto the main handler
242 mHandler.post(r);
243 } else {
244 r.run();
245 }
246 }
247
248 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
249 * posted on the worker thread handler. */
Sunny Goyal639e9062015-08-19 19:17:06 -0700250 @Thunk static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700251 if (sWorkerThread.getThreadId() == Process.myTid()) {
252 r.run();
253 } else {
254 // If we are not on the worker thread, then post to the worker handler
255 sWorker.post(r);
256 }
257 }
258
Sunny Goyal756adbc2015-04-16 15:20:51 -0700259 public void setPackageState(final PackageInstallInfo installInfo) {
260 Runnable updateRunnable = new Runnable() {
261
262 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500263 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700264 synchronized (sBgLock) {
265 final HashSet<ItemInfo> updates = new HashSet<>();
266
267 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
268 // Ignore install success events as they are handled by Package add events.
269 return;
270 }
271
Sunny Goyale2df0622015-04-24 11:27:00 -0700272 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700273 if (info instanceof ShortcutInfo) {
274 ShortcutInfo si = (ShortcutInfo) info;
275 ComponentName cn = si.getTargetComponent();
276 if (si.isPromise() && (cn != null)
277 && installInfo.packageName.equals(cn.getPackageName())) {
278 si.setInstallProgress(installInfo.progress);
279
280 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
281 // Mark this info as broken.
282 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
283 }
284 updates.add(si);
285 }
286 }
287 }
288
289 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
290 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
291 widget.installProgress = installInfo.progress;
292 updates.add(widget);
293 }
294 }
295
296 if (!updates.isEmpty()) {
297 // Push changes to the callback.
298 Runnable r = new Runnable() {
299 public void run() {
300 Callbacks callbacks = getCallback();
301 if (callbacks != null) {
302 callbacks.bindRestoreItemsChange(updates);
303 }
304 }
305 };
306 mHandler.post(r);
307 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500308 }
309 }
310 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700311 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500312 }
313
Sunny Goyal756adbc2015-04-16 15:20:51 -0700314 /**
315 * Updates the icons and label of all pending icons for the provided package name.
316 */
317 public void updateSessionDisplayInfo(final String packageName) {
318 Runnable updateRunnable = new Runnable() {
319
320 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700321 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700322 synchronized (sBgLock) {
323 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
324 final UserHandleCompat user = UserHandleCompat.myUserHandle();
325
Sunny Goyale2df0622015-04-24 11:27:00 -0700326 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700327 if (info instanceof ShortcutInfo) {
328 ShortcutInfo si = (ShortcutInfo) info;
329 ComponentName cn = si.getTargetComponent();
330 if (si.isPromise() && (cn != null)
331 && packageName.equals(cn.getPackageName())) {
332 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
333 // For auto install apps update the icon as well as label.
334 mIconCache.getTitleAndIcon(si,
335 si.promisedIntent, user,
336 si.shouldUseLowResIcon());
337 } else {
338 // Only update the icon for restored apps.
339 si.updateIcon(mIconCache);
340 }
341 updates.add(si);
342 }
343 }
344 }
345
346 if (!updates.isEmpty()) {
347 // Push changes to the callback.
348 Runnable r = new Runnable() {
349 public void run() {
350 Callbacks callbacks = getCallback();
351 if (callbacks != null) {
352 callbacks.bindShortcutsChanged(updates,
353 new ArrayList<ShortcutInfo>(), user);
354 }
355 }
356 };
357 mHandler.post(r);
358 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700359 }
360 }
361 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700362 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700363 }
364
Adam Cohen76a47a12014-02-05 11:47:43 -0800365 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800366 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800367
368 if (allAppsApps == null) {
369 throw new RuntimeException("allAppsApps must not be null");
370 }
371 if (allAppsApps.isEmpty()) {
372 return;
373 }
374
375 // Process the newly added applications and add them to the database first
376 Runnable r = new Runnable() {
377 public void run() {
378 runOnMainThread(new Runnable() {
379 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800380 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800381 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500382 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800383 }
384 }
385 });
386 }
387 };
388 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700389 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800390
Sunny Goyala9116722015-04-29 13:55:58 -0700391 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800392 int[] xy, int spanX, int spanY) {
393 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700394 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700395
396 GridOccupancy occupied = new GridOccupancy(profile.numColumns, profile.numRows);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800397 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700398 for (ItemInfo r : occupiedPos) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700399 occupied.markCells(r, true);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800400 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800401 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700402 return occupied.findVacantCell(xy, spanX, spanY);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800403 }
404
405 /**
406 * Find a position on the screen for the given size or adds a new screen.
407 * @return screenId and the coordinates for the item.
408 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700409 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyalc3642d42015-10-30 10:27:08 -0700410 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800411 ArrayList<Long> workspaceScreens,
412 ArrayList<Long> addedWorkspaceScreensFinal,
413 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700414 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800415
Sunny Goyala9116722015-04-29 13:55:58 -0700416 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700417 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700418 synchronized (sBgLock) {
419 for (ItemInfo info : sBgItemsIdMap) {
420 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
421 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
422 if (items == null) {
423 items = new ArrayList<>();
424 screenItems.put(info.screenId, items);
425 }
426 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800427 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800428 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800429 }
430
431 // Find appropriate space for the item.
432 long screenId = 0;
433 int[] cordinates = new int[2];
434 boolean found = false;
435
436 int screenCount = workspaceScreens.size();
437 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700438 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800439 if (preferredScreenIndex < screenCount) {
440 screenId = workspaceScreens.get(preferredScreenIndex);
441 found = findNextAvailableIconSpaceInScreen(
442 screenItems.get(screenId), cordinates, spanX, spanY);
443 }
444
445 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700446 // Search on any of the screens starting from the first screen.
447 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800448 screenId = workspaceScreens.get(screen);
449 if (findNextAvailableIconSpaceInScreen(
450 screenItems.get(screenId), cordinates, spanX, spanY)) {
451 // We found a space for it
452 found = true;
453 break;
454 }
455 }
456 }
457
458 if (!found) {
459 // Still no position found. Add a new screen to the end.
Sunny Goyald2497482015-09-22 18:24:19 -0700460 screenId = LauncherSettings.Settings.call(context.getContentResolver(),
461 LauncherSettings.Settings.METHOD_NEW_SCREEN_ID)
462 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800463
464 // Save the screen id for binding in the workspace
465 workspaceScreens.add(screenId);
466 addedWorkspaceScreensFinal.add(screenId);
467
468 // If we still can't find an empty space, then God help us all!!!
469 if (!findNextAvailableIconSpaceInScreen(
470 screenItems.get(screenId), cordinates, spanX, spanY)) {
471 throw new RuntimeException("Can't find space to add the item");
472 }
473 }
474 return Pair.create(screenId, cordinates);
475 }
476
477 /**
478 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800479 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700480 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700481 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800482 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800483 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700484 return;
Winson Chung997a9232013-07-24 15:33:46 -0700485 }
Winson Chung64359a52013-07-08 17:17:08 -0700486 // Process the newly added applications and add them to the database first
487 Runnable r = new Runnable() {
488 public void run() {
489 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
490 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
491
Winson Chung76828c82013-08-19 15:43:29 -0700492 // Get the list of workspace screens. We need to append to this list and
493 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
494 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800495 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700496 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800497 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700498 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800499 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700500 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800501 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700502 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800503 }
Winson Chung76828c82013-08-19 15:43:29 -0700504
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800505 // Find appropriate space for the item.
Sunny Goyalc3642d42015-10-30 10:27:08 -0700506 Pair<Long, int[]> coords = findSpaceForItem(context,
Sunny Goyal9eba1fd2015-10-16 08:58:57 -0700507 workspaceScreens, addedWorkspaceScreensFinal, 1, 1);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800508 long screenId = coords.first;
509 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700510
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700511 ItemInfo itemInfo;
512 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
513 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800514 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700515 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700516 } else {
517 throw new RuntimeException("Unexpected info type");
518 }
Winson Chung94d67682013-09-25 16:29:40 -0700519
Winson Chung64359a52013-07-08 17:17:08 -0700520 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700521 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700522 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700523 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700524 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700525 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700526 }
527 }
528
Winson Chung76828c82013-08-19 15:43:29 -0700529 // Update the workspace screens
530 updateWorkspaceScreenOrder(context, workspaceScreens);
531
Adam Cohen76a47a12014-02-05 11:47:43 -0800532 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700533 runOnMainThread(new Runnable() {
534 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800535 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700536 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700537 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
538 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700539 if (!addedShortcutsFinal.isEmpty()) {
540 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
541 long lastScreenId = info.screenId;
542 for (ItemInfo i : addedShortcutsFinal) {
543 if (i.screenId == lastScreenId) {
544 addAnimated.add(i);
545 } else {
546 addNotAnimated.add(i);
547 }
Winson Chung997a9232013-07-24 15:33:46 -0700548 }
549 }
Winson Chungd64d1762013-08-20 14:37:16 -0700550 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800551 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700552 }
Winson Chung64359a52013-07-08 17:17:08 -0700553 }
Winson Chung997a9232013-07-24 15:33:46 -0700554 });
555 }
Winson Chung64359a52013-07-08 17:17:08 -0700556 }
557 };
558 runOnWorkerThread(r);
559 }
560
Joe Onorato9c1289c2009-08-17 11:03:03 -0400561 /**
562 * Adds an item to the DB if it was not created previously, or move it to a new
563 * <container, screen, cellX, cellY>
564 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800565 public static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700566 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400567 if (item.container == ItemInfo.NO_ID) {
568 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700569 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400570 } else {
571 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700572 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800573 }
574 }
575
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700576 static void checkItemInfoLocked(
577 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
578 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
579 if (modelItem != null && item != modelItem) {
580 // check all the data is consistent
581 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
582 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
583 ShortcutInfo shortcut = (ShortcutInfo) item;
584 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
585 modelShortcut.intent.filterEquals(shortcut.intent) &&
586 modelShortcut.id == shortcut.id &&
587 modelShortcut.itemType == shortcut.itemType &&
588 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700589 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700590 modelShortcut.cellX == shortcut.cellX &&
591 modelShortcut.cellY == shortcut.cellY &&
592 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700593 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700594 // For all intents and purposes, this is the same object
595 return;
596 }
597 }
598
599 // the modelItem needs to match up perfectly with item if our model is
600 // to be consistent with the database-- for now, just require
601 // modelItem == item or the equality check above
602 String msg = "item: " + ((item != null) ? item.toString() : "null") +
603 "modelItem: " +
604 ((modelItem != null) ? modelItem.toString() : "null") +
605 "Error: ItemInfo passed to checkItemInfo doesn't match original";
606 RuntimeException e = new RuntimeException(msg);
607 if (stackTrace != null) {
608 e.setStackTrace(stackTrace);
609 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800610 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700611 }
612 }
613
Michael Jurka816474f2012-06-25 14:49:02 -0700614 static void checkItemInfo(final ItemInfo item) {
615 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
616 final long itemId = item.id;
617 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700618 public void run() {
619 synchronized (sBgLock) {
620 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700621 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700622 }
623 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700624 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700625 }
626
Michael Jurkac9d95c52011-08-29 14:03:34 -0700627 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
628 final ItemInfo item, final String callingFunction) {
629 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700630 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700631 final ContentResolver cr = context.getContentResolver();
632
Adam Cohen487f7dd2012-06-28 18:12:10 -0700633 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700634 Runnable r = new Runnable() {
635 public void run() {
636 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700637 updateItemArrays(item, itemId, stackTrace);
638 }
639 };
640 runOnWorkerThread(r);
641 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700642
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700643 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
644 final ArrayList<ItemInfo> items, final String callingFunction) {
645 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700646
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700647 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
648 Runnable r = new Runnable() {
649 public void run() {
650 ArrayList<ContentProviderOperation> ops =
651 new ArrayList<ContentProviderOperation>();
652 int count = items.size();
653 for (int i = 0; i < count; i++) {
654 ItemInfo item = items.get(i);
655 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700656 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700657 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700658
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700659 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
660 updateItemArrays(item, itemId, stackTrace);
661
662 }
663 try {
664 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
665 } catch (Exception e) {
666 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700667 }
668 }
669 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700670 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700671 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700672
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700673 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
674 // Lock on mBgLock *after* the db operation
675 synchronized (sBgLock) {
676 checkItemInfoLocked(itemId, item, stackTrace);
677
678 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
679 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
680 // Item is in a folder, make sure this folder exists
681 if (!sBgFolders.containsKey(item.container)) {
682 // An items container is being set to a that of an item which is not in
683 // the list of Folders.
684 String msg = "item: " + item + " container being set to: " +
685 item.container + ", not in the list of folders";
686 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700687 }
688 }
689
690 // Items are added/removed from the corresponding FolderInfo elsewhere, such
691 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
692 // that are on the desktop, as appropriate
693 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800694 if (modelItem != null &&
695 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
696 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700697 switch (modelItem.itemType) {
698 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
699 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700700 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700701 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
702 if (!sBgWorkspaceItems.contains(modelItem)) {
703 sBgWorkspaceItems.add(modelItem);
704 }
705 break;
706 default:
707 break;
708 }
709 } else {
710 sBgWorkspaceItems.remove(modelItem);
711 }
712 }
713 }
714
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400716 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700717 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700718 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700719 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400720 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400721 item.cellX = cellX;
722 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700723
Winson Chung3d503fb2011-07-13 17:25:49 -0700724 // We store hotseat items in canonical form which is this orientation invariant position
725 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700726 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700727 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700728 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700729 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700730 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700731 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400732
733 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400734 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700735 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
736 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800737 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700738 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400739
Michael Jurkac9d95c52011-08-29 14:03:34 -0700740 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700741 }
742
743 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700744 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
745 * cellX, cellY have already been updated on the ItemInfos.
746 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800747 public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700748 final long container, final int screen) {
749
750 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
751 int count = items.size();
752
753 for (int i = 0; i < count; i++) {
754 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700755 item.container = container;
756
757 // We store hotseat items in canonical form which is this orientation invariant position
758 // in the hotseat
759 if (context instanceof Launcher && screen < 0 &&
760 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700761 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700762 item.cellY);
763 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700764 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700765 }
766
767 final ContentValues values = new ContentValues();
768 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
769 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
770 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800771 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700772 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700773
774 contentValues.add(values);
775 }
776 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
777 }
778
779 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700780 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800781 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700782 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700783 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700784 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800785 item.cellX = cellX;
786 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700787 item.spanX = spanX;
788 item.spanY = spanY;
789
790 // We store hotseat items in canonical form which is this orientation invariant position
791 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700792 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700793 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700794 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700795 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700796 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700797 }
Adam Cohend4844c32011-02-18 19:25:06 -0800798
Adam Cohend4844c32011-02-18 19:25:06 -0800799 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800800 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700801 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
802 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800803 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700804 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
805 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700806 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800807
Michael Jurka816474f2012-06-25 14:49:02 -0700808 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700809 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700810
811 /**
812 * Update an item to the database in a specified container.
813 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700814 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700815 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100816 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700817 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800818 }
819
Sunny Goyal756a28a2015-04-23 17:07:55 -0700820 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700821 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700822 synchronized (mLock) {
823 if (!mHasLoaderCompletedOnce ||
824 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
825 throw new RuntimeException("Trying to add shortcut while loader is running");
826 }
827 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700828 }
829 }
830
Adam Cohend4844c32011-02-18 19:25:06 -0800831 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700832 * Returns true if the shortcuts already exists on the workspace. This must be called after
833 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800834 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700835 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
836 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700837 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700838 if (intent.getComponent() != null) {
839 // If component is not null, an intent with null package will produce
840 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700841 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700842 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700843 intentWithPkg = intent.toUri(0);
844 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700845 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700846 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
847 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700848 }
849 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700850 intentWithPkg = intent.toUri(0);
851 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700852 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700853
854 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700855 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700856 if (item instanceof ShortcutInfo) {
857 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700858 Intent targetIntent = info.promisedIntent == null
859 ? info.intent : info.promisedIntent;
860 if (targetIntent != null && info.user.equals(user)) {
861 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700862 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
863 return true;
864 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700865 }
866 }
867 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700869 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700870 }
871
Joe Onorato9c1289c2009-08-17 11:03:03 -0400872 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400873 * Add an item to the database in a specified container. Sets the container, screen, cellX and
874 * cellY fields of the item. Also assigns an ID to the item.
875 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700876 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700877 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400878 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400879 item.cellX = cellX;
880 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700881 // We store hotseat items in canonical form which is this orientation invariant position
882 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700883 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700884 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700885 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700886 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700887 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700888 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400889
890 final ContentValues values = new ContentValues();
891 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100892 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400893
Sunny Goyald2497482015-09-22 18:24:19 -0700894 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
895 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
896
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700897 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700898
Jason Monk8e19cf22014-03-20 15:06:57 -0400899 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700900 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700901 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700902 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400903
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700904 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700905 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400906 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700907 sBgItemsIdMap.put(item.id, item);
908 switch (item.itemType) {
909 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
910 sBgFolders.put(item.id, (FolderInfo) item);
911 // Fall through
912 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
913 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700914 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700915 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
916 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
917 sBgWorkspaceItems.add(item);
918 } else {
919 if (!sBgFolders.containsKey(item.container)) {
920 // Adding an item to a folder that doesn't exist.
921 String msg = "adding item: " + item + " to a folder that " +
922 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700923 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700924 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700925 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700926 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
927 ShortcutInfo shortcutInfo = (ShortcutInfo) item;
928 ShortcutKey shortcutToPin = new ShortcutKey(
929 shortcutInfo.intent.getPackage(),
930 shortcutInfo.user,
931 shortcutInfo.getDeepShortcutId());
932 incrementPinnedShortcutCount(shortcutToPin, true /* shouldPin */);
933 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700934 break;
935 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
936 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
937 break;
938 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700939 }
940 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700941 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700942 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700943 }
944
Sunny Goyal34942622014-08-29 17:20:55 -0700945 private static ArrayList<ItemInfo> getItemsByPackageName(
946 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700947 ItemInfoFilter filter = new ItemInfoFilter() {
948 @Override
949 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
950 return cn.getPackageName().equals(pn) && info.user.equals(user);
951 }
952 };
Sunny Goyale2df0622015-04-24 11:27:00 -0700953 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -0700954 }
955
956 /**
957 * Removes all the items from the database corresponding to the specified package.
958 */
959 static void deletePackageFromDatabase(Context context, final String pn,
960 final UserHandleCompat user) {
961 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700962 }
963
964 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700965 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -0400966 */
Sunny Goyalfa401a12015-04-10 13:45:42 -0700967 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700968 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
969 items.add(item);
970 deleteItemsFromDatabase(context, items);
971 }
972
973 /**
974 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700975 */
Sunny Goyal4390ace2014-10-13 11:33:11 -0700976 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400977 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -0700978 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700979 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700980 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700981 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700982 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700983
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700984 // Lock on mBgLock *after* the db operation
985 synchronized (sBgLock) {
986 switch (item.itemType) {
987 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
988 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -0700989 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700990 if (info.container == item.id) {
991 // We are deleting a folder which still contains items that
992 // think they are contained by that folder.
993 String msg = "deleting a folder (" + item + ") which still " +
994 "contains items (" + info + ")";
995 Log.e(TAG, msg);
996 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700997 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700998 sBgWorkspaceItems.remove(item);
999 break;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001000 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
1001 ShortcutInfo shortcutInfo = ((ShortcutInfo) item);
1002 ShortcutKey pinnedShortcut = new ShortcutKey(
1003 shortcutInfo.intent.getPackage(),
1004 shortcutInfo.user,
1005 shortcutInfo.getDeepShortcutId());
1006 decrementPinnedShortcutCount(pinnedShortcut);
1007 // Fall through.
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001008 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1009 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1010 sBgWorkspaceItems.remove(item);
1011 break;
1012 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1013 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1014 break;
1015 }
1016 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001017 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001018 }
1019 }
Michael Jurka83df1882011-08-31 20:59:26 -07001020 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001021 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001022 }
1023
1024 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001025 * Decrement the count for the given pinned shortcut, unpinning it if the count becomes 0.
1026 */
1027 private static void decrementPinnedShortcutCount(final ShortcutKey pinnedShortcut) {
1028 synchronized (sBgLock) {
1029 MutableInt count = sBgPinnedShortcutCounts.get(pinnedShortcut);
1030 if (count == null || --count.value == 0) {
1031 LauncherAppState.getInstance().getShortcutManager().unpinShortcut(pinnedShortcut);
1032 }
1033 }
1034 }
1035
1036 /**
1037 * Increment the count for the given shortcut, pinning it if the count becomes 1.
1038 *
1039 * As an optimization, the caller can pass shouldPin == false to avoid
1040 * unnecessary RPC's if the shortcut is already pinned.
1041 */
1042 private static void incrementPinnedShortcutCount(ShortcutKey pinnedShortcut, boolean shouldPin) {
1043 synchronized (sBgLock) {
1044 MutableInt count = sBgPinnedShortcutCounts.get(pinnedShortcut);
1045 if (count == null) {
1046 count = new MutableInt(1);
1047 sBgPinnedShortcutCounts.put(pinnedShortcut, count);
1048 } else {
1049 count.value++;
1050 }
1051 if (shouldPin && count.value == 1) {
1052 LauncherAppState.getInstance().getShortcutManager().pinShortcut(pinnedShortcut);
1053 }
1054 }
1055 }
1056
1057 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001058 * Update the order of the workspace screens in the database. The array list contains
1059 * a list of screen ids in the order that they should appear.
1060 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001061 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001062 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001063 final ContentResolver cr = context.getContentResolver();
1064 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1065
1066 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001067 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001068 while (iter.hasNext()) {
1069 long id = iter.next();
1070 if (id < 0) {
1071 iter.remove();
1072 }
1073 }
1074
1075 Runnable r = new Runnable() {
1076 @Override
1077 public void run() {
Yura085c8532014-02-11 15:15:29 +00001078 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001079 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001080 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001081 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001082 for (int i = 0; i < count; i++) {
1083 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001084 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001085 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1086 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001087 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001088 }
Yura085c8532014-02-11 15:15:29 +00001089
1090 try {
1091 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1092 } catch (Exception ex) {
1093 throw new RuntimeException(ex);
1094 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001095
Winson Chungba9c37f2013-08-30 14:11:37 -07001096 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001097 sBgWorkspaceScreens.clear();
1098 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001099 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001100 }
1101 };
1102 runOnWorkerThread(r);
1103 }
1104
1105 /**
Winsonc0b52fe2015-09-09 16:38:15 -07001106 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001107 */
Winsonc0b52fe2015-09-09 16:38:15 -07001108 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001109 final ContentResolver cr = context.getContentResolver();
1110
Michael Jurkac9d95c52011-08-29 14:03:34 -07001111 Runnable r = new Runnable() {
1112 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001113 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001114 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001115 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001116 sBgItemsIdMap.remove(info.id);
1117 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001118 sBgWorkspaceItems.remove(info);
1119 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001120
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001121 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001122 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001123 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001124 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001125 for (ItemInfo childInfo : info.contents) {
1126 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001127 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001128 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001129 }
1130 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001131 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001132 }
1133
1134 /**
1135 * Set this as the current Launcher activity object for the loader.
1136 */
1137 public void initialize(Callbacks callbacks) {
1138 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001139 Preconditions.assertUIThread();
1140 // Remove any queued UI runnables
1141 mHandler.cancelAll();
1142 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001143 }
1144 }
1145
Kenny Guyed131872014-04-30 03:02:21 +01001146 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001147 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001148 int op = PackageUpdatedTask.OP_UPDATE;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001149 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001150 user));
1151 }
1152
1153 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001154 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001155 int op = PackageUpdatedTask.OP_REMOVE;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001156 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001157 user));
1158 }
1159
1160 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001161 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001162 int op = PackageUpdatedTask.OP_ADD;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001163 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001164 user));
1165 }
1166
1167 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001168 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001169 boolean replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001170 enqueueItemUpdatedTask(
Sunny Goyal144154d2016-03-30 16:47:03 -07001171 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, packageNames, user));
Kenny Guyed131872014-04-30 03:02:21 +01001172 }
1173
1174 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001175 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001176 boolean replacing) {
1177 if (!replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001178 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guyed131872014-04-30 03:02:21 +01001179 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1180 user));
1181 }
Kenny Guyed131872014-04-30 03:02:21 +01001182 }
1183
Kenny Guy44cba692016-01-21 19:50:02 +00001184 @Override
1185 public void onPackagesSuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001186 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001187 PackageUpdatedTask.OP_SUSPEND, packageNames,
1188 user));
1189 }
1190
1191 @Override
1192 public void onPackagesUnsuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001193 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001194 PackageUpdatedTask.OP_UNSUSPEND, packageNames,
1195 user));
1196 }
1197
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001198 @Override
1199 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
1200 UserHandleCompat user) {
1201 enqueueItemUpdatedTask(new ShortcutsChangedTask(packageName, shortcuts, user));
1202 }
1203
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001204 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001205 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1206 * ACTION_PACKAGE_CHANGED.
1207 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001208 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001209 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001210 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001211
Joe Onorato36115782010-06-17 13:28:48 -04001212 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001213 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001214 // If we have changed locale we need to clear out the labels in all apps/workspace.
1215 forceReload();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001216 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
Sunny Goyalda891c12016-03-18 18:29:24 -07001217 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001218 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001219 forceReload();
Rubin Xuac6e5d72016-04-04 16:13:35 +01001220 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1221 LauncherAppsCompat.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
Sunny Goyalda891c12016-03-18 18:29:24 -07001222 UserHandleCompat user = UserHandleCompat.fromIntent(intent);
1223 if (user != null) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001224 enqueueItemUpdatedTask(new PackageUpdatedTask(
Sunny Goyalda891c12016-03-18 18:29:24 -07001225 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE,
1226 new String[0], user));
1227 }
Tony Wickham827cef22016-03-17 15:39:39 -07001228 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
1229 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001230 }
1231 }
1232
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001233 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001234 resetLoadedState(true, true);
1235
Reena Lee93f824a2011-09-23 17:20:28 -07001236 // Do this here because if the launcher activity is running it will be restarted.
1237 // If it's not running startLoaderFromBackground will merely tell it that it needs
1238 // to reload.
1239 startLoaderFromBackground();
1240 }
1241
Winson Chungf0c6ae02012-03-21 16:10:31 -07001242 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1243 synchronized (mLock) {
1244 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1245 // mWorkspaceLoaded to true later
1246 stopLoaderLocked();
1247 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1248 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001249 // Always reset deep shortcuts loaded.
1250 mDeepShortcutsLoaded = false;
Winson Chungf0c6ae02012-03-21 16:10:31 -07001251 }
1252 }
1253
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001254 /**
1255 * When the launcher is in the background, it's possible for it to miss paired
1256 * configuration changes. So whenever we trigger the loader from the background
1257 * tell the launcher that it needs to re-run the loader when it comes back instead
1258 * of doing it now.
1259 */
1260 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001261 Callbacks callbacks = getCallback();
1262 if (callbacks != null) {
1263 // Only actually run the loader if they're not paused.
1264 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -07001265 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001266 }
1267 }
Joe Onorato36115782010-06-17 13:28:48 -04001268 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001269
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001270 /**
1271 * If there is already a loader task running, tell it to stop.
1272 */
1273 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001274 LoaderTask oldTask = mLoaderTask;
1275 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001276 oldTask.stopLocked();
1277 }
Reena Lee93f824a2011-09-23 17:20:28 -07001278 }
1279
Adam Cohen1a85c582014-09-30 09:48:49 -07001280 public boolean isCurrentCallbacks(Callbacks callbacks) {
1281 return (mCallbacks != null && mCallbacks.get() == callbacks);
1282 }
1283
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001284 public void startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001285 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1286 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001287 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -04001288 // Don't bother to start the thread if we know it's not going to do anything
1289 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001290 final Callbacks oldCallbacks = mCallbacks.get();
1291 // Clear any pending bind-runnables from the synchronized load process.
1292 runOnMainThread(new Runnable() {
1293 public void run() {
1294 oldCallbacks.clearPendingBinds();
1295 }
1296 });
1297
Joe Onorato36115782010-06-17 13:28:48 -04001298 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001299 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001300 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Derek Prothro7aff3992013-12-10 14:00:37 -05001301 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001302 && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001303 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1304 } else {
1305 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1306 sWorker.post(mLoaderTask);
1307 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001308 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001309 }
1310 }
1311
Joe Onorato36115782010-06-17 13:28:48 -04001312 public void stopLoader() {
1313 synchronized (mLock) {
1314 if (mLoaderTask != null) {
1315 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001316 }
1317 }
Joe Onorato36115782010-06-17 13:28:48 -04001318 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001319
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001320 /**
1321 * Loads the workspace screen ids in an ordered list.
1322 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001323 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001324 final ContentResolver contentResolver = context.getContentResolver();
1325 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001326
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001327 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001328 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
1329 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -07001330 }
1331
Joe Onorato36115782010-06-17 13:28:48 -04001332 /**
1333 * Runnable for the thread that loads the contents of the launcher:
1334 * - workspace icons
1335 * - widgets
1336 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001337 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -04001338 */
1339 private class LoaderTask implements Runnable {
1340 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001341 private int mPageToBindFirst;
1342
Adam Cohen091440a2015-03-18 14:16:05 -07001343 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001344 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001345 @Thunk boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001346
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001347 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -04001348 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001349 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001350 }
1351
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001352 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001353 mIsLoadingAndBindingWorkspace = true;
1354
Joe Onorato36115782010-06-17 13:28:48 -04001355 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001356 if (DEBUG_LOADERS) {
1357 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001358 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001359
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001360 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001361 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001362 synchronized (LoaderTask.this) {
1363 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001364 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001365 }
1366 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001367 }
1368 }
1369
Joe Onorato36115782010-06-17 13:28:48 -04001370 // Bind the workspace
Sunny Goyal93f878c2016-03-30 17:31:24 -07001371 bindWorkspace(mPageToBindFirst);
Joe Onorato36115782010-06-17 13:28:48 -04001372 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001373
Joe Onorato36115782010-06-17 13:28:48 -04001374 private void waitForIdle() {
1375 // Wait until the either we're stopped or the other threads are done.
1376 // This way we don't start loading all apps until the workspace has settled
1377 // down.
1378 synchronized (LoaderTask.this) {
1379 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001380
Joe Onorato36115782010-06-17 13:28:48 -04001381 mHandler.postIdle(new Runnable() {
1382 public void run() {
1383 synchronized (LoaderTask.this) {
1384 mLoadAndBindStepFinished = true;
1385 if (DEBUG_LOADERS) {
1386 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001387 }
Joe Onorato36115782010-06-17 13:28:48 -04001388 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001389 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001390 }
Joe Onorato36115782010-06-17 13:28:48 -04001391 });
1392
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001393 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001394 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001395 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1396 // wait no longer than 1sec at a time
1397 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001398 } catch (InterruptedException ex) {
1399 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001400 }
1401 }
Joe Onorato36115782010-06-17 13:28:48 -04001402 if (DEBUG_LOADERS) {
1403 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001404 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001405 + "ms for previous step to finish binding");
1406 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001407 }
Joe Onorato36115782010-06-17 13:28:48 -04001408 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001409
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001410 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001411 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001412 // Ensure that we have a valid page index to load synchronously
1413 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1414 "valid page index");
1415 }
1416 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1417 // Ensure that we don't try and bind a specified page when the pages have not been
1418 // loaded already (we should load everything asynchronously in that case)
1419 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1420 }
1421 synchronized (mLock) {
1422 if (mIsLoaderTaskRunning) {
1423 // Ensure that we are never running the background loading at this point since
1424 // we also touch the background collections
1425 throw new RuntimeException("Error! Background loading is already running");
1426 }
1427 }
1428
1429 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1430 // data structures, we can't allow any other thread to touch that data, but because
1431 // this call is synchronous, we can get away with not locking).
1432
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001433 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001434 // operations from the previous activity. We need to ensure that all queued operations
1435 // are executed before any synchronous binding work is done.
1436 mHandler.flush();
1437
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001438 // Divide the set of loaded items into those that we are binding synchronously, and
1439 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001440 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001441 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1442 // arise from that.
1443 onlyBindAllApps();
1444 }
1445
Joe Onorato36115782010-06-17 13:28:48 -04001446 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001447 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001448 if (mStopped) {
1449 return;
1450 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001451 mIsLoaderTaskRunning = true;
1452 }
Joe Onorato36115782010-06-17 13:28:48 -04001453 // Optimize for end-user experience: if the Launcher is up and // running with the
1454 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1455 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001456 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001457 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001458 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001459
Joe Onorato36115782010-06-17 13:28:48 -04001460 if (mStopped) {
1461 break keep_running;
1462 }
1463
Joe Onorato36115782010-06-17 13:28:48 -04001464 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001465
1466 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001467 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1468 loadAndBindAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001469
1470 waitForIdle();
1471
1472 // third step
1473 if (DEBUG_LOADERS) Log.d(TAG, "step 3: loading deep shortcuts");
1474 loadAndBindDeepShortcuts();
Joe Onorato36115782010-06-17 13:28:48 -04001475 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001476
Joe Onorato36115782010-06-17 13:28:48 -04001477 // Clear out this reference, otherwise we end up holding it until all of the
1478 // callback runnables are done.
1479 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001480
Joe Onorato36115782010-06-17 13:28:48 -04001481 synchronized (mLock) {
1482 // If we are still the last one to be scheduled, remove ourselves.
1483 if (mLoaderTask == this) {
1484 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001485 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001486 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001487 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001488 }
Joe Onorato36115782010-06-17 13:28:48 -04001489 }
1490
1491 public void stopLocked() {
1492 synchronized (LoaderTask.this) {
1493 mStopped = true;
1494 this.notify();
1495 }
1496 }
1497
1498 /**
1499 * Gets the callbacks object. If we've been stopped, or if the launcher object
1500 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1501 * object that was around when the deferred message was scheduled, and if there's
1502 * a new Callbacks object around then also return null. This will save us from
1503 * calling onto it with data that will be ignored.
1504 */
1505 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1506 synchronized (mLock) {
1507 if (mStopped) {
1508 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001509 }
Joe Onorato36115782010-06-17 13:28:48 -04001510
1511 if (mCallbacks == null) {
1512 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001513 }
Joe Onorato36115782010-06-17 13:28:48 -04001514
1515 final Callbacks callbacks = mCallbacks.get();
1516 if (callbacks != oldCallbacks) {
1517 return null;
1518 }
1519 if (callbacks == null) {
1520 Log.w(TAG, "no mCallbacks");
1521 return null;
1522 }
1523
1524 return callbacks;
1525 }
1526 }
1527
1528 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001529 private boolean checkItemPlacement(LongArrayMap<GridOccupancy> occupied, ItemInfo item,
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001530 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001531 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001532 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung892c74d2013-08-22 16:15:50 -07001533
Adam Cohendcd297f2013-06-18 13:13:40 -07001534 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001535 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001536 // Return early if we detect that an item is under the hotseat button
Sunny Goyalbb011da2016-06-15 15:42:29 -07001537 if (!FeatureFlags.NO_ALL_APPS_ICON &&
1538 profile.isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001539 Log.e(TAG, "Error loading shortcut into hotseat " + item
1540 + " into position (" + item.screenId + ":" + item.cellX + ","
1541 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001542 return false;
1543 }
1544
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001545 final GridOccupancy hotseatOccupancy =
Dan Sandler295ae182013-12-10 16:05:47 -05001546 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1547
Adam Cohen2e6da152015-05-06 11:42:25 -07001548 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001549 Log.e(TAG, "Error loading shortcut " + item
1550 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001551 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001552 + ")");
1553 return false;
1554 }
1555
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001556 if (hotseatOccupancy != null) {
1557 if (hotseatOccupancy.cells[(int) item.screenId][0]) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001558 Log.e(TAG, "Error loading shortcut into hotseat " + item
1559 + " into position (" + item.screenId + ":" + item.cellX + ","
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001560 + item.cellY + ") already occupied");
Adam Cohendcd297f2013-06-18 13:13:40 -07001561 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001562 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001563 hotseatOccupancy.cells[(int) item.screenId][0] = true;
Dan Sandler295ae182013-12-10 16:05:47 -05001564 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001565 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001566 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001567 final GridOccupancy occupancy = new GridOccupancy(profile.numHotseatIcons, 1);
1568 occupancy.cells[(int) item.screenId][0] = true;
1569 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, occupancy);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001570 return true;
1571 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001572 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1573 if (!workspaceScreens.contains((Long) item.screenId)) {
1574 // The item has an invalid screen id.
1575 return false;
1576 }
1577 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001578 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001579 return true;
1580 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001581
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001582 final int countX = profile.numColumns;
1583 final int countY = profile.numRows;
Adam Cohenae4409d2013-11-26 10:34:59 -08001584 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1585 item.cellX < 0 || item.cellY < 0 ||
1586 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1587 Log.e(TAG, "Error loading shortcut " + item
1588 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1589 + item.cellX + "," + item.cellY
1590 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1591 return false;
1592 }
1593
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001594 if (!occupied.containsKey(item.screenId)) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001595 GridOccupancy screen = new GridOccupancy(countX + 1, countY + 1);
1596 if (item.screenId == Workspace.FIRST_SCREEN_ID) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001597 // Mark the first row as occupied (if the feature is enabled)
1598 // in order to account for the QSB.
1599 screen.markCells(0, 0, countX + 1, 1, FeatureFlags.QSB_ON_FIRST_SCREEN);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001600 }
1601 occupied.put(item.screenId, screen);
Joe Onorato36115782010-06-17 13:28:48 -04001602 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001603 final GridOccupancy occupancy = occupied.get(item.screenId);
Winson Chungf30ad5f2011-08-08 10:55:42 -07001604
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001605 // Check if any workspace icons overlap with each other
1606 if (occupancy.isRegionVacant(item.cellX, item.cellY, item.spanX, item.spanY)) {
1607 occupancy.markCells(item, true);
1608 return true;
1609 } else {
1610 Log.e(TAG, "Error loading shortcut " + item
1611 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1612 + item.cellX + "," + item.cellX + "," + item.spanX + "," + item.spanY
1613 + ") already occupied");
1614 return false;
1615 }
Joe Onorato36115782010-06-17 13:28:48 -04001616 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001617
Winson Chungba9c37f2013-08-30 14:11:37 -07001618 /** Clears all the sBg data structures */
1619 private void clearSBgDataStructures() {
1620 synchronized (sBgLock) {
1621 sBgWorkspaceItems.clear();
1622 sBgAppWidgets.clear();
1623 sBgFolders.clear();
1624 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001625 sBgWorkspaceScreens.clear();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001626 sBgPinnedShortcutCounts.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001627 }
1628 }
1629
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001630 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -07001631 if (LauncherAppState.PROFILE_STARTUP) {
1632 Trace.beginSection("Loading Workspace");
1633 }
Joe Onorato36115782010-06-17 13:28:48 -04001634 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001635
Joe Onorato36115782010-06-17 13:28:48 -04001636 final Context context = mContext;
1637 final ContentResolver contentResolver = context.getContentResolver();
1638 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001639 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001640 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001641 final boolean isSdCardReady = Utilities.isBootCompleted();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001642
Winson Chung892c74d2013-08-22 16:15:50 -07001643 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001644 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001645 int countX = profile.numColumns;
1646 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001647
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001648 boolean clearDb = false;
Sunny Goyalf076eae2016-01-11 12:25:10 -08001649 if (GridSizeMigrationTask.ENABLED &&
1650 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1651 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001652 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001653 }
1654
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001655 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -07001656 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001657 LauncherSettings.Settings.call(contentResolver,
1658 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001659 }
1660
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001661 Log.d(TAG, "loadWorkspace: loading default favorites");
1662 LauncherSettings.Settings.call(contentResolver,
1663 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -07001664
Winson Chung2abf94d2012-07-18 18:16:38 -07001665 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001666 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001667 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001668 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001669 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001670
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001671 final ArrayList<Long> itemsToRemove = new ArrayList<>();
1672 final ArrayList<Long> restoredRows = new ArrayList<>();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001673 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001674 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001675 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001676 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001677
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001678 // +1 for the hotseat (it can be larger than the workspace)
1679 // Load workspace in reverse order to ensure that latest items are loaded first (and
1680 // before any earlier duplicates)
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001681 final LongArrayMap<GridOccupancy> occupied = new LongArrayMap<>();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001682 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001683
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001684 try {
1685 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1686 final int intentIndex = c.getColumnIndexOrThrow
1687 (LauncherSettings.Favorites.INTENT);
1688 final int titleIndex = c.getColumnIndexOrThrow
1689 (LauncherSettings.Favorites.TITLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001690 final int containerIndex = c.getColumnIndexOrThrow(
1691 LauncherSettings.Favorites.CONTAINER);
1692 final int itemTypeIndex = c.getColumnIndexOrThrow(
1693 LauncherSettings.Favorites.ITEM_TYPE);
1694 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1695 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001696 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1697 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001698 final int screenIndex = c.getColumnIndexOrThrow(
1699 LauncherSettings.Favorites.SCREEN);
1700 final int cellXIndex = c.getColumnIndexOrThrow
1701 (LauncherSettings.Favorites.CELLX);
1702 final int cellYIndex = c.getColumnIndexOrThrow
1703 (LauncherSettings.Favorites.CELLY);
1704 final int spanXIndex = c.getColumnIndexOrThrow
1705 (LauncherSettings.Favorites.SPANX);
1706 final int spanYIndex = c.getColumnIndexOrThrow(
1707 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001708 final int rankIndex = c.getColumnIndexOrThrow(
1709 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001710 final int restoredIndex = c.getColumnIndexOrThrow(
1711 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001712 final int profileIdIndex = c.getColumnIndexOrThrow(
1713 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001714 final int optionsIndex = c.getColumnIndexOrThrow(
1715 LauncherSettings.Favorites.OPTIONS);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001716 final CursorIconInfo cursorIconInfo = new CursorIconInfo(c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001717
Sunny Goyal7f834d22015-04-21 10:10:23 -07001718 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
Kenny Guyff05f432016-01-22 17:48:29 +00001719 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyal7f834d22015-04-21 10:10:23 -07001720 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001721 long serialNo = mUserManager.getSerialNumberForUser(user);
1722 allUsers.put(serialNo, user);
1723 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001724
1725 List<ShortcutInfoCompat> pinnedShortcuts = mDeepShortcutManager
1726 .queryForPinnedShortcuts(null, user);
1727 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
1728 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
1729 shortcut);
1730 }
Sunny Goyal7f834d22015-04-21 10:10:23 -07001731 }
1732
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001733 ShortcutInfo info;
1734 String intentDescription;
1735 LauncherAppWidgetInfo appWidgetInfo;
1736 int container;
1737 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001738 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001739 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001740 UserHandleCompat user;
Sunny Goyald09c3702016-04-06 16:18:20 -07001741 String targetPackage;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001742
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001743 while (!mStopped && c.moveToNext()) {
1744 try {
1745 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001746 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001747 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001748 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001749
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001750 switch (itemType) {
1751 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1752 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001753 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001754 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001755 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001756 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001757 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001758 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001759 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001760 boolean itemReplaced = false;
Sunny Goyald09c3702016-04-06 16:18:20 -07001761 targetPackage = null;
Kenny Guyed131872014-04-30 03:02:21 +01001762 if (user == null) {
1763 // User has been deleted remove the item.
1764 itemsToRemove.add(id);
1765 continue;
1766 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001767 try {
1768 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001769 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001770 if (cn != null && cn.getPackageName() != null) {
1771 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1772 cn.getPackageName(), user);
1773 boolean validComponent = validPkg &&
1774 launcherApps.isActivityEnabledForProfile(cn, user);
Sunny Goyald09c3702016-04-06 16:18:20 -07001775 if (validPkg) {
1776 targetPackage = cn.getPackageName();
1777 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001778
1779 if (validComponent) {
1780 if (restored) {
1781 // no special handling necessary for this item
1782 restoredRows.add(id);
1783 restored = false;
1784 }
Kenny Guyff05f432016-01-22 17:48:29 +00001785 if (quietMode.get(serialNumber)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001786 disabledState = ShortcutInfo.FLAG_DISABLED_QUIET_USER;
Kenny Guyff05f432016-01-22 17:48:29 +00001787 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001788 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001789 intent = null;
1790 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1791 // We allow auto install apps to have their intent
1792 // updated after an install.
1793 intent = manager.getLaunchIntentForPackage(
1794 cn.getPackageName());
1795 if (intent != null) {
1796 ContentValues values = new ContentValues();
1797 values.put(LauncherSettings.Favorites.INTENT,
1798 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001799 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001800 }
1801 }
1802
1803 if (intent == null) {
1804 // The app is installed but the component is no
1805 // longer available.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001806 FileLog.d(TAG, "Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001807 itemsToRemove.add(id);
1808 continue;
1809 } else {
1810 // no special handling necessary for this item
1811 restoredRows.add(id);
1812 restored = false;
1813 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001814 } else if (restored) {
1815 // Package is not yet available but might be
1816 // installed later.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001817 FileLog.d(TAG, "package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001818
1819 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1820 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001821 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001822 // App restore has started. Update the flag
1823 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1824 ContentValues values = new ContentValues();
1825 values.put(LauncherSettings.Favorites.RESTORED,
1826 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001827 updateItem(id, values);
1828 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1829 // This is a common app. Try to replace this.
1830 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1831 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1832 if (parser.findDefaultApp()) {
1833 // Default app found. Replace it.
1834 intent = parser.parsedIntent;
1835 cn = intent.getComponent();
1836 ContentValues values = parser.parsedValues;
1837 values.put(LauncherSettings.Favorites.RESTORED, 0);
1838 updateItem(id, values);
1839 restored = false;
1840 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001841
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001842 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001843 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001844 itemsToRemove.add(id);
1845 continue;
1846 }
Sunny Goyal94485362014-09-18 16:13:58 -07001847 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001848 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001849 itemsToRemove.add(id);
1850 continue;
1851 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001852 } else if (PackageManagerHelper.isAppOnSdcard(
1853 manager, cn.getPackageName())) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07001854 // Package is present but not available.
1855 allowMissingTarget = true;
1856 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1857 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001858 // SdCard is not ready yet. Package might get available,
1859 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001860 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001861 HashSet<String> pkgs = sPendingPackages.get(user);
1862 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001863 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001864 sPendingPackages.put(user, pkgs);
1865 }
1866 pkgs.add(cn.getPackageName());
1867 allowMissingTarget = true;
1868 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001869
1870 } else {
1871 // Do not wait for external media load anymore.
1872 // Log the invalid package, and remove it
Sunny Goyal713edfc2016-05-06 09:58:34 -07001873 FileLog.d(TAG, "Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001874 itemsToRemove.add(id);
1875 continue;
Winson Chungee055712013-07-30 14:46:24 -07001876 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001877 } else if (cn == null) {
1878 // For shortcuts with no component, keep them as they are
1879 restoredRows.add(id);
1880 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001881 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001882 } catch (URISyntaxException e) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001883 FileLog.d(TAG, "Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001884 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001885 continue;
1886 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001887
Sunny Goyal34b65272015-03-11 16:56:52 -07001888 boolean useLowResIcon = container >= 0 &&
1889 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1890
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001891 if (itemReplaced) {
1892 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001893 info = getAppShortcutInfo(intent, user, context, null,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001894 cursorIconInfo.iconIndex, titleIndex,
1895 false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001896 } else {
1897 // Don't replace items for other profiles.
1898 itemsToRemove.add(id);
1899 continue;
1900 }
1901 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001902 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal34b65272015-03-11 16:56:52 -07001903 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07001904 promiseType, itemType, cursorIconInfo, context);
Kenny Guyed131872014-04-30 03:02:21 +01001905 intent = getRestoredItemIntent(c, context, intent);
1906 } else {
1907 // Don't restore items for other profiles.
1908 itemsToRemove.add(id);
1909 continue;
1910 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001911 } else if (itemType ==
1912 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001913 info = getAppShortcutInfo(intent, user, context, c,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001914 cursorIconInfo.iconIndex, titleIndex,
1915 allowMissingTarget, useLowResIcon);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001916 } else if (itemType ==
1917 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
1918 String shortcutId = intent.getStringExtra(
1919 ShortcutInfoCompat.EXTRA_SHORTCUT_ID);
1920 String packageName = intent.getPackage();
1921 ShortcutKey key = new ShortcutKey(intent.getPackage(),
1922 user, shortcutId);
1923 ShortcutInfoCompat pinnedShortcut =
1924 shortcutKeyToPinnedShortcuts.get(key);
1925 boolean shouldPin = false; // It's already pinned.
1926 if (pinnedShortcut == null) {
1927 // It shouldn't be possible for a shortcut to be on the
1928 // workspace without being pinned, but if one somehow is,
1929 // we should pin it now to get back to a good state.
1930 Log.w(TAG, "Shortcut was on workspace but wasn't pinned");
1931 // Get full details; incrementing the count will pin it.
1932 List<ShortcutInfoCompat> fullDetails = mDeepShortcutManager
1933 .queryForFullDetails(packageName,
1934 Collections.singletonList(shortcutId), user);
Tonyacd86ff2016-06-26 22:16:46 -07001935 if (fullDetails != null && !fullDetails.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001936 pinnedShortcut = fullDetails.get(0);
1937 shouldPin = true;
1938 }
1939 }
1940 incrementPinnedShortcutCount(key, shouldPin);
Sunny Goyal9994b2b2016-06-23 14:17:24 -07001941 info = ShortcutInfo.fromDeepShortcutInfo(
1942 pinnedShortcut, context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001943 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001944 info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001945
Sunny Goyald09c3702016-04-06 16:18:20 -07001946 // Shortcuts are only available on the primary profile
1947 if (PackageManagerHelper.isAppSuspended(manager, targetPackage)) {
1948 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1949 }
1950
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001951 // App shortcuts that used to be automatically added to Launcher
1952 // didn't always have the correct intent flags set, so do that
1953 // here
1954 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001955 intent.getCategories() != null &&
1956 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001957 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001958 intent.addFlags(
1959 Intent.FLAG_ACTIVITY_NEW_TASK |
1960 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1961 }
Michael Jurka96879562012-03-22 05:54:33 -07001962 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001963
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001964 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001965 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001966 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001967 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001968 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001969 info.cellX = c.getInt(cellXIndex);
1970 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08001971 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001972 info.spanX = 1;
1973 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01001974 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001975 if (info.promisedIntent != null) {
1976 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
1977 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001978 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001979 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1980 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1981 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001982
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001983 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001984 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001985 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001986 break;
1987 }
1988
Sunny Goyal756adbc2015-04-16 15:20:51 -07001989 if (restored) {
1990 ComponentName cn = info.getTargetComponent();
1991 if (cn != null) {
1992 Integer progress = installingPkgs.get(cn.getPackageName());
1993 if (progress != null) {
1994 info.setInstallProgress(progress);
1995 } else {
1996 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
1997 }
1998 }
1999 }
2000
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002001 switch (container) {
2002 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2003 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2004 sBgWorkspaceItems.add(info);
2005 break;
2006 default:
2007 // Item is in a user folder
2008 FolderInfo folderInfo =
2009 findOrMakeFolder(sBgFolders, container);
Sunny Goyalc52ba712016-04-05 15:59:05 -07002010 folderInfo.add(info, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002011 break;
2012 }
2013 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002014 } else {
2015 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002016 }
2017 break;
2018
2019 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2020 id = c.getLong(idIndex);
2021 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2022
Sunny Goyala508e4f2015-05-21 09:33:57 -07002023 // Do not trim the folder label, as is was set by the user.
2024 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002025 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002026 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002027 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002028 folderInfo.cellX = c.getInt(cellXIndex);
2029 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002030 folderInfo.spanX = 1;
2031 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002032 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002033
Daniel Sandler8802e962010-05-26 16:28:16 -04002034 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002035 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002036 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002037 break;
2038 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002039
Joe Onorato9c1289c2009-08-17 11:03:03 -04002040 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002041 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2042 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2043 sBgWorkspaceItems.add(folderInfo);
2044 break;
Joe Onorato36115782010-06-17 13:28:48 -04002045 }
Joe Onorato17a89222011-02-08 17:26:11 -08002046
Chris Wrenf4d08112014-01-16 18:13:56 -05002047 if (restored) {
2048 // no special handling required for restored folders
2049 restoredRows.add(id);
2050 }
2051
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002052 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2053 sBgFolders.put(folderInfo.id, folderInfo);
2054 break;
2055
2056 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002057 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002058 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002059 boolean customWidget = itemType ==
2060 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2061
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002062 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002063 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002064 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002065 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002066 user = allUsers.get(serialNumber);
2067 if (user == null) {
2068 itemsToRemove.add(id);
2069 continue;
2070 }
2071
Sunny Goyalff572272014-07-23 13:58:07 -07002072 final ComponentName component =
2073 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002074
Sunny Goyal651077b2014-06-30 14:15:31 -07002075 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002076 final boolean isIdValid = (restoreStatus &
2077 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002078 final boolean wasProviderReady = (restoreStatus &
2079 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002080
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002081 if (widgetProvidersMap == null) {
2082 widgetProvidersMap = AppWidgetManagerCompat
2083 .getInstance(mContext).getAllProvidersMap();
2084 }
2085 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
2086 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00002087 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002088 user));
Sunny Goyalff572272014-07-23 13:58:07 -07002089
2090 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002091 if (!isSafeMode && !customWidget &&
2092 wasProviderReady && !isProviderReady) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002093 FileLog.d(TAG, "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07002094 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002095 itemsToRemove.add(id);
2096 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002097 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002098 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2099 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002100
Sunny Goyal53f96722015-07-13 19:54:53 -07002101 // The provider is available. So the widget is either
2102 // available or not available. We do not need to track
2103 // any future restore updates.
2104 int status = restoreStatus &
2105 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002106 if (!wasProviderReady) {
2107 // If provider was not previously ready, update the
2108 // status and UI flag.
2109
2110 // Id would be valid only if the widget restore broadcast was received.
2111 if (isIdValid) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002112 status = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002113 } else {
2114 status &= ~LauncherAppWidgetInfo
2115 .FLAG_PROVIDER_NOT_READY;
2116 }
2117 }
2118 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002119 } else {
2120 Log.v(TAG, "Widget restore pending id=" + id
2121 + " appWidgetId=" + appWidgetId
2122 + " status =" + restoreStatus);
2123 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002124 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002125 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002126 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002127
2128 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2129 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002130 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002131 // App restore has started. Update the flag
2132 appWidgetInfo.restoreStatus |=
2133 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002134 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002135 FileLog.d(TAG, "Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07002136 itemsToRemove.add(id);
2137 continue;
2138 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002139
2140 appWidgetInfo.installProgress =
2141 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002142 }
Sunny Goyalff572272014-07-23 13:58:07 -07002143
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002144 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002145 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002146 appWidgetInfo.cellX = c.getInt(cellXIndex);
2147 appWidgetInfo.cellY = c.getInt(cellYIndex);
2148 appWidgetInfo.spanX = c.getInt(spanXIndex);
2149 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002150 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002151
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002152 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2153 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2154 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002155 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2156 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002157 continue;
2158 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002159
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002160 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002161 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002162 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002163 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002164 break;
2165 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002166
Adam Cohen59400422014-03-05 18:07:04 -08002167 if (!customWidget) {
2168 String providerName =
2169 appWidgetInfo.providerName.flattenToString();
2170 if (!providerName.equals(savedProvider) ||
2171 (appWidgetInfo.restoreStatus != restoreStatus)) {
2172 ContentValues values = new ContentValues();
2173 values.put(
2174 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2175 providerName);
2176 values.put(LauncherSettings.Favorites.RESTORED,
2177 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002178 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002179 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002180 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002181 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2182 sBgAppWidgets.add(appWidgetInfo);
2183 }
Joe Onorato36115782010-06-17 13:28:48 -04002184 break;
2185 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002186 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002187 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002188 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002189 }
2190 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002191 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002192 }
2193
Winson Chungba9c37f2013-08-30 14:11:37 -07002194 // Break early if we've stopped loading
2195 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002196 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002197 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002198 }
2199
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002200 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002201 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002202 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2203 Utilities.createDbSelectionQuery(
2204 LauncherSettings.Favorites._ID, itemsToRemove), null);
2205 if (DEBUG_LOADERS) {
2206 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2207 LauncherSettings.Favorites._ID, itemsToRemove));
2208 }
2209
2210 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07002211 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
2212 .call(contentResolver,
2213 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
2214 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
2215 for (long folderId : deletedFolderIds) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002216 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2217 sBgFolders.remove(folderId);
2218 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002219 }
2220 }
2221
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002222 // Unpin shortcuts that don't exist on the workspace.
2223 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
2224 MutableInt numTimesPinned = sBgPinnedShortcutCounts.get(key);
2225 if (numTimesPinned == null || numTimesPinned.value == 0) {
2226 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
2227 mDeepShortcutManager.unpinShortcut(key);
2228 }
2229 }
2230
Sunny Goyal317698b2015-07-29 11:45:41 -07002231 // Sort all the folder items and make sure the first 3 items are high resolution.
2232 for (FolderInfo folder : sBgFolders) {
2233 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2234 int pos = 0;
2235 for (ShortcutInfo info : folder.contents) {
2236 if (info.usingLowResIcon) {
2237 info.updateIcon(mIconCache, false);
2238 }
2239 pos ++;
2240 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2241 break;
2242 }
2243 }
2244 }
2245
Chris Wrenf4d08112014-01-16 18:13:56 -05002246 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002247 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002248 ContentValues values = new ContentValues();
2249 values.put(LauncherSettings.Favorites.RESTORED, 0);
2250 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2251 Utilities.createDbSelectionQuery(
2252 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002253 }
2254
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002255 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2256 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002257 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002258 null, sWorker);
2259 }
2260
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002261 // Remove any empty screens
2262 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002263 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002264 long screenId = item.screenId;
2265 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2266 unusedScreens.contains(screenId)) {
2267 unusedScreens.remove(screenId);
2268 }
2269 }
2270
2271 // If there are any empty screens remove them, and update.
2272 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002273 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002274 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002275 }
2276
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002277 if (DEBUG_LOADERS) {
2278 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2279 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002280 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002281 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002282 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002283
Sunny Goyale2df0622015-04-24 11:27:00 -07002284 for (int i = 0; i < nScreens; i++) {
2285 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002286 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002287 line += " | ";
2288 }
Joe Onorato36115782010-06-17 13:28:48 -04002289 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002290 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002291 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002292 }
Joe Onorato36115782010-06-17 13:28:48 -04002293 }
Sunny Goyale26d1002016-06-20 14:52:14 -07002294 if (LauncherAppState.PROFILE_STARTUP) {
2295 Trace.endSection();
2296 }
Adam Cohene25af792013-06-06 23:08:25 -07002297 }
2298
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002299 /**
2300 * Partially updates the item without any notification. Must be called on the worker thread.
2301 */
2302 private void updateItem(long itemId, ContentValues update) {
2303 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002304 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002305 update,
2306 BaseColumns._ID + "= ?",
2307 new String[]{Long.toString(itemId)});
2308 }
2309
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002310 /** Filters the set of items who are directly or indirectly (via another container) on the
2311 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002312 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002313 ArrayList<ItemInfo> allWorkspaceItems,
2314 ArrayList<ItemInfo> currentScreenItems,
2315 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002316 // Purge any null ItemInfos
2317 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2318 while (iter.hasNext()) {
2319 ItemInfo i = iter.next();
2320 if (i == null) {
2321 iter.remove();
2322 }
2323 }
2324
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002325 // Order the set of items by their containers first, this allows use to walk through the
2326 // list sequentially, build up a list of containers that are in the specified screen,
2327 // as well as all items in those containers.
2328 Set<Long> itemsOnScreen = new HashSet<Long>();
2329 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2330 @Override
2331 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07002332 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002333 }
2334 });
2335 for (ItemInfo info : allWorkspaceItems) {
2336 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002337 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002338 currentScreenItems.add(info);
2339 itemsOnScreen.add(info.id);
2340 } else {
2341 otherScreenItems.add(info);
2342 }
2343 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2344 currentScreenItems.add(info);
2345 itemsOnScreen.add(info.id);
2346 } else {
2347 if (itemsOnScreen.contains(info.container)) {
2348 currentScreenItems.add(info);
2349 itemsOnScreen.add(info.id);
2350 } else {
2351 otherScreenItems.add(info);
2352 }
2353 }
2354 }
2355 }
2356
2357 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002358 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002359 ArrayList<LauncherAppWidgetInfo> appWidgets,
2360 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2361 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002362
2363 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002364 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002365 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002366 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002367 currentScreenWidgets.add(widget);
2368 } else {
2369 otherScreenWidgets.add(widget);
2370 }
2371 }
2372 }
2373
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002374 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2375 * right) */
2376 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002377 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002378 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002379 final int screenCols = profile.numColumns;
2380 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002381 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002382 @Override
2383 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002384 if (lhs.container == rhs.container) {
2385 // Within containers, order by their spatial position in that container
2386 switch ((int) lhs.container) {
2387 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2388 long lr = (lhs.screenId * screenCellCount +
2389 lhs.cellY * screenCols + lhs.cellX);
2390 long rr = (rhs.screenId * screenCellCount +
2391 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07002392 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07002393 }
2394 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2395 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07002396 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07002397 }
2398 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002399 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002400 throw new RuntimeException("Unexpected container type when " +
2401 "sorting workspace items.");
2402 }
2403 return 0;
2404 }
2405 } else {
2406 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07002407 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07002408 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002409 }
2410 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002411 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002412
Adam Cohendcd297f2013-06-18 13:13:40 -07002413 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2414 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002415 final Runnable r = new Runnable() {
2416 @Override
2417 public void run() {
2418 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2419 if (callbacks != null) {
2420 callbacks.bindScreens(orderedScreens);
2421 }
2422 }
2423 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002424 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002425 }
2426
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002427 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2428 final ArrayList<ItemInfo> workspaceItems,
2429 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002430 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002431
2432 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002433 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002434 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002435 final int start = i;
2436 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002437 final Runnable r = new Runnable() {
2438 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002439 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002440 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002441 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002442 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2443 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002444 }
2445 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002446 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002447 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002448 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002449
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002450 // Bind the widgets, one at a time
2451 N = appWidgets.size();
2452 for (int i = 0; i < N; i++) {
2453 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2454 final Runnable r = new Runnable() {
2455 public void run() {
2456 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2457 if (callbacks != null) {
2458 callbacks.bindAppWidget(widget);
2459 }
2460 }
2461 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002462 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002463 }
2464 }
2465
2466 /**
2467 * Binds all loaded data to actual views on the main thread.
2468 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002469 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002470 final long t = SystemClock.uptimeMillis();
2471 Runnable r;
2472
2473 // Don't use these two variables in any of the callback runnables.
2474 // Otherwise we hold a reference to them.
2475 final Callbacks oldCallbacks = mCallbacks.get();
2476 if (oldCallbacks == null) {
2477 // This launcher has exited and nobody bothered to tell us. Just bail.
2478 Log.w(TAG, "LoaderTask running with no launcher");
2479 return;
2480 }
2481
Winson Chung9b9fb962013-11-15 15:39:34 -08002482 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07002483 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
2484 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
2485 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002486
Winson Chung2abf94d2012-07-18 18:16:38 -07002487 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002488 workspaceItems.addAll(sBgWorkspaceItems);
2489 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002490 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002491 }
2492
Derek Prothro7aff3992013-12-10 14:00:37 -05002493 final boolean isLoadingSynchronously =
2494 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002495 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002496 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002497 if (currScreen >= orderedScreenIds.size()) {
2498 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002499 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002500 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002501 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002502 final long currentScreenId = currentScreen < 0
2503 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002504
Winson Chung9b9fb962013-11-15 15:39:34 -08002505 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07002506 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
2507 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
2508 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
2509 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002510
Winson Chung9b9fb962013-11-15 15:39:34 -08002511 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002512 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002513 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002514 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002515 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2516 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2517
2518 // Tell the workspace that we're about to start binding items
2519 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002520 public void run() {
2521 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2522 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002523 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002524 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002525 }
2526 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002527 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002528 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002529
Adam Cohendcd297f2013-06-18 13:13:40 -07002530 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2531
Sunny Goyal527c7d32015-08-28 15:19:36 -07002532 Executor mainExecutor = new DeferredMainThreadExecutor();
2533 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07002534 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002535
Sunny Goyal527c7d32015-08-28 15:19:36 -07002536 // In case of isLoadingSynchronously, only bind the first screen, and defer binding the
2537 // remaining screens after first onDraw is called. This ensures that the first screen
2538 // is immediately visible (eg. during rotation)
2539 // In case of !isLoadingSynchronously, bind all pages one after other.
2540 final Executor deferredExecutor = isLoadingSynchronously ?
2541 new ViewOnDrawExecutor(mHandler) : mainExecutor;
2542
Sunny Goyal44c06432016-04-02 10:56:02 -07002543 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002544
2545 // Tell the workspace that we're done binding items
2546 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002547 public void run() {
2548 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2549 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002550 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002551 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002552
Sunny Goyal639e9062015-08-19 19:17:06 -07002553 mIsLoadingAndBindingWorkspace = false;
2554
2555 // Run all the bind complete runnables after workspace is bound.
2556 if (!mBindCompleteRunnables.isEmpty()) {
2557 synchronized (mBindCompleteRunnables) {
2558 for (final Runnable r : mBindCompleteRunnables) {
2559 runOnWorkerThread(r);
2560 }
2561 mBindCompleteRunnables.clear();
2562 }
2563 }
2564
Winson Chung98e030b2012-05-07 16:01:11 -07002565 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002566 if (DEBUG_LOADERS) {
2567 Log.d(TAG, "bound workspace in "
2568 + (SystemClock.uptimeMillis()-t) + "ms");
2569 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002570
Joe Onorato36115782010-06-17 13:28:48 -04002571 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002572 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002573 deferredExecutor.execute(r);
2574
Winson Chung4a2afa32012-07-19 14:53:05 -07002575 if (isLoadingSynchronously) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002576 r = new Runnable() {
2577 public void run() {
2578 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2579 if (callbacks != null) {
2580 // We are loading synchronously, which means, some of the pages will be
2581 // bound after first draw. Inform the callbacks that page binding is
2582 // not complete, and schedule the remaining pages.
2583 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2584 callbacks.onPageBoundSynchronously(currentScreen);
2585 }
2586 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2587 }
2588 }
2589 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002590 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002591 }
Joe Onorato36115782010-06-17 13:28:48 -04002592 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002593
Joe Onorato36115782010-06-17 13:28:48 -04002594 private void loadAndBindAllApps() {
2595 if (DEBUG_LOADERS) {
2596 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2597 }
2598 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002599 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002600 synchronized (LoaderTask.this) {
2601 if (mStopped) {
2602 return;
2603 }
2604 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002605 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002606 synchronized (LoaderTask.this) {
2607 if (mStopped) {
2608 return;
2609 }
2610 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002611 }
Joe Onorato36115782010-06-17 13:28:48 -04002612 } else {
2613 onlyBindAllApps();
2614 }
2615 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002616
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002617 private void updateIconCache() {
2618 // Ignore packages which have a promise icon.
2619 HashSet<String> packagesToIgnore = new HashSet<>();
2620 synchronized (sBgLock) {
2621 for (ItemInfo info : sBgItemsIdMap) {
2622 if (info instanceof ShortcutInfo) {
2623 ShortcutInfo si = (ShortcutInfo) info;
2624 if (si.isPromise() && si.getTargetComponent() != null) {
2625 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2626 }
2627 } else if (info instanceof LauncherAppWidgetInfo) {
2628 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2629 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2630 packagesToIgnore.add(lawi.providerName.getPackageName());
2631 }
2632 }
2633 }
2634 }
2635 mIconCache.updateDbIcons(packagesToIgnore);
2636 }
2637
Joe Onorato36115782010-06-17 13:28:48 -04002638 private void onlyBindAllApps() {
2639 final Callbacks oldCallbacks = mCallbacks.get();
2640 if (oldCallbacks == null) {
2641 // This launcher has exited and nobody bothered to tell us. Just bail.
2642 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2643 return;
2644 }
2645
2646 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002647 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002648 final ArrayList<AppInfo> list
2649 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002650 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002651 public void run() {
2652 final long t = SystemClock.uptimeMillis();
2653 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2654 if (callbacks != null) {
2655 callbacks.bindAllApplications(list);
2656 }
2657 if (DEBUG_LOADERS) {
2658 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002659 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04002660 }
2661 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002662 };
2663 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002664 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002665 r.run();
2666 } else {
2667 mHandler.post(r);
2668 }
Joe Onorato36115782010-06-17 13:28:48 -04002669 }
2670
Winson Chung64359a52013-07-08 17:17:08 -07002671 private void loadAllApps() {
2672 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002673
Joe Onorato36115782010-06-17 13:28:48 -04002674 final Callbacks oldCallbacks = mCallbacks.get();
2675 if (oldCallbacks == null) {
2676 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002677 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002678 return;
2679 }
2680
Kenny Guyed131872014-04-30 03:02:21 +01002681 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2682
Winson Chung64359a52013-07-08 17:17:08 -07002683 // Clear the list of apps
2684 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002685 for (UserHandleCompat user : profiles) {
2686 // Query for the set of apps
2687 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002688 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002689 if (DEBUG_LOADERS) {
2690 Log.d(TAG, "getActivityList took "
2691 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2692 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2693 }
2694 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002695 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002696 if (apps == null || apps.isEmpty()) {
2697 return;
2698 }
Kenny Guyff05f432016-01-22 17:48:29 +00002699 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002700 // Create the ApplicationInfos
2701 for (int i = 0; i < apps.size(); i++) {
2702 LauncherActivityInfoCompat app = apps.get(i);
2703 // This builds the icon bitmaps.
Kenny Guyff05f432016-01-22 17:48:29 +00002704 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode));
Kenny Guyed131872014-04-30 03:02:21 +01002705 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002706
Sunny Goyal756a28a2015-04-23 17:07:55 -07002707 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2708 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002709 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002710
2711 @Override
2712 public void run() {
2713 heuristic.processUserApps(apps);
2714 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002715 };
2716 runOnMainThread(new Runnable() {
2717
2718 @Override
2719 public void run() {
2720 // Check isLoadingWorkspace on the UI thread, as it is updated on
2721 // the UI thread.
2722 if (mIsLoadingAndBindingWorkspace) {
2723 synchronized (mBindCompleteRunnables) {
2724 mBindCompleteRunnables.add(r);
2725 }
2726 } else {
2727 runOnWorkerThread(r);
2728 }
2729 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002730 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002731 }
Winson Chung64359a52013-07-08 17:17:08 -07002732 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002733 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002734 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2735 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002736
2737 // Post callback on main thread
2738 mHandler.post(new Runnable() {
2739 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002740
Winson Chung64359a52013-07-08 17:17:08 -07002741 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002742 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002743 if (callbacks != null) {
2744 callbacks.bindAllApplications(added);
2745 if (DEBUG_LOADERS) {
2746 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002747 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07002748 }
2749 } else {
2750 Log.i(TAG, "not binding apps: no Launcher activity");
2751 }
2752 }
2753 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002754 // Cleanup any data stored for a deleted user.
2755 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04002756 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002757 Log.d(TAG, "Icons processed in "
2758 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002759 }
2760 }
2761
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002762 private void loadAndBindDeepShortcuts() {
2763 if (DEBUG_LOADERS) {
2764 Log.d(TAG, "loadAndBindDeepShortcuts mDeepShortcutsLoaded=" + mDeepShortcutsLoaded);
2765 }
2766 if (!mDeepShortcutsLoaded) {
2767 mBgDeepShortcutMap.clear();
2768 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
2769 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager
2770 .queryForAllShortcuts(user);
2771 updateDeepShortcutMap(null, shortcuts);
2772 }
2773 synchronized (LoaderTask.this) {
2774 if (mStopped) {
2775 return;
2776 }
2777 mDeepShortcutsLoaded = true;
2778 }
2779 }
2780 bindDeepShortcutMapOnMainThread();
2781 }
2782
Joe Onoratobe386092009-11-17 17:32:16 -08002783 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002784 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002785 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002786 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2787 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2788 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2789 }
Joe Onorato36115782010-06-17 13:28:48 -04002790 }
2791 }
2792
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002793 // Clear all the shortcuts for the given package, and re-add the new shortcuts.
2794 private void updateDeepShortcutMap(String packageName, List<ShortcutInfoCompat> shortcuts) {
2795 // Remove all keys associated with the given package.
2796 if (packageName != null) {
2797 Iterator<ComponentKey> keysIter = mBgDeepShortcutMap.keySet().iterator();
2798 while (keysIter.hasNext()) {
2799 if (keysIter.next().componentName.getPackageName().equals(packageName)) {
2800 keysIter.remove();
2801 }
2802 }
2803 }
2804
2805 // Now add the new shortcuts to the map.
2806 for (ShortcutInfoCompat shortcut : shortcuts) {
2807 ComponentKey targetComponent
2808 = new ComponentKey(shortcut.getActivity(), shortcut.getUserHandle());
2809 mBgDeepShortcutMap.addToList(targetComponent, shortcut.getId());
2810 }
2811 }
2812
2813 private void bindDeepShortcutMapOnMainThread() {
2814 final MultiHashMap<ComponentKey, String> shortcutMapCopy = new MultiHashMap<>();
2815 shortcutMapCopy.putAll(mBgDeepShortcutMap);
2816 mHandler.post(new Runnable() {
2817 @Override
2818 public void run() {
2819 Callbacks callbacks = getCallback();
2820 if (callbacks != null) {
2821 callbacks.bindDeepShortcutMap(shortcutMapCopy);
2822 }
2823 }
2824 });
2825 }
2826
Sunny Goyal75b0f552015-05-20 21:57:06 -07002827 /**
2828 * Called when the icons for packages have been updated in the icon cache.
2829 */
2830 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2831 final Callbacks callbacks = getCallback();
2832 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2833 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2834
2835 // If any package icon has changed (app was updated while launcher was dead),
2836 // update the corresponding shortcuts.
2837 synchronized (sBgLock) {
2838 for (ItemInfo info : sBgItemsIdMap) {
2839 if (info instanceof ShortcutInfo && user.equals(info.user)
2840 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2841 ShortcutInfo si = (ShortcutInfo) info;
2842 ComponentName cn = si.getTargetComponent();
2843 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2844 si.updateIcon(mIconCache);
2845 updatedShortcuts.add(si);
2846 }
2847 }
2848 }
2849 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2850 }
2851
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002852 bindUpdatedShortcuts(updatedShortcuts, user);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002853
2854 if (!updatedApps.isEmpty()) {
2855 mHandler.post(new Runnable() {
2856
2857 public void run() {
2858 Callbacks cb = getCallback();
2859 if (cb != null && callbacks == cb) {
2860 cb.bindAppsUpdated(updatedApps);
2861 }
2862 }
2863 });
2864 }
2865 }
2866
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002867 private void bindUpdatedShortcuts(final ArrayList<ShortcutInfo> updatedShortcuts,
2868 UserHandleCompat user) {
2869 if (!updatedShortcuts.isEmpty()) {
2870 final Callbacks callbacks = getCallback();
2871 final UserHandleCompat userFinal = user;
2872 mHandler.post(new Runnable() {
2873
2874 public void run() {
2875 Callbacks cb = getCallback();
2876 if (cb != null && callbacks == cb) {
2877 cb.bindShortcutsChanged(updatedShortcuts,
2878 new ArrayList<ShortcutInfo>(), userFinal);
2879 }
2880 }
2881 });
2882 }
2883 }
2884
2885 void enqueueItemUpdatedTask(Runnable task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002886 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002887 }
2888
Adam Cohen091440a2015-03-18 14:16:05 -07002889 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002890
2891 @Override
2892 public void onReceive(Context context, Intent intent) {
2893 synchronized (sBgLock) {
2894 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2895 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002896 final PackageManager manager = context.getPackageManager();
2897 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2898 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002899 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2900 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002901 packagesRemoved.clear();
2902 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002903 for (String pkg : entry.getValue()) {
2904 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07002905 if (PackageManagerHelper.isAppOnSdcard(manager, pkg)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002906 packagesUnavailable.add(pkg);
2907 } else {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002908 packagesRemoved.add(pkg);
2909 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002910 }
2911 }
2912 if (!packagesRemoved.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002913 enqueueItemUpdatedTask(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002914 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2915 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002916 if (!packagesUnavailable.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002917 enqueueItemUpdatedTask(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
Sunny Goyal1a745e82014-10-02 15:58:31 -07002918 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2919 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002920 }
Sunny Goyal34942622014-08-29 17:20:55 -07002921 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002922 }
2923 }
2924 }
2925
Joe Onorato36115782010-06-17 13:28:48 -04002926 private class PackageUpdatedTask implements Runnable {
2927 int mOp;
2928 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002929 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002930
2931 public static final int OP_NONE = 0;
2932 public static final int OP_ADD = 1;
2933 public static final int OP_UPDATE = 2;
2934 public static final int OP_REMOVE = 3; // uninstlled
2935 public static final int OP_UNAVAILABLE = 4; // external media unmounted
Kenny Guy44cba692016-01-21 19:50:02 +00002936 public static final int OP_SUSPEND = 5; // package suspended
2937 public static final int OP_UNSUSPEND = 6; // package unsuspended
Sunny Goyalda891c12016-03-18 18:29:24 -07002938 public static final int OP_USER_AVAILABILITY_CHANGE = 7; // user available/unavailable
Joe Onorato36115782010-06-17 13:28:48 -04002939
Kenny Guyed131872014-04-30 03:02:21 +01002940 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002941 mOp = op;
2942 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002943 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002944 }
2945
2946 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002947 if (!mHasLoaderCompletedOnce) {
2948 // Loader has not yet run.
2949 return;
2950 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002951 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002952
2953 final String[] packages = mPackages;
2954 final int N = packages.length;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002955 FlagOp flagOp = FlagOp.NO_OP;
Sunny Goyalda891c12016-03-18 18:29:24 -07002956 StringFilter pkgFilter = StringFilter.of(new HashSet<>(Arrays.asList(packages)));
Joe Onorato36115782010-06-17 13:28:48 -04002957 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002958 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002959 for (int i=0; i<N; i++) {
2960 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002961 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002962 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002963 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002964
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002965 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2966 if (heuristic != null) {
2967 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002968 }
Joe Onorato36115782010-06-17 13:28:48 -04002969 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002970 }
Joe Onorato36115782010-06-17 13:28:48 -04002971 case OP_UPDATE:
2972 for (int i=0; i<N; i++) {
2973 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002974 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01002975 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002976 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002977 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002978 // Since package was just updated, the target must be available now.
2979 flagOp = FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04002980 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002981 case OP_REMOVE: {
2982 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
2983 if (heuristic != null) {
2984 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08002985 }
Joe Onorato36115782010-06-17 13:28:48 -04002986 for (int i=0; i<N; i++) {
2987 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002988 mIconCache.removeIconsForPkg(packages[i], mUser);
2989 }
2990 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002991 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08002992 case OP_UNAVAILABLE:
2993 for (int i=0; i<N; i++) {
2994 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
2995 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07002996 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002997 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002998 flagOp = FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04002999 break;
Kenny Guy44cba692016-01-21 19:50:02 +00003000 case OP_SUSPEND:
3001 case OP_UNSUSPEND:
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003002 flagOp = mOp == OP_SUSPEND ?
3003 FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED) :
3004 FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED);
Sunny Goyal17763cb2016-03-24 13:53:22 -07003005 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.(un)suspend " + N);
Sunny Goyalda891c12016-03-18 18:29:24 -07003006 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
3007 break;
3008 case OP_USER_AVAILABILITY_CHANGE:
3009 flagOp = UserManagerCompat.getInstance(context).isQuietModeEnabled(mUser)
3010 ? FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER)
3011 : FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER);
3012 // We want to update all packages for this user.
3013 pkgFilter = StringFilter.matchesAll();
3014 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
Kenny Guy44cba692016-01-21 19:50:02 +00003015 break;
Joe Onorato36115782010-06-17 13:28:48 -04003016 }
3017
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003018 ArrayList<AppInfo> added = null;
3019 ArrayList<AppInfo> modified = null;
3020 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003021
Adam Cohen487f7dd2012-06-28 18:12:10 -07003022 if (mBgAllAppsList.added.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003023 added = new ArrayList<>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003024 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003025 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003026 if (mBgAllAppsList.modified.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003027 modified = new ArrayList<>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003028 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003029 }
Winson Chung5d55f332012-07-16 20:45:03 -07003030 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003031 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003032 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003033 }
3034
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003035 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps = new HashMap<>();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003036
Joe Onorato36115782010-06-17 13:28:48 -04003037 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003038 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003039 for (AppInfo ai : added) {
3040 addedOrUpdatedApps.put(ai.componentName, ai);
3041 }
Joe Onorato36115782010-06-17 13:28:48 -04003042 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003043
Joe Onorato36115782010-06-17 13:28:48 -04003044 if (modified != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003045 final Callbacks callbacks = getCallback();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003046 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003047 for (AppInfo ai : modified) {
3048 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003049 }
3050
Joe Onorato36115782010-06-17 13:28:48 -04003051 mHandler.post(new Runnable() {
3052 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003053 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003054 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003055 callbacks.bindAppsUpdated(modifiedFinal);
3056 }
3057 }
3058 });
3059 }
Winson Chung83892cc2013-05-01 16:53:33 -07003060
Sunny Goyal4390ace2014-10-13 11:33:11 -07003061 // Update shortcut infos
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003062 if (mOp == OP_ADD || flagOp != FlagOp.NO_OP) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003063 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3064 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3065 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3066
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003067 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003068 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003069 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3070 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003071 boolean infoUpdated = false;
3072 boolean shortcutUpdated = false;
3073
3074 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003075 if ((si.iconResource != null)
Sunny Goyalda891c12016-03-18 18:29:24 -07003076 && pkgFilter.matches(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003077 Bitmap icon = Utilities.createIconBitmap(
3078 si.iconResource.packageName,
3079 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003080 if (icon != null) {
3081 si.setIcon(icon);
3082 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003083 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003084 }
3085 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003086
3087 ComponentName cn = si.getTargetComponent();
Sunny Goyalda891c12016-03-18 18:29:24 -07003088 if (cn != null && pkgFilter.matches(cn.getPackageName())) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003089 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3090
3091 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003092 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3093 // Auto install icon
3094 PackageManager pm = context.getPackageManager();
3095 ResolveInfo matched = pm.resolveActivity(
3096 new Intent(Intent.ACTION_MAIN)
3097 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3098 PackageManager.MATCH_DEFAULT_ONLY);
3099 if (matched == null) {
3100 // Try to find the best match activity.
3101 Intent intent = pm.getLaunchIntentForPackage(
3102 cn.getPackageName());
3103 if (intent != null) {
3104 cn = intent.getComponent();
3105 appInfo = addedOrUpdatedApps.get(cn);
3106 }
3107
3108 if ((intent == null) || (appInfo == null)) {
3109 removedShortcuts.add(si);
3110 continue;
3111 }
3112 si.promisedIntent = intent;
3113 }
3114 }
3115
3116 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003117 if (appInfo != null) {
3118 si.flags = appInfo.flags;
3119 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003120
Sunny Goyal756adbc2015-04-16 15:20:51 -07003121 si.intent = si.promisedIntent;
3122 si.promisedIntent = null;
3123 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003124 infoUpdated = true;
3125 si.updateIcon(mIconCache);
3126 }
3127
3128 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3129 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3130 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003131 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003132 si.contentDescription = appInfo.contentDescription;
3133 infoUpdated = true;
3134 }
3135
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003136 int oldDisabledFlags = si.isDisabled;
3137 si.isDisabled = flagOp.apply(si.isDisabled);
3138 if (si.isDisabled != oldDisabledFlags) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003139 shortcutUpdated = true;
3140 }
3141 }
3142
3143 if (infoUpdated || shortcutUpdated) {
3144 updatedShortcuts.add(si);
3145 }
3146 if (infoUpdated) {
3147 updateItemInDatabase(context, si);
3148 }
Sunny Goyalda891c12016-03-18 18:29:24 -07003149 } else if (info instanceof LauncherAppWidgetInfo && mOp == OP_ADD) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003150 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3151 if (mUser.equals(widgetInfo.user)
3152 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
Sunny Goyalda891c12016-03-18 18:29:24 -07003153 && pkgFilter.matches(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003154 widgetInfo.restoreStatus &=
3155 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3156 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003157
3158 // adding this flag ensures that launcher shows 'click to setup'
3159 // if the widget has a config activity. In case there is no config
3160 // activity, it will be marked as 'restored' during bind.
3161 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3162
Sunny Goyal4390ace2014-10-13 11:33:11 -07003163 widgets.add(widgetInfo);
3164 updateItemInDatabase(context, widgetInfo);
3165 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003166 }
3167 }
3168 }
3169
Sunny Goyal4390ace2014-10-13 11:33:11 -07003170 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003171 final Callbacks callbacks = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003172 mHandler.post(new Runnable() {
3173
3174 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003175 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003176 if (callbacks == cb && cb != null) {
3177 callbacks.bindShortcutsChanged(
3178 updatedShortcuts, removedShortcuts, mUser);
3179 }
3180 }
3181 });
3182 if (!removedShortcuts.isEmpty()) {
3183 deleteItemsFromDatabase(context, removedShortcuts);
3184 }
3185 }
3186 if (!widgets.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003187 final Callbacks callbacks = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003188 mHandler.post(new Runnable() {
3189 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003190 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003191 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003192 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003193 }
3194 }
3195 });
3196 }
3197 }
3198
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003199 final HashSet<String> removedPackages = new HashSet<>();
3200 final HashSet<ComponentName> removedComponents = new HashSet<>();
3201 if (mOp == OP_REMOVE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003202 // Mark all packages in the broadcast to be removed
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003203 Collections.addAll(removedPackages, packages);
3204
3205 // No need to update the removedComponents as
3206 // removedPackages is a super-set of removedComponents
Winson Chungdf95eb12013-10-16 14:57:07 -07003207 } else if (mOp == OP_UPDATE) {
3208 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003209 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003210 if (isPackageDisabled(context, packages[i], mUser)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003211 removedPackages.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003212 }
3213 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003214
3215 // Update removedComponents as some components can get removed during package update
3216 for (AppInfo info : removedApps) {
3217 removedComponents.add(info.componentName);
3218 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003219 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003220
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003221 if (!removedPackages.isEmpty() || !removedComponents.isEmpty()) {
3222 for (String pn : removedPackages) {
3223 deletePackageFromDatabase(context, pn, mUser);
3224 }
3225 for (ComponentName cn : removedComponents) {
3226 deleteItemsFromDatabase(context, getItemInfoForComponentName(cn, mUser));
Sunny Goyal1a745e82014-10-02 15:58:31 -07003227 }
3228
Winson Chungdf95eb12013-10-16 14:57:07 -07003229 // Remove any queued items from the install queue
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003230 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackages, mUser);
3231
Winson Chungdf95eb12013-10-16 14:57:07 -07003232 // Call the components-removed callback
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003233 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003234 mHandler.post(new Runnable() {
3235 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003236 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003237 if (callbacks == cb && cb != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003238 callbacks.bindWorkspaceComponentsRemoved(
3239 removedPackages, removedComponents, mUser);
3240 }
3241 }
3242 });
3243 }
3244
3245 if (!removedApps.isEmpty()) {
3246 // Remove corresponding apps from All-Apps
3247 final Callbacks callbacks = getCallback();
3248 mHandler.post(new Runnable() {
3249 public void run() {
3250 Callbacks cb = getCallback();
3251 if (callbacks == cb && cb != null) {
3252 callbacks.bindAppInfosRemoved(removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04003253 }
3254 }
3255 });
Joe Onoratobe386092009-11-17 17:32:16 -08003256 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003257
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003258 // Notify launcher of widget update. From marshmallow onwards we use AppWidgetHost to
3259 // get widget update signals.
3260 if (!Utilities.ATLEAST_MARSHMALLOW &&
3261 (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003262 final Callbacks callbacks = getCallback();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003263 mHandler.post(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003264 public void run() {
3265 Callbacks cb = getCallback();
3266 if (callbacks == cb && cb != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003267 callbacks.notifyWidgetProvidersChanged();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003268 }
3269 }
3270 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003271 }
3272 }
3273 }
3274
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003275 private class ShortcutsChangedTask implements Runnable {
3276 private String mPackageName;
3277 private List<ShortcutInfoCompat> mShortcuts;
3278 private UserHandleCompat mUser;
3279
3280 public ShortcutsChangedTask(String packageName, List<ShortcutInfoCompat> shortcuts,
3281 UserHandleCompat user) {
3282 mPackageName = packageName;
3283 mShortcuts = shortcuts;
3284 mUser = user;
3285 }
3286
3287 @Override
3288 public void run() {
3289 mDeepShortcutManager.onShortcutsChanged(mShortcuts);
3290
3291 Map<String, ShortcutInfoCompat> idsToShortcuts = new HashMap<>();
3292 for (ShortcutInfoCompat shortcut : mShortcuts) {
3293 idsToShortcuts.put(shortcut.getId(), shortcut);
3294 }
3295
3296 // Find ShortcutInfo's that have changed on the workspace.
3297 MultiHashMap<String, ShortcutInfo> idsToWorkspaceShortcutInfos = new MultiHashMap<>();
3298 for (ItemInfo itemInfo : sBgItemsIdMap) {
3299 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
3300 ShortcutInfo si = (ShortcutInfo) itemInfo;
3301 String shortcutId = si.getDeepShortcutId();
3302 if (idsToShortcuts.containsKey(shortcutId)) {
3303 idsToWorkspaceShortcutInfos.addToList(shortcutId, si);
3304 }
3305 }
3306 }
3307
3308 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3309 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager.queryForFullDetails(
3310 mPackageName, new ArrayList<>(idsToWorkspaceShortcutInfos.keySet()), mUser);
3311 ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3312 Context context = LauncherAppState.getInstance().getContext();
3313 for (ShortcutInfoCompat fullDetails : shortcuts) {
3314 List<ShortcutInfo> shortcutInfos = idsToWorkspaceShortcutInfos
3315 .get(fullDetails.getId());
3316 for (ShortcutInfo shortcutInfo : shortcutInfos) {
Sunny Goyal9994b2b2016-06-23 14:17:24 -07003317 shortcutInfo.updateFromDeepShortcutInfo(fullDetails, context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003318 updatedShortcutInfos.add(shortcutInfo);
3319 }
3320 }
3321 bindUpdatedShortcuts(updatedShortcutInfos, mUser);
3322
3323 // Update the deep shortcut map, in case the list of ids has changed for an activity.
3324 updateDeepShortcutMap(mPackageName, mShortcuts);
3325 bindDeepShortcutMapOnMainThread();
3326 }
3327 }
3328
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003329 private void bindWidgetsModel(final Callbacks callbacks, final WidgetsModel model) {
3330 mHandler.post(new Runnable() {
3331 @Override
3332 public void run() {
3333 Callbacks cb = getCallback();
3334 if (callbacks == cb && cb != null) {
3335 callbacks.bindWidgetsModel(model);
3336 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003337 }
3338 });
3339 }
3340
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003341 public void refreshAndBindWidgetsAndShortcuts(
3342 final Callbacks callbacks, final boolean bindFirst) {
3343 runOnWorkerThread(new Runnable() {
3344 @Override
3345 public void run() {
3346 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
3347 bindWidgetsModel(callbacks, mBgWidgetsModel.clone());
Sunny Goyal31860582015-09-18 08:38:57 -07003348 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003349 final WidgetsModel model = mBgWidgetsModel.updateAndClone(mApp.getContext());
3350 bindWidgetsModel(callbacks, model);
3351 // update the Widget entries inside DB on the worker thread.
3352 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3353 model.getRawList());
Sunny Goyal31860582015-09-18 08:38:57 -07003354 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003355 });
Michael Jurkac402cd92013-05-20 15:49:32 +02003356 }
3357
Adam Cohen091440a2015-03-18 14:16:05 -07003358 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003359 UserHandleCompat user) {
3360 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3361 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003362 }
Adam Cohen556f6132014-01-15 15:18:08 -08003363
Kenny Guyed131872014-04-30 03:02:21 +01003364 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3365 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003366 if (cn == null) {
3367 return false;
3368 }
Kenny Guyed131872014-04-30 03:02:21 +01003369 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3370 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003371 return false;
3372 }
Kenny Guyed131872014-04-30 03:02:21 +01003373 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003374 }
3375
Adam Cohena28b78e2014-05-20 17:03:04 -07003376 public static boolean isValidPackage(Context context, String packageName,
3377 UserHandleCompat user) {
3378 if (packageName == null) {
3379 return false;
3380 }
3381 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3382 return launcherApps.isPackageEnabledForProfile(packageName, user);
3383 }
3384
Joe Onorato9c1289c2009-08-17 11:03:03 -04003385 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003386 * Make an ShortcutInfo object for a restored application or shortcut item that points
3387 * to a package that is not yet installed on the system.
3388 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003389 public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07003390 int promiseType, int itemType, CursorIconInfo iconInfo, Context context) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003391 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003392 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003393
3394 Bitmap icon = iconInfo.loadIcon(c, info, context);
3395 // the fallback icon
3396 if (icon == null) {
3397 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3398 } else {
3399 info.setIcon(icon);
3400 }
Sunny Goyal34942622014-08-29 17:20:55 -07003401
3402 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003403 String title = (c != null) ? c.getString(titleIndex) : null;
Sunny Goyal34942622014-08-29 17:20:55 -07003404 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003405 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003406 }
Sunny Goyal34942622014-08-29 17:20:55 -07003407 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3408 if (TextUtils.isEmpty(info.title)) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003409 info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003410 }
Sunny Goyal34942622014-08-29 17:20:55 -07003411 } else {
3412 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3413 }
3414
Winson Chung82b016c2015-05-08 17:00:10 -07003415 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003416 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003417 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003418 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003419 return info;
3420 }
3421
3422 /**
3423 * Make an Intent object for a restored application or shortcut item that points
3424 * to the market page for the item.
3425 */
Adam Cohen091440a2015-03-18 14:16:05 -07003426 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003427 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003428 return getMarketIntent(componentName.getPackageName());
3429 }
3430
3431 static Intent getMarketIntent(String packageName) {
3432 return new Intent(Intent.ACTION_VIEW)
3433 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003434 .scheme("market")
3435 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003436 .appendQueryParameter("id", packageName)
3437 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003438 }
3439
3440 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003441 * Make an ShortcutInfo object for a shortcut that is an application.
3442 *
3443 * If c is not null, then it will be used to fill in missing data like the title and icon.
3444 */
Sunny Goyald09c3702016-04-06 16:18:20 -07003445 public ShortcutInfo getAppShortcutInfo(Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003446 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003447 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003448 if (user == null) {
3449 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003450 return null;
3451 }
3452
Kenny Guyed131872014-04-30 03:02:21 +01003453 ComponentName componentName = intent.getComponent();
3454 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08003455 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01003456 return null;
3457 }
3458
3459 Intent newIntent = new Intent(intent.getAction(), null);
3460 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3461 newIntent.setComponent(componentName);
3462 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003463 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003464 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3465 return null;
3466 }
3467
3468 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003469 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003470 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3471 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3472 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003473 }
3474
Sunny Goyald09c3702016-04-06 16:18:20 -07003475 if (lai != null && PackageManagerHelper.isAppSuspended(lai.getApplicationInfo())) {
3476 info.isDisabled = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
3477 }
3478
Joe Onorato56d82912010-03-07 14:32:10 -05003479 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003480 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003481 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003482 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003483
Joe Onorato56d82912010-03-07 14:32:10 -05003484 // fall back to the class name of the activity
3485 if (info.title == null) {
3486 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003487 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003488
Joe Onorato9c1289c2009-08-17 11:03:03 -04003489 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003490 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003491 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003492 if (lai != null) {
3493 info.flags = AppInfo.initFlags(lai);
3494 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003495 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003496 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003497
Sunny Goyale2df0622015-04-24 11:27:00 -07003498 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003499 ItemInfoFilter f) {
3500 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3501 for (ItemInfo i : infos) {
3502 if (i instanceof ShortcutInfo) {
3503 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003504 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003505 if (cn != null && f.filterItem(null, info, cn)) {
3506 filtered.add(info);
3507 }
3508 } else if (i instanceof FolderInfo) {
3509 FolderInfo info = (FolderInfo) i;
3510 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003511 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003512 if (cn != null && f.filterItem(info, s, cn)) {
3513 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003514 }
3515 }
Winson Chung64359a52013-07-08 17:17:08 -07003516 } else if (i instanceof LauncherAppWidgetInfo) {
3517 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3518 ComponentName cn = info.providerName;
3519 if (cn != null && f.filterItem(null, info, cn)) {
3520 filtered.add(info);
3521 }
Winson Chung8a435102012-08-30 17:16:53 -07003522 }
3523 }
Winson Chung64359a52013-07-08 17:17:08 -07003524 return new ArrayList<ItemInfo>(filtered);
3525 }
3526
Adam Cohen091440a2015-03-18 14:16:05 -07003527 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003528 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003529 ItemInfoFilter filter = new ItemInfoFilter() {
3530 @Override
3531 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003532 if (info.user == null) {
3533 return cn.equals(cname);
3534 } else {
3535 return cn.equals(cname) && info.user.equals(user);
3536 }
Winson Chung64359a52013-07-08 17:17:08 -07003537 }
3538 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003539 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003540 }
3541
Sunny Goyal1a745e82014-10-02 15:58:31 -07003542 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003543 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003544 */
Adam Cohen091440a2015-03-18 14:16:05 -07003545 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003546 int titleIndex, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003547 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003548 // Non-app shortcuts are only supported for current user.
3549 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003550 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003551
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003552 // TODO: If there's an explicit component and we can't install that, delete it.
3553
Winson Chung82b016c2015-05-08 17:00:10 -07003554 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003555
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003556 Bitmap icon = iconInfo.loadIcon(c, info, context);
3557 // the fallback icon
3558 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003559 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003560 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003561 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003562 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003563 return info;
3564 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003565
Sunny Goyal2350bc92014-10-14 16:42:54 -07003566 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003567 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3568 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3569 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3570
Adam Cohend9198822011-11-22 16:42:47 -08003571 if (intent == null) {
3572 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3573 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3574 return null;
3575 }
3576
Joe Onorato0589f0f2010-02-08 13:44:00 -08003577 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003578 boolean customIcon = false;
3579 ShortcutIconResource iconResource = null;
3580
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003581 if (bitmap instanceof Bitmap) {
3582 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003583 customIcon = true;
3584 } else {
3585 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003586 if (extra instanceof ShortcutIconResource) {
3587 iconResource = (ShortcutIconResource) extra;
3588 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003589 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003590 }
3591 }
3592
Michael Jurkac9d95c52011-08-29 14:03:34 -07003593 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003594
Kenny Guyed131872014-04-30 03:02:21 +01003595 // Only support intents for current user for now. Intents sent from other
3596 // users wouldn't get here without intent forwarding anyway.
3597 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003598 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003599 icon = mIconCache.getDefaultIcon(info.user);
3600 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003601 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003602 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003603
Winson Chung82b016c2015-05-08 17:00:10 -07003604 info.title = Utilities.trim(name);
3605 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003606 info.intent = intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003607 info.iconResource = iconResource;
3608
3609 return info;
3610 }
3611
Joe Onorato9c1289c2009-08-17 11:03:03 -04003612 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003613 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003614 * or make a new one.
3615 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003616 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003617 // See if a placeholder was created for us already
3618 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003619 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003620 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003621 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003622 folders.put(id, folderInfo);
3623 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003624 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003625 }
3626
Joe Onoratobe386092009-11-17 17:32:16 -08003627
Sunny Goyal651077b2014-06-30 14:15:31 -07003628 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3629 return (provider != null) && (provider.provider != null)
3630 && (provider.provider.getPackageName() != null);
3631 }
3632
Joe Onoratobe386092009-11-17 17:32:16 -08003633 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003634 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003635 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3636 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3637 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3638 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003639 if (mLoaderTask != null) {
3640 mLoaderTask.dumpState();
3641 } else {
3642 Log.d(TAG, "mLoaderTask=null");
3643 }
Joe Onoratobe386092009-11-17 17:32:16 -08003644 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003645
3646 public Callbacks getCallback() {
3647 return mCallbacks != null ? mCallbacks.get() : null;
3648 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003649
3650 /**
3651 * @return {@link FolderInfo} if its already loaded.
3652 */
3653 public FolderInfo findFolderById(Long folderId) {
3654 synchronized (sBgLock) {
3655 return sBgFolders.get(folderId);
3656 }
3657 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003658
Sunny Goyal527c7d32015-08-28 15:19:36 -07003659 @Thunk class DeferredMainThreadExecutor implements Executor {
3660
3661 @Override
3662 public void execute(Runnable command) {
3663 runOnMainThread(command);
3664 }
3665 }
3666
Sunny Goyal756adbc2015-04-16 15:20:51 -07003667 /**
3668 * @return the looper for the worker thread which can be used to start background tasks.
3669 */
3670 public static Looper getWorkerLooper() {
3671 return sWorkerThread.getLooper();
3672 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003673}