blob: a51fd07e41047b8a8ed5bba5d93c07a0684068d4 [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) {
Sunny Goyal50941fb2016-08-04 12:03:52 -07001182 enqueueItemUpdatedTask(new ShortcutsChangedTask(packageName, shortcuts, user, true));
1183 }
1184
1185 public void updatePinnedShortcuts(String packageName, List<ShortcutInfoCompat> shortcuts,
1186 UserHandleCompat user) {
1187 enqueueItemUpdatedTask(new ShortcutsChangedTask(packageName, shortcuts, user, false));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001188 }
1189
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001190 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001191 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1192 * ACTION_PACKAGE_CHANGED.
1193 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001194 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001195 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001196 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001197
Joe Onorato36115782010-06-17 13:28:48 -04001198 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001199 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001200 // If we have changed locale we need to clear out the labels in all apps/workspace.
1201 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001202 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1203 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001204 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001205 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001206 } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1207 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
1208 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyalda891c12016-03-18 18:29:24 -07001209 UserHandleCompat user = UserHandleCompat.fromIntent(intent);
1210 if (user != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001211 if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1212 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
1213 enqueueItemUpdatedTask(new PackageUpdatedTask(
1214 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE,
1215 new String[0], user));
1216 }
1217
1218 // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so
1219 // we need to run the state change task again.
1220 if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
1221 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
1222 enqueueItemUpdatedTask(new UserLockStateChangedTask(user));
1223 }
Sunny Goyalda891c12016-03-18 18:29:24 -07001224 }
Tony Wickham827cef22016-03-17 15:39:39 -07001225 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
1226 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001227 }
1228 }
1229
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001230 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001231 resetLoadedState(true, true);
1232
Reena Lee93f824a2011-09-23 17:20:28 -07001233 // Do this here because if the launcher activity is running it will be restarted.
1234 // If it's not running startLoaderFromBackground will merely tell it that it needs
1235 // to reload.
1236 startLoaderFromBackground();
1237 }
1238
Winson Chungf0c6ae02012-03-21 16:10:31 -07001239 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1240 synchronized (mLock) {
1241 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1242 // mWorkspaceLoaded to true later
1243 stopLoaderLocked();
1244 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1245 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001246 // Always reset deep shortcuts loaded.
1247 mDeepShortcutsLoaded = false;
Winson Chungf0c6ae02012-03-21 16:10:31 -07001248 }
1249 }
1250
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001251 /**
1252 * When the launcher is in the background, it's possible for it to miss paired
1253 * configuration changes. So whenever we trigger the loader from the background
1254 * tell the launcher that it needs to re-run the loader when it comes back instead
1255 * of doing it now.
1256 */
1257 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001258 Callbacks callbacks = getCallback();
1259 if (callbacks != null) {
1260 // Only actually run the loader if they're not paused.
1261 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -07001262 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001263 }
1264 }
Joe Onorato36115782010-06-17 13:28:48 -04001265 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001266
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001267 /**
1268 * If there is already a loader task running, tell it to stop.
1269 */
1270 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001271 LoaderTask oldTask = mLoaderTask;
1272 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001273 oldTask.stopLocked();
1274 }
Reena Lee93f824a2011-09-23 17:20:28 -07001275 }
1276
Adam Cohen1a85c582014-09-30 09:48:49 -07001277 public boolean isCurrentCallbacks(Callbacks callbacks) {
1278 return (mCallbacks != null && mCallbacks.get() == callbacks);
1279 }
1280
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001281 /**
1282 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
1283 * @return true if the page could be bound synchronously.
1284 */
1285 public boolean startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001286 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1287 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001288 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -04001289 // Don't bother to start the thread if we know it's not going to do anything
1290 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001291 final Callbacks oldCallbacks = mCallbacks.get();
1292 // Clear any pending bind-runnables from the synchronized load process.
1293 runOnMainThread(new Runnable() {
1294 public void run() {
1295 oldCallbacks.clearPendingBinds();
1296 }
1297 });
1298
Joe Onorato36115782010-06-17 13:28:48 -04001299 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001300 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001301 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Tony Wickham80f57872016-06-29 18:12:15 -07001302 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE && mAllAppsLoaded
1303 && mWorkspaceLoaded && mDeepShortcutsLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001304 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001305 return true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001306 } else {
1307 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1308 sWorker.post(mLoaderTask);
1309 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001310 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001311 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001312 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001313 }
1314
Joe Onorato36115782010-06-17 13:28:48 -04001315 public void stopLoader() {
1316 synchronized (mLock) {
1317 if (mLoaderTask != null) {
1318 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001319 }
1320 }
Joe Onorato36115782010-06-17 13:28:48 -04001321 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001322
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001323 /**
1324 * Loads the workspace screen ids in an ordered list.
1325 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001326 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001327 final ContentResolver contentResolver = context.getContentResolver();
1328 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001329
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001330 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001331 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
1332 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -07001333 }
1334
Joe Onorato36115782010-06-17 13:28:48 -04001335 /**
1336 * Runnable for the thread that loads the contents of the launcher:
1337 * - workspace icons
1338 * - widgets
1339 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001340 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -04001341 */
1342 private class LoaderTask implements Runnable {
1343 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001344 private int mPageToBindFirst;
1345
Adam Cohen091440a2015-03-18 14:16:05 -07001346 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001347 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001348 @Thunk boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001349
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001350 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -04001351 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001352 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001353 }
1354
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001355 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001356 mIsLoadingAndBindingWorkspace = true;
1357
Joe Onorato36115782010-06-17 13:28:48 -04001358 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001359 if (DEBUG_LOADERS) {
1360 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001361 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001362
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001363 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001364 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001365 synchronized (LoaderTask.this) {
1366 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001367 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001368 }
1369 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001370 }
1371 }
1372
Joe Onorato36115782010-06-17 13:28:48 -04001373 // Bind the workspace
Sunny Goyal93f878c2016-03-30 17:31:24 -07001374 bindWorkspace(mPageToBindFirst);
Joe Onorato36115782010-06-17 13:28:48 -04001375 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001376
Joe Onorato36115782010-06-17 13:28:48 -04001377 private void waitForIdle() {
1378 // Wait until the either we're stopped or the other threads are done.
1379 // This way we don't start loading all apps until the workspace has settled
1380 // down.
1381 synchronized (LoaderTask.this) {
1382 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001383
Joe Onorato36115782010-06-17 13:28:48 -04001384 mHandler.postIdle(new Runnable() {
1385 public void run() {
1386 synchronized (LoaderTask.this) {
1387 mLoadAndBindStepFinished = true;
1388 if (DEBUG_LOADERS) {
1389 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001390 }
Joe Onorato36115782010-06-17 13:28:48 -04001391 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001392 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001393 }
Joe Onorato36115782010-06-17 13:28:48 -04001394 });
1395
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001396 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001397 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001398 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1399 // wait no longer than 1sec at a time
1400 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001401 } catch (InterruptedException ex) {
1402 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001403 }
1404 }
Joe Onorato36115782010-06-17 13:28:48 -04001405 if (DEBUG_LOADERS) {
1406 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001407 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001408 + "ms for previous step to finish binding");
1409 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001410 }
Joe Onorato36115782010-06-17 13:28:48 -04001411 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001412
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001413 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001414 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001415 // Ensure that we have a valid page index to load synchronously
1416 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1417 "valid page index");
1418 }
1419 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1420 // Ensure that we don't try and bind a specified page when the pages have not been
1421 // loaded already (we should load everything asynchronously in that case)
1422 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1423 }
1424 synchronized (mLock) {
1425 if (mIsLoaderTaskRunning) {
1426 // Ensure that we are never running the background loading at this point since
1427 // we also touch the background collections
1428 throw new RuntimeException("Error! Background loading is already running");
1429 }
1430 }
1431
1432 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1433 // data structures, we can't allow any other thread to touch that data, but because
1434 // this call is synchronous, we can get away with not locking).
1435
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001436 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001437 // operations from the previous activity. We need to ensure that all queued operations
1438 // are executed before any synchronous binding work is done.
1439 mHandler.flush();
1440
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001441 // Divide the set of loaded items into those that we are binding synchronously, and
1442 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001443 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001444 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1445 // arise from that.
1446 onlyBindAllApps();
Tony Wickham80f57872016-06-29 18:12:15 -07001447
1448 bindDeepShortcuts();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001449 }
1450
Joe Onorato36115782010-06-17 13:28:48 -04001451 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001452 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001453 if (mStopped) {
1454 return;
1455 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001456 mIsLoaderTaskRunning = true;
1457 }
Joe Onorato36115782010-06-17 13:28:48 -04001458 // Optimize for end-user experience: if the Launcher is up and // running with the
1459 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1460 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001461 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001462 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001463 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001464
Joe Onorato36115782010-06-17 13:28:48 -04001465 if (mStopped) {
1466 break keep_running;
1467 }
1468
Joe Onorato36115782010-06-17 13:28:48 -04001469 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001470
1471 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001472 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1473 loadAndBindAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001474
1475 waitForIdle();
1476
1477 // third step
1478 if (DEBUG_LOADERS) Log.d(TAG, "step 3: loading deep shortcuts");
1479 loadAndBindDeepShortcuts();
Joe Onorato36115782010-06-17 13:28:48 -04001480 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001481
Joe Onorato36115782010-06-17 13:28:48 -04001482 // Clear out this reference, otherwise we end up holding it until all of the
1483 // callback runnables are done.
1484 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001485
Joe Onorato36115782010-06-17 13:28:48 -04001486 synchronized (mLock) {
1487 // If we are still the last one to be scheduled, remove ourselves.
1488 if (mLoaderTask == this) {
1489 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001490 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001491 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001492 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001493 }
Joe Onorato36115782010-06-17 13:28:48 -04001494 }
1495
1496 public void stopLocked() {
1497 synchronized (LoaderTask.this) {
1498 mStopped = true;
1499 this.notify();
1500 }
1501 }
1502
1503 /**
1504 * Gets the callbacks object. If we've been stopped, or if the launcher object
1505 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1506 * object that was around when the deferred message was scheduled, and if there's
1507 * a new Callbacks object around then also return null. This will save us from
1508 * calling onto it with data that will be ignored.
1509 */
1510 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1511 synchronized (mLock) {
1512 if (mStopped) {
1513 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001514 }
Joe Onorato36115782010-06-17 13:28:48 -04001515
1516 if (mCallbacks == null) {
1517 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001518 }
Joe Onorato36115782010-06-17 13:28:48 -04001519
1520 final Callbacks callbacks = mCallbacks.get();
1521 if (callbacks != oldCallbacks) {
1522 return null;
1523 }
1524 if (callbacks == null) {
1525 Log.w(TAG, "no mCallbacks");
1526 return null;
1527 }
1528
1529 return callbacks;
1530 }
1531 }
1532
1533 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001534 private boolean checkItemPlacement(LongArrayMap<GridOccupancy> occupied, ItemInfo item,
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001535 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001536 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001537 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung892c74d2013-08-22 16:15:50 -07001538
Adam Cohendcd297f2013-06-18 13:13:40 -07001539 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001540 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001541 // Return early if we detect that an item is under the hotseat button
Sunny Goyalbb011da2016-06-15 15:42:29 -07001542 if (!FeatureFlags.NO_ALL_APPS_ICON &&
1543 profile.isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001544 Log.e(TAG, "Error loading shortcut into hotseat " + item
1545 + " into position (" + item.screenId + ":" + item.cellX + ","
1546 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001547 return false;
1548 }
1549
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001550 final GridOccupancy hotseatOccupancy =
Dan Sandler295ae182013-12-10 16:05:47 -05001551 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1552
Adam Cohen2e6da152015-05-06 11:42:25 -07001553 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001554 Log.e(TAG, "Error loading shortcut " + item
1555 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001556 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001557 + ")");
1558 return false;
1559 }
1560
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001561 if (hotseatOccupancy != null) {
1562 if (hotseatOccupancy.cells[(int) item.screenId][0]) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001563 Log.e(TAG, "Error loading shortcut into hotseat " + item
1564 + " into position (" + item.screenId + ":" + item.cellX + ","
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001565 + item.cellY + ") already occupied");
Adam Cohendcd297f2013-06-18 13:13:40 -07001566 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001567 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001568 hotseatOccupancy.cells[(int) item.screenId][0] = true;
Dan Sandler295ae182013-12-10 16:05:47 -05001569 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001570 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001571 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001572 final GridOccupancy occupancy = new GridOccupancy(profile.numHotseatIcons, 1);
1573 occupancy.cells[(int) item.screenId][0] = true;
1574 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, occupancy);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001575 return true;
1576 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001577 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1578 if (!workspaceScreens.contains((Long) item.screenId)) {
1579 // The item has an invalid screen id.
1580 return false;
1581 }
1582 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001583 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001584 return true;
1585 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001586
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001587 final int countX = profile.numColumns;
1588 final int countY = profile.numRows;
Adam Cohenae4409d2013-11-26 10:34:59 -08001589 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1590 item.cellX < 0 || item.cellY < 0 ||
1591 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1592 Log.e(TAG, "Error loading shortcut " + item
1593 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1594 + item.cellX + "," + item.cellY
1595 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1596 return false;
1597 }
1598
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001599 if (!occupied.containsKey(item.screenId)) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001600 GridOccupancy screen = new GridOccupancy(countX + 1, countY + 1);
1601 if (item.screenId == Workspace.FIRST_SCREEN_ID) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001602 // Mark the first row as occupied (if the feature is enabled)
1603 // in order to account for the QSB.
1604 screen.markCells(0, 0, countX + 1, 1, FeatureFlags.QSB_ON_FIRST_SCREEN);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001605 }
1606 occupied.put(item.screenId, screen);
Joe Onorato36115782010-06-17 13:28:48 -04001607 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001608 final GridOccupancy occupancy = occupied.get(item.screenId);
Winson Chungf30ad5f2011-08-08 10:55:42 -07001609
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001610 // Check if any workspace icons overlap with each other
1611 if (occupancy.isRegionVacant(item.cellX, item.cellY, item.spanX, item.spanY)) {
1612 occupancy.markCells(item, true);
1613 return true;
1614 } else {
1615 Log.e(TAG, "Error loading shortcut " + item
1616 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1617 + item.cellX + "," + item.cellX + "," + item.spanX + "," + item.spanY
1618 + ") already occupied");
1619 return false;
1620 }
Joe Onorato36115782010-06-17 13:28:48 -04001621 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001622
Winson Chungba9c37f2013-08-30 14:11:37 -07001623 /** Clears all the sBg data structures */
1624 private void clearSBgDataStructures() {
1625 synchronized (sBgLock) {
1626 sBgWorkspaceItems.clear();
1627 sBgAppWidgets.clear();
1628 sBgFolders.clear();
1629 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001630 sBgWorkspaceScreens.clear();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001631 sBgPinnedShortcutCounts.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001632 }
1633 }
1634
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001635 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -07001636 if (LauncherAppState.PROFILE_STARTUP) {
1637 Trace.beginSection("Loading Workspace");
1638 }
Joe Onorato36115782010-06-17 13:28:48 -04001639 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001640
Joe Onorato36115782010-06-17 13:28:48 -04001641 final Context context = mContext;
1642 final ContentResolver contentResolver = context.getContentResolver();
1643 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001644 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001645 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001646 final boolean isSdCardReady = Utilities.isBootCompleted();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001647
Winson Chung892c74d2013-08-22 16:15:50 -07001648 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001649 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001650 int countX = profile.numColumns;
1651 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001652
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001653 boolean clearDb = false;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -07001654 try {
1655 ImportDataTask.performImportIfPossible(context);
1656 } catch (Exception e) {
1657 // Migration failed. Clear workspace.
1658 clearDb = true;
1659 }
1660
1661 if (!clearDb && GridSizeMigrationTask.ENABLED &&
Sunny Goyalf076eae2016-01-11 12:25:10 -08001662 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1663 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001664 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001665 }
1666
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001667 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -07001668 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001669 LauncherSettings.Settings.call(contentResolver,
1670 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001671 }
1672
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001673 Log.d(TAG, "loadWorkspace: loading default favorites");
1674 LauncherSettings.Settings.call(contentResolver,
1675 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -07001676
Winson Chung2abf94d2012-07-18 18:16:38 -07001677 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001678 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001679 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001680 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001681 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001682
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001683 final ArrayList<Long> itemsToRemove = new ArrayList<>();
1684 final ArrayList<Long> restoredRows = new ArrayList<>();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001685 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001686 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001687 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001688 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001689
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001690 // +1 for the hotseat (it can be larger than the workspace)
1691 // Load workspace in reverse order to ensure that latest items are loaded first (and
1692 // before any earlier duplicates)
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001693 final LongArrayMap<GridOccupancy> occupied = new LongArrayMap<>();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001694 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001695
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001696 try {
1697 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1698 final int intentIndex = c.getColumnIndexOrThrow
1699 (LauncherSettings.Favorites.INTENT);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001700 final int containerIndex = c.getColumnIndexOrThrow(
1701 LauncherSettings.Favorites.CONTAINER);
1702 final int itemTypeIndex = c.getColumnIndexOrThrow(
1703 LauncherSettings.Favorites.ITEM_TYPE);
1704 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1705 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001706 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1707 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001708 final int screenIndex = c.getColumnIndexOrThrow(
1709 LauncherSettings.Favorites.SCREEN);
1710 final int cellXIndex = c.getColumnIndexOrThrow
1711 (LauncherSettings.Favorites.CELLX);
1712 final int cellYIndex = c.getColumnIndexOrThrow
1713 (LauncherSettings.Favorites.CELLY);
1714 final int spanXIndex = c.getColumnIndexOrThrow
1715 (LauncherSettings.Favorites.SPANX);
1716 final int spanYIndex = c.getColumnIndexOrThrow(
1717 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001718 final int rankIndex = c.getColumnIndexOrThrow(
1719 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001720 final int restoredIndex = c.getColumnIndexOrThrow(
1721 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001722 final int profileIdIndex = c.getColumnIndexOrThrow(
1723 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001724 final int optionsIndex = c.getColumnIndexOrThrow(
1725 LauncherSettings.Favorites.OPTIONS);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001726 final CursorIconInfo cursorIconInfo = new CursorIconInfo(mContext, c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001727
Sunny Goyal7f834d22015-04-21 10:10:23 -07001728 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
Kenny Guyff05f432016-01-22 17:48:29 +00001729 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001730 final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>();
Sunny Goyal7f834d22015-04-21 10:10:23 -07001731 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001732 long serialNo = mUserManager.getSerialNumberForUser(user);
1733 allUsers.put(serialNo, user);
1734 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001735
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001736 boolean userUnlocked = mUserManager.isUserUnlocked(user);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001737
1738 // We can only query for shortcuts when the user is unlocked.
1739 if (userUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07001740 List<ShortcutInfoCompat> pinnedShortcuts =
1741 mDeepShortcutManager.queryForPinnedShortcuts(null, user);
1742 if (mDeepShortcutManager.wasLastCallSuccess()) {
1743 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
1744 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
1745 shortcut);
1746 }
1747 } else {
1748 // Shortcut manager can fail due to some race condition when the
1749 // lock state changes too frequently. For the purpose of the loading
1750 // shortcuts, consider the user is still locked.
1751 userUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001752 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001753 }
Sunny Goyal49f4f032016-08-01 15:45:49 -07001754 unlockedUsers.put(serialNo, userUnlocked);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001755 }
1756
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001757 ShortcutInfo info;
1758 String intentDescription;
1759 LauncherAppWidgetInfo appWidgetInfo;
1760 int container;
1761 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001762 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001763 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001764 UserHandleCompat user;
Sunny Goyald09c3702016-04-06 16:18:20 -07001765 String targetPackage;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001766
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001767 while (!mStopped && c.moveToNext()) {
1768 try {
1769 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001770 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001771 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001772 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001773
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001774 switch (itemType) {
1775 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1776 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001777 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001778 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001779 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001780 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001781 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001782 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001783 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001784 boolean itemReplaced = false;
Sunny Goyald09c3702016-04-06 16:18:20 -07001785 targetPackage = null;
Kenny Guyed131872014-04-30 03:02:21 +01001786 if (user == null) {
1787 // User has been deleted remove the item.
1788 itemsToRemove.add(id);
1789 continue;
1790 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001791 try {
1792 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001793 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001794 if (cn != null && cn.getPackageName() != null) {
1795 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1796 cn.getPackageName(), user);
1797 boolean validComponent = validPkg &&
1798 launcherApps.isActivityEnabledForProfile(cn, user);
Sunny Goyald09c3702016-04-06 16:18:20 -07001799 if (validPkg) {
1800 targetPackage = cn.getPackageName();
1801 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001802
1803 if (validComponent) {
1804 if (restored) {
1805 // no special handling necessary for this item
1806 restoredRows.add(id);
1807 restored = false;
1808 }
Kenny Guyff05f432016-01-22 17:48:29 +00001809 if (quietMode.get(serialNumber)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001810 disabledState = ShortcutInfo.FLAG_DISABLED_QUIET_USER;
Kenny Guyff05f432016-01-22 17:48:29 +00001811 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001812 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001813 intent = null;
1814 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1815 // We allow auto install apps to have their intent
1816 // updated after an install.
1817 intent = manager.getLaunchIntentForPackage(
1818 cn.getPackageName());
1819 if (intent != null) {
1820 ContentValues values = new ContentValues();
1821 values.put(LauncherSettings.Favorites.INTENT,
1822 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001823 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001824 }
1825 }
1826
1827 if (intent == null) {
1828 // The app is installed but the component is no
1829 // longer available.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001830 FileLog.d(TAG, "Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001831 itemsToRemove.add(id);
1832 continue;
1833 } else {
1834 // no special handling necessary for this item
1835 restoredRows.add(id);
1836 restored = false;
1837 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001838 } else if (restored) {
1839 // Package is not yet available but might be
1840 // installed later.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001841 FileLog.d(TAG, "package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001842
1843 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1844 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001845 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001846 // App restore has started. Update the flag
1847 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1848 ContentValues values = new ContentValues();
1849 values.put(LauncherSettings.Favorites.RESTORED,
1850 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001851 updateItem(id, values);
1852 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1853 // This is a common app. Try to replace this.
1854 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1855 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1856 if (parser.findDefaultApp()) {
1857 // Default app found. Replace it.
1858 intent = parser.parsedIntent;
1859 cn = intent.getComponent();
1860 ContentValues values = parser.parsedValues;
1861 values.put(LauncherSettings.Favorites.RESTORED, 0);
1862 updateItem(id, values);
1863 restored = false;
1864 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001865
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001866 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001867 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001868 itemsToRemove.add(id);
1869 continue;
1870 }
Sunny Goyal94485362014-09-18 16:13:58 -07001871 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001872 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001873 itemsToRemove.add(id);
1874 continue;
1875 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001876 } else if (PackageManagerHelper.isAppOnSdcard(
1877 manager, cn.getPackageName())) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07001878 // Package is present but not available.
1879 allowMissingTarget = true;
1880 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1881 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001882 // SdCard is not ready yet. Package might get available,
1883 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001884 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001885 HashSet<String> pkgs = sPendingPackages.get(user);
1886 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001887 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001888 sPendingPackages.put(user, pkgs);
1889 }
1890 pkgs.add(cn.getPackageName());
1891 allowMissingTarget = true;
1892 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001893
1894 } else {
1895 // Do not wait for external media load anymore.
1896 // Log the invalid package, and remove it
Sunny Goyal713edfc2016-05-06 09:58:34 -07001897 FileLog.d(TAG, "Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001898 itemsToRemove.add(id);
1899 continue;
Winson Chungee055712013-07-30 14:46:24 -07001900 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001901 } else if (cn == null) {
1902 // For shortcuts with no component, keep them as they are
1903 restoredRows.add(id);
1904 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001905 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001906 } catch (URISyntaxException e) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001907 FileLog.d(TAG, "Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001908 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001909 continue;
1910 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001911
Sunny Goyal34b65272015-03-11 16:56:52 -07001912 boolean useLowResIcon = container >= 0 &&
1913 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1914
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001915 if (itemReplaced) {
1916 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001917 info = getAppShortcutInfo(intent, user, null,
1918 cursorIconInfo, false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001919 } else {
1920 // Don't replace items for other profiles.
1921 itemsToRemove.add(id);
1922 continue;
1923 }
1924 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001925 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001926 info = getRestoredItemInfo(c, intent,
1927 promiseType, itemType, cursorIconInfo);
Kenny Guyed131872014-04-30 03:02:21 +01001928 intent = getRestoredItemIntent(c, context, intent);
1929 } else {
1930 // Don't restore items for other profiles.
1931 itemsToRemove.add(id);
1932 continue;
1933 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001934 } else if (itemType ==
1935 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001936 info = getAppShortcutInfo(intent, user, c,
1937 cursorIconInfo, allowMissingTarget, useLowResIcon);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001938 } else if (itemType ==
1939 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001940
1941 ShortcutKey key = ShortcutKey.fromIntent(intent, user);
1942 if (unlockedUsers.get(serialNumber)) {
1943 ShortcutInfoCompat pinnedShortcut =
1944 shortcutKeyToPinnedShortcuts.get(key);
1945 if (pinnedShortcut == null) {
1946 // The shortcut is no longer valid.
1947 itemsToRemove.add(id);
1948 continue;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001949 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001950 info = new ShortcutInfo(pinnedShortcut, context);
1951 intent = info.intent;
1952 } else {
1953 // Create a shortcut info in disabled mode for now.
1954 info = new ShortcutInfo();
1955 info.user = user;
1956 info.itemType = itemType;
1957 loadInfoFromCursor(info, c, cursorIconInfo);
1958
1959 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001960 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001961 incrementPinnedShortcutCount(key, false /* shouldPin */);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001962 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001963 info = getShortcutInfo(c, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001964
Sunny Goyald09c3702016-04-06 16:18:20 -07001965 // Shortcuts are only available on the primary profile
1966 if (PackageManagerHelper.isAppSuspended(manager, targetPackage)) {
1967 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1968 }
1969
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001970 // App shortcuts that used to be automatically added to Launcher
1971 // didn't always have the correct intent flags set, so do that
1972 // here
1973 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001974 intent.getCategories() != null &&
1975 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001976 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001977 intent.addFlags(
1978 Intent.FLAG_ACTIVITY_NEW_TASK |
1979 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1980 }
Michael Jurka96879562012-03-22 05:54:33 -07001981 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001982
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001983 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001984 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001985 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001986 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001987 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001988 info.cellX = c.getInt(cellXIndex);
1989 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08001990 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001991 info.spanX = 1;
1992 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01001993 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001994 if (info.promisedIntent != null) {
1995 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
1996 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001997 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07001998 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
1999 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2000 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002001
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002002 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002003 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002004 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002005 break;
2006 }
2007
Sunny Goyal756adbc2015-04-16 15:20:51 -07002008 if (restored) {
2009 ComponentName cn = info.getTargetComponent();
2010 if (cn != null) {
2011 Integer progress = installingPkgs.get(cn.getPackageName());
2012 if (progress != null) {
2013 info.setInstallProgress(progress);
2014 } else {
2015 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2016 }
2017 }
2018 }
2019
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002020 switch (container) {
2021 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2022 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2023 sBgWorkspaceItems.add(info);
2024 break;
2025 default:
2026 // Item is in a user folder
2027 FolderInfo folderInfo =
2028 findOrMakeFolder(sBgFolders, container);
Sunny Goyalc52ba712016-04-05 15:59:05 -07002029 folderInfo.add(info, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002030 break;
2031 }
2032 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002033 } else {
2034 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002035 }
2036 break;
2037
2038 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2039 id = c.getLong(idIndex);
2040 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2041
Sunny Goyala508e4f2015-05-21 09:33:57 -07002042 // Do not trim the folder label, as is was set by the user.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002043 folderInfo.title = c.getString(cursorIconInfo.titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002044 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002045 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002046 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002047 folderInfo.cellX = c.getInt(cellXIndex);
2048 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002049 folderInfo.spanX = 1;
2050 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002051 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002052
Daniel Sandler8802e962010-05-26 16:28:16 -04002053 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002054 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002055 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002056 break;
2057 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002058
Joe Onorato9c1289c2009-08-17 11:03:03 -04002059 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002060 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2061 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2062 sBgWorkspaceItems.add(folderInfo);
2063 break;
Joe Onorato36115782010-06-17 13:28:48 -04002064 }
Joe Onorato17a89222011-02-08 17:26:11 -08002065
Chris Wrenf4d08112014-01-16 18:13:56 -05002066 if (restored) {
2067 // no special handling required for restored folders
2068 restoredRows.add(id);
2069 }
2070
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002071 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2072 sBgFolders.put(folderInfo.id, folderInfo);
2073 break;
2074
2075 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002076 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002077 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002078 boolean customWidget = itemType ==
2079 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2080
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002081 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002082 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002083 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002084 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002085 user = allUsers.get(serialNumber);
2086 if (user == null) {
2087 itemsToRemove.add(id);
2088 continue;
2089 }
2090
Sunny Goyalff572272014-07-23 13:58:07 -07002091 final ComponentName component =
2092 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002093
Sunny Goyal651077b2014-06-30 14:15:31 -07002094 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002095 final boolean isIdValid = (restoreStatus &
2096 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002097 final boolean wasProviderReady = (restoreStatus &
2098 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002099
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002100 if (widgetProvidersMap == null) {
2101 widgetProvidersMap = AppWidgetManagerCompat
2102 .getInstance(mContext).getAllProvidersMap();
2103 }
2104 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
2105 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00002106 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002107 user));
Sunny Goyalff572272014-07-23 13:58:07 -07002108
2109 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002110 if (!isSafeMode && !customWidget &&
2111 wasProviderReady && !isProviderReady) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002112 FileLog.d(TAG, "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07002113 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002114 itemsToRemove.add(id);
2115 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002116 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002117 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2118 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002119
Sunny Goyal53f96722015-07-13 19:54:53 -07002120 // The provider is available. So the widget is either
2121 // available or not available. We do not need to track
2122 // any future restore updates.
2123 int status = restoreStatus &
2124 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002125 if (!wasProviderReady) {
2126 // If provider was not previously ready, update the
2127 // status and UI flag.
2128
2129 // Id would be valid only if the widget restore broadcast was received.
2130 if (isIdValid) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002131 status = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002132 } else {
2133 status &= ~LauncherAppWidgetInfo
2134 .FLAG_PROVIDER_NOT_READY;
2135 }
2136 }
2137 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002138 } else {
2139 Log.v(TAG, "Widget restore pending id=" + id
2140 + " appWidgetId=" + appWidgetId
2141 + " status =" + restoreStatus);
2142 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002143 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002144 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002145 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002146
2147 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2148 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002149 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002150 // App restore has started. Update the flag
2151 appWidgetInfo.restoreStatus |=
2152 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002153 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002154 FileLog.d(TAG, "Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07002155 itemsToRemove.add(id);
2156 continue;
2157 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002158
2159 appWidgetInfo.installProgress =
2160 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002161 }
Sunny Goyalff572272014-07-23 13:58:07 -07002162
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002163 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002164 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002165 appWidgetInfo.cellX = c.getInt(cellXIndex);
2166 appWidgetInfo.cellY = c.getInt(cellYIndex);
2167 appWidgetInfo.spanX = c.getInt(spanXIndex);
2168 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002169 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002170
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002171 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2172 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2173 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002174 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2175 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002176 continue;
2177 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002178
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002179 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002180 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002181 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002182 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002183 break;
2184 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002185
Adam Cohen59400422014-03-05 18:07:04 -08002186 if (!customWidget) {
2187 String providerName =
2188 appWidgetInfo.providerName.flattenToString();
2189 if (!providerName.equals(savedProvider) ||
2190 (appWidgetInfo.restoreStatus != restoreStatus)) {
2191 ContentValues values = new ContentValues();
2192 values.put(
2193 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2194 providerName);
2195 values.put(LauncherSettings.Favorites.RESTORED,
2196 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002197 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002198 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002199 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002200 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2201 sBgAppWidgets.add(appWidgetInfo);
2202 }
Joe Onorato36115782010-06-17 13:28:48 -04002203 break;
2204 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002205 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002206 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002207 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002208 }
2209 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002210 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002211 }
2212
Winson Chungba9c37f2013-08-30 14:11:37 -07002213 // Break early if we've stopped loading
2214 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002215 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002216 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002217 }
2218
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002219 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002220 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002221 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2222 Utilities.createDbSelectionQuery(
2223 LauncherSettings.Favorites._ID, itemsToRemove), null);
2224 if (DEBUG_LOADERS) {
2225 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2226 LauncherSettings.Favorites._ID, itemsToRemove));
2227 }
2228
2229 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07002230 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
2231 .call(contentResolver,
2232 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
2233 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
2234 for (long folderId : deletedFolderIds) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002235 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2236 sBgFolders.remove(folderId);
2237 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002238 }
2239 }
2240
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002241 // Unpin shortcuts that don't exist on the workspace.
2242 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
2243 MutableInt numTimesPinned = sBgPinnedShortcutCounts.get(key);
2244 if (numTimesPinned == null || numTimesPinned.value == 0) {
2245 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
2246 mDeepShortcutManager.unpinShortcut(key);
2247 }
2248 }
2249
Sunny Goyal317698b2015-07-29 11:45:41 -07002250 // Sort all the folder items and make sure the first 3 items are high resolution.
2251 for (FolderInfo folder : sBgFolders) {
2252 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2253 int pos = 0;
2254 for (ShortcutInfo info : folder.contents) {
2255 if (info.usingLowResIcon) {
2256 info.updateIcon(mIconCache, false);
2257 }
2258 pos ++;
2259 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2260 break;
2261 }
2262 }
2263 }
2264
Chris Wrenf4d08112014-01-16 18:13:56 -05002265 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002266 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002267 ContentValues values = new ContentValues();
2268 values.put(LauncherSettings.Favorites.RESTORED, 0);
2269 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2270 Utilities.createDbSelectionQuery(
2271 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002272 }
2273
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002274 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2275 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002276 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002277 null, sWorker);
2278 }
2279
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002280 // Remove any empty screens
2281 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002282 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002283 long screenId = item.screenId;
2284 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2285 unusedScreens.contains(screenId)) {
2286 unusedScreens.remove(screenId);
2287 }
2288 }
2289
2290 // If there are any empty screens remove them, and update.
2291 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002292 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002293 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002294 }
2295
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002296 if (DEBUG_LOADERS) {
2297 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2298 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002299 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002300 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002301 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002302
Sunny Goyale2df0622015-04-24 11:27:00 -07002303 for (int i = 0; i < nScreens; i++) {
2304 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002305 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002306 line += " | ";
2307 }
Joe Onorato36115782010-06-17 13:28:48 -04002308 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002309 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002310 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002311 }
Joe Onorato36115782010-06-17 13:28:48 -04002312 }
Sunny Goyale26d1002016-06-20 14:52:14 -07002313 if (LauncherAppState.PROFILE_STARTUP) {
2314 Trace.endSection();
2315 }
Adam Cohene25af792013-06-06 23:08:25 -07002316 }
2317
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002318 /**
2319 * Partially updates the item without any notification. Must be called on the worker thread.
2320 */
2321 private void updateItem(long itemId, ContentValues update) {
2322 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002323 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002324 update,
2325 BaseColumns._ID + "= ?",
2326 new String[]{Long.toString(itemId)});
2327 }
2328
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002329 /** Filters the set of items who are directly or indirectly (via another container) on the
2330 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002331 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002332 ArrayList<ItemInfo> allWorkspaceItems,
2333 ArrayList<ItemInfo> currentScreenItems,
2334 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002335 // Purge any null ItemInfos
2336 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2337 while (iter.hasNext()) {
2338 ItemInfo i = iter.next();
2339 if (i == null) {
2340 iter.remove();
2341 }
2342 }
2343
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002344 // Order the set of items by their containers first, this allows use to walk through the
2345 // list sequentially, build up a list of containers that are in the specified screen,
2346 // as well as all items in those containers.
2347 Set<Long> itemsOnScreen = new HashSet<Long>();
2348 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2349 @Override
2350 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07002351 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002352 }
2353 });
2354 for (ItemInfo info : allWorkspaceItems) {
2355 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002356 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002357 currentScreenItems.add(info);
2358 itemsOnScreen.add(info.id);
2359 } else {
2360 otherScreenItems.add(info);
2361 }
2362 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2363 currentScreenItems.add(info);
2364 itemsOnScreen.add(info.id);
2365 } else {
2366 if (itemsOnScreen.contains(info.container)) {
2367 currentScreenItems.add(info);
2368 itemsOnScreen.add(info.id);
2369 } else {
2370 otherScreenItems.add(info);
2371 }
2372 }
2373 }
2374 }
2375
2376 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002377 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002378 ArrayList<LauncherAppWidgetInfo> appWidgets,
2379 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2380 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002381
2382 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002383 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002384 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002385 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002386 currentScreenWidgets.add(widget);
2387 } else {
2388 otherScreenWidgets.add(widget);
2389 }
2390 }
2391 }
2392
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002393 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2394 * right) */
2395 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002396 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002397 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002398 final int screenCols = profile.numColumns;
2399 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002400 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002401 @Override
2402 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002403 if (lhs.container == rhs.container) {
2404 // Within containers, order by their spatial position in that container
2405 switch ((int) lhs.container) {
2406 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2407 long lr = (lhs.screenId * screenCellCount +
2408 lhs.cellY * screenCols + lhs.cellX);
2409 long rr = (rhs.screenId * screenCellCount +
2410 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07002411 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07002412 }
2413 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2414 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07002415 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07002416 }
2417 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002418 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002419 throw new RuntimeException("Unexpected container type when " +
2420 "sorting workspace items.");
2421 }
2422 return 0;
2423 }
2424 } else {
2425 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07002426 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07002427 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002428 }
2429 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002430 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002431
Adam Cohendcd297f2013-06-18 13:13:40 -07002432 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2433 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002434 final Runnable r = new Runnable() {
2435 @Override
2436 public void run() {
2437 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2438 if (callbacks != null) {
2439 callbacks.bindScreens(orderedScreens);
2440 }
2441 }
2442 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002443 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002444 }
2445
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002446 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2447 final ArrayList<ItemInfo> workspaceItems,
2448 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002449 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002450
2451 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002452 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002453 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002454 final int start = i;
2455 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002456 final Runnable r = new Runnable() {
2457 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002458 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002459 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002460 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002461 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2462 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002463 }
2464 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002465 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002466 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002467 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002468
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002469 // Bind the widgets, one at a time
2470 N = appWidgets.size();
2471 for (int i = 0; i < N; i++) {
2472 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2473 final Runnable r = new Runnable() {
2474 public void run() {
2475 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2476 if (callbacks != null) {
2477 callbacks.bindAppWidget(widget);
2478 }
2479 }
2480 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002481 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002482 }
2483 }
2484
2485 /**
2486 * Binds all loaded data to actual views on the main thread.
2487 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002488 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002489 final long t = SystemClock.uptimeMillis();
2490 Runnable r;
2491
2492 // Don't use these two variables in any of the callback runnables.
2493 // Otherwise we hold a reference to them.
2494 final Callbacks oldCallbacks = mCallbacks.get();
2495 if (oldCallbacks == null) {
2496 // This launcher has exited and nobody bothered to tell us. Just bail.
2497 Log.w(TAG, "LoaderTask running with no launcher");
2498 return;
2499 }
2500
Winson Chung9b9fb962013-11-15 15:39:34 -08002501 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07002502 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
2503 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
2504 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002505
Winson Chung2abf94d2012-07-18 18:16:38 -07002506 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002507 workspaceItems.addAll(sBgWorkspaceItems);
2508 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002509 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002510 }
2511
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002512 final int currentScreen;
2513 {
2514 int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE
2515 ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen();
2516 if (currScreen >= orderedScreenIds.size()) {
2517 // There may be no workspace screens (just hotseat items and an empty page).
2518 currScreen = PagedView.INVALID_RESTORE_PAGE;
2519 }
2520 currentScreen = currScreen;
Winson Chung9b9fb962013-11-15 15:39:34 -08002521 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002522 final boolean validFirstPage = currentScreen >= 0;
2523 final long currentScreenId =
2524 validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID;
Winson Chung9b9fb962013-11-15 15:39:34 -08002525
Winson Chung9b9fb962013-11-15 15:39:34 -08002526 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07002527 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
2528 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
2529 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
2530 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002531
Winson Chung9b9fb962013-11-15 15:39:34 -08002532 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002533 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002534 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002535 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002536 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2537 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2538
2539 // Tell the workspace that we're about to start binding items
2540 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002541 public void run() {
2542 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2543 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002544 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002545 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002546 }
2547 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002548 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002549 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002550
Adam Cohendcd297f2013-06-18 13:13:40 -07002551 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2552
Sunny Goyal527c7d32015-08-28 15:19:36 -07002553 Executor mainExecutor = new DeferredMainThreadExecutor();
2554 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07002555 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002556
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002557 // In case of validFirstPage, only bind the first screen, and defer binding the
2558 // remaining screens after first onDraw (and an optional the fade animation whichever
2559 // happens later).
2560 // This ensures that the first screen is immediately visible (eg. during rotation)
2561 // In case of !validFirstPage, bind all pages one after other.
2562 final Executor deferredExecutor =
2563 validFirstPage ? new ViewOnDrawExecutor(mHandler) : mainExecutor;
2564
2565 mainExecutor.execute(new Runnable() {
2566 @Override
2567 public void run() {
2568 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2569 if (callbacks != null) {
2570 callbacks.finishFirstPageBind(
2571 validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null);
2572 }
2573 }
2574 });
Sunny Goyal527c7d32015-08-28 15:19:36 -07002575
Sunny Goyal44c06432016-04-02 10:56:02 -07002576 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002577
2578 // Tell the workspace that we're done binding items
2579 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002580 public void run() {
2581 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2582 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002583 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002584 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002585
Sunny Goyal639e9062015-08-19 19:17:06 -07002586 mIsLoadingAndBindingWorkspace = false;
2587
2588 // Run all the bind complete runnables after workspace is bound.
2589 if (!mBindCompleteRunnables.isEmpty()) {
2590 synchronized (mBindCompleteRunnables) {
2591 for (final Runnable r : mBindCompleteRunnables) {
2592 runOnWorkerThread(r);
2593 }
2594 mBindCompleteRunnables.clear();
2595 }
2596 }
2597
Winson Chung98e030b2012-05-07 16:01:11 -07002598 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002599 if (DEBUG_LOADERS) {
2600 Log.d(TAG, "bound workspace in "
2601 + (SystemClock.uptimeMillis()-t) + "ms");
2602 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002603
Joe Onorato36115782010-06-17 13:28:48 -04002604 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002605 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002606 deferredExecutor.execute(r);
2607
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002608 if (validFirstPage) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002609 r = new Runnable() {
2610 public void run() {
2611 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2612 if (callbacks != null) {
2613 // We are loading synchronously, which means, some of the pages will be
2614 // bound after first draw. Inform the callbacks that page binding is
2615 // not complete, and schedule the remaining pages.
2616 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2617 callbacks.onPageBoundSynchronously(currentScreen);
2618 }
2619 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2620 }
2621 }
2622 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002623 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002624 }
Joe Onorato36115782010-06-17 13:28:48 -04002625 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002626
Joe Onorato36115782010-06-17 13:28:48 -04002627 private void loadAndBindAllApps() {
2628 if (DEBUG_LOADERS) {
2629 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2630 }
2631 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002632 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002633 synchronized (LoaderTask.this) {
2634 if (mStopped) {
2635 return;
2636 }
2637 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002638 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002639 synchronized (LoaderTask.this) {
2640 if (mStopped) {
2641 return;
2642 }
2643 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002644 }
Joe Onorato36115782010-06-17 13:28:48 -04002645 } else {
2646 onlyBindAllApps();
2647 }
2648 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002649
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002650 private void updateIconCache() {
2651 // Ignore packages which have a promise icon.
2652 HashSet<String> packagesToIgnore = new HashSet<>();
2653 synchronized (sBgLock) {
2654 for (ItemInfo info : sBgItemsIdMap) {
2655 if (info instanceof ShortcutInfo) {
2656 ShortcutInfo si = (ShortcutInfo) info;
2657 if (si.isPromise() && si.getTargetComponent() != null) {
2658 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2659 }
2660 } else if (info instanceof LauncherAppWidgetInfo) {
2661 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2662 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2663 packagesToIgnore.add(lawi.providerName.getPackageName());
2664 }
2665 }
2666 }
2667 }
2668 mIconCache.updateDbIcons(packagesToIgnore);
2669 }
2670
Joe Onorato36115782010-06-17 13:28:48 -04002671 private void onlyBindAllApps() {
2672 final Callbacks oldCallbacks = mCallbacks.get();
2673 if (oldCallbacks == null) {
2674 // This launcher has exited and nobody bothered to tell us. Just bail.
2675 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2676 return;
2677 }
2678
2679 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002680 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002681 final ArrayList<AppInfo> list
2682 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002683 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002684 public void run() {
2685 final long t = SystemClock.uptimeMillis();
2686 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2687 if (callbacks != null) {
2688 callbacks.bindAllApplications(list);
2689 }
2690 if (DEBUG_LOADERS) {
2691 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002692 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04002693 }
2694 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002695 };
Tony Wickham80f57872016-06-29 18:12:15 -07002696 runOnMainThread(r);
Joe Onorato36115782010-06-17 13:28:48 -04002697 }
2698
Winson Chung64359a52013-07-08 17:17:08 -07002699 private void loadAllApps() {
2700 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002701
Joe Onorato36115782010-06-17 13:28:48 -04002702 final Callbacks oldCallbacks = mCallbacks.get();
2703 if (oldCallbacks == null) {
2704 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002705 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002706 return;
2707 }
2708
Kenny Guyed131872014-04-30 03:02:21 +01002709 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2710
Winson Chung64359a52013-07-08 17:17:08 -07002711 // Clear the list of apps
2712 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002713 for (UserHandleCompat user : profiles) {
2714 // Query for the set of apps
2715 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002716 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002717 if (DEBUG_LOADERS) {
2718 Log.d(TAG, "getActivityList took "
2719 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2720 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2721 }
2722 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002723 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002724 if (apps == null || apps.isEmpty()) {
2725 return;
2726 }
Kenny Guyff05f432016-01-22 17:48:29 +00002727 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002728 // Create the ApplicationInfos
2729 for (int i = 0; i < apps.size(); i++) {
2730 LauncherActivityInfoCompat app = apps.get(i);
2731 // This builds the icon bitmaps.
Kenny Guyff05f432016-01-22 17:48:29 +00002732 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode));
Kenny Guyed131872014-04-30 03:02:21 +01002733 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002734
Sunny Goyal756a28a2015-04-23 17:07:55 -07002735 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2736 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002737 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002738
2739 @Override
2740 public void run() {
2741 heuristic.processUserApps(apps);
2742 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002743 };
2744 runOnMainThread(new Runnable() {
2745
2746 @Override
2747 public void run() {
2748 // Check isLoadingWorkspace on the UI thread, as it is updated on
2749 // the UI thread.
2750 if (mIsLoadingAndBindingWorkspace) {
2751 synchronized (mBindCompleteRunnables) {
2752 mBindCompleteRunnables.add(r);
2753 }
2754 } else {
2755 runOnWorkerThread(r);
2756 }
2757 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002758 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002759 }
Winson Chung64359a52013-07-08 17:17:08 -07002760 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002761 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002762 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2763 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002764
2765 // Post callback on main thread
2766 mHandler.post(new Runnable() {
2767 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002768
Winson Chung64359a52013-07-08 17:17:08 -07002769 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002770 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002771 if (callbacks != null) {
2772 callbacks.bindAllApplications(added);
2773 if (DEBUG_LOADERS) {
2774 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002775 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07002776 }
2777 } else {
2778 Log.i(TAG, "not binding apps: no Launcher activity");
2779 }
2780 }
2781 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002782 // Cleanup any data stored for a deleted user.
2783 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04002784 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002785 Log.d(TAG, "Icons processed in "
2786 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002787 }
2788 }
2789
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002790 private void loadAndBindDeepShortcuts() {
2791 if (DEBUG_LOADERS) {
2792 Log.d(TAG, "loadAndBindDeepShortcuts mDeepShortcutsLoaded=" + mDeepShortcutsLoaded);
2793 }
2794 if (!mDeepShortcutsLoaded) {
2795 mBgDeepShortcutMap.clear();
2796 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002797 if (mUserManager.isUserUnlocked(user)) {
2798 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager
2799 .queryForAllShortcuts(user);
2800 updateDeepShortcutMap(null, user, shortcuts);
2801 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002802 }
2803 synchronized (LoaderTask.this) {
2804 if (mStopped) {
2805 return;
2806 }
2807 mDeepShortcutsLoaded = true;
2808 }
2809 }
Tony Wickham80f57872016-06-29 18:12:15 -07002810 bindDeepShortcuts();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002811 }
2812
Joe Onoratobe386092009-11-17 17:32:16 -08002813 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002814 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002815 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002816 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2817 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2818 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2819 }
Joe Onorato36115782010-06-17 13:28:48 -04002820 }
2821 }
2822
Sunny Goyal860538d2016-07-20 12:35:16 -07002823 /**
2824 * Clear all the shortcuts for the given package, and re-add the new shortcuts.
2825 */
2826 private void updateDeepShortcutMap(
2827 String packageName, UserHandleCompat user, List<ShortcutInfoCompat> shortcuts) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002828 if (packageName != null) {
2829 Iterator<ComponentKey> keysIter = mBgDeepShortcutMap.keySet().iterator();
2830 while (keysIter.hasNext()) {
Sunny Goyal860538d2016-07-20 12:35:16 -07002831 ComponentKey next = keysIter.next();
2832 if (next.componentName.getPackageName().equals(packageName)
2833 && next.user.equals(user)) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002834 keysIter.remove();
2835 }
2836 }
2837 }
2838
2839 // Now add the new shortcuts to the map.
2840 for (ShortcutInfoCompat shortcut : shortcuts) {
Tony Wickhamca258b32016-07-28 12:31:28 -07002841 boolean shouldShowInContainer = shortcut.isEnabled()
2842 && (shortcut.isDeclaredInManifest() || shortcut.isDynamic());
2843 if (shouldShowInContainer) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002844 ComponentKey targetComponent
2845 = new ComponentKey(shortcut.getActivity(), shortcut.getUserHandle());
2846 mBgDeepShortcutMap.addToList(targetComponent, shortcut.getId());
2847 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002848 }
2849 }
2850
Tony Wickham80f57872016-06-29 18:12:15 -07002851 public void bindDeepShortcuts() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002852 final MultiHashMap<ComponentKey, String> shortcutMapCopy = new MultiHashMap<>();
2853 shortcutMapCopy.putAll(mBgDeepShortcutMap);
Tony Wickham80f57872016-06-29 18:12:15 -07002854 Runnable r = new Runnable() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002855 @Override
2856 public void run() {
2857 Callbacks callbacks = getCallback();
2858 if (callbacks != null) {
2859 callbacks.bindDeepShortcutMap(shortcutMapCopy);
2860 }
2861 }
Tony Wickham80f57872016-06-29 18:12:15 -07002862 };
2863 runOnMainThread(r);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002864 }
2865
Sunny Goyal75b0f552015-05-20 21:57:06 -07002866 /**
2867 * Called when the icons for packages have been updated in the icon cache.
2868 */
2869 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2870 final Callbacks callbacks = getCallback();
2871 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2872 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2873
2874 // If any package icon has changed (app was updated while launcher was dead),
2875 // update the corresponding shortcuts.
2876 synchronized (sBgLock) {
2877 for (ItemInfo info : sBgItemsIdMap) {
2878 if (info instanceof ShortcutInfo && user.equals(info.user)
2879 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2880 ShortcutInfo si = (ShortcutInfo) info;
2881 ComponentName cn = si.getTargetComponent();
2882 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2883 si.updateIcon(mIconCache);
2884 updatedShortcuts.add(si);
2885 }
2886 }
2887 }
2888 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2889 }
2890
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002891 bindUpdatedShortcuts(updatedShortcuts, user);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002892
2893 if (!updatedApps.isEmpty()) {
2894 mHandler.post(new Runnable() {
2895
2896 public void run() {
2897 Callbacks cb = getCallback();
2898 if (cb != null && callbacks == cb) {
2899 cb.bindAppsUpdated(updatedApps);
2900 }
2901 }
2902 });
2903 }
2904 }
2905
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002906 private void bindUpdatedShortcuts(
2907 ArrayList<ShortcutInfo> updatedShortcuts, UserHandleCompat user) {
2908 bindUpdatedShortcuts(updatedShortcuts, new ArrayList<ShortcutInfo>(), user);
2909 }
2910
2911 private void bindUpdatedShortcuts(
2912 final ArrayList<ShortcutInfo> updatedShortcuts,
2913 final ArrayList<ShortcutInfo> removedShortcuts,
2914 final UserHandleCompat user) {
2915 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002916 final Callbacks callbacks = getCallback();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002917 mHandler.post(new Runnable() {
2918
2919 public void run() {
2920 Callbacks cb = getCallback();
2921 if (cb != null && callbacks == cb) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002922 cb.bindShortcutsChanged(updatedShortcuts, removedShortcuts, user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002923 }
2924 }
2925 });
2926 }
2927 }
2928
2929 void enqueueItemUpdatedTask(Runnable task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002930 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002931 }
2932
Adam Cohen091440a2015-03-18 14:16:05 -07002933 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002934
2935 @Override
2936 public void onReceive(Context context, Intent intent) {
2937 synchronized (sBgLock) {
2938 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2939 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002940 final PackageManager manager = context.getPackageManager();
2941 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2942 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002943 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2944 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002945 packagesRemoved.clear();
2946 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002947 for (String pkg : entry.getValue()) {
2948 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07002949 if (PackageManagerHelper.isAppOnSdcard(manager, pkg)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002950 packagesUnavailable.add(pkg);
2951 } else {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002952 packagesRemoved.add(pkg);
2953 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002954 }
2955 }
2956 if (!packagesRemoved.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002957 enqueueItemUpdatedTask(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002958 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2959 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002960 if (!packagesUnavailable.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002961 enqueueItemUpdatedTask(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
Sunny Goyal1a745e82014-10-02 15:58:31 -07002962 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2963 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002964 }
Sunny Goyal34942622014-08-29 17:20:55 -07002965 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002966 }
2967 }
2968 }
2969
Joe Onorato36115782010-06-17 13:28:48 -04002970 private class PackageUpdatedTask implements Runnable {
2971 int mOp;
2972 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002973 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002974
2975 public static final int OP_NONE = 0;
2976 public static final int OP_ADD = 1;
2977 public static final int OP_UPDATE = 2;
2978 public static final int OP_REMOVE = 3; // uninstlled
2979 public static final int OP_UNAVAILABLE = 4; // external media unmounted
Kenny Guy44cba692016-01-21 19:50:02 +00002980 public static final int OP_SUSPEND = 5; // package suspended
2981 public static final int OP_UNSUSPEND = 6; // package unsuspended
Sunny Goyalda891c12016-03-18 18:29:24 -07002982 public static final int OP_USER_AVAILABILITY_CHANGE = 7; // user available/unavailable
Joe Onorato36115782010-06-17 13:28:48 -04002983
Kenny Guyed131872014-04-30 03:02:21 +01002984 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002985 mOp = op;
2986 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002987 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002988 }
2989
2990 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002991 if (!mHasLoaderCompletedOnce) {
2992 // Loader has not yet run.
2993 return;
2994 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002995 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002996
2997 final String[] packages = mPackages;
2998 final int N = packages.length;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002999 FlagOp flagOp = FlagOp.NO_OP;
Sunny Goyalda891c12016-03-18 18:29:24 -07003000 StringFilter pkgFilter = StringFilter.of(new HashSet<>(Arrays.asList(packages)));
Joe Onorato36115782010-06-17 13:28:48 -04003001 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003002 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04003003 for (int i=0; i<N; i++) {
3004 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003005 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003006 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003007 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003008
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003009 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3010 if (heuristic != null) {
3011 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003012 }
Joe Onorato36115782010-06-17 13:28:48 -04003013 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003014 }
Joe Onorato36115782010-06-17 13:28:48 -04003015 case OP_UPDATE:
3016 for (int i=0; i<N; i++) {
3017 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003018 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003019 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003020 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003021 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003022 // Since package was just updated, the target must be available now.
3023 flagOp = FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04003024 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003025 case OP_REMOVE: {
3026 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3027 if (heuristic != null) {
3028 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003029 }
Joe Onorato36115782010-06-17 13:28:48 -04003030 for (int i=0; i<N; i++) {
3031 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003032 mIconCache.removeIconsForPkg(packages[i], mUser);
3033 }
3034 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003035 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003036 case OP_UNAVAILABLE:
3037 for (int i=0; i<N; i++) {
3038 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3039 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003040 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003041 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003042 flagOp = FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04003043 break;
Kenny Guy44cba692016-01-21 19:50:02 +00003044 case OP_SUSPEND:
3045 case OP_UNSUSPEND:
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003046 flagOp = mOp == OP_SUSPEND ?
3047 FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED) :
3048 FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED);
Sunny Goyal17763cb2016-03-24 13:53:22 -07003049 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.(un)suspend " + N);
Sunny Goyalda891c12016-03-18 18:29:24 -07003050 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
3051 break;
3052 case OP_USER_AVAILABILITY_CHANGE:
3053 flagOp = UserManagerCompat.getInstance(context).isQuietModeEnabled(mUser)
3054 ? FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER)
3055 : FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER);
3056 // We want to update all packages for this user.
3057 pkgFilter = StringFilter.matchesAll();
3058 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
Kenny Guy44cba692016-01-21 19:50:02 +00003059 break;
Joe Onorato36115782010-06-17 13:28:48 -04003060 }
3061
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003062 ArrayList<AppInfo> added = null;
3063 ArrayList<AppInfo> modified = null;
3064 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003065
Adam Cohen487f7dd2012-06-28 18:12:10 -07003066 if (mBgAllAppsList.added.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003067 added = new ArrayList<>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003068 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003069 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003070 if (mBgAllAppsList.modified.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003071 modified = new ArrayList<>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003072 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003073 }
Winson Chung5d55f332012-07-16 20:45:03 -07003074 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003075 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003076 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003077 }
3078
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003079 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps = new HashMap<>();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003080
Joe Onorato36115782010-06-17 13:28:48 -04003081 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003082 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003083 for (AppInfo ai : added) {
3084 addedOrUpdatedApps.put(ai.componentName, ai);
3085 }
Joe Onorato36115782010-06-17 13:28:48 -04003086 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003087
Joe Onorato36115782010-06-17 13:28:48 -04003088 if (modified != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003089 final Callbacks callbacks = getCallback();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003090 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003091 for (AppInfo ai : modified) {
3092 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003093 }
3094
Joe Onorato36115782010-06-17 13:28:48 -04003095 mHandler.post(new Runnable() {
3096 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003097 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003098 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003099 callbacks.bindAppsUpdated(modifiedFinal);
3100 }
3101 }
3102 });
3103 }
Winson Chung83892cc2013-05-01 16:53:33 -07003104
Sunny Goyal4390ace2014-10-13 11:33:11 -07003105 // Update shortcut infos
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003106 if (mOp == OP_ADD || flagOp != FlagOp.NO_OP) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003107 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3108 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3109 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3110
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003111 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003112 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003113 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3114 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003115 boolean infoUpdated = false;
3116 boolean shortcutUpdated = false;
3117
3118 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003119 if ((si.iconResource != null)
Sunny Goyalda891c12016-03-18 18:29:24 -07003120 && pkgFilter.matches(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003121 Bitmap icon = Utilities.createIconBitmap(
3122 si.iconResource.packageName,
3123 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003124 if (icon != null) {
3125 si.setIcon(icon);
3126 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003127 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003128 }
3129 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003130
3131 ComponentName cn = si.getTargetComponent();
Sunny Goyalda891c12016-03-18 18:29:24 -07003132 if (cn != null && pkgFilter.matches(cn.getPackageName())) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003133 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3134
3135 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003136 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3137 // Auto install icon
3138 PackageManager pm = context.getPackageManager();
3139 ResolveInfo matched = pm.resolveActivity(
3140 new Intent(Intent.ACTION_MAIN)
3141 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3142 PackageManager.MATCH_DEFAULT_ONLY);
3143 if (matched == null) {
3144 // Try to find the best match activity.
3145 Intent intent = pm.getLaunchIntentForPackage(
3146 cn.getPackageName());
3147 if (intent != null) {
3148 cn = intent.getComponent();
3149 appInfo = addedOrUpdatedApps.get(cn);
3150 }
3151
3152 if ((intent == null) || (appInfo == null)) {
3153 removedShortcuts.add(si);
3154 continue;
3155 }
3156 si.promisedIntent = intent;
3157 }
3158 }
3159
3160 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003161 if (appInfo != null) {
3162 si.flags = appInfo.flags;
3163 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003164
Sunny Goyal756adbc2015-04-16 15:20:51 -07003165 si.intent = si.promisedIntent;
3166 si.promisedIntent = null;
3167 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003168 infoUpdated = true;
3169 si.updateIcon(mIconCache);
3170 }
3171
3172 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3173 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3174 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003175 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003176 si.contentDescription = appInfo.contentDescription;
3177 infoUpdated = true;
3178 }
3179
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003180 int oldDisabledFlags = si.isDisabled;
3181 si.isDisabled = flagOp.apply(si.isDisabled);
3182 if (si.isDisabled != oldDisabledFlags) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003183 shortcutUpdated = true;
3184 }
3185 }
3186
3187 if (infoUpdated || shortcutUpdated) {
3188 updatedShortcuts.add(si);
3189 }
3190 if (infoUpdated) {
3191 updateItemInDatabase(context, si);
3192 }
Sunny Goyalda891c12016-03-18 18:29:24 -07003193 } else if (info instanceof LauncherAppWidgetInfo && mOp == OP_ADD) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003194 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3195 if (mUser.equals(widgetInfo.user)
3196 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
Sunny Goyalda891c12016-03-18 18:29:24 -07003197 && pkgFilter.matches(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003198 widgetInfo.restoreStatus &=
3199 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3200 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003201
3202 // adding this flag ensures that launcher shows 'click to setup'
3203 // if the widget has a config activity. In case there is no config
3204 // activity, it will be marked as 'restored' during bind.
3205 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3206
Sunny Goyal4390ace2014-10-13 11:33:11 -07003207 widgets.add(widgetInfo);
3208 updateItemInDatabase(context, widgetInfo);
3209 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003210 }
3211 }
3212 }
3213
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003214 bindUpdatedShortcuts(updatedShortcuts, removedShortcuts, mUser);
3215 if (!removedShortcuts.isEmpty()) {
3216 deleteItemsFromDatabase(context, removedShortcuts);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003217 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003218
Sunny Goyal4390ace2014-10-13 11:33:11 -07003219 if (!widgets.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003220 final Callbacks callbacks = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003221 mHandler.post(new Runnable() {
3222 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003223 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003224 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003225 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003226 }
3227 }
3228 });
3229 }
3230 }
3231
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003232 final HashSet<String> removedPackages = new HashSet<>();
3233 final HashSet<ComponentName> removedComponents = new HashSet<>();
3234 if (mOp == OP_REMOVE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003235 // Mark all packages in the broadcast to be removed
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003236 Collections.addAll(removedPackages, packages);
3237
3238 // No need to update the removedComponents as
3239 // removedPackages is a super-set of removedComponents
Winson Chungdf95eb12013-10-16 14:57:07 -07003240 } else if (mOp == OP_UPDATE) {
3241 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003242 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003243 if (isPackageDisabled(context, packages[i], mUser)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003244 removedPackages.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003245 }
3246 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003247
3248 // Update removedComponents as some components can get removed during package update
3249 for (AppInfo info : removedApps) {
3250 removedComponents.add(info.componentName);
3251 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003252 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003253
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003254 if (!removedPackages.isEmpty() || !removedComponents.isEmpty()) {
3255 for (String pn : removedPackages) {
3256 deletePackageFromDatabase(context, pn, mUser);
3257 }
3258 for (ComponentName cn : removedComponents) {
3259 deleteItemsFromDatabase(context, getItemInfoForComponentName(cn, mUser));
Sunny Goyal1a745e82014-10-02 15:58:31 -07003260 }
3261
Winson Chungdf95eb12013-10-16 14:57:07 -07003262 // Remove any queued items from the install queue
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003263 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackages, mUser);
3264
Winson Chungdf95eb12013-10-16 14:57:07 -07003265 // Call the components-removed callback
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003266 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003267 mHandler.post(new Runnable() {
3268 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003269 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003270 if (callbacks == cb && cb != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003271 callbacks.bindWorkspaceComponentsRemoved(
3272 removedPackages, removedComponents, mUser);
3273 }
3274 }
3275 });
3276 }
3277
3278 if (!removedApps.isEmpty()) {
3279 // Remove corresponding apps from All-Apps
3280 final Callbacks callbacks = getCallback();
3281 mHandler.post(new Runnable() {
3282 public void run() {
3283 Callbacks cb = getCallback();
3284 if (callbacks == cb && cb != null) {
3285 callbacks.bindAppInfosRemoved(removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04003286 }
3287 }
3288 });
Joe Onoratobe386092009-11-17 17:32:16 -08003289 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003290
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003291 // Notify launcher of widget update. From marshmallow onwards we use AppWidgetHost to
3292 // get widget update signals.
3293 if (!Utilities.ATLEAST_MARSHMALLOW &&
3294 (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003295 final Callbacks callbacks = getCallback();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003296 mHandler.post(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003297 public void run() {
3298 Callbacks cb = getCallback();
3299 if (callbacks == cb && cb != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003300 callbacks.notifyWidgetProvidersChanged();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003301 }
3302 }
3303 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003304 }
3305 }
3306 }
3307
Sunny Goyal10923b32016-07-20 15:42:44 -07003308 /**
3309 * Repopulates the shortcut info, possibly updating any icon already on the workspace.
3310 */
3311 public void updateShortcutInfo(final ShortcutInfoCompat fullDetail, final ShortcutInfo info) {
3312 enqueueItemUpdatedTask(new Runnable() {
3313 @Override
3314 public void run() {
3315 info.updateFromDeepShortcutInfo(
3316 fullDetail, LauncherAppState.getInstance().getContext());
3317 ArrayList<ShortcutInfo> update = new ArrayList<ShortcutInfo>();
3318 update.add(info);
3319 bindUpdatedShortcuts(update, fullDetail.getUserHandle());
3320 }
3321 });
3322 }
3323
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003324 private class ShortcutsChangedTask implements Runnable {
Sunny Goyal50941fb2016-08-04 12:03:52 -07003325 private final String mPackageName;
3326 private final List<ShortcutInfoCompat> mShortcuts;
3327 private final UserHandleCompat mUser;
3328 private final boolean mUpdateIdMap;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003329
3330 public ShortcutsChangedTask(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal50941fb2016-08-04 12:03:52 -07003331 UserHandleCompat user, boolean updateIdMap) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003332 mPackageName = packageName;
3333 mShortcuts = shortcuts;
3334 mUser = user;
Sunny Goyal50941fb2016-08-04 12:03:52 -07003335 mUpdateIdMap = updateIdMap;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003336 }
3337
3338 @Override
3339 public void run() {
3340 mDeepShortcutManager.onShortcutsChanged(mShortcuts);
3341
3342 Map<String, ShortcutInfoCompat> idsToShortcuts = new HashMap<>();
3343 for (ShortcutInfoCompat shortcut : mShortcuts) {
3344 idsToShortcuts.put(shortcut.getId(), shortcut);
3345 }
3346
3347 // Find ShortcutInfo's that have changed on the workspace.
3348 MultiHashMap<String, ShortcutInfo> idsToWorkspaceShortcutInfos = new MultiHashMap<>();
3349 for (ItemInfo itemInfo : sBgItemsIdMap) {
3350 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
3351 ShortcutInfo si = (ShortcutInfo) itemInfo;
Tony Wickham672d07f2016-07-27 18:22:41 -07003352 if (si.getIntent().getPackage().equals(mPackageName) && si.user.equals(mUser)) {
3353 String shortcutId = si.getDeepShortcutId();
3354 if (idsToShortcuts.containsKey(shortcutId)) {
3355 idsToWorkspaceShortcutInfos.addToList(shortcutId, si);
3356 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003357 }
3358 }
3359 }
3360
3361 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3362 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager.queryForFullDetails(
3363 mPackageName, new ArrayList<>(idsToWorkspaceShortcutInfos.keySet()), mUser);
3364 ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3365 Context context = LauncherAppState.getInstance().getContext();
3366 for (ShortcutInfoCompat fullDetails : shortcuts) {
3367 List<ShortcutInfo> shortcutInfos = idsToWorkspaceShortcutInfos
3368 .get(fullDetails.getId());
3369 for (ShortcutInfo shortcutInfo : shortcutInfos) {
Sunny Goyal9994b2b2016-06-23 14:17:24 -07003370 shortcutInfo.updateFromDeepShortcutInfo(fullDetails, context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003371 updatedShortcutInfos.add(shortcutInfo);
3372 }
3373 }
3374 bindUpdatedShortcuts(updatedShortcutInfos, mUser);
3375
Sunny Goyal50941fb2016-08-04 12:03:52 -07003376 if (mUpdateIdMap) {
3377 // Update the deep shortcut map if the list of ids has changed for an activity.
3378 updateDeepShortcutMap(mPackageName, mUser, mShortcuts);
3379 bindDeepShortcuts();
3380 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003381 }
3382 }
3383
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003384 /**
3385 * Task to handle changing of lock state of the user
3386 */
3387 private class UserLockStateChangedTask implements Runnable {
3388
3389 private final UserHandleCompat mUser;
3390
3391 public UserLockStateChangedTask(UserHandleCompat user) {
3392 mUser = user;
3393 }
3394
3395 @Override
3396 public void run() {
3397 boolean isUserUnlocked = mUserManager.isUserUnlocked(mUser);
3398 Context context = mApp.getContext();
3399
3400 HashMap<ShortcutKey, ShortcutInfoCompat> pinnedShortcuts = new HashMap<>();
3401 if (isUserUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07003402 List<ShortcutInfoCompat> shortcuts =
3403 mDeepShortcutManager.queryForPinnedShortcuts(null, mUser);
3404 if (mDeepShortcutManager.wasLastCallSuccess()) {
3405 for (ShortcutInfoCompat shortcut : shortcuts) {
3406 pinnedShortcuts.put(ShortcutKey.fromInfo(shortcut), shortcut);
3407 }
3408 } else {
3409 // Shortcut manager can fail due to some race condition when the lock state
3410 // changes too frequently. For the purpose of the update,
3411 // consider it as still locked.
3412 isUserUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003413 }
3414 }
3415
3416 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3417 ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3418 ArrayList<ShortcutInfo> deletedShortcutInfos = new ArrayList<>();
3419 for (ItemInfo itemInfo : sBgItemsIdMap) {
3420 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT
3421 && mUser.equals(itemInfo.user)) {
3422 ShortcutInfo si = (ShortcutInfo) itemInfo;
3423 if (isUserUnlocked) {
3424 ShortcutInfoCompat shortcut =
3425 pinnedShortcuts.get(ShortcutKey.fromItemInfo(si));
3426 // We couldn't verify the shortcut during loader. If its no longer available
3427 // (probably due to clear data), delete the workspace item as well
3428 if (shortcut == null) {
3429 deletedShortcutInfos.add(si);
3430 continue;
3431 }
3432 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
3433 si.updateFromDeepShortcutInfo(shortcut, context);
3434 } else {
3435 si.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
3436 }
3437 updatedShortcutInfos.add(si);
3438 }
3439 }
3440 bindUpdatedShortcuts(updatedShortcutInfos, deletedShortcutInfos, mUser);
3441 if (!deletedShortcutInfos.isEmpty()) {
3442 deleteItemsFromDatabase(context, deletedShortcutInfos);
3443 }
3444
3445 // Remove shortcut id map for that user
3446 Iterator<ComponentKey> keysIter = mBgDeepShortcutMap.keySet().iterator();
3447 while (keysIter.hasNext()) {
3448 if (keysIter.next().user.equals(mUser)) {
3449 keysIter.remove();
3450 }
3451 }
3452
3453 if (isUserUnlocked) {
3454 updateDeepShortcutMap(null, mUser, mDeepShortcutManager.queryForAllShortcuts(mUser));
3455 }
3456 bindDeepShortcuts();
3457 }
3458 }
3459
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003460 private void bindWidgetsModel(final Callbacks callbacks, final WidgetsModel model) {
3461 mHandler.post(new Runnable() {
3462 @Override
3463 public void run() {
3464 Callbacks cb = getCallback();
3465 if (callbacks == cb && cb != null) {
3466 callbacks.bindWidgetsModel(model);
3467 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003468 }
3469 });
3470 }
3471
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003472 public void refreshAndBindWidgetsAndShortcuts(
3473 final Callbacks callbacks, final boolean bindFirst) {
3474 runOnWorkerThread(new Runnable() {
3475 @Override
3476 public void run() {
3477 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
3478 bindWidgetsModel(callbacks, mBgWidgetsModel.clone());
Sunny Goyal31860582015-09-18 08:38:57 -07003479 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003480 final WidgetsModel model = mBgWidgetsModel.updateAndClone(mApp.getContext());
3481 bindWidgetsModel(callbacks, model);
3482 // update the Widget entries inside DB on the worker thread.
3483 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3484 model.getRawList());
Sunny Goyal31860582015-09-18 08:38:57 -07003485 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003486 });
Michael Jurkac402cd92013-05-20 15:49:32 +02003487 }
3488
Adam Cohen091440a2015-03-18 14:16:05 -07003489 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003490 UserHandleCompat user) {
3491 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3492 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003493 }
Adam Cohen556f6132014-01-15 15:18:08 -08003494
Kenny Guyed131872014-04-30 03:02:21 +01003495 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3496 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003497 if (cn == null) {
3498 return false;
3499 }
Kenny Guyed131872014-04-30 03:02:21 +01003500 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3501 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003502 return false;
3503 }
Kenny Guyed131872014-04-30 03:02:21 +01003504 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003505 }
3506
Adam Cohena28b78e2014-05-20 17:03:04 -07003507 public static boolean isValidPackage(Context context, String packageName,
3508 UserHandleCompat user) {
3509 if (packageName == null) {
3510 return false;
3511 }
3512 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3513 return launcherApps.isPackageEnabledForProfile(packageName, user);
3514 }
3515
Joe Onorato9c1289c2009-08-17 11:03:03 -04003516 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003517 * Make an ShortcutInfo object for a restored application or shortcut item that points
3518 * to a package that is not yet installed on the system.
3519 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003520 public ShortcutInfo getRestoredItemInfo(Cursor c, Intent intent,
3521 int promiseType, int itemType, CursorIconInfo iconInfo) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003522 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003523 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003524
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003525 Bitmap icon = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003526 // the fallback icon
3527 if (icon == null) {
3528 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3529 } else {
3530 info.setIcon(icon);
3531 }
Sunny Goyal34942622014-08-29 17:20:55 -07003532
3533 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003534 String title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07003535 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003536 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003537 }
Sunny Goyal34942622014-08-29 17:20:55 -07003538 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3539 if (TextUtils.isEmpty(info.title)) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003540 info.title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07003541 }
Sunny Goyal34942622014-08-29 17:20:55 -07003542 } else {
3543 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3544 }
3545
Winson Chung82b016c2015-05-08 17:00:10 -07003546 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003547 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003548 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003549 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003550 return info;
3551 }
3552
3553 /**
3554 * Make an Intent object for a restored application or shortcut item that points
3555 * to the market page for the item.
3556 */
Adam Cohen091440a2015-03-18 14:16:05 -07003557 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003558 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003559 return getMarketIntent(componentName.getPackageName());
3560 }
3561
3562 static Intent getMarketIntent(String packageName) {
3563 return new Intent(Intent.ACTION_VIEW)
3564 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003565 .scheme("market")
3566 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003567 .appendQueryParameter("id", packageName)
3568 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003569 }
3570
3571 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003572 * Make an ShortcutInfo object for a shortcut that is an application.
3573 *
3574 * If c is not null, then it will be used to fill in missing data like the title and icon.
3575 */
Sunny Goyald09c3702016-04-06 16:18:20 -07003576 public ShortcutInfo getAppShortcutInfo(Intent intent,
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003577 UserHandleCompat user, Cursor c, CursorIconInfo iconInfo,
Sunny Goyal34b65272015-03-11 16:56:52 -07003578 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003579 if (user == null) {
3580 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003581 return null;
3582 }
3583
Kenny Guyed131872014-04-30 03:02:21 +01003584 ComponentName componentName = intent.getComponent();
3585 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08003586 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01003587 return null;
3588 }
3589
3590 Intent newIntent = new Intent(intent.getAction(), null);
3591 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3592 newIntent.setComponent(componentName);
3593 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003594 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003595 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3596 return null;
3597 }
3598
3599 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003600 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003601 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003602 Bitmap icon = iconInfo.loadIcon(c);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003603 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003604 }
3605
Sunny Goyald09c3702016-04-06 16:18:20 -07003606 if (lai != null && PackageManagerHelper.isAppSuspended(lai.getApplicationInfo())) {
3607 info.isDisabled = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
3608 }
3609
Joe Onorato56d82912010-03-07 14:32:10 -05003610 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003611 if (TextUtils.isEmpty(info.title) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003612 info.title = iconInfo.getTitle(c);
Joe Onorato56d82912010-03-07 14:32:10 -05003613 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003614
Joe Onorato56d82912010-03-07 14:32:10 -05003615 // fall back to the class name of the activity
3616 if (info.title == null) {
3617 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003618 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003619
Joe Onorato9c1289c2009-08-17 11:03:03 -04003620 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003621 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003622 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003623 if (lai != null) {
3624 info.flags = AppInfo.initFlags(lai);
3625 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003626 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003627 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003628
Sunny Goyale2df0622015-04-24 11:27:00 -07003629 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003630 ItemInfoFilter f) {
3631 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3632 for (ItemInfo i : infos) {
3633 if (i instanceof ShortcutInfo) {
3634 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003635 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003636 if (cn != null && f.filterItem(null, info, cn)) {
3637 filtered.add(info);
3638 }
3639 } else if (i instanceof FolderInfo) {
3640 FolderInfo info = (FolderInfo) i;
3641 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003642 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003643 if (cn != null && f.filterItem(info, s, cn)) {
3644 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003645 }
3646 }
Winson Chung64359a52013-07-08 17:17:08 -07003647 } else if (i instanceof LauncherAppWidgetInfo) {
3648 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3649 ComponentName cn = info.providerName;
3650 if (cn != null && f.filterItem(null, info, cn)) {
3651 filtered.add(info);
3652 }
Winson Chung8a435102012-08-30 17:16:53 -07003653 }
3654 }
Winson Chung64359a52013-07-08 17:17:08 -07003655 return new ArrayList<ItemInfo>(filtered);
3656 }
3657
Adam Cohen091440a2015-03-18 14:16:05 -07003658 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003659 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003660 ItemInfoFilter filter = new ItemInfoFilter() {
3661 @Override
3662 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003663 if (info.user == null) {
3664 return cn.equals(cname);
3665 } else {
3666 return cn.equals(cname) && info.user.equals(user);
3667 }
Winson Chung64359a52013-07-08 17:17:08 -07003668 }
3669 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003670 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003671 }
3672
Sunny Goyal1a745e82014-10-02 15:58:31 -07003673 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003674 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003675 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003676 @Thunk ShortcutInfo getShortcutInfo(Cursor c, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003677 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003678 // Non-app shortcuts are only supported for current user.
3679 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003680 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003681
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003682 // TODO: If there's an explicit component and we can't install that, delete it.
3683
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003684 loadInfoFromCursor(info, c, iconInfo);
3685 return info;
3686 }
Joe Onorato56d82912010-03-07 14:32:10 -05003687
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003688 /**
3689 * Make an ShortcutInfo object for a shortcut that isn't an application.
3690 */
3691 public void loadInfoFromCursor(ShortcutInfo info, Cursor c, CursorIconInfo iconInfo) {
3692 info.title = iconInfo.getTitle(c);
3693 Bitmap icon = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003694 // the fallback icon
3695 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003696 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003697 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003698 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003699 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003700 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003701
Sunny Goyal2350bc92014-10-14 16:42:54 -07003702 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003703 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3704 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3705 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3706
Adam Cohend9198822011-11-22 16:42:47 -08003707 if (intent == null) {
3708 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3709 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3710 return null;
3711 }
3712
Joe Onorato0589f0f2010-02-08 13:44:00 -08003713 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003714 boolean customIcon = false;
3715 ShortcutIconResource iconResource = null;
3716
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003717 if (bitmap instanceof Bitmap) {
3718 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003719 customIcon = true;
3720 } else {
3721 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003722 if (extra instanceof ShortcutIconResource) {
3723 iconResource = (ShortcutIconResource) extra;
3724 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003725 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003726 }
3727 }
3728
Michael Jurkac9d95c52011-08-29 14:03:34 -07003729 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003730
Kenny Guyed131872014-04-30 03:02:21 +01003731 // Only support intents for current user for now. Intents sent from other
3732 // users wouldn't get here without intent forwarding anyway.
3733 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003734 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003735 icon = mIconCache.getDefaultIcon(info.user);
3736 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003737 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003738 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003739
Winson Chung82b016c2015-05-08 17:00:10 -07003740 info.title = Utilities.trim(name);
3741 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003742 info.intent = intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003743 info.iconResource = iconResource;
3744
3745 return info;
3746 }
3747
Joe Onorato9c1289c2009-08-17 11:03:03 -04003748 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003749 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003750 * or make a new one.
3751 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003752 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003753 // See if a placeholder was created for us already
3754 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003755 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003756 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003757 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003758 folders.put(id, folderInfo);
3759 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003760 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003761 }
3762
Joe Onoratobe386092009-11-17 17:32:16 -08003763
Sunny Goyal651077b2014-06-30 14:15:31 -07003764 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3765 return (provider != null) && (provider.provider != null)
3766 && (provider.provider.getPackageName() != null);
3767 }
3768
Joe Onoratobe386092009-11-17 17:32:16 -08003769 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003770 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003771 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3772 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3773 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3774 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003775 if (mLoaderTask != null) {
3776 mLoaderTask.dumpState();
3777 } else {
3778 Log.d(TAG, "mLoaderTask=null");
3779 }
Joe Onoratobe386092009-11-17 17:32:16 -08003780 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003781
3782 public Callbacks getCallback() {
3783 return mCallbacks != null ? mCallbacks.get() : null;
3784 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003785
3786 /**
3787 * @return {@link FolderInfo} if its already loaded.
3788 */
3789 public FolderInfo findFolderById(Long folderId) {
3790 synchronized (sBgLock) {
3791 return sBgFolders.get(folderId);
3792 }
3793 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003794
Sunny Goyal527c7d32015-08-28 15:19:36 -07003795 @Thunk class DeferredMainThreadExecutor implements Executor {
3796
3797 @Override
3798 public void execute(Runnable command) {
3799 runOnMainThread(command);
3800 }
3801 }
3802
Sunny Goyal756adbc2015-04-16 15:20:51 -07003803 /**
3804 * @return the looper for the worker thread which can be used to start background tasks.
3805 */
3806 public static Looper getWorkerLooper() {
3807 return sWorkerThread.getLooper();
3808 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003809}