blob: 9fddd3d8dba04089896e7e5dad99de5306814d00 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Romain Guy629de3e2010-01-13 12:20:59 -080019import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070020import android.content.BroadcastReceiver;
21import android.content.ComponentName;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070022import android.content.ContentProviderOperation;
23import android.content.ContentResolver;
24import android.content.ContentValues;
25import android.content.Context;
26import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080027import android.content.Intent.ShortcutIconResource;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070028import android.content.IntentFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.content.pm.PackageManager;
30import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.database.Cursor;
32import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.net.Uri;
Joe Onorato36115782010-06-17 13:28:48 -040034import android.os.Handler;
35import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070036import android.os.Looper;
Joe Onorato0589f0f2010-02-08 13:44:00 -080037import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040039import android.os.SystemClock;
Sunny Goyale26d1002016-06-20 14:52:14 -070040import android.os.Trace;
Chris Wrenc3919c02013-09-18 09:48:33 -040041import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080042import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070043import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080044import android.util.LongSparseArray;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070045import android.util.MutableInt;
Winson Chungc9168342013-06-26 14:54:55 -070046import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010047
Sunny Goyalffe83f12014-08-14 17:39:34 -070048import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010049import com.android.launcher3.compat.LauncherActivityInfoCompat;
50import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070051import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070052import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010053import com.android.launcher3.compat.UserHandleCompat;
54import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070055import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -070056import com.android.launcher3.config.ProviderConfig;
Tony Wickham827cef22016-03-17 15:39:39 -070057import com.android.launcher3.dynamicui.ExtractionUtils;
Sunny Goyal26119432016-02-18 22:09:23 +000058import com.android.launcher3.folder.Folder;
59import com.android.launcher3.folder.FolderIcon;
Sunny Goyal1acb9e92016-05-16 12:41:09 -070060import com.android.launcher3.logging.FileLog;
Sunny Goyalf862a262015-12-14 14:27:38 -080061import com.android.launcher3.model.GridSizeMigrationTask;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070062import com.android.launcher3.model.WidgetsModel;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -070063import com.android.launcher3.provider.ImportDataTask;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070064import com.android.launcher3.provider.LauncherDbUtils;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070065import com.android.launcher3.shortcuts.DeepShortcutManager;
66import com.android.launcher3.shortcuts.ShortcutInfoCompat;
67import com.android.launcher3.shortcuts.ShortcutKey;
Robin Lee26ace122015-03-16 19:41:43 +000068import com.android.launcher3.util.ComponentKey;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070069import com.android.launcher3.util.CursorIconInfo;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -070070import com.android.launcher3.util.FlagOp;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070071import com.android.launcher3.util.GridOccupancy;
Sunny Goyale2df0622015-04-24 11:27:00 -070072import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070073import com.android.launcher3.util.ManagedProfileHeuristic;
Tony Wickhamd82a39d2016-07-01 15:44:13 -070074import com.android.launcher3.util.MultiHashMap;
Sunny Goyald09c3702016-04-06 16:18:20 -070075import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070076import com.android.launcher3.util.Preconditions;
Sunny Goyalda891c12016-03-18 18:29:24 -070077import com.android.launcher3.util.StringFilter;
Adam Cohen091440a2015-03-18 14:16:05 -070078import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070079import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080080
Michael Jurkac2f801e2011-07-12 14:19:46 -070081import java.lang.ref.WeakReference;
82import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070083import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070084import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070085import java.util.Arrays;
Michael Jurkac2f801e2011-07-12 14:19:46 -070086import java.util.Collections;
87import java.util.Comparator;
88import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070089import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070090import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070091import java.util.List;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070092import java.util.Map;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070093import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070094import java.util.Set;
Sunny Goyal527c7d32015-08-28 15:19:36 -070095import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -070096
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097/**
98 * Maintains in-memory state of the Launcher. It is expected that there should be only one
99 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700100 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101 */
Kenny Guyed131872014-04-30 03:02:21 +0100102public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +0100103 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800104 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -0400105 private static final boolean DEBUG_RECEIVER = false;
Sunny Goyal94485362014-09-18 16:13:58 -0700106 private static final boolean REMOVE_UNRESTORED_ICONS = true;
Chris Wrenb358f812014-04-16 13:37:00 -0400107
Joe Onorato9c1289c2009-08-17 11:03:03 -0400108 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700109
Joe Onorato36115782010-06-17 13:28:48 -0400110 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500111 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800112
Adam Cohen091440a2015-03-18 14:16:05 -0700113 @Thunk final LauncherAppState mApp;
114 @Thunk final Object mLock = new Object();
115 @Thunk DeferredHandler mHandler = new DeferredHandler();
116 @Thunk LoaderTask mLoaderTask;
117 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700118 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119
Adam Cohen091440a2015-03-18 14:16:05 -0700120 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700121 static {
122 sWorkerThread.start();
123 }
Adam Cohen091440a2015-03-18 14:16:05 -0700124 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700125
Joe Onoratocc67f472010-06-08 10:54:30 -0700126 // We start off with everything not loaded. After that, we assume that
127 // our monitoring of the package manager provides all updates and we never
128 // need to do a requery. These are only ever touched from the loader thread.
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700129 private boolean mWorkspaceLoaded;
130 private boolean mAllAppsLoaded;
131 private boolean mDeepShortcutsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700132
Sunny Goyal756a28a2015-04-23 17:07:55 -0700133 /**
134 * Set of runnables to be called on the background thread after the workspace binding
135 * is complete.
136 */
137 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
138
Adam Cohen091440a2015-03-18 14:16:05 -0700139 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700141 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800142 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700143 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800144 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800145
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700146 // Maps all launcher activities to the id's of their shortcuts (if they have any).
147 private final MultiHashMap<ComponentKey, String> mBgDeepShortcutMap = new MultiHashMap<>();
148
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700149 // The lock that must be acquired before referencing any static bg data structures. Unlike
150 // other locks, this one can generally be held long-term because we never expect any of these
151 // static data structures to be referenced outside of the worker thread except on the first
152 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700153 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700154
Adam Cohen487f7dd2012-06-28 18:12:10 -0700155 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700156 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700157 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700158
Adam Cohen487f7dd2012-06-28 18:12:10 -0700159 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
160 // created by LauncherModel that are directly on the home screen (however, no widgets or
161 // shortcuts within folders).
162 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700163
Adam Cohen487f7dd2012-06-28 18:12:10 -0700164 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
165 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700166 new ArrayList<LauncherAppWidgetInfo>();
167
Adam Cohen487f7dd2012-06-28 18:12:10 -0700168 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700169 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700170
Adam Cohendcd297f2013-06-18 13:13:40 -0700171 // sBgWorkspaceScreens is the ordered set of workspace screens.
172 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
173
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700174 // sBgPinnedShortcutCounts is the ComponentKey representing a pinned shortcut to the number of
175 // times it is pinned.
176 static final Map<ShortcutKey, MutableInt> sBgPinnedShortcutCounts = new HashMap<>();
177
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700178 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700179 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
180 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700181
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700182 // </ only access in worker thread >
183
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700184 private IconCache mIconCache;
185 private DeepShortcutManager mDeepShortcutManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700187 private final LauncherAppsCompat mLauncherApps;
188 private final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100189
Joe Onorato9c1289c2009-08-17 11:03:03 -0400190 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700191 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400192 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700193 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400194 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700195 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
196 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700197 public void bindScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700198 public void finishFirstPageBind(ViewOnDrawExecutor executor);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800199 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400200 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200201 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700202 public void bindAppsAdded(ArrayList<Long> newScreens,
203 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700204 ArrayList<ItemInfo> addAnimated,
205 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200206 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700207 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
208 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
209 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700210 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700211 public void bindWorkspaceComponentsRemoved(
212 HashSet<String> packageNames, HashSet<ComponentName> components,
213 UserHandleCompat user);
214 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800215 public void notifyWidgetProvidersChanged();
216 public void bindWidgetsModel(WidgetsModel model);
Adam Cohen1462de32012-07-24 22:34:36 -0700217 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700218 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700219 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMap);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400220 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221
Winson Chung64359a52013-07-08 17:17:08 -0700222 public interface ItemInfoFilter {
223 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
224 }
225
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700226 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter,
227 DeepShortcutManager deepShortcutManager) {
Winson Chunga6945242014-01-08 14:04:34 -0800228 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400229 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100230 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700231 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800232 mIconCache = iconCache;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700233 mDeepShortcutManager = deepShortcutManager;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800234
Kenny Guyed131872014-04-30 03:02:21 +0100235 mLauncherApps = LauncherAppsCompat.getInstance(context);
236 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800237 }
238
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700239 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
240 * posted on the main thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700241 private void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700242 if (sWorkerThread.getThreadId() == Process.myTid()) {
243 // If we are on the worker thread, post onto the main handler
244 mHandler.post(r);
245 } else {
246 r.run();
247 }
248 }
249
250 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
251 * posted on the worker thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700252 private static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700253 if (sWorkerThread.getThreadId() == Process.myTid()) {
254 r.run();
255 } else {
256 // If we are not on the worker thread, then post to the worker handler
257 sWorker.post(r);
258 }
259 }
260
Sunny Goyal756adbc2015-04-16 15:20:51 -0700261 public void setPackageState(final PackageInstallInfo installInfo) {
262 Runnable updateRunnable = new Runnable() {
263
264 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500265 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700266 synchronized (sBgLock) {
267 final HashSet<ItemInfo> updates = new HashSet<>();
268
269 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
270 // Ignore install success events as they are handled by Package add events.
271 return;
272 }
273
Sunny Goyale2df0622015-04-24 11:27:00 -0700274 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700275 if (info instanceof ShortcutInfo) {
276 ShortcutInfo si = (ShortcutInfo) info;
277 ComponentName cn = si.getTargetComponent();
278 if (si.isPromise() && (cn != null)
279 && installInfo.packageName.equals(cn.getPackageName())) {
280 si.setInstallProgress(installInfo.progress);
281
282 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
283 // Mark this info as broken.
284 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
285 }
286 updates.add(si);
287 }
288 }
289 }
290
291 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
292 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
293 widget.installProgress = installInfo.progress;
294 updates.add(widget);
295 }
296 }
297
298 if (!updates.isEmpty()) {
299 // Push changes to the callback.
300 Runnable r = new Runnable() {
301 public void run() {
302 Callbacks callbacks = getCallback();
303 if (callbacks != null) {
304 callbacks.bindRestoreItemsChange(updates);
305 }
306 }
307 };
308 mHandler.post(r);
309 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500310 }
311 }
312 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700313 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500314 }
315
Sunny Goyal756adbc2015-04-16 15:20:51 -0700316 /**
317 * Updates the icons and label of all pending icons for the provided package name.
318 */
319 public void updateSessionDisplayInfo(final String packageName) {
320 Runnable updateRunnable = new Runnable() {
321
322 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700323 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700324 synchronized (sBgLock) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700325 ArrayList<ShortcutInfo> updates = new ArrayList<>();
326 UserHandleCompat user = UserHandleCompat.myUserHandle();
Sunny Goyal756adbc2015-04-16 15:20:51 -0700327
Sunny Goyale2df0622015-04-24 11:27:00 -0700328 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700329 if (info instanceof ShortcutInfo) {
330 ShortcutInfo si = (ShortcutInfo) info;
331 ComponentName cn = si.getTargetComponent();
332 if (si.isPromise() && (cn != null)
333 && packageName.equals(cn.getPackageName())) {
334 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
335 // For auto install apps update the icon as well as label.
336 mIconCache.getTitleAndIcon(si,
337 si.promisedIntent, user,
338 si.shouldUseLowResIcon());
339 } else {
340 // Only update the icon for restored apps.
341 si.updateIcon(mIconCache);
342 }
343 updates.add(si);
344 }
345 }
346 }
347
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700348 bindUpdatedShortcuts(updates, user);
Sunny Goyala22666f2014-09-18 13:25:15 -0700349 }
350 }
351 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700352 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700353 }
354
Adam Cohen76a47a12014-02-05 11:47:43 -0800355 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800356 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800357
358 if (allAppsApps == null) {
359 throw new RuntimeException("allAppsApps must not be null");
360 }
361 if (allAppsApps.isEmpty()) {
362 return;
363 }
364
365 // Process the newly added applications and add them to the database first
366 Runnable r = new Runnable() {
367 public void run() {
368 runOnMainThread(new Runnable() {
369 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800370 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800371 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500372 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800373 }
374 }
375 });
376 }
377 };
378 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700379 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800380
Sunny Goyala9116722015-04-29 13:55:58 -0700381 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800382 int[] xy, int spanX, int spanY) {
383 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700384 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700385
386 GridOccupancy occupied = new GridOccupancy(profile.numColumns, profile.numRows);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800387 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700388 for (ItemInfo r : occupiedPos) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700389 occupied.markCells(r, true);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800390 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800391 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700392 return occupied.findVacantCell(xy, spanX, spanY);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800393 }
394
395 /**
396 * Find a position on the screen for the given size or adds a new screen.
397 * @return screenId and the coordinates for the item.
398 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700399 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyalc3642d42015-10-30 10:27:08 -0700400 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800401 ArrayList<Long> workspaceScreens,
402 ArrayList<Long> addedWorkspaceScreensFinal,
403 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700404 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800405
Sunny Goyala9116722015-04-29 13:55:58 -0700406 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700407 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700408 synchronized (sBgLock) {
409 for (ItemInfo info : sBgItemsIdMap) {
410 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
411 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
412 if (items == null) {
413 items = new ArrayList<>();
414 screenItems.put(info.screenId, items);
415 }
416 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800417 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800418 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800419 }
420
421 // Find appropriate space for the item.
422 long screenId = 0;
423 int[] cordinates = new int[2];
424 boolean found = false;
425
426 int screenCount = workspaceScreens.size();
427 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700428 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800429 if (preferredScreenIndex < screenCount) {
430 screenId = workspaceScreens.get(preferredScreenIndex);
431 found = findNextAvailableIconSpaceInScreen(
432 screenItems.get(screenId), cordinates, spanX, spanY);
433 }
434
435 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700436 // Search on any of the screens starting from the first screen.
437 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800438 screenId = workspaceScreens.get(screen);
439 if (findNextAvailableIconSpaceInScreen(
440 screenItems.get(screenId), cordinates, spanX, spanY)) {
441 // We found a space for it
442 found = true;
443 break;
444 }
445 }
446 }
447
448 if (!found) {
449 // Still no position found. Add a new screen to the end.
Sunny Goyald2497482015-09-22 18:24:19 -0700450 screenId = LauncherSettings.Settings.call(context.getContentResolver(),
451 LauncherSettings.Settings.METHOD_NEW_SCREEN_ID)
452 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800453
454 // Save the screen id for binding in the workspace
455 workspaceScreens.add(screenId);
456 addedWorkspaceScreensFinal.add(screenId);
457
458 // If we still can't find an empty space, then God help us all!!!
459 if (!findNextAvailableIconSpaceInScreen(
460 screenItems.get(screenId), cordinates, spanX, spanY)) {
461 throw new RuntimeException("Can't find space to add the item");
462 }
463 }
464 return Pair.create(screenId, cordinates);
465 }
466
467 /**
468 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800469 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700470 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700471 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800472 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800473 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700474 return;
Winson Chung997a9232013-07-24 15:33:46 -0700475 }
Winson Chung64359a52013-07-08 17:17:08 -0700476 // Process the newly added applications and add them to the database first
477 Runnable r = new Runnable() {
478 public void run() {
479 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
480 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
481
Winson Chung76828c82013-08-19 15:43:29 -0700482 // Get the list of workspace screens. We need to append to this list and
483 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
484 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800485 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700486 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800487 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700488 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800489 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700490 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800491 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700492 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800493 }
Winson Chung76828c82013-08-19 15:43:29 -0700494
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800495 // Find appropriate space for the item.
Sunny Goyalc3642d42015-10-30 10:27:08 -0700496 Pair<Long, int[]> coords = findSpaceForItem(context,
Sunny Goyal9eba1fd2015-10-16 08:58:57 -0700497 workspaceScreens, addedWorkspaceScreensFinal, 1, 1);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800498 long screenId = coords.first;
499 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700500
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700501 ItemInfo itemInfo;
502 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
503 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800504 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700505 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700506 } else {
507 throw new RuntimeException("Unexpected info type");
508 }
Winson Chung94d67682013-09-25 16:29:40 -0700509
Winson Chung64359a52013-07-08 17:17:08 -0700510 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700511 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700512 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700513 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700514 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700515 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700516 }
517 }
518
Winson Chung76828c82013-08-19 15:43:29 -0700519 // Update the workspace screens
520 updateWorkspaceScreenOrder(context, workspaceScreens);
521
Adam Cohen76a47a12014-02-05 11:47:43 -0800522 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700523 runOnMainThread(new Runnable() {
524 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800525 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700526 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700527 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
528 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700529 if (!addedShortcutsFinal.isEmpty()) {
530 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
531 long lastScreenId = info.screenId;
532 for (ItemInfo i : addedShortcutsFinal) {
533 if (i.screenId == lastScreenId) {
534 addAnimated.add(i);
535 } else {
536 addNotAnimated.add(i);
537 }
Winson Chung997a9232013-07-24 15:33:46 -0700538 }
539 }
Winson Chungd64d1762013-08-20 14:37:16 -0700540 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800541 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700542 }
Winson Chung64359a52013-07-08 17:17:08 -0700543 }
Winson Chung997a9232013-07-24 15:33:46 -0700544 });
545 }
Winson Chung64359a52013-07-08 17:17:08 -0700546 }
547 };
548 runOnWorkerThread(r);
549 }
550
Joe Onorato9c1289c2009-08-17 11:03:03 -0400551 /**
552 * Adds an item to the DB if it was not created previously, or move it to a new
553 * <container, screen, cellX, cellY>
554 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800555 public static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700556 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400557 if (item.container == ItemInfo.NO_ID) {
558 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700559 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400560 } else {
561 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700562 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800563 }
564 }
565
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700566 static void checkItemInfoLocked(
567 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
568 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
569 if (modelItem != null && item != modelItem) {
570 // check all the data is consistent
571 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
572 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
573 ShortcutInfo shortcut = (ShortcutInfo) item;
574 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
575 modelShortcut.intent.filterEquals(shortcut.intent) &&
576 modelShortcut.id == shortcut.id &&
577 modelShortcut.itemType == shortcut.itemType &&
578 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700579 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700580 modelShortcut.cellX == shortcut.cellX &&
581 modelShortcut.cellY == shortcut.cellY &&
582 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700583 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700584 // For all intents and purposes, this is the same object
585 return;
586 }
587 }
588
589 // the modelItem needs to match up perfectly with item if our model is
590 // to be consistent with the database-- for now, just require
591 // modelItem == item or the equality check above
592 String msg = "item: " + ((item != null) ? item.toString() : "null") +
593 "modelItem: " +
594 ((modelItem != null) ? modelItem.toString() : "null") +
595 "Error: ItemInfo passed to checkItemInfo doesn't match original";
596 RuntimeException e = new RuntimeException(msg);
597 if (stackTrace != null) {
598 e.setStackTrace(stackTrace);
599 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800600 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700601 }
602 }
603
Michael Jurka816474f2012-06-25 14:49:02 -0700604 static void checkItemInfo(final ItemInfo item) {
605 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
606 final long itemId = item.id;
607 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700608 public void run() {
609 synchronized (sBgLock) {
610 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700611 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700612 }
613 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700614 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700615 }
616
Michael Jurkac9d95c52011-08-29 14:03:34 -0700617 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
618 final ItemInfo item, final String callingFunction) {
619 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700620 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700621 final ContentResolver cr = context.getContentResolver();
622
Adam Cohen487f7dd2012-06-28 18:12:10 -0700623 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700624 Runnable r = new Runnable() {
625 public void run() {
626 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700627 updateItemArrays(item, itemId, stackTrace);
628 }
629 };
630 runOnWorkerThread(r);
631 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700632
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700633 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
634 final ArrayList<ItemInfo> items, final String callingFunction) {
635 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700636
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700637 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
638 Runnable r = new Runnable() {
639 public void run() {
640 ArrayList<ContentProviderOperation> ops =
641 new ArrayList<ContentProviderOperation>();
642 int count = items.size();
643 for (int i = 0; i < count; i++) {
644 ItemInfo item = items.get(i);
645 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700646 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700647 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700648
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700649 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
650 updateItemArrays(item, itemId, stackTrace);
651
652 }
653 try {
654 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
655 } catch (Exception e) {
656 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700657 }
658 }
659 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700660 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700661 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700662
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700663 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
664 // Lock on mBgLock *after* the db operation
665 synchronized (sBgLock) {
666 checkItemInfoLocked(itemId, item, stackTrace);
667
668 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
669 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
670 // Item is in a folder, make sure this folder exists
671 if (!sBgFolders.containsKey(item.container)) {
672 // An items container is being set to a that of an item which is not in
673 // the list of Folders.
674 String msg = "item: " + item + " container being set to: " +
675 item.container + ", not in the list of folders";
676 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700677 }
678 }
679
680 // Items are added/removed from the corresponding FolderInfo elsewhere, such
681 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
682 // that are on the desktop, as appropriate
683 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800684 if (modelItem != null &&
685 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
686 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700687 switch (modelItem.itemType) {
688 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
689 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700690 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700691 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
692 if (!sBgWorkspaceItems.contains(modelItem)) {
693 sBgWorkspaceItems.add(modelItem);
694 }
695 break;
696 default:
697 break;
698 }
699 } else {
700 sBgWorkspaceItems.remove(modelItem);
701 }
702 }
703 }
704
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400706 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700707 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700708 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700709 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400710 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400711 item.cellX = cellX;
712 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700713
Winson Chung3d503fb2011-07-13 17:25:49 -0700714 // We store hotseat items in canonical form which is this orientation invariant position
715 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700716 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700717 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700718 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700719 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700720 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700721 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400722
723 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400724 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700725 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
726 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800727 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700728 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400729
Michael Jurkac9d95c52011-08-29 14:03:34 -0700730 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700731 }
732
733 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700734 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
735 * cellX, cellY have already been updated on the ItemInfos.
736 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800737 public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700738 final long container, final int screen) {
739
740 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
741 int count = items.size();
742
743 for (int i = 0; i < count; i++) {
744 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700745 item.container = container;
746
747 // We store hotseat items in canonical form which is this orientation invariant position
748 // in the hotseat
749 if (context instanceof Launcher && screen < 0 &&
750 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700751 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700752 item.cellY);
753 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700754 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700755 }
756
757 final ContentValues values = new ContentValues();
758 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
759 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
760 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800761 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700762 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700763
764 contentValues.add(values);
765 }
766 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
767 }
768
769 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700770 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800771 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700772 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700773 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700774 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800775 item.cellX = cellX;
776 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700777 item.spanX = spanX;
778 item.spanY = spanY;
779
780 // We store hotseat items in canonical form which is this orientation invariant position
781 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700782 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700783 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700784 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700785 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700786 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700787 }
Adam Cohend4844c32011-02-18 19:25:06 -0800788
Adam Cohend4844c32011-02-18 19:25:06 -0800789 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800790 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700791 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
792 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800793 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700794 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
795 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700796 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800797
Michael Jurka816474f2012-06-25 14:49:02 -0700798 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700799 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700800
801 /**
802 * Update an item to the database in a specified container.
803 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700804 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700805 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100806 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700807 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800808 }
809
Sunny Goyal756a28a2015-04-23 17:07:55 -0700810 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700811 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700812 synchronized (mLock) {
813 if (!mHasLoaderCompletedOnce ||
814 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
815 throw new RuntimeException("Trying to add shortcut while loader is running");
816 }
817 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700818 }
819 }
820
Adam Cohend4844c32011-02-18 19:25:06 -0800821 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700822 * Returns true if the shortcuts already exists on the workspace. This must be called after
823 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800824 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700825 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
826 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700827 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700828 if (intent.getComponent() != null) {
829 // If component is not null, an intent with null package will produce
830 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700831 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700832 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700833 intentWithPkg = intent.toUri(0);
834 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700835 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700836 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
837 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700838 }
839 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700840 intentWithPkg = intent.toUri(0);
841 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700842 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700843
844 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700845 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700846 if (item instanceof ShortcutInfo) {
847 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700848 Intent targetIntent = info.promisedIntent == null
849 ? info.intent : info.promisedIntent;
850 if (targetIntent != null && info.user.equals(user)) {
851 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700852 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
853 return true;
854 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700855 }
856 }
857 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700859 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700860 }
861
Joe Onorato9c1289c2009-08-17 11:03:03 -0400862 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400863 * Add an item to the database in a specified container. Sets the container, screen, cellX and
864 * cellY fields of the item. Also assigns an ID to the item.
865 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700866 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700867 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400868 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400869 item.cellX = cellX;
870 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700871 // We store hotseat items in canonical form which is this orientation invariant position
872 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700873 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700874 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700875 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700876 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700877 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700878 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400879
880 final ContentValues values = new ContentValues();
881 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100882 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400883
Sunny Goyald2497482015-09-22 18:24:19 -0700884 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
885 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
886
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700887 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700888
Jason Monk8e19cf22014-03-20 15:06:57 -0400889 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700890 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700891 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700892 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400893
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700894 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700895 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400896 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700897 sBgItemsIdMap.put(item.id, item);
898 switch (item.itemType) {
899 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
900 sBgFolders.put(item.id, (FolderInfo) item);
901 // Fall through
902 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
903 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700904 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700905 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
906 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
907 sBgWorkspaceItems.add(item);
908 } else {
909 if (!sBgFolders.containsKey(item.container)) {
910 // Adding an item to a folder that doesn't exist.
911 String msg = "adding item: " + item + " to a folder that " +
912 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700913 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700914 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700915 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700916 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700917 incrementPinnedShortcutCount(
918 ShortcutKey.fromItemInfo(item), true /* shouldPin */);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700919 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700920 break;
921 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
922 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
923 break;
924 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700925 }
926 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700927 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700928 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700929 }
930
Sunny Goyal34942622014-08-29 17:20:55 -0700931 private static ArrayList<ItemInfo> getItemsByPackageName(
932 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700933 ItemInfoFilter filter = new ItemInfoFilter() {
934 @Override
935 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
936 return cn.getPackageName().equals(pn) && info.user.equals(user);
937 }
938 };
Sunny Goyale2df0622015-04-24 11:27:00 -0700939 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -0700940 }
941
942 /**
943 * Removes all the items from the database corresponding to the specified package.
944 */
945 static void deletePackageFromDatabase(Context context, final String pn,
946 final UserHandleCompat user) {
947 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700948 }
949
950 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700951 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -0400952 */
Sunny Goyalfa401a12015-04-10 13:45:42 -0700953 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700954 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
955 items.add(item);
956 deleteItemsFromDatabase(context, items);
957 }
958
959 /**
960 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700961 */
Sunny Goyal4390ace2014-10-13 11:33:11 -0700962 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400963 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -0700964 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700965 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700966 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700967 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700968 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700969
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700970 // Lock on mBgLock *after* the db operation
971 synchronized (sBgLock) {
972 switch (item.itemType) {
973 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
974 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -0700975 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700976 if (info.container == item.id) {
977 // We are deleting a folder which still contains items that
978 // think they are contained by that folder.
979 String msg = "deleting a folder (" + item + ") which still " +
980 "contains items (" + info + ")";
981 Log.e(TAG, msg);
982 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700983 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700984 sBgWorkspaceItems.remove(item);
985 break;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700986 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700987 decrementPinnedShortcutCount(ShortcutKey.fromItemInfo(item));
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700988 // Fall through.
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700989 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
990 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
991 sBgWorkspaceItems.remove(item);
992 break;
993 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
994 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
995 break;
996 }
997 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700998 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700999 }
1000 }
Michael Jurka83df1882011-08-31 20:59:26 -07001001 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001002 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001003 }
1004
1005 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001006 * Decrement the count for the given pinned shortcut, unpinning it if the count becomes 0.
1007 */
1008 private static void decrementPinnedShortcutCount(final ShortcutKey pinnedShortcut) {
1009 synchronized (sBgLock) {
1010 MutableInt count = sBgPinnedShortcutCounts.get(pinnedShortcut);
1011 if (count == null || --count.value == 0) {
1012 LauncherAppState.getInstance().getShortcutManager().unpinShortcut(pinnedShortcut);
1013 }
1014 }
1015 }
1016
1017 /**
1018 * Increment the count for the given shortcut, pinning it if the count becomes 1.
1019 *
1020 * As an optimization, the caller can pass shouldPin == false to avoid
1021 * unnecessary RPC's if the shortcut is already pinned.
1022 */
1023 private static void incrementPinnedShortcutCount(ShortcutKey pinnedShortcut, boolean shouldPin) {
1024 synchronized (sBgLock) {
1025 MutableInt count = sBgPinnedShortcutCounts.get(pinnedShortcut);
1026 if (count == null) {
1027 count = new MutableInt(1);
1028 sBgPinnedShortcutCounts.put(pinnedShortcut, count);
1029 } else {
1030 count.value++;
1031 }
1032 if (shouldPin && count.value == 1) {
1033 LauncherAppState.getInstance().getShortcutManager().pinShortcut(pinnedShortcut);
1034 }
1035 }
1036 }
1037
1038 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001039 * Update the order of the workspace screens in the database. The array list contains
1040 * a list of screen ids in the order that they should appear.
1041 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001042 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001043 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001044 final ContentResolver cr = context.getContentResolver();
1045 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1046
1047 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001048 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001049 while (iter.hasNext()) {
1050 long id = iter.next();
1051 if (id < 0) {
1052 iter.remove();
1053 }
1054 }
1055
1056 Runnable r = new Runnable() {
1057 @Override
1058 public void run() {
Yura085c8532014-02-11 15:15:29 +00001059 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001060 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001061 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001062 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001063 for (int i = 0; i < count; i++) {
1064 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001065 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001066 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1067 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001068 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001069 }
Yura085c8532014-02-11 15:15:29 +00001070
1071 try {
1072 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1073 } catch (Exception ex) {
1074 throw new RuntimeException(ex);
1075 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001076
Winson Chungba9c37f2013-08-30 14:11:37 -07001077 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001078 sBgWorkspaceScreens.clear();
1079 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001080 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001081 }
1082 };
1083 runOnWorkerThread(r);
1084 }
1085
1086 /**
Winsonc0b52fe2015-09-09 16:38:15 -07001087 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001088 */
Winsonc0b52fe2015-09-09 16:38:15 -07001089 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001090 final ContentResolver cr = context.getContentResolver();
1091
Michael Jurkac9d95c52011-08-29 14:03:34 -07001092 Runnable r = new Runnable() {
1093 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001094 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001095 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001096 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001097 sBgItemsIdMap.remove(info.id);
1098 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001099 sBgWorkspaceItems.remove(info);
1100 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001101
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001102 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001103 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001104 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001105 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001106 for (ItemInfo childInfo : info.contents) {
1107 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001108 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001109 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001110 }
1111 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001112 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001113 }
1114
1115 /**
1116 * Set this as the current Launcher activity object for the loader.
1117 */
1118 public void initialize(Callbacks callbacks) {
1119 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001120 Preconditions.assertUIThread();
1121 // Remove any queued UI runnables
1122 mHandler.cancelAll();
1123 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001124 }
1125 }
1126
Kenny Guyed131872014-04-30 03:02:21 +01001127 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001128 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001129 int op = PackageUpdatedTask.OP_UPDATE;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001130 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001131 user));
1132 }
1133
1134 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001135 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001136 int op = PackageUpdatedTask.OP_REMOVE;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001137 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001138 user));
1139 }
1140
1141 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001142 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001143 int op = PackageUpdatedTask.OP_ADD;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001144 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001145 user));
1146 }
1147
1148 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001149 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001150 boolean replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001151 enqueueItemUpdatedTask(
Sunny Goyal144154d2016-03-30 16:47:03 -07001152 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, packageNames, user));
Kenny Guyed131872014-04-30 03:02:21 +01001153 }
1154
1155 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001156 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001157 boolean replacing) {
1158 if (!replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001159 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guyed131872014-04-30 03:02:21 +01001160 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1161 user));
1162 }
Kenny Guyed131872014-04-30 03:02:21 +01001163 }
1164
Kenny Guy44cba692016-01-21 19:50:02 +00001165 @Override
1166 public void onPackagesSuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001167 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001168 PackageUpdatedTask.OP_SUSPEND, packageNames,
1169 user));
1170 }
1171
1172 @Override
1173 public void onPackagesUnsuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001174 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001175 PackageUpdatedTask.OP_UNSUSPEND, packageNames,
1176 user));
1177 }
1178
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001179 @Override
1180 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
1181 UserHandleCompat user) {
1182 enqueueItemUpdatedTask(new ShortcutsChangedTask(packageName, shortcuts, user));
1183 }
1184
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001185 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001186 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1187 * ACTION_PACKAGE_CHANGED.
1188 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001189 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001190 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001191 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001192
Joe Onorato36115782010-06-17 13:28:48 -04001193 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001194 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001195 // If we have changed locale we need to clear out the labels in all apps/workspace.
1196 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001197 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1198 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001199 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001200 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001201 } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1202 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
1203 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyalda891c12016-03-18 18:29:24 -07001204 UserHandleCompat user = UserHandleCompat.fromIntent(intent);
1205 if (user != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001206 if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1207 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
1208 enqueueItemUpdatedTask(new PackageUpdatedTask(
1209 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE,
1210 new String[0], user));
1211 }
1212
1213 // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so
1214 // we need to run the state change task again.
1215 if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
1216 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
1217 enqueueItemUpdatedTask(new UserLockStateChangedTask(user));
1218 }
Sunny Goyalda891c12016-03-18 18:29:24 -07001219 }
Tony Wickham827cef22016-03-17 15:39:39 -07001220 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
1221 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001222 }
1223 }
1224
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001225 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001226 resetLoadedState(true, true);
1227
Reena Lee93f824a2011-09-23 17:20:28 -07001228 // Do this here because if the launcher activity is running it will be restarted.
1229 // If it's not running startLoaderFromBackground will merely tell it that it needs
1230 // to reload.
1231 startLoaderFromBackground();
1232 }
1233
Winson Chungf0c6ae02012-03-21 16:10:31 -07001234 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1235 synchronized (mLock) {
1236 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1237 // mWorkspaceLoaded to true later
1238 stopLoaderLocked();
1239 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1240 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001241 // Always reset deep shortcuts loaded.
1242 mDeepShortcutsLoaded = false;
Winson Chungf0c6ae02012-03-21 16:10:31 -07001243 }
1244 }
1245
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001246 /**
1247 * When the launcher is in the background, it's possible for it to miss paired
1248 * configuration changes. So whenever we trigger the loader from the background
1249 * tell the launcher that it needs to re-run the loader when it comes back instead
1250 * of doing it now.
1251 */
1252 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001253 Callbacks callbacks = getCallback();
1254 if (callbacks != null) {
1255 // Only actually run the loader if they're not paused.
1256 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -07001257 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001258 }
1259 }
Joe Onorato36115782010-06-17 13:28:48 -04001260 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001261
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001262 /**
1263 * If there is already a loader task running, tell it to stop.
1264 */
1265 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001266 LoaderTask oldTask = mLoaderTask;
1267 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001268 oldTask.stopLocked();
1269 }
Reena Lee93f824a2011-09-23 17:20:28 -07001270 }
1271
Adam Cohen1a85c582014-09-30 09:48:49 -07001272 public boolean isCurrentCallbacks(Callbacks callbacks) {
1273 return (mCallbacks != null && mCallbacks.get() == callbacks);
1274 }
1275
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001276 /**
1277 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
1278 * @return true if the page could be bound synchronously.
1279 */
1280 public boolean startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001281 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1282 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001283 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -04001284 // Don't bother to start the thread if we know it's not going to do anything
1285 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001286 final Callbacks oldCallbacks = mCallbacks.get();
1287 // Clear any pending bind-runnables from the synchronized load process.
1288 runOnMainThread(new Runnable() {
1289 public void run() {
1290 oldCallbacks.clearPendingBinds();
1291 }
1292 });
1293
Joe Onorato36115782010-06-17 13:28:48 -04001294 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001295 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001296 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Tony Wickham80f57872016-06-29 18:12:15 -07001297 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE && mAllAppsLoaded
1298 && mWorkspaceLoaded && mDeepShortcutsLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001299 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001300 return true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001301 } else {
1302 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1303 sWorker.post(mLoaderTask);
1304 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001305 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001306 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001307 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001308 }
1309
Joe Onorato36115782010-06-17 13:28:48 -04001310 public void stopLoader() {
1311 synchronized (mLock) {
1312 if (mLoaderTask != null) {
1313 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001314 }
1315 }
Joe Onorato36115782010-06-17 13:28:48 -04001316 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001317
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001318 /**
1319 * Loads the workspace screen ids in an ordered list.
1320 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001321 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001322 final ContentResolver contentResolver = context.getContentResolver();
1323 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001324
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001325 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001326 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
1327 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -07001328 }
1329
Joe Onorato36115782010-06-17 13:28:48 -04001330 /**
1331 * Runnable for the thread that loads the contents of the launcher:
1332 * - workspace icons
1333 * - widgets
1334 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001335 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -04001336 */
1337 private class LoaderTask implements Runnable {
1338 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001339 private int mPageToBindFirst;
1340
Adam Cohen091440a2015-03-18 14:16:05 -07001341 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001342 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001343 @Thunk boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001344
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001345 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -04001346 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001347 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001348 }
1349
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001350 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001351 mIsLoadingAndBindingWorkspace = true;
1352
Joe Onorato36115782010-06-17 13:28:48 -04001353 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001354 if (DEBUG_LOADERS) {
1355 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001356 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001357
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001358 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001359 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001360 synchronized (LoaderTask.this) {
1361 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001362 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001363 }
1364 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001365 }
1366 }
1367
Joe Onorato36115782010-06-17 13:28:48 -04001368 // Bind the workspace
Sunny Goyal93f878c2016-03-30 17:31:24 -07001369 bindWorkspace(mPageToBindFirst);
Joe Onorato36115782010-06-17 13:28:48 -04001370 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001371
Joe Onorato36115782010-06-17 13:28:48 -04001372 private void waitForIdle() {
1373 // Wait until the either we're stopped or the other threads are done.
1374 // This way we don't start loading all apps until the workspace has settled
1375 // down.
1376 synchronized (LoaderTask.this) {
1377 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001378
Joe Onorato36115782010-06-17 13:28:48 -04001379 mHandler.postIdle(new Runnable() {
1380 public void run() {
1381 synchronized (LoaderTask.this) {
1382 mLoadAndBindStepFinished = true;
1383 if (DEBUG_LOADERS) {
1384 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001385 }
Joe Onorato36115782010-06-17 13:28:48 -04001386 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001387 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001388 }
Joe Onorato36115782010-06-17 13:28:48 -04001389 });
1390
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001391 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001392 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001393 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1394 // wait no longer than 1sec at a time
1395 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001396 } catch (InterruptedException ex) {
1397 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001398 }
1399 }
Joe Onorato36115782010-06-17 13:28:48 -04001400 if (DEBUG_LOADERS) {
1401 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001402 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001403 + "ms for previous step to finish binding");
1404 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001405 }
Joe Onorato36115782010-06-17 13:28:48 -04001406 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001407
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001408 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001409 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001410 // Ensure that we have a valid page index to load synchronously
1411 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1412 "valid page index");
1413 }
1414 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1415 // Ensure that we don't try and bind a specified page when the pages have not been
1416 // loaded already (we should load everything asynchronously in that case)
1417 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1418 }
1419 synchronized (mLock) {
1420 if (mIsLoaderTaskRunning) {
1421 // Ensure that we are never running the background loading at this point since
1422 // we also touch the background collections
1423 throw new RuntimeException("Error! Background loading is already running");
1424 }
1425 }
1426
1427 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1428 // data structures, we can't allow any other thread to touch that data, but because
1429 // this call is synchronous, we can get away with not locking).
1430
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001431 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001432 // operations from the previous activity. We need to ensure that all queued operations
1433 // are executed before any synchronous binding work is done.
1434 mHandler.flush();
1435
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001436 // Divide the set of loaded items into those that we are binding synchronously, and
1437 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001438 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001439 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1440 // arise from that.
1441 onlyBindAllApps();
Tony Wickham80f57872016-06-29 18:12:15 -07001442
1443 bindDeepShortcuts();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001444 }
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 Goyala5c8a9e2016-07-08 08:32:44 -07001649 try {
1650 ImportDataTask.performImportIfPossible(context);
1651 } catch (Exception e) {
1652 // Migration failed. Clear workspace.
1653 clearDb = true;
1654 }
1655
1656 if (!clearDb && GridSizeMigrationTask.ENABLED &&
Sunny Goyalf076eae2016-01-11 12:25:10 -08001657 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1658 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001659 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001660 }
1661
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001662 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -07001663 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001664 LauncherSettings.Settings.call(contentResolver,
1665 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001666 }
1667
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001668 Log.d(TAG, "loadWorkspace: loading default favorites");
1669 LauncherSettings.Settings.call(contentResolver,
1670 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -07001671
Winson Chung2abf94d2012-07-18 18:16:38 -07001672 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001673 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001674 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001675 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001676 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001677
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001678 final ArrayList<Long> itemsToRemove = new ArrayList<>();
1679 final ArrayList<Long> restoredRows = new ArrayList<>();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001680 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001681 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001682 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001683 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001684
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001685 // +1 for the hotseat (it can be larger than the workspace)
1686 // Load workspace in reverse order to ensure that latest items are loaded first (and
1687 // before any earlier duplicates)
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001688 final LongArrayMap<GridOccupancy> occupied = new LongArrayMap<>();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001689 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001690
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001691 try {
1692 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1693 final int intentIndex = c.getColumnIndexOrThrow
1694 (LauncherSettings.Favorites.INTENT);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001695 final int containerIndex = c.getColumnIndexOrThrow(
1696 LauncherSettings.Favorites.CONTAINER);
1697 final int itemTypeIndex = c.getColumnIndexOrThrow(
1698 LauncherSettings.Favorites.ITEM_TYPE);
1699 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1700 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001701 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1702 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001703 final int screenIndex = c.getColumnIndexOrThrow(
1704 LauncherSettings.Favorites.SCREEN);
1705 final int cellXIndex = c.getColumnIndexOrThrow
1706 (LauncherSettings.Favorites.CELLX);
1707 final int cellYIndex = c.getColumnIndexOrThrow
1708 (LauncherSettings.Favorites.CELLY);
1709 final int spanXIndex = c.getColumnIndexOrThrow
1710 (LauncherSettings.Favorites.SPANX);
1711 final int spanYIndex = c.getColumnIndexOrThrow(
1712 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001713 final int rankIndex = c.getColumnIndexOrThrow(
1714 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001715 final int restoredIndex = c.getColumnIndexOrThrow(
1716 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001717 final int profileIdIndex = c.getColumnIndexOrThrow(
1718 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001719 final int optionsIndex = c.getColumnIndexOrThrow(
1720 LauncherSettings.Favorites.OPTIONS);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001721 final CursorIconInfo cursorIconInfo = new CursorIconInfo(mContext, c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001722
Sunny Goyal7f834d22015-04-21 10:10:23 -07001723 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
Kenny Guyff05f432016-01-22 17:48:29 +00001724 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001725 final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>();
Sunny Goyal7f834d22015-04-21 10:10:23 -07001726 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001727 long serialNo = mUserManager.getSerialNumberForUser(user);
1728 allUsers.put(serialNo, user);
1729 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001730
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001731 boolean userUnlocked = mUserManager.isUserUnlocked(user);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001732
1733 // We can only query for shortcuts when the user is unlocked.
1734 if (userUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07001735 List<ShortcutInfoCompat> pinnedShortcuts =
1736 mDeepShortcutManager.queryForPinnedShortcuts(null, user);
1737 if (mDeepShortcutManager.wasLastCallSuccess()) {
1738 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
1739 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
1740 shortcut);
1741 }
1742 } else {
1743 // Shortcut manager can fail due to some race condition when the
1744 // lock state changes too frequently. For the purpose of the loading
1745 // shortcuts, consider the user is still locked.
1746 userUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001747 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001748 }
Sunny Goyal49f4f032016-08-01 15:45:49 -07001749 unlockedUsers.put(serialNo, userUnlocked);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001750 }
1751
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001752 ShortcutInfo info;
1753 String intentDescription;
1754 LauncherAppWidgetInfo appWidgetInfo;
1755 int container;
1756 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001757 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001758 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001759 UserHandleCompat user;
Sunny Goyald09c3702016-04-06 16:18:20 -07001760 String targetPackage;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001761
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001762 while (!mStopped && c.moveToNext()) {
1763 try {
1764 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001765 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001766 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001767 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001768
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001769 switch (itemType) {
1770 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1771 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001772 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001773 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001774 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001775 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001776 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001777 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001778 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001779 boolean itemReplaced = false;
Sunny Goyald09c3702016-04-06 16:18:20 -07001780 targetPackage = null;
Kenny Guyed131872014-04-30 03:02:21 +01001781 if (user == null) {
1782 // User has been deleted remove the item.
1783 itemsToRemove.add(id);
1784 continue;
1785 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001786 try {
1787 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001788 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001789 if (cn != null && cn.getPackageName() != null) {
1790 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1791 cn.getPackageName(), user);
1792 boolean validComponent = validPkg &&
1793 launcherApps.isActivityEnabledForProfile(cn, user);
Sunny Goyald09c3702016-04-06 16:18:20 -07001794 if (validPkg) {
1795 targetPackage = cn.getPackageName();
1796 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001797
1798 if (validComponent) {
1799 if (restored) {
1800 // no special handling necessary for this item
1801 restoredRows.add(id);
1802 restored = false;
1803 }
Kenny Guyff05f432016-01-22 17:48:29 +00001804 if (quietMode.get(serialNumber)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001805 disabledState = ShortcutInfo.FLAG_DISABLED_QUIET_USER;
Kenny Guyff05f432016-01-22 17:48:29 +00001806 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001807 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001808 intent = null;
1809 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1810 // We allow auto install apps to have their intent
1811 // updated after an install.
1812 intent = manager.getLaunchIntentForPackage(
1813 cn.getPackageName());
1814 if (intent != null) {
1815 ContentValues values = new ContentValues();
1816 values.put(LauncherSettings.Favorites.INTENT,
1817 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001818 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001819 }
1820 }
1821
1822 if (intent == null) {
1823 // The app is installed but the component is no
1824 // longer available.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001825 FileLog.d(TAG, "Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001826 itemsToRemove.add(id);
1827 continue;
1828 } else {
1829 // no special handling necessary for this item
1830 restoredRows.add(id);
1831 restored = false;
1832 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001833 } else if (restored) {
1834 // Package is not yet available but might be
1835 // installed later.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001836 FileLog.d(TAG, "package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001837
1838 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1839 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001840 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001841 // App restore has started. Update the flag
1842 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1843 ContentValues values = new ContentValues();
1844 values.put(LauncherSettings.Favorites.RESTORED,
1845 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001846 updateItem(id, values);
1847 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1848 // This is a common app. Try to replace this.
1849 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1850 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1851 if (parser.findDefaultApp()) {
1852 // Default app found. Replace it.
1853 intent = parser.parsedIntent;
1854 cn = intent.getComponent();
1855 ContentValues values = parser.parsedValues;
1856 values.put(LauncherSettings.Favorites.RESTORED, 0);
1857 updateItem(id, values);
1858 restored = false;
1859 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001860
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001861 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001862 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001863 itemsToRemove.add(id);
1864 continue;
1865 }
Sunny Goyal94485362014-09-18 16:13:58 -07001866 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001867 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001868 itemsToRemove.add(id);
1869 continue;
1870 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001871 } else if (PackageManagerHelper.isAppOnSdcard(
1872 manager, cn.getPackageName())) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07001873 // Package is present but not available.
1874 allowMissingTarget = true;
1875 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1876 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001877 // SdCard is not ready yet. Package might get available,
1878 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001879 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001880 HashSet<String> pkgs = sPendingPackages.get(user);
1881 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001882 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001883 sPendingPackages.put(user, pkgs);
1884 }
1885 pkgs.add(cn.getPackageName());
1886 allowMissingTarget = true;
1887 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001888
1889 } else {
1890 // Do not wait for external media load anymore.
1891 // Log the invalid package, and remove it
Sunny Goyal713edfc2016-05-06 09:58:34 -07001892 FileLog.d(TAG, "Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001893 itemsToRemove.add(id);
1894 continue;
Winson Chungee055712013-07-30 14:46:24 -07001895 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001896 } else if (cn == null) {
1897 // For shortcuts with no component, keep them as they are
1898 restoredRows.add(id);
1899 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001900 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001901 } catch (URISyntaxException e) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001902 FileLog.d(TAG, "Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001903 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001904 continue;
1905 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001906
Sunny Goyal34b65272015-03-11 16:56:52 -07001907 boolean useLowResIcon = container >= 0 &&
1908 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1909
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001910 if (itemReplaced) {
1911 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001912 info = getAppShortcutInfo(intent, user, null,
1913 cursorIconInfo, false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001914 } else {
1915 // Don't replace items for other profiles.
1916 itemsToRemove.add(id);
1917 continue;
1918 }
1919 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001920 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001921 info = getRestoredItemInfo(c, intent,
1922 promiseType, itemType, cursorIconInfo);
Kenny Guyed131872014-04-30 03:02:21 +01001923 intent = getRestoredItemIntent(c, context, intent);
1924 } else {
1925 // Don't restore items for other profiles.
1926 itemsToRemove.add(id);
1927 continue;
1928 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001929 } else if (itemType ==
1930 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001931 info = getAppShortcutInfo(intent, user, c,
1932 cursorIconInfo, allowMissingTarget, useLowResIcon);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001933 } else if (itemType ==
1934 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001935
1936 ShortcutKey key = ShortcutKey.fromIntent(intent, user);
1937 if (unlockedUsers.get(serialNumber)) {
1938 ShortcutInfoCompat pinnedShortcut =
1939 shortcutKeyToPinnedShortcuts.get(key);
1940 if (pinnedShortcut == null) {
1941 // The shortcut is no longer valid.
1942 itemsToRemove.add(id);
1943 continue;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001944 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001945 info = new ShortcutInfo(pinnedShortcut, context);
1946 intent = info.intent;
1947 } else {
1948 // Create a shortcut info in disabled mode for now.
1949 info = new ShortcutInfo();
1950 info.user = user;
1951 info.itemType = itemType;
1952 loadInfoFromCursor(info, c, cursorIconInfo);
1953
1954 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001955 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001956 incrementPinnedShortcutCount(key, false /* shouldPin */);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001957 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001958 info = getShortcutInfo(c, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001959
Sunny Goyald09c3702016-04-06 16:18:20 -07001960 // Shortcuts are only available on the primary profile
1961 if (PackageManagerHelper.isAppSuspended(manager, targetPackage)) {
1962 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1963 }
1964
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001965 // App shortcuts that used to be automatically added to Launcher
1966 // didn't always have the correct intent flags set, so do that
1967 // here
1968 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001969 intent.getCategories() != null &&
1970 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001971 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001972 intent.addFlags(
1973 Intent.FLAG_ACTIVITY_NEW_TASK |
1974 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1975 }
Michael Jurka96879562012-03-22 05:54:33 -07001976 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001977
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001978 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001979 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001980 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001981 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001982 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001983 info.cellX = c.getInt(cellXIndex);
1984 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08001985 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001986 info.spanX = 1;
1987 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01001988 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001989 if (info.promisedIntent != null) {
1990 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
1991 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001992 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001993 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1994 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
1995 }
Adam Cohenae4409d2013-11-26 10:34:59 -08001996
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001997 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001998 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001999 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002000 break;
2001 }
2002
Sunny Goyal756adbc2015-04-16 15:20:51 -07002003 if (restored) {
2004 ComponentName cn = info.getTargetComponent();
2005 if (cn != null) {
2006 Integer progress = installingPkgs.get(cn.getPackageName());
2007 if (progress != null) {
2008 info.setInstallProgress(progress);
2009 } else {
2010 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2011 }
2012 }
2013 }
2014
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002015 switch (container) {
2016 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2017 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2018 sBgWorkspaceItems.add(info);
2019 break;
2020 default:
2021 // Item is in a user folder
2022 FolderInfo folderInfo =
2023 findOrMakeFolder(sBgFolders, container);
Sunny Goyalc52ba712016-04-05 15:59:05 -07002024 folderInfo.add(info, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002025 break;
2026 }
2027 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002028 } else {
2029 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002030 }
2031 break;
2032
2033 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2034 id = c.getLong(idIndex);
2035 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2036
Sunny Goyala508e4f2015-05-21 09:33:57 -07002037 // Do not trim the folder label, as is was set by the user.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002038 folderInfo.title = c.getString(cursorIconInfo.titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002039 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002040 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002041 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002042 folderInfo.cellX = c.getInt(cellXIndex);
2043 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002044 folderInfo.spanX = 1;
2045 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002046 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002047
Daniel Sandler8802e962010-05-26 16:28:16 -04002048 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002049 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002050 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002051 break;
2052 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002053
Joe Onorato9c1289c2009-08-17 11:03:03 -04002054 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002055 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2056 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2057 sBgWorkspaceItems.add(folderInfo);
2058 break;
Joe Onorato36115782010-06-17 13:28:48 -04002059 }
Joe Onorato17a89222011-02-08 17:26:11 -08002060
Chris Wrenf4d08112014-01-16 18:13:56 -05002061 if (restored) {
2062 // no special handling required for restored folders
2063 restoredRows.add(id);
2064 }
2065
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002066 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2067 sBgFolders.put(folderInfo.id, folderInfo);
2068 break;
2069
2070 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002071 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002072 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002073 boolean customWidget = itemType ==
2074 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2075
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002076 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002077 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002078 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002079 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002080 user = allUsers.get(serialNumber);
2081 if (user == null) {
2082 itemsToRemove.add(id);
2083 continue;
2084 }
2085
Sunny Goyalff572272014-07-23 13:58:07 -07002086 final ComponentName component =
2087 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002088
Sunny Goyal651077b2014-06-30 14:15:31 -07002089 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002090 final boolean isIdValid = (restoreStatus &
2091 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002092 final boolean wasProviderReady = (restoreStatus &
2093 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002094
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002095 if (widgetProvidersMap == null) {
2096 widgetProvidersMap = AppWidgetManagerCompat
2097 .getInstance(mContext).getAllProvidersMap();
2098 }
2099 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
2100 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00002101 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002102 user));
Sunny Goyalff572272014-07-23 13:58:07 -07002103
2104 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002105 if (!isSafeMode && !customWidget &&
2106 wasProviderReady && !isProviderReady) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002107 FileLog.d(TAG, "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07002108 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002109 itemsToRemove.add(id);
2110 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002111 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002112 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2113 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002114
Sunny Goyal53f96722015-07-13 19:54:53 -07002115 // The provider is available. So the widget is either
2116 // available or not available. We do not need to track
2117 // any future restore updates.
2118 int status = restoreStatus &
2119 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002120 if (!wasProviderReady) {
2121 // If provider was not previously ready, update the
2122 // status and UI flag.
2123
2124 // Id would be valid only if the widget restore broadcast was received.
2125 if (isIdValid) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002126 status = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002127 } else {
2128 status &= ~LauncherAppWidgetInfo
2129 .FLAG_PROVIDER_NOT_READY;
2130 }
2131 }
2132 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002133 } else {
2134 Log.v(TAG, "Widget restore pending id=" + id
2135 + " appWidgetId=" + appWidgetId
2136 + " status =" + restoreStatus);
2137 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002138 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002139 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002140 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002141
2142 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2143 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002144 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002145 // App restore has started. Update the flag
2146 appWidgetInfo.restoreStatus |=
2147 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002148 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002149 FileLog.d(TAG, "Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07002150 itemsToRemove.add(id);
2151 continue;
2152 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002153
2154 appWidgetInfo.installProgress =
2155 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002156 }
Sunny Goyalff572272014-07-23 13:58:07 -07002157
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002158 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002159 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002160 appWidgetInfo.cellX = c.getInt(cellXIndex);
2161 appWidgetInfo.cellY = c.getInt(cellYIndex);
2162 appWidgetInfo.spanX = c.getInt(spanXIndex);
2163 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002164 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002165
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002166 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2167 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2168 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002169 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2170 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002171 continue;
2172 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002173
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002174 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002175 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002176 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002177 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002178 break;
2179 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002180
Adam Cohen59400422014-03-05 18:07:04 -08002181 if (!customWidget) {
2182 String providerName =
2183 appWidgetInfo.providerName.flattenToString();
2184 if (!providerName.equals(savedProvider) ||
2185 (appWidgetInfo.restoreStatus != restoreStatus)) {
2186 ContentValues values = new ContentValues();
2187 values.put(
2188 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2189 providerName);
2190 values.put(LauncherSettings.Favorites.RESTORED,
2191 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002192 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002193 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002194 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002195 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2196 sBgAppWidgets.add(appWidgetInfo);
2197 }
Joe Onorato36115782010-06-17 13:28:48 -04002198 break;
2199 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002200 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002201 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002202 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002203 }
2204 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002205 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002206 }
2207
Winson Chungba9c37f2013-08-30 14:11:37 -07002208 // Break early if we've stopped loading
2209 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002210 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002211 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002212 }
2213
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002214 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002215 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002216 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2217 Utilities.createDbSelectionQuery(
2218 LauncherSettings.Favorites._ID, itemsToRemove), null);
2219 if (DEBUG_LOADERS) {
2220 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2221 LauncherSettings.Favorites._ID, itemsToRemove));
2222 }
2223
2224 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07002225 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
2226 .call(contentResolver,
2227 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
2228 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
2229 for (long folderId : deletedFolderIds) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002230 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2231 sBgFolders.remove(folderId);
2232 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002233 }
2234 }
2235
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002236 // Unpin shortcuts that don't exist on the workspace.
2237 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
2238 MutableInt numTimesPinned = sBgPinnedShortcutCounts.get(key);
2239 if (numTimesPinned == null || numTimesPinned.value == 0) {
2240 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
2241 mDeepShortcutManager.unpinShortcut(key);
2242 }
2243 }
2244
Sunny Goyal317698b2015-07-29 11:45:41 -07002245 // Sort all the folder items and make sure the first 3 items are high resolution.
2246 for (FolderInfo folder : sBgFolders) {
2247 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2248 int pos = 0;
2249 for (ShortcutInfo info : folder.contents) {
2250 if (info.usingLowResIcon) {
2251 info.updateIcon(mIconCache, false);
2252 }
2253 pos ++;
2254 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2255 break;
2256 }
2257 }
2258 }
2259
Chris Wrenf4d08112014-01-16 18:13:56 -05002260 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002261 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002262 ContentValues values = new ContentValues();
2263 values.put(LauncherSettings.Favorites.RESTORED, 0);
2264 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2265 Utilities.createDbSelectionQuery(
2266 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002267 }
2268
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002269 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2270 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002271 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002272 null, sWorker);
2273 }
2274
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002275 // Remove any empty screens
2276 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002277 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002278 long screenId = item.screenId;
2279 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2280 unusedScreens.contains(screenId)) {
2281 unusedScreens.remove(screenId);
2282 }
2283 }
2284
2285 // If there are any empty screens remove them, and update.
2286 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002287 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002288 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002289 }
2290
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002291 if (DEBUG_LOADERS) {
2292 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2293 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002294 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002295 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002296 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002297
Sunny Goyale2df0622015-04-24 11:27:00 -07002298 for (int i = 0; i < nScreens; i++) {
2299 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002300 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002301 line += " | ";
2302 }
Joe Onorato36115782010-06-17 13:28:48 -04002303 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002304 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002305 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002306 }
Joe Onorato36115782010-06-17 13:28:48 -04002307 }
Sunny Goyale26d1002016-06-20 14:52:14 -07002308 if (LauncherAppState.PROFILE_STARTUP) {
2309 Trace.endSection();
2310 }
Adam Cohene25af792013-06-06 23:08:25 -07002311 }
2312
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002313 /**
2314 * Partially updates the item without any notification. Must be called on the worker thread.
2315 */
2316 private void updateItem(long itemId, ContentValues update) {
2317 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002318 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002319 update,
2320 BaseColumns._ID + "= ?",
2321 new String[]{Long.toString(itemId)});
2322 }
2323
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002324 /** Filters the set of items who are directly or indirectly (via another container) on the
2325 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002326 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002327 ArrayList<ItemInfo> allWorkspaceItems,
2328 ArrayList<ItemInfo> currentScreenItems,
2329 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002330 // Purge any null ItemInfos
2331 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2332 while (iter.hasNext()) {
2333 ItemInfo i = iter.next();
2334 if (i == null) {
2335 iter.remove();
2336 }
2337 }
2338
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002339 // Order the set of items by their containers first, this allows use to walk through the
2340 // list sequentially, build up a list of containers that are in the specified screen,
2341 // as well as all items in those containers.
2342 Set<Long> itemsOnScreen = new HashSet<Long>();
2343 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2344 @Override
2345 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07002346 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002347 }
2348 });
2349 for (ItemInfo info : allWorkspaceItems) {
2350 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002351 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002352 currentScreenItems.add(info);
2353 itemsOnScreen.add(info.id);
2354 } else {
2355 otherScreenItems.add(info);
2356 }
2357 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2358 currentScreenItems.add(info);
2359 itemsOnScreen.add(info.id);
2360 } else {
2361 if (itemsOnScreen.contains(info.container)) {
2362 currentScreenItems.add(info);
2363 itemsOnScreen.add(info.id);
2364 } else {
2365 otherScreenItems.add(info);
2366 }
2367 }
2368 }
2369 }
2370
2371 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002372 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002373 ArrayList<LauncherAppWidgetInfo> appWidgets,
2374 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2375 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002376
2377 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002378 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002379 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002380 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002381 currentScreenWidgets.add(widget);
2382 } else {
2383 otherScreenWidgets.add(widget);
2384 }
2385 }
2386 }
2387
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002388 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2389 * right) */
2390 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002391 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002392 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002393 final int screenCols = profile.numColumns;
2394 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002395 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002396 @Override
2397 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002398 if (lhs.container == rhs.container) {
2399 // Within containers, order by their spatial position in that container
2400 switch ((int) lhs.container) {
2401 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2402 long lr = (lhs.screenId * screenCellCount +
2403 lhs.cellY * screenCols + lhs.cellX);
2404 long rr = (rhs.screenId * screenCellCount +
2405 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07002406 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07002407 }
2408 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2409 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07002410 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07002411 }
2412 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002413 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002414 throw new RuntimeException("Unexpected container type when " +
2415 "sorting workspace items.");
2416 }
2417 return 0;
2418 }
2419 } else {
2420 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07002421 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07002422 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002423 }
2424 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002425 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002426
Adam Cohendcd297f2013-06-18 13:13:40 -07002427 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2428 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002429 final Runnable r = new Runnable() {
2430 @Override
2431 public void run() {
2432 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2433 if (callbacks != null) {
2434 callbacks.bindScreens(orderedScreens);
2435 }
2436 }
2437 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002438 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002439 }
2440
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002441 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2442 final ArrayList<ItemInfo> workspaceItems,
2443 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002444 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002445
2446 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002447 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002448 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002449 final int start = i;
2450 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002451 final Runnable r = new Runnable() {
2452 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002453 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002454 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002455 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002456 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2457 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002458 }
2459 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002460 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002461 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002462 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002463
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002464 // Bind the widgets, one at a time
2465 N = appWidgets.size();
2466 for (int i = 0; i < N; i++) {
2467 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2468 final Runnable r = new Runnable() {
2469 public void run() {
2470 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2471 if (callbacks != null) {
2472 callbacks.bindAppWidget(widget);
2473 }
2474 }
2475 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002476 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002477 }
2478 }
2479
2480 /**
2481 * Binds all loaded data to actual views on the main thread.
2482 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002483 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002484 final long t = SystemClock.uptimeMillis();
2485 Runnable r;
2486
2487 // Don't use these two variables in any of the callback runnables.
2488 // Otherwise we hold a reference to them.
2489 final Callbacks oldCallbacks = mCallbacks.get();
2490 if (oldCallbacks == null) {
2491 // This launcher has exited and nobody bothered to tell us. Just bail.
2492 Log.w(TAG, "LoaderTask running with no launcher");
2493 return;
2494 }
2495
Winson Chung9b9fb962013-11-15 15:39:34 -08002496 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07002497 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
2498 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
2499 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002500
Winson Chung2abf94d2012-07-18 18:16:38 -07002501 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002502 workspaceItems.addAll(sBgWorkspaceItems);
2503 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002504 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002505 }
2506
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002507 final int currentScreen;
2508 {
2509 int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE
2510 ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen();
2511 if (currScreen >= orderedScreenIds.size()) {
2512 // There may be no workspace screens (just hotseat items and an empty page).
2513 currScreen = PagedView.INVALID_RESTORE_PAGE;
2514 }
2515 currentScreen = currScreen;
Winson Chung9b9fb962013-11-15 15:39:34 -08002516 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002517 final boolean validFirstPage = currentScreen >= 0;
2518 final long currentScreenId =
2519 validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID;
Winson Chung9b9fb962013-11-15 15:39:34 -08002520
Winson Chung9b9fb962013-11-15 15:39:34 -08002521 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07002522 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
2523 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
2524 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
2525 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002526
Winson Chung9b9fb962013-11-15 15:39:34 -08002527 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002528 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002529 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002530 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002531 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2532 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2533
2534 // Tell the workspace that we're about to start binding items
2535 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002536 public void run() {
2537 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2538 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002539 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002540 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002541 }
2542 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002543 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002544 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002545
Adam Cohendcd297f2013-06-18 13:13:40 -07002546 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2547
Sunny Goyal527c7d32015-08-28 15:19:36 -07002548 Executor mainExecutor = new DeferredMainThreadExecutor();
2549 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07002550 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002551
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002552 // In case of validFirstPage, only bind the first screen, and defer binding the
2553 // remaining screens after first onDraw (and an optional the fade animation whichever
2554 // happens later).
2555 // This ensures that the first screen is immediately visible (eg. during rotation)
2556 // In case of !validFirstPage, bind all pages one after other.
2557 final Executor deferredExecutor =
2558 validFirstPage ? new ViewOnDrawExecutor(mHandler) : mainExecutor;
2559
2560 mainExecutor.execute(new Runnable() {
2561 @Override
2562 public void run() {
2563 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2564 if (callbacks != null) {
2565 callbacks.finishFirstPageBind(
2566 validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null);
2567 }
2568 }
2569 });
Sunny Goyal527c7d32015-08-28 15:19:36 -07002570
Sunny Goyal44c06432016-04-02 10:56:02 -07002571 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002572
2573 // Tell the workspace that we're done binding items
2574 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002575 public void run() {
2576 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2577 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002578 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002579 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002580
Sunny Goyal639e9062015-08-19 19:17:06 -07002581 mIsLoadingAndBindingWorkspace = false;
2582
2583 // Run all the bind complete runnables after workspace is bound.
2584 if (!mBindCompleteRunnables.isEmpty()) {
2585 synchronized (mBindCompleteRunnables) {
2586 for (final Runnable r : mBindCompleteRunnables) {
2587 runOnWorkerThread(r);
2588 }
2589 mBindCompleteRunnables.clear();
2590 }
2591 }
2592
Winson Chung98e030b2012-05-07 16:01:11 -07002593 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002594 if (DEBUG_LOADERS) {
2595 Log.d(TAG, "bound workspace in "
2596 + (SystemClock.uptimeMillis()-t) + "ms");
2597 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002598
Joe Onorato36115782010-06-17 13:28:48 -04002599 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002600 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002601 deferredExecutor.execute(r);
2602
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002603 if (validFirstPage) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002604 r = new Runnable() {
2605 public void run() {
2606 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2607 if (callbacks != null) {
2608 // We are loading synchronously, which means, some of the pages will be
2609 // bound after first draw. Inform the callbacks that page binding is
2610 // not complete, and schedule the remaining pages.
2611 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2612 callbacks.onPageBoundSynchronously(currentScreen);
2613 }
2614 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2615 }
2616 }
2617 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002618 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002619 }
Joe Onorato36115782010-06-17 13:28:48 -04002620 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002621
Joe Onorato36115782010-06-17 13:28:48 -04002622 private void loadAndBindAllApps() {
2623 if (DEBUG_LOADERS) {
2624 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2625 }
2626 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002627 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002628 synchronized (LoaderTask.this) {
2629 if (mStopped) {
2630 return;
2631 }
2632 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002633 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002634 synchronized (LoaderTask.this) {
2635 if (mStopped) {
2636 return;
2637 }
2638 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002639 }
Joe Onorato36115782010-06-17 13:28:48 -04002640 } else {
2641 onlyBindAllApps();
2642 }
2643 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002644
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002645 private void updateIconCache() {
2646 // Ignore packages which have a promise icon.
2647 HashSet<String> packagesToIgnore = new HashSet<>();
2648 synchronized (sBgLock) {
2649 for (ItemInfo info : sBgItemsIdMap) {
2650 if (info instanceof ShortcutInfo) {
2651 ShortcutInfo si = (ShortcutInfo) info;
2652 if (si.isPromise() && si.getTargetComponent() != null) {
2653 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2654 }
2655 } else if (info instanceof LauncherAppWidgetInfo) {
2656 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2657 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2658 packagesToIgnore.add(lawi.providerName.getPackageName());
2659 }
2660 }
2661 }
2662 }
2663 mIconCache.updateDbIcons(packagesToIgnore);
2664 }
2665
Joe Onorato36115782010-06-17 13:28:48 -04002666 private void onlyBindAllApps() {
2667 final Callbacks oldCallbacks = mCallbacks.get();
2668 if (oldCallbacks == null) {
2669 // This launcher has exited and nobody bothered to tell us. Just bail.
2670 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2671 return;
2672 }
2673
2674 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002675 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002676 final ArrayList<AppInfo> list
2677 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002678 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002679 public void run() {
2680 final long t = SystemClock.uptimeMillis();
2681 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2682 if (callbacks != null) {
2683 callbacks.bindAllApplications(list);
2684 }
2685 if (DEBUG_LOADERS) {
2686 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002687 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04002688 }
2689 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002690 };
Tony Wickham80f57872016-06-29 18:12:15 -07002691 runOnMainThread(r);
Joe Onorato36115782010-06-17 13:28:48 -04002692 }
2693
Winson Chung64359a52013-07-08 17:17:08 -07002694 private void loadAllApps() {
2695 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002696
Joe Onorato36115782010-06-17 13:28:48 -04002697 final Callbacks oldCallbacks = mCallbacks.get();
2698 if (oldCallbacks == null) {
2699 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002700 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002701 return;
2702 }
2703
Kenny Guyed131872014-04-30 03:02:21 +01002704 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2705
Winson Chung64359a52013-07-08 17:17:08 -07002706 // Clear the list of apps
2707 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002708 for (UserHandleCompat user : profiles) {
2709 // Query for the set of apps
2710 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002711 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002712 if (DEBUG_LOADERS) {
2713 Log.d(TAG, "getActivityList took "
2714 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2715 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2716 }
2717 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002718 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002719 if (apps == null || apps.isEmpty()) {
2720 return;
2721 }
Kenny Guyff05f432016-01-22 17:48:29 +00002722 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002723 // Create the ApplicationInfos
2724 for (int i = 0; i < apps.size(); i++) {
2725 LauncherActivityInfoCompat app = apps.get(i);
2726 // This builds the icon bitmaps.
Kenny Guyff05f432016-01-22 17:48:29 +00002727 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode));
Kenny Guyed131872014-04-30 03:02:21 +01002728 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002729
Sunny Goyal756a28a2015-04-23 17:07:55 -07002730 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2731 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002732 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002733
2734 @Override
2735 public void run() {
2736 heuristic.processUserApps(apps);
2737 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002738 };
2739 runOnMainThread(new Runnable() {
2740
2741 @Override
2742 public void run() {
2743 // Check isLoadingWorkspace on the UI thread, as it is updated on
2744 // the UI thread.
2745 if (mIsLoadingAndBindingWorkspace) {
2746 synchronized (mBindCompleteRunnables) {
2747 mBindCompleteRunnables.add(r);
2748 }
2749 } else {
2750 runOnWorkerThread(r);
2751 }
2752 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002753 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002754 }
Winson Chung64359a52013-07-08 17:17:08 -07002755 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002756 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002757 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2758 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002759
2760 // Post callback on main thread
2761 mHandler.post(new Runnable() {
2762 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002763
Winson Chung64359a52013-07-08 17:17:08 -07002764 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002765 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002766 if (callbacks != null) {
2767 callbacks.bindAllApplications(added);
2768 if (DEBUG_LOADERS) {
2769 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002770 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07002771 }
2772 } else {
2773 Log.i(TAG, "not binding apps: no Launcher activity");
2774 }
2775 }
2776 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002777 // Cleanup any data stored for a deleted user.
2778 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04002779 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002780 Log.d(TAG, "Icons processed in "
2781 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002782 }
2783 }
2784
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002785 private void loadAndBindDeepShortcuts() {
2786 if (DEBUG_LOADERS) {
2787 Log.d(TAG, "loadAndBindDeepShortcuts mDeepShortcutsLoaded=" + mDeepShortcutsLoaded);
2788 }
2789 if (!mDeepShortcutsLoaded) {
2790 mBgDeepShortcutMap.clear();
2791 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002792 if (mUserManager.isUserUnlocked(user)) {
2793 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager
2794 .queryForAllShortcuts(user);
2795 updateDeepShortcutMap(null, user, shortcuts);
2796 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002797 }
2798 synchronized (LoaderTask.this) {
2799 if (mStopped) {
2800 return;
2801 }
2802 mDeepShortcutsLoaded = true;
2803 }
2804 }
Tony Wickham80f57872016-06-29 18:12:15 -07002805 bindDeepShortcuts();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002806 }
2807
Joe Onoratobe386092009-11-17 17:32:16 -08002808 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002809 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002810 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002811 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2812 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2813 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2814 }
Joe Onorato36115782010-06-17 13:28:48 -04002815 }
2816 }
2817
Sunny Goyal860538d2016-07-20 12:35:16 -07002818 /**
2819 * Clear all the shortcuts for the given package, and re-add the new shortcuts.
2820 */
2821 private void updateDeepShortcutMap(
2822 String packageName, UserHandleCompat user, List<ShortcutInfoCompat> shortcuts) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002823 if (packageName != null) {
2824 Iterator<ComponentKey> keysIter = mBgDeepShortcutMap.keySet().iterator();
2825 while (keysIter.hasNext()) {
Sunny Goyal860538d2016-07-20 12:35:16 -07002826 ComponentKey next = keysIter.next();
2827 if (next.componentName.getPackageName().equals(packageName)
2828 && next.user.equals(user)) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002829 keysIter.remove();
2830 }
2831 }
2832 }
2833
2834 // Now add the new shortcuts to the map.
2835 for (ShortcutInfoCompat shortcut : shortcuts) {
Tony Wickhamca258b32016-07-28 12:31:28 -07002836 boolean shouldShowInContainer = shortcut.isEnabled()
2837 && (shortcut.isDeclaredInManifest() || shortcut.isDynamic());
2838 if (shouldShowInContainer) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002839 ComponentKey targetComponent
2840 = new ComponentKey(shortcut.getActivity(), shortcut.getUserHandle());
2841 mBgDeepShortcutMap.addToList(targetComponent, shortcut.getId());
2842 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002843 }
2844 }
2845
Tony Wickham80f57872016-06-29 18:12:15 -07002846 public void bindDeepShortcuts() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002847 final MultiHashMap<ComponentKey, String> shortcutMapCopy = new MultiHashMap<>();
2848 shortcutMapCopy.putAll(mBgDeepShortcutMap);
Tony Wickham80f57872016-06-29 18:12:15 -07002849 Runnable r = new Runnable() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002850 @Override
2851 public void run() {
2852 Callbacks callbacks = getCallback();
2853 if (callbacks != null) {
2854 callbacks.bindDeepShortcutMap(shortcutMapCopy);
2855 }
2856 }
Tony Wickham80f57872016-06-29 18:12:15 -07002857 };
2858 runOnMainThread(r);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002859 }
2860
Sunny Goyal75b0f552015-05-20 21:57:06 -07002861 /**
2862 * Called when the icons for packages have been updated in the icon cache.
2863 */
2864 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2865 final Callbacks callbacks = getCallback();
2866 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2867 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2868
2869 // If any package icon has changed (app was updated while launcher was dead),
2870 // update the corresponding shortcuts.
2871 synchronized (sBgLock) {
2872 for (ItemInfo info : sBgItemsIdMap) {
2873 if (info instanceof ShortcutInfo && user.equals(info.user)
2874 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2875 ShortcutInfo si = (ShortcutInfo) info;
2876 ComponentName cn = si.getTargetComponent();
2877 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2878 si.updateIcon(mIconCache);
2879 updatedShortcuts.add(si);
2880 }
2881 }
2882 }
2883 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2884 }
2885
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002886 bindUpdatedShortcuts(updatedShortcuts, user);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002887
2888 if (!updatedApps.isEmpty()) {
2889 mHandler.post(new Runnable() {
2890
2891 public void run() {
2892 Callbacks cb = getCallback();
2893 if (cb != null && callbacks == cb) {
2894 cb.bindAppsUpdated(updatedApps);
2895 }
2896 }
2897 });
2898 }
2899 }
2900
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002901 private void bindUpdatedShortcuts(
2902 ArrayList<ShortcutInfo> updatedShortcuts, UserHandleCompat user) {
2903 bindUpdatedShortcuts(updatedShortcuts, new ArrayList<ShortcutInfo>(), user);
2904 }
2905
2906 private void bindUpdatedShortcuts(
2907 final ArrayList<ShortcutInfo> updatedShortcuts,
2908 final ArrayList<ShortcutInfo> removedShortcuts,
2909 final UserHandleCompat user) {
2910 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002911 final Callbacks callbacks = getCallback();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002912 mHandler.post(new Runnable() {
2913
2914 public void run() {
2915 Callbacks cb = getCallback();
2916 if (cb != null && callbacks == cb) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002917 cb.bindShortcutsChanged(updatedShortcuts, removedShortcuts, user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002918 }
2919 }
2920 });
2921 }
2922 }
2923
2924 void enqueueItemUpdatedTask(Runnable task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002925 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002926 }
2927
Adam Cohen091440a2015-03-18 14:16:05 -07002928 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002929
2930 @Override
2931 public void onReceive(Context context, Intent intent) {
2932 synchronized (sBgLock) {
2933 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2934 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002935 final PackageManager manager = context.getPackageManager();
2936 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2937 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002938 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2939 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002940 packagesRemoved.clear();
2941 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002942 for (String pkg : entry.getValue()) {
2943 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07002944 if (PackageManagerHelper.isAppOnSdcard(manager, pkg)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002945 packagesUnavailable.add(pkg);
2946 } else {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002947 packagesRemoved.add(pkg);
2948 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002949 }
2950 }
2951 if (!packagesRemoved.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002952 enqueueItemUpdatedTask(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002953 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2954 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002955 if (!packagesUnavailable.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002956 enqueueItemUpdatedTask(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
Sunny Goyal1a745e82014-10-02 15:58:31 -07002957 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2958 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002959 }
Sunny Goyal34942622014-08-29 17:20:55 -07002960 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002961 }
2962 }
2963 }
2964
Joe Onorato36115782010-06-17 13:28:48 -04002965 private class PackageUpdatedTask implements Runnable {
2966 int mOp;
2967 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002968 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002969
2970 public static final int OP_NONE = 0;
2971 public static final int OP_ADD = 1;
2972 public static final int OP_UPDATE = 2;
2973 public static final int OP_REMOVE = 3; // uninstlled
2974 public static final int OP_UNAVAILABLE = 4; // external media unmounted
Kenny Guy44cba692016-01-21 19:50:02 +00002975 public static final int OP_SUSPEND = 5; // package suspended
2976 public static final int OP_UNSUSPEND = 6; // package unsuspended
Sunny Goyalda891c12016-03-18 18:29:24 -07002977 public static final int OP_USER_AVAILABILITY_CHANGE = 7; // user available/unavailable
Joe Onorato36115782010-06-17 13:28:48 -04002978
Kenny Guyed131872014-04-30 03:02:21 +01002979 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002980 mOp = op;
2981 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002982 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002983 }
2984
2985 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002986 if (!mHasLoaderCompletedOnce) {
2987 // Loader has not yet run.
2988 return;
2989 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002990 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002991
2992 final String[] packages = mPackages;
2993 final int N = packages.length;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002994 FlagOp flagOp = FlagOp.NO_OP;
Sunny Goyalda891c12016-03-18 18:29:24 -07002995 StringFilter pkgFilter = StringFilter.of(new HashSet<>(Arrays.asList(packages)));
Joe Onorato36115782010-06-17 13:28:48 -04002996 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002997 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002998 for (int i=0; i<N; i++) {
2999 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003000 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003001 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003002 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003003
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003004 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3005 if (heuristic != null) {
3006 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003007 }
Joe Onorato36115782010-06-17 13:28:48 -04003008 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003009 }
Joe Onorato36115782010-06-17 13:28:48 -04003010 case OP_UPDATE:
3011 for (int i=0; i<N; i++) {
3012 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003013 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003014 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003015 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003016 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003017 // Since package was just updated, the target must be available now.
3018 flagOp = FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04003019 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003020 case OP_REMOVE: {
3021 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3022 if (heuristic != null) {
3023 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003024 }
Joe Onorato36115782010-06-17 13:28:48 -04003025 for (int i=0; i<N; i++) {
3026 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003027 mIconCache.removeIconsForPkg(packages[i], mUser);
3028 }
3029 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003030 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003031 case OP_UNAVAILABLE:
3032 for (int i=0; i<N; i++) {
3033 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3034 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003035 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003036 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003037 flagOp = FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04003038 break;
Kenny Guy44cba692016-01-21 19:50:02 +00003039 case OP_SUSPEND:
3040 case OP_UNSUSPEND:
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003041 flagOp = mOp == OP_SUSPEND ?
3042 FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED) :
3043 FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED);
Sunny Goyal17763cb2016-03-24 13:53:22 -07003044 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.(un)suspend " + N);
Sunny Goyalda891c12016-03-18 18:29:24 -07003045 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
3046 break;
3047 case OP_USER_AVAILABILITY_CHANGE:
3048 flagOp = UserManagerCompat.getInstance(context).isQuietModeEnabled(mUser)
3049 ? FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER)
3050 : FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER);
3051 // We want to update all packages for this user.
3052 pkgFilter = StringFilter.matchesAll();
3053 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
Kenny Guy44cba692016-01-21 19:50:02 +00003054 break;
Joe Onorato36115782010-06-17 13:28:48 -04003055 }
3056
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003057 ArrayList<AppInfo> added = null;
3058 ArrayList<AppInfo> modified = null;
3059 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003060
Adam Cohen487f7dd2012-06-28 18:12:10 -07003061 if (mBgAllAppsList.added.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003062 added = new ArrayList<>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003063 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003064 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003065 if (mBgAllAppsList.modified.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003066 modified = new ArrayList<>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003067 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003068 }
Winson Chung5d55f332012-07-16 20:45:03 -07003069 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003070 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003071 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003072 }
3073
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003074 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps = new HashMap<>();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003075
Joe Onorato36115782010-06-17 13:28:48 -04003076 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003077 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003078 for (AppInfo ai : added) {
3079 addedOrUpdatedApps.put(ai.componentName, ai);
3080 }
Joe Onorato36115782010-06-17 13:28:48 -04003081 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003082
Joe Onorato36115782010-06-17 13:28:48 -04003083 if (modified != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003084 final Callbacks callbacks = getCallback();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003085 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003086 for (AppInfo ai : modified) {
3087 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003088 }
3089
Joe Onorato36115782010-06-17 13:28:48 -04003090 mHandler.post(new Runnable() {
3091 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003092 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003093 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003094 callbacks.bindAppsUpdated(modifiedFinal);
3095 }
3096 }
3097 });
3098 }
Winson Chung83892cc2013-05-01 16:53:33 -07003099
Sunny Goyal4390ace2014-10-13 11:33:11 -07003100 // Update shortcut infos
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003101 if (mOp == OP_ADD || flagOp != FlagOp.NO_OP) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003102 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3103 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3104 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3105
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003106 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003107 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003108 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3109 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003110 boolean infoUpdated = false;
3111 boolean shortcutUpdated = false;
3112
3113 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003114 if ((si.iconResource != null)
Sunny Goyalda891c12016-03-18 18:29:24 -07003115 && pkgFilter.matches(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003116 Bitmap icon = Utilities.createIconBitmap(
3117 si.iconResource.packageName,
3118 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003119 if (icon != null) {
3120 si.setIcon(icon);
3121 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003122 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003123 }
3124 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003125
3126 ComponentName cn = si.getTargetComponent();
Sunny Goyalda891c12016-03-18 18:29:24 -07003127 if (cn != null && pkgFilter.matches(cn.getPackageName())) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003128 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3129
3130 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003131 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3132 // Auto install icon
3133 PackageManager pm = context.getPackageManager();
3134 ResolveInfo matched = pm.resolveActivity(
3135 new Intent(Intent.ACTION_MAIN)
3136 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3137 PackageManager.MATCH_DEFAULT_ONLY);
3138 if (matched == null) {
3139 // Try to find the best match activity.
3140 Intent intent = pm.getLaunchIntentForPackage(
3141 cn.getPackageName());
3142 if (intent != null) {
3143 cn = intent.getComponent();
3144 appInfo = addedOrUpdatedApps.get(cn);
3145 }
3146
3147 if ((intent == null) || (appInfo == null)) {
3148 removedShortcuts.add(si);
3149 continue;
3150 }
3151 si.promisedIntent = intent;
3152 }
3153 }
3154
3155 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003156 if (appInfo != null) {
3157 si.flags = appInfo.flags;
3158 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003159
Sunny Goyal756adbc2015-04-16 15:20:51 -07003160 si.intent = si.promisedIntent;
3161 si.promisedIntent = null;
3162 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003163 infoUpdated = true;
3164 si.updateIcon(mIconCache);
3165 }
3166
3167 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3168 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3169 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003170 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003171 si.contentDescription = appInfo.contentDescription;
3172 infoUpdated = true;
3173 }
3174
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003175 int oldDisabledFlags = si.isDisabled;
3176 si.isDisabled = flagOp.apply(si.isDisabled);
3177 if (si.isDisabled != oldDisabledFlags) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003178 shortcutUpdated = true;
3179 }
3180 }
3181
3182 if (infoUpdated || shortcutUpdated) {
3183 updatedShortcuts.add(si);
3184 }
3185 if (infoUpdated) {
3186 updateItemInDatabase(context, si);
3187 }
Sunny Goyalda891c12016-03-18 18:29:24 -07003188 } else if (info instanceof LauncherAppWidgetInfo && mOp == OP_ADD) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003189 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3190 if (mUser.equals(widgetInfo.user)
3191 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
Sunny Goyalda891c12016-03-18 18:29:24 -07003192 && pkgFilter.matches(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003193 widgetInfo.restoreStatus &=
3194 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3195 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003196
3197 // adding this flag ensures that launcher shows 'click to setup'
3198 // if the widget has a config activity. In case there is no config
3199 // activity, it will be marked as 'restored' during bind.
3200 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3201
Sunny Goyal4390ace2014-10-13 11:33:11 -07003202 widgets.add(widgetInfo);
3203 updateItemInDatabase(context, widgetInfo);
3204 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003205 }
3206 }
3207 }
3208
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003209 bindUpdatedShortcuts(updatedShortcuts, removedShortcuts, mUser);
3210 if (!removedShortcuts.isEmpty()) {
3211 deleteItemsFromDatabase(context, removedShortcuts);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003212 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003213
Sunny Goyal4390ace2014-10-13 11:33:11 -07003214 if (!widgets.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003215 final Callbacks callbacks = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003216 mHandler.post(new Runnable() {
3217 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003218 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003219 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003220 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003221 }
3222 }
3223 });
3224 }
3225 }
3226
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003227 final HashSet<String> removedPackages = new HashSet<>();
3228 final HashSet<ComponentName> removedComponents = new HashSet<>();
3229 if (mOp == OP_REMOVE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003230 // Mark all packages in the broadcast to be removed
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003231 Collections.addAll(removedPackages, packages);
3232
3233 // No need to update the removedComponents as
3234 // removedPackages is a super-set of removedComponents
Winson Chungdf95eb12013-10-16 14:57:07 -07003235 } else if (mOp == OP_UPDATE) {
3236 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003237 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003238 if (isPackageDisabled(context, packages[i], mUser)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003239 removedPackages.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003240 }
3241 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003242
3243 // Update removedComponents as some components can get removed during package update
3244 for (AppInfo info : removedApps) {
3245 removedComponents.add(info.componentName);
3246 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003247 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003248
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003249 if (!removedPackages.isEmpty() || !removedComponents.isEmpty()) {
3250 for (String pn : removedPackages) {
3251 deletePackageFromDatabase(context, pn, mUser);
3252 }
3253 for (ComponentName cn : removedComponents) {
3254 deleteItemsFromDatabase(context, getItemInfoForComponentName(cn, mUser));
Sunny Goyal1a745e82014-10-02 15:58:31 -07003255 }
3256
Winson Chungdf95eb12013-10-16 14:57:07 -07003257 // Remove any queued items from the install queue
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003258 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackages, mUser);
3259
Winson Chungdf95eb12013-10-16 14:57:07 -07003260 // Call the components-removed callback
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003261 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003262 mHandler.post(new Runnable() {
3263 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003264 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003265 if (callbacks == cb && cb != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003266 callbacks.bindWorkspaceComponentsRemoved(
3267 removedPackages, removedComponents, mUser);
3268 }
3269 }
3270 });
3271 }
3272
3273 if (!removedApps.isEmpty()) {
3274 // Remove corresponding apps from All-Apps
3275 final Callbacks callbacks = getCallback();
3276 mHandler.post(new Runnable() {
3277 public void run() {
3278 Callbacks cb = getCallback();
3279 if (callbacks == cb && cb != null) {
3280 callbacks.bindAppInfosRemoved(removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04003281 }
3282 }
3283 });
Joe Onoratobe386092009-11-17 17:32:16 -08003284 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003285
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003286 // Notify launcher of widget update. From marshmallow onwards we use AppWidgetHost to
3287 // get widget update signals.
3288 if (!Utilities.ATLEAST_MARSHMALLOW &&
3289 (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003290 final Callbacks callbacks = getCallback();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003291 mHandler.post(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003292 public void run() {
3293 Callbacks cb = getCallback();
3294 if (callbacks == cb && cb != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003295 callbacks.notifyWidgetProvidersChanged();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003296 }
3297 }
3298 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003299 }
3300 }
3301 }
3302
Sunny Goyal10923b32016-07-20 15:42:44 -07003303 /**
3304 * Repopulates the shortcut info, possibly updating any icon already on the workspace.
3305 */
3306 public void updateShortcutInfo(final ShortcutInfoCompat fullDetail, final ShortcutInfo info) {
3307 enqueueItemUpdatedTask(new Runnable() {
3308 @Override
3309 public void run() {
3310 info.updateFromDeepShortcutInfo(
3311 fullDetail, LauncherAppState.getInstance().getContext());
3312 ArrayList<ShortcutInfo> update = new ArrayList<ShortcutInfo>();
3313 update.add(info);
3314 bindUpdatedShortcuts(update, fullDetail.getUserHandle());
3315 }
3316 });
3317 }
3318
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003319 private class ShortcutsChangedTask implements Runnable {
3320 private String mPackageName;
3321 private List<ShortcutInfoCompat> mShortcuts;
3322 private UserHandleCompat mUser;
3323
3324 public ShortcutsChangedTask(String packageName, List<ShortcutInfoCompat> shortcuts,
3325 UserHandleCompat user) {
3326 mPackageName = packageName;
3327 mShortcuts = shortcuts;
3328 mUser = user;
3329 }
3330
3331 @Override
3332 public void run() {
3333 mDeepShortcutManager.onShortcutsChanged(mShortcuts);
3334
3335 Map<String, ShortcutInfoCompat> idsToShortcuts = new HashMap<>();
3336 for (ShortcutInfoCompat shortcut : mShortcuts) {
3337 idsToShortcuts.put(shortcut.getId(), shortcut);
3338 }
3339
3340 // Find ShortcutInfo's that have changed on the workspace.
3341 MultiHashMap<String, ShortcutInfo> idsToWorkspaceShortcutInfos = new MultiHashMap<>();
3342 for (ItemInfo itemInfo : sBgItemsIdMap) {
3343 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
3344 ShortcutInfo si = (ShortcutInfo) itemInfo;
Tony Wickham672d07f2016-07-27 18:22:41 -07003345 if (si.getIntent().getPackage().equals(mPackageName) && si.user.equals(mUser)) {
3346 String shortcutId = si.getDeepShortcutId();
3347 if (idsToShortcuts.containsKey(shortcutId)) {
3348 idsToWorkspaceShortcutInfos.addToList(shortcutId, si);
3349 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003350 }
3351 }
3352 }
3353
3354 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3355 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager.queryForFullDetails(
3356 mPackageName, new ArrayList<>(idsToWorkspaceShortcutInfos.keySet()), mUser);
3357 ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3358 Context context = LauncherAppState.getInstance().getContext();
3359 for (ShortcutInfoCompat fullDetails : shortcuts) {
3360 List<ShortcutInfo> shortcutInfos = idsToWorkspaceShortcutInfos
3361 .get(fullDetails.getId());
3362 for (ShortcutInfo shortcutInfo : shortcutInfos) {
Sunny Goyal9994b2b2016-06-23 14:17:24 -07003363 shortcutInfo.updateFromDeepShortcutInfo(fullDetails, context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003364 updatedShortcutInfos.add(shortcutInfo);
3365 }
3366 }
3367 bindUpdatedShortcuts(updatedShortcutInfos, mUser);
3368
3369 // Update the deep shortcut map, in case the list of ids has changed for an activity.
Sunny Goyal860538d2016-07-20 12:35:16 -07003370 updateDeepShortcutMap(mPackageName, mUser, mShortcuts);
Tony Wickham80f57872016-06-29 18:12:15 -07003371 bindDeepShortcuts();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003372 }
3373 }
3374
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003375 /**
3376 * Task to handle changing of lock state of the user
3377 */
3378 private class UserLockStateChangedTask implements Runnable {
3379
3380 private final UserHandleCompat mUser;
3381
3382 public UserLockStateChangedTask(UserHandleCompat user) {
3383 mUser = user;
3384 }
3385
3386 @Override
3387 public void run() {
3388 boolean isUserUnlocked = mUserManager.isUserUnlocked(mUser);
3389 Context context = mApp.getContext();
3390
3391 HashMap<ShortcutKey, ShortcutInfoCompat> pinnedShortcuts = new HashMap<>();
3392 if (isUserUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07003393 List<ShortcutInfoCompat> shortcuts =
3394 mDeepShortcutManager.queryForPinnedShortcuts(null, mUser);
3395 if (mDeepShortcutManager.wasLastCallSuccess()) {
3396 for (ShortcutInfoCompat shortcut : shortcuts) {
3397 pinnedShortcuts.put(ShortcutKey.fromInfo(shortcut), shortcut);
3398 }
3399 } else {
3400 // Shortcut manager can fail due to some race condition when the lock state
3401 // changes too frequently. For the purpose of the update,
3402 // consider it as still locked.
3403 isUserUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003404 }
3405 }
3406
3407 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3408 ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3409 ArrayList<ShortcutInfo> deletedShortcutInfos = new ArrayList<>();
3410 for (ItemInfo itemInfo : sBgItemsIdMap) {
3411 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT
3412 && mUser.equals(itemInfo.user)) {
3413 ShortcutInfo si = (ShortcutInfo) itemInfo;
3414 if (isUserUnlocked) {
3415 ShortcutInfoCompat shortcut =
3416 pinnedShortcuts.get(ShortcutKey.fromItemInfo(si));
3417 // We couldn't verify the shortcut during loader. If its no longer available
3418 // (probably due to clear data), delete the workspace item as well
3419 if (shortcut == null) {
3420 deletedShortcutInfos.add(si);
3421 continue;
3422 }
3423 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
3424 si.updateFromDeepShortcutInfo(shortcut, context);
3425 } else {
3426 si.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
3427 }
3428 updatedShortcutInfos.add(si);
3429 }
3430 }
3431 bindUpdatedShortcuts(updatedShortcutInfos, deletedShortcutInfos, mUser);
3432 if (!deletedShortcutInfos.isEmpty()) {
3433 deleteItemsFromDatabase(context, deletedShortcutInfos);
3434 }
3435
3436 // Remove shortcut id map for that user
3437 Iterator<ComponentKey> keysIter = mBgDeepShortcutMap.keySet().iterator();
3438 while (keysIter.hasNext()) {
3439 if (keysIter.next().user.equals(mUser)) {
3440 keysIter.remove();
3441 }
3442 }
3443
3444 if (isUserUnlocked) {
3445 updateDeepShortcutMap(null, mUser, mDeepShortcutManager.queryForAllShortcuts(mUser));
3446 }
3447 bindDeepShortcuts();
3448 }
3449 }
3450
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003451 private void bindWidgetsModel(final Callbacks callbacks, final WidgetsModel model) {
3452 mHandler.post(new Runnable() {
3453 @Override
3454 public void run() {
3455 Callbacks cb = getCallback();
3456 if (callbacks == cb && cb != null) {
3457 callbacks.bindWidgetsModel(model);
3458 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003459 }
3460 });
3461 }
3462
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003463 public void refreshAndBindWidgetsAndShortcuts(
3464 final Callbacks callbacks, final boolean bindFirst) {
3465 runOnWorkerThread(new Runnable() {
3466 @Override
3467 public void run() {
3468 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
3469 bindWidgetsModel(callbacks, mBgWidgetsModel.clone());
Sunny Goyal31860582015-09-18 08:38:57 -07003470 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003471 final WidgetsModel model = mBgWidgetsModel.updateAndClone(mApp.getContext());
3472 bindWidgetsModel(callbacks, model);
3473 // update the Widget entries inside DB on the worker thread.
3474 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3475 model.getRawList());
Sunny Goyal31860582015-09-18 08:38:57 -07003476 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003477 });
Michael Jurkac402cd92013-05-20 15:49:32 +02003478 }
3479
Adam Cohen091440a2015-03-18 14:16:05 -07003480 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003481 UserHandleCompat user) {
3482 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3483 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003484 }
Adam Cohen556f6132014-01-15 15:18:08 -08003485
Kenny Guyed131872014-04-30 03:02:21 +01003486 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3487 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003488 if (cn == null) {
3489 return false;
3490 }
Kenny Guyed131872014-04-30 03:02:21 +01003491 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3492 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003493 return false;
3494 }
Kenny Guyed131872014-04-30 03:02:21 +01003495 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003496 }
3497
Adam Cohena28b78e2014-05-20 17:03:04 -07003498 public static boolean isValidPackage(Context context, String packageName,
3499 UserHandleCompat user) {
3500 if (packageName == null) {
3501 return false;
3502 }
3503 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3504 return launcherApps.isPackageEnabledForProfile(packageName, user);
3505 }
3506
Joe Onorato9c1289c2009-08-17 11:03:03 -04003507 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003508 * Make an ShortcutInfo object for a restored application or shortcut item that points
3509 * to a package that is not yet installed on the system.
3510 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003511 public ShortcutInfo getRestoredItemInfo(Cursor c, Intent intent,
3512 int promiseType, int itemType, CursorIconInfo iconInfo) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003513 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003514 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003515
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003516 Bitmap icon = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003517 // the fallback icon
3518 if (icon == null) {
3519 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3520 } else {
3521 info.setIcon(icon);
3522 }
Sunny Goyal34942622014-08-29 17:20:55 -07003523
3524 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003525 String title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07003526 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003527 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003528 }
Sunny Goyal34942622014-08-29 17:20:55 -07003529 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3530 if (TextUtils.isEmpty(info.title)) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003531 info.title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07003532 }
Sunny Goyal34942622014-08-29 17:20:55 -07003533 } else {
3534 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3535 }
3536
Winson Chung82b016c2015-05-08 17:00:10 -07003537 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003538 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003539 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003540 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003541 return info;
3542 }
3543
3544 /**
3545 * Make an Intent object for a restored application or shortcut item that points
3546 * to the market page for the item.
3547 */
Adam Cohen091440a2015-03-18 14:16:05 -07003548 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003549 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003550 return getMarketIntent(componentName.getPackageName());
3551 }
3552
3553 static Intent getMarketIntent(String packageName) {
3554 return new Intent(Intent.ACTION_VIEW)
3555 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003556 .scheme("market")
3557 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003558 .appendQueryParameter("id", packageName)
3559 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003560 }
3561
3562 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003563 * Make an ShortcutInfo object for a shortcut that is an application.
3564 *
3565 * If c is not null, then it will be used to fill in missing data like the title and icon.
3566 */
Sunny Goyald09c3702016-04-06 16:18:20 -07003567 public ShortcutInfo getAppShortcutInfo(Intent intent,
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003568 UserHandleCompat user, Cursor c, CursorIconInfo iconInfo,
Sunny Goyal34b65272015-03-11 16:56:52 -07003569 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003570 if (user == null) {
3571 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003572 return null;
3573 }
3574
Kenny Guyed131872014-04-30 03:02:21 +01003575 ComponentName componentName = intent.getComponent();
3576 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08003577 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01003578 return null;
3579 }
3580
3581 Intent newIntent = new Intent(intent.getAction(), null);
3582 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3583 newIntent.setComponent(componentName);
3584 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003585 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003586 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3587 return null;
3588 }
3589
3590 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003591 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003592 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003593 Bitmap icon = iconInfo.loadIcon(c);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003594 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003595 }
3596
Sunny Goyald09c3702016-04-06 16:18:20 -07003597 if (lai != null && PackageManagerHelper.isAppSuspended(lai.getApplicationInfo())) {
3598 info.isDisabled = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
3599 }
3600
Joe Onorato56d82912010-03-07 14:32:10 -05003601 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003602 if (TextUtils.isEmpty(info.title) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003603 info.title = iconInfo.getTitle(c);
Joe Onorato56d82912010-03-07 14:32:10 -05003604 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003605
Joe Onorato56d82912010-03-07 14:32:10 -05003606 // fall back to the class name of the activity
3607 if (info.title == null) {
3608 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003609 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003610
Joe Onorato9c1289c2009-08-17 11:03:03 -04003611 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003612 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003613 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003614 if (lai != null) {
3615 info.flags = AppInfo.initFlags(lai);
3616 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003617 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003618 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003619
Sunny Goyale2df0622015-04-24 11:27:00 -07003620 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003621 ItemInfoFilter f) {
3622 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3623 for (ItemInfo i : infos) {
3624 if (i instanceof ShortcutInfo) {
3625 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003626 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003627 if (cn != null && f.filterItem(null, info, cn)) {
3628 filtered.add(info);
3629 }
3630 } else if (i instanceof FolderInfo) {
3631 FolderInfo info = (FolderInfo) i;
3632 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003633 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003634 if (cn != null && f.filterItem(info, s, cn)) {
3635 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003636 }
3637 }
Winson Chung64359a52013-07-08 17:17:08 -07003638 } else if (i instanceof LauncherAppWidgetInfo) {
3639 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3640 ComponentName cn = info.providerName;
3641 if (cn != null && f.filterItem(null, info, cn)) {
3642 filtered.add(info);
3643 }
Winson Chung8a435102012-08-30 17:16:53 -07003644 }
3645 }
Winson Chung64359a52013-07-08 17:17:08 -07003646 return new ArrayList<ItemInfo>(filtered);
3647 }
3648
Adam Cohen091440a2015-03-18 14:16:05 -07003649 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003650 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003651 ItemInfoFilter filter = new ItemInfoFilter() {
3652 @Override
3653 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003654 if (info.user == null) {
3655 return cn.equals(cname);
3656 } else {
3657 return cn.equals(cname) && info.user.equals(user);
3658 }
Winson Chung64359a52013-07-08 17:17:08 -07003659 }
3660 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003661 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003662 }
3663
Sunny Goyal1a745e82014-10-02 15:58:31 -07003664 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003665 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003666 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003667 @Thunk ShortcutInfo getShortcutInfo(Cursor c, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003668 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003669 // Non-app shortcuts are only supported for current user.
3670 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003671 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003672
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003673 // TODO: If there's an explicit component and we can't install that, delete it.
3674
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003675 loadInfoFromCursor(info, c, iconInfo);
3676 return info;
3677 }
Joe Onorato56d82912010-03-07 14:32:10 -05003678
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003679 /**
3680 * Make an ShortcutInfo object for a shortcut that isn't an application.
3681 */
3682 public void loadInfoFromCursor(ShortcutInfo info, Cursor c, CursorIconInfo iconInfo) {
3683 info.title = iconInfo.getTitle(c);
3684 Bitmap icon = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003685 // the fallback icon
3686 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003687 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003688 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003689 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003690 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003691 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003692
Sunny Goyal2350bc92014-10-14 16:42:54 -07003693 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003694 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3695 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3696 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3697
Adam Cohend9198822011-11-22 16:42:47 -08003698 if (intent == null) {
3699 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3700 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3701 return null;
3702 }
3703
Joe Onorato0589f0f2010-02-08 13:44:00 -08003704 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003705 boolean customIcon = false;
3706 ShortcutIconResource iconResource = null;
3707
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003708 if (bitmap instanceof Bitmap) {
3709 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003710 customIcon = true;
3711 } else {
3712 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003713 if (extra instanceof ShortcutIconResource) {
3714 iconResource = (ShortcutIconResource) extra;
3715 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003716 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003717 }
3718 }
3719
Michael Jurkac9d95c52011-08-29 14:03:34 -07003720 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003721
Kenny Guyed131872014-04-30 03:02:21 +01003722 // Only support intents for current user for now. Intents sent from other
3723 // users wouldn't get here without intent forwarding anyway.
3724 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003725 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003726 icon = mIconCache.getDefaultIcon(info.user);
3727 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003728 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003729 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003730
Winson Chung82b016c2015-05-08 17:00:10 -07003731 info.title = Utilities.trim(name);
3732 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003733 info.intent = intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003734 info.iconResource = iconResource;
3735
3736 return info;
3737 }
3738
Joe Onorato9c1289c2009-08-17 11:03:03 -04003739 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003740 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003741 * or make a new one.
3742 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003743 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003744 // See if a placeholder was created for us already
3745 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003746 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003747 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003748 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003749 folders.put(id, folderInfo);
3750 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003751 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003752 }
3753
Joe Onoratobe386092009-11-17 17:32:16 -08003754
Sunny Goyal651077b2014-06-30 14:15:31 -07003755 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3756 return (provider != null) && (provider.provider != null)
3757 && (provider.provider.getPackageName() != null);
3758 }
3759
Joe Onoratobe386092009-11-17 17:32:16 -08003760 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003761 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003762 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3763 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3764 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3765 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003766 if (mLoaderTask != null) {
3767 mLoaderTask.dumpState();
3768 } else {
3769 Log.d(TAG, "mLoaderTask=null");
3770 }
Joe Onoratobe386092009-11-17 17:32:16 -08003771 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003772
3773 public Callbacks getCallback() {
3774 return mCallbacks != null ? mCallbacks.get() : null;
3775 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003776
3777 /**
3778 * @return {@link FolderInfo} if its already loaded.
3779 */
3780 public FolderInfo findFolderById(Long folderId) {
3781 synchronized (sBgLock) {
3782 return sBgFolders.get(folderId);
3783 }
3784 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003785
Sunny Goyal527c7d32015-08-28 15:19:36 -07003786 @Thunk class DeferredMainThreadExecutor implements Executor {
3787
3788 @Override
3789 public void execute(Runnable command) {
3790 runOnMainThread(command);
3791 }
3792 }
3793
Sunny Goyal756adbc2015-04-16 15:20:51 -07003794 /**
3795 * @return the looper for the worker thread which can be used to start background tasks.
3796 */
3797 public static Looper getWorkerLooper() {
3798 return sWorkerThread.getLooper();
3799 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003800}