blob: e744e18683f907ed35cb82e6d4b30370b7bcc433 [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) {
325 final ArrayList<ShortcutInfo> updates = new ArrayList<>();
326 final UserHandleCompat user = UserHandleCompat.myUserHandle();
327
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
348 if (!updates.isEmpty()) {
349 // Push changes to the callback.
350 Runnable r = new Runnable() {
351 public void run() {
352 Callbacks callbacks = getCallback();
353 if (callbacks != null) {
354 callbacks.bindShortcutsChanged(updates,
355 new ArrayList<ShortcutInfo>(), user);
356 }
357 }
358 };
359 mHandler.post(r);
360 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700361 }
362 }
363 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700364 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700365 }
366
Adam Cohen76a47a12014-02-05 11:47:43 -0800367 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800368 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800369
370 if (allAppsApps == null) {
371 throw new RuntimeException("allAppsApps must not be null");
372 }
373 if (allAppsApps.isEmpty()) {
374 return;
375 }
376
377 // Process the newly added applications and add them to the database first
378 Runnable r = new Runnable() {
379 public void run() {
380 runOnMainThread(new Runnable() {
381 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800382 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800383 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500384 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800385 }
386 }
387 });
388 }
389 };
390 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700391 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800392
Sunny Goyala9116722015-04-29 13:55:58 -0700393 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800394 int[] xy, int spanX, int spanY) {
395 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700396 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700397
398 GridOccupancy occupied = new GridOccupancy(profile.numColumns, profile.numRows);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800399 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700400 for (ItemInfo r : occupiedPos) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700401 occupied.markCells(r, true);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800402 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800403 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700404 return occupied.findVacantCell(xy, spanX, spanY);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800405 }
406
407 /**
408 * Find a position on the screen for the given size or adds a new screen.
409 * @return screenId and the coordinates for the item.
410 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700411 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyalc3642d42015-10-30 10:27:08 -0700412 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800413 ArrayList<Long> workspaceScreens,
414 ArrayList<Long> addedWorkspaceScreensFinal,
415 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700416 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800417
Sunny Goyala9116722015-04-29 13:55:58 -0700418 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700419 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700420 synchronized (sBgLock) {
421 for (ItemInfo info : sBgItemsIdMap) {
422 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
423 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
424 if (items == null) {
425 items = new ArrayList<>();
426 screenItems.put(info.screenId, items);
427 }
428 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800429 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800430 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800431 }
432
433 // Find appropriate space for the item.
434 long screenId = 0;
435 int[] cordinates = new int[2];
436 boolean found = false;
437
438 int screenCount = workspaceScreens.size();
439 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700440 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800441 if (preferredScreenIndex < screenCount) {
442 screenId = workspaceScreens.get(preferredScreenIndex);
443 found = findNextAvailableIconSpaceInScreen(
444 screenItems.get(screenId), cordinates, spanX, spanY);
445 }
446
447 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700448 // Search on any of the screens starting from the first screen.
449 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800450 screenId = workspaceScreens.get(screen);
451 if (findNextAvailableIconSpaceInScreen(
452 screenItems.get(screenId), cordinates, spanX, spanY)) {
453 // We found a space for it
454 found = true;
455 break;
456 }
457 }
458 }
459
460 if (!found) {
461 // Still no position found. Add a new screen to the end.
Sunny Goyald2497482015-09-22 18:24:19 -0700462 screenId = LauncherSettings.Settings.call(context.getContentResolver(),
463 LauncherSettings.Settings.METHOD_NEW_SCREEN_ID)
464 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800465
466 // Save the screen id for binding in the workspace
467 workspaceScreens.add(screenId);
468 addedWorkspaceScreensFinal.add(screenId);
469
470 // If we still can't find an empty space, then God help us all!!!
471 if (!findNextAvailableIconSpaceInScreen(
472 screenItems.get(screenId), cordinates, spanX, spanY)) {
473 throw new RuntimeException("Can't find space to add the item");
474 }
475 }
476 return Pair.create(screenId, cordinates);
477 }
478
479 /**
480 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800481 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700482 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700483 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800484 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800485 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700486 return;
Winson Chung997a9232013-07-24 15:33:46 -0700487 }
Winson Chung64359a52013-07-08 17:17:08 -0700488 // Process the newly added applications and add them to the database first
489 Runnable r = new Runnable() {
490 public void run() {
491 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
492 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
493
Winson Chung76828c82013-08-19 15:43:29 -0700494 // Get the list of workspace screens. We need to append to this list and
495 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
496 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800497 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700498 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800499 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700500 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800501 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700502 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800503 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700504 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800505 }
Winson Chung76828c82013-08-19 15:43:29 -0700506
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800507 // Find appropriate space for the item.
Sunny Goyalc3642d42015-10-30 10:27:08 -0700508 Pair<Long, int[]> coords = findSpaceForItem(context,
Sunny Goyal9eba1fd2015-10-16 08:58:57 -0700509 workspaceScreens, addedWorkspaceScreensFinal, 1, 1);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800510 long screenId = coords.first;
511 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700512
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700513 ItemInfo itemInfo;
514 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
515 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800516 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700517 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700518 } else {
519 throw new RuntimeException("Unexpected info type");
520 }
Winson Chung94d67682013-09-25 16:29:40 -0700521
Winson Chung64359a52013-07-08 17:17:08 -0700522 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700523 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700524 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700525 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700526 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700527 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700528 }
529 }
530
Winson Chung76828c82013-08-19 15:43:29 -0700531 // Update the workspace screens
532 updateWorkspaceScreenOrder(context, workspaceScreens);
533
Adam Cohen76a47a12014-02-05 11:47:43 -0800534 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700535 runOnMainThread(new Runnable() {
536 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800537 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700538 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700539 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
540 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700541 if (!addedShortcutsFinal.isEmpty()) {
542 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
543 long lastScreenId = info.screenId;
544 for (ItemInfo i : addedShortcutsFinal) {
545 if (i.screenId == lastScreenId) {
546 addAnimated.add(i);
547 } else {
548 addNotAnimated.add(i);
549 }
Winson Chung997a9232013-07-24 15:33:46 -0700550 }
551 }
Winson Chungd64d1762013-08-20 14:37:16 -0700552 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800553 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700554 }
Winson Chung64359a52013-07-08 17:17:08 -0700555 }
Winson Chung997a9232013-07-24 15:33:46 -0700556 });
557 }
Winson Chung64359a52013-07-08 17:17:08 -0700558 }
559 };
560 runOnWorkerThread(r);
561 }
562
Joe Onorato9c1289c2009-08-17 11:03:03 -0400563 /**
564 * Adds an item to the DB if it was not created previously, or move it to a new
565 * <container, screen, cellX, cellY>
566 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800567 public static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700568 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400569 if (item.container == ItemInfo.NO_ID) {
570 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700571 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400572 } else {
573 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700574 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800575 }
576 }
577
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700578 static void checkItemInfoLocked(
579 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
580 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
581 if (modelItem != null && item != modelItem) {
582 // check all the data is consistent
583 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
584 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
585 ShortcutInfo shortcut = (ShortcutInfo) item;
586 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
587 modelShortcut.intent.filterEquals(shortcut.intent) &&
588 modelShortcut.id == shortcut.id &&
589 modelShortcut.itemType == shortcut.itemType &&
590 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700591 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700592 modelShortcut.cellX == shortcut.cellX &&
593 modelShortcut.cellY == shortcut.cellY &&
594 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700595 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700596 // For all intents and purposes, this is the same object
597 return;
598 }
599 }
600
601 // the modelItem needs to match up perfectly with item if our model is
602 // to be consistent with the database-- for now, just require
603 // modelItem == item or the equality check above
604 String msg = "item: " + ((item != null) ? item.toString() : "null") +
605 "modelItem: " +
606 ((modelItem != null) ? modelItem.toString() : "null") +
607 "Error: ItemInfo passed to checkItemInfo doesn't match original";
608 RuntimeException e = new RuntimeException(msg);
609 if (stackTrace != null) {
610 e.setStackTrace(stackTrace);
611 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800612 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700613 }
614 }
615
Michael Jurka816474f2012-06-25 14:49:02 -0700616 static void checkItemInfo(final ItemInfo item) {
617 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
618 final long itemId = item.id;
619 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700620 public void run() {
621 synchronized (sBgLock) {
622 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700623 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700624 }
625 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700626 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700627 }
628
Michael Jurkac9d95c52011-08-29 14:03:34 -0700629 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
630 final ItemInfo item, final String callingFunction) {
631 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700632 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700633 final ContentResolver cr = context.getContentResolver();
634
Adam Cohen487f7dd2012-06-28 18:12:10 -0700635 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700636 Runnable r = new Runnable() {
637 public void run() {
638 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700639 updateItemArrays(item, itemId, stackTrace);
640 }
641 };
642 runOnWorkerThread(r);
643 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700644
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700645 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
646 final ArrayList<ItemInfo> items, final String callingFunction) {
647 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700648
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700649 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
650 Runnable r = new Runnable() {
651 public void run() {
652 ArrayList<ContentProviderOperation> ops =
653 new ArrayList<ContentProviderOperation>();
654 int count = items.size();
655 for (int i = 0; i < count; i++) {
656 ItemInfo item = items.get(i);
657 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700658 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700659 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700660
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700661 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
662 updateItemArrays(item, itemId, stackTrace);
663
664 }
665 try {
666 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
667 } catch (Exception e) {
668 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700669 }
670 }
671 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700672 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700673 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700674
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700675 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
676 // Lock on mBgLock *after* the db operation
677 synchronized (sBgLock) {
678 checkItemInfoLocked(itemId, item, stackTrace);
679
680 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
681 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
682 // Item is in a folder, make sure this folder exists
683 if (!sBgFolders.containsKey(item.container)) {
684 // An items container is being set to a that of an item which is not in
685 // the list of Folders.
686 String msg = "item: " + item + " container being set to: " +
687 item.container + ", not in the list of folders";
688 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700689 }
690 }
691
692 // Items are added/removed from the corresponding FolderInfo elsewhere, such
693 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
694 // that are on the desktop, as appropriate
695 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800696 if (modelItem != null &&
697 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
698 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700699 switch (modelItem.itemType) {
700 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
701 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700702 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700703 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
704 if (!sBgWorkspaceItems.contains(modelItem)) {
705 sBgWorkspaceItems.add(modelItem);
706 }
707 break;
708 default:
709 break;
710 }
711 } else {
712 sBgWorkspaceItems.remove(modelItem);
713 }
714 }
715 }
716
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800717 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400718 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700719 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700720 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700721 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400722 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400723 item.cellX = cellX;
724 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700725
Winson Chung3d503fb2011-07-13 17:25:49 -0700726 // We store hotseat items in canonical form which is this orientation invariant position
727 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700728 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700729 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700730 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700731 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700732 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700733 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400734
735 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400736 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700737 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
738 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800739 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700740 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400741
Michael Jurkac9d95c52011-08-29 14:03:34 -0700742 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700743 }
744
745 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700746 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
747 * cellX, cellY have already been updated on the ItemInfos.
748 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800749 public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700750 final long container, final int screen) {
751
752 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
753 int count = items.size();
754
755 for (int i = 0; i < count; i++) {
756 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700757 item.container = container;
758
759 // We store hotseat items in canonical form which is this orientation invariant position
760 // in the hotseat
761 if (context instanceof Launcher && screen < 0 &&
762 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700763 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700764 item.cellY);
765 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700766 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700767 }
768
769 final ContentValues values = new ContentValues();
770 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
771 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
772 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800773 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700774 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700775
776 contentValues.add(values);
777 }
778 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
779 }
780
781 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700782 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800783 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700784 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700785 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700786 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800787 item.cellX = cellX;
788 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700789 item.spanX = spanX;
790 item.spanY = spanY;
791
792 // We store hotseat items in canonical form which is this orientation invariant position
793 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700794 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700795 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700796 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700797 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700798 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700799 }
Adam Cohend4844c32011-02-18 19:25:06 -0800800
Adam Cohend4844c32011-02-18 19:25:06 -0800801 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800802 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700803 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
804 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800805 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700806 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
807 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700808 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800809
Michael Jurka816474f2012-06-25 14:49:02 -0700810 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700811 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700812
813 /**
814 * Update an item to the database in a specified container.
815 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700816 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700817 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100818 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700819 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800820 }
821
Sunny Goyal756a28a2015-04-23 17:07:55 -0700822 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700823 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700824 synchronized (mLock) {
825 if (!mHasLoaderCompletedOnce ||
826 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
827 throw new RuntimeException("Trying to add shortcut while loader is running");
828 }
829 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700830 }
831 }
832
Adam Cohend4844c32011-02-18 19:25:06 -0800833 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700834 * Returns true if the shortcuts already exists on the workspace. This must be called after
835 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700837 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
838 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700839 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700840 if (intent.getComponent() != null) {
841 // If component is not null, an intent with null package will produce
842 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700843 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700844 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700845 intentWithPkg = intent.toUri(0);
846 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700847 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700848 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
849 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700850 }
851 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700852 intentWithPkg = intent.toUri(0);
853 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700854 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700855
856 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700857 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700858 if (item instanceof ShortcutInfo) {
859 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700860 Intent targetIntent = info.promisedIntent == null
861 ? info.intent : info.promisedIntent;
862 if (targetIntent != null && info.user.equals(user)) {
863 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700864 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
865 return true;
866 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700867 }
868 }
869 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800870 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700871 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700872 }
873
Joe Onorato9c1289c2009-08-17 11:03:03 -0400874 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400875 * Add an item to the database in a specified container. Sets the container, screen, cellX and
876 * cellY fields of the item. Also assigns an ID to the item.
877 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700878 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700879 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400880 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400881 item.cellX = cellX;
882 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700883 // We store hotseat items in canonical form which is this orientation invariant position
884 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700885 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700886 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700887 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700888 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700889 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700890 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400891
892 final ContentValues values = new ContentValues();
893 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100894 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400895
Sunny Goyald2497482015-09-22 18:24:19 -0700896 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
897 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
898
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700899 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700900
Jason Monk8e19cf22014-03-20 15:06:57 -0400901 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700902 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700903 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700904 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400905
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700906 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700907 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400908 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700909 sBgItemsIdMap.put(item.id, item);
910 switch (item.itemType) {
911 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
912 sBgFolders.put(item.id, (FolderInfo) item);
913 // Fall through
914 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
915 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700916 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700917 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
918 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
919 sBgWorkspaceItems.add(item);
920 } else {
921 if (!sBgFolders.containsKey(item.container)) {
922 // Adding an item to a folder that doesn't exist.
923 String msg = "adding item: " + item + " to a folder that " +
924 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700925 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700926 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700927 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700928 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
929 ShortcutInfo shortcutInfo = (ShortcutInfo) item;
930 ShortcutKey shortcutToPin = new ShortcutKey(
931 shortcutInfo.intent.getPackage(),
932 shortcutInfo.user,
933 shortcutInfo.getDeepShortcutId());
934 incrementPinnedShortcutCount(shortcutToPin, true /* shouldPin */);
935 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700936 break;
937 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
938 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
939 break;
940 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700941 }
942 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700943 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700944 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700945 }
946
Sunny Goyal34942622014-08-29 17:20:55 -0700947 private static ArrayList<ItemInfo> getItemsByPackageName(
948 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700949 ItemInfoFilter filter = new ItemInfoFilter() {
950 @Override
951 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
952 return cn.getPackageName().equals(pn) && info.user.equals(user);
953 }
954 };
Sunny Goyale2df0622015-04-24 11:27:00 -0700955 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -0700956 }
957
958 /**
959 * Removes all the items from the database corresponding to the specified package.
960 */
961 static void deletePackageFromDatabase(Context context, final String pn,
962 final UserHandleCompat user) {
963 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700964 }
965
966 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700967 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -0400968 */
Sunny Goyalfa401a12015-04-10 13:45:42 -0700969 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700970 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
971 items.add(item);
972 deleteItemsFromDatabase(context, items);
973 }
974
975 /**
976 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700977 */
Sunny Goyal4390ace2014-10-13 11:33:11 -0700978 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400979 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -0700980 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700981 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700982 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700983 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700984 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700985
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700986 // Lock on mBgLock *after* the db operation
987 synchronized (sBgLock) {
988 switch (item.itemType) {
989 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
990 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -0700991 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700992 if (info.container == item.id) {
993 // We are deleting a folder which still contains items that
994 // think they are contained by that folder.
995 String msg = "deleting a folder (" + item + ") which still " +
996 "contains items (" + info + ")";
997 Log.e(TAG, msg);
998 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700999 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001000 sBgWorkspaceItems.remove(item);
1001 break;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001002 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
1003 ShortcutInfo shortcutInfo = ((ShortcutInfo) item);
1004 ShortcutKey pinnedShortcut = new ShortcutKey(
1005 shortcutInfo.intent.getPackage(),
1006 shortcutInfo.user,
1007 shortcutInfo.getDeepShortcutId());
1008 decrementPinnedShortcutCount(pinnedShortcut);
1009 // Fall through.
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001010 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1011 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1012 sBgWorkspaceItems.remove(item);
1013 break;
1014 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1015 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1016 break;
1017 }
1018 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001019 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001020 }
1021 }
Michael Jurka83df1882011-08-31 20:59:26 -07001022 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001023 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001024 }
1025
1026 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001027 * Decrement the count for the given pinned shortcut, unpinning it if the count becomes 0.
1028 */
1029 private static void decrementPinnedShortcutCount(final ShortcutKey pinnedShortcut) {
1030 synchronized (sBgLock) {
1031 MutableInt count = sBgPinnedShortcutCounts.get(pinnedShortcut);
1032 if (count == null || --count.value == 0) {
1033 LauncherAppState.getInstance().getShortcutManager().unpinShortcut(pinnedShortcut);
1034 }
1035 }
1036 }
1037
1038 /**
1039 * Increment the count for the given shortcut, pinning it if the count becomes 1.
1040 *
1041 * As an optimization, the caller can pass shouldPin == false to avoid
1042 * unnecessary RPC's if the shortcut is already pinned.
1043 */
1044 private static void incrementPinnedShortcutCount(ShortcutKey pinnedShortcut, boolean shouldPin) {
1045 synchronized (sBgLock) {
1046 MutableInt count = sBgPinnedShortcutCounts.get(pinnedShortcut);
1047 if (count == null) {
1048 count = new MutableInt(1);
1049 sBgPinnedShortcutCounts.put(pinnedShortcut, count);
1050 } else {
1051 count.value++;
1052 }
1053 if (shouldPin && count.value == 1) {
1054 LauncherAppState.getInstance().getShortcutManager().pinShortcut(pinnedShortcut);
1055 }
1056 }
1057 }
1058
1059 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001060 * Update the order of the workspace screens in the database. The array list contains
1061 * a list of screen ids in the order that they should appear.
1062 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001063 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001064 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001065 final ContentResolver cr = context.getContentResolver();
1066 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1067
1068 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001069 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001070 while (iter.hasNext()) {
1071 long id = iter.next();
1072 if (id < 0) {
1073 iter.remove();
1074 }
1075 }
1076
1077 Runnable r = new Runnable() {
1078 @Override
1079 public void run() {
Yura085c8532014-02-11 15:15:29 +00001080 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001081 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001082 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001083 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001084 for (int i = 0; i < count; i++) {
1085 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001086 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001087 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1088 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001089 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001090 }
Yura085c8532014-02-11 15:15:29 +00001091
1092 try {
1093 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1094 } catch (Exception ex) {
1095 throw new RuntimeException(ex);
1096 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001097
Winson Chungba9c37f2013-08-30 14:11:37 -07001098 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001099 sBgWorkspaceScreens.clear();
1100 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001101 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001102 }
1103 };
1104 runOnWorkerThread(r);
1105 }
1106
1107 /**
Winsonc0b52fe2015-09-09 16:38:15 -07001108 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001109 */
Winsonc0b52fe2015-09-09 16:38:15 -07001110 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001111 final ContentResolver cr = context.getContentResolver();
1112
Michael Jurkac9d95c52011-08-29 14:03:34 -07001113 Runnable r = new Runnable() {
1114 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001115 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001116 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001117 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001118 sBgItemsIdMap.remove(info.id);
1119 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001120 sBgWorkspaceItems.remove(info);
1121 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001122
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001123 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001124 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001125 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001126 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001127 for (ItemInfo childInfo : info.contents) {
1128 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001129 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001130 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001131 }
1132 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001133 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001134 }
1135
1136 /**
1137 * Set this as the current Launcher activity object for the loader.
1138 */
1139 public void initialize(Callbacks callbacks) {
1140 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001141 Preconditions.assertUIThread();
1142 // Remove any queued UI runnables
1143 mHandler.cancelAll();
1144 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001145 }
1146 }
1147
Kenny Guyed131872014-04-30 03:02:21 +01001148 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001149 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001150 int op = PackageUpdatedTask.OP_UPDATE;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001151 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001152 user));
1153 }
1154
1155 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001156 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001157 int op = PackageUpdatedTask.OP_REMOVE;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001158 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001159 user));
1160 }
1161
1162 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001163 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001164 int op = PackageUpdatedTask.OP_ADD;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001165 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001166 user));
1167 }
1168
1169 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001170 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001171 boolean replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001172 enqueueItemUpdatedTask(
Sunny Goyal144154d2016-03-30 16:47:03 -07001173 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, packageNames, user));
Kenny Guyed131872014-04-30 03:02:21 +01001174 }
1175
1176 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001177 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001178 boolean replacing) {
1179 if (!replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001180 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guyed131872014-04-30 03:02:21 +01001181 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1182 user));
1183 }
Kenny Guyed131872014-04-30 03:02:21 +01001184 }
1185
Kenny Guy44cba692016-01-21 19:50:02 +00001186 @Override
1187 public void onPackagesSuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001188 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001189 PackageUpdatedTask.OP_SUSPEND, packageNames,
1190 user));
1191 }
1192
1193 @Override
1194 public void onPackagesUnsuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001195 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001196 PackageUpdatedTask.OP_UNSUSPEND, packageNames,
1197 user));
1198 }
1199
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001200 @Override
1201 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
1202 UserHandleCompat user) {
1203 enqueueItemUpdatedTask(new ShortcutsChangedTask(packageName, shortcuts, user));
1204 }
1205
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001206 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001207 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1208 * ACTION_PACKAGE_CHANGED.
1209 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001210 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001211 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001212 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001213
Joe Onorato36115782010-06-17 13:28:48 -04001214 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001215 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001216 // If we have changed locale we need to clear out the labels in all apps/workspace.
1217 forceReload();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001218 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
Sunny Goyalda891c12016-03-18 18:29:24 -07001219 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001220 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001221 forceReload();
Rubin Xuac6e5d72016-04-04 16:13:35 +01001222 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1223 LauncherAppsCompat.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
Sunny Goyalda891c12016-03-18 18:29:24 -07001224 UserHandleCompat user = UserHandleCompat.fromIntent(intent);
1225 if (user != null) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001226 enqueueItemUpdatedTask(new PackageUpdatedTask(
Sunny Goyalda891c12016-03-18 18:29:24 -07001227 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE,
1228 new String[0], user));
1229 }
Tony Wickham827cef22016-03-17 15:39:39 -07001230 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
1231 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001232 }
1233 }
1234
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001235 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001236 resetLoadedState(true, true);
1237
Reena Lee93f824a2011-09-23 17:20:28 -07001238 // Do this here because if the launcher activity is running it will be restarted.
1239 // If it's not running startLoaderFromBackground will merely tell it that it needs
1240 // to reload.
1241 startLoaderFromBackground();
1242 }
1243
Winson Chungf0c6ae02012-03-21 16:10:31 -07001244 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1245 synchronized (mLock) {
1246 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1247 // mWorkspaceLoaded to true later
1248 stopLoaderLocked();
1249 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1250 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001251 // Always reset deep shortcuts loaded.
1252 mDeepShortcutsLoaded = false;
Winson Chungf0c6ae02012-03-21 16:10:31 -07001253 }
1254 }
1255
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001256 /**
1257 * When the launcher is in the background, it's possible for it to miss paired
1258 * configuration changes. So whenever we trigger the loader from the background
1259 * tell the launcher that it needs to re-run the loader when it comes back instead
1260 * of doing it now.
1261 */
1262 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001263 Callbacks callbacks = getCallback();
1264 if (callbacks != null) {
1265 // Only actually run the loader if they're not paused.
1266 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -07001267 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001268 }
1269 }
Joe Onorato36115782010-06-17 13:28:48 -04001270 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001271
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001272 /**
1273 * If there is already a loader task running, tell it to stop.
1274 */
1275 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001276 LoaderTask oldTask = mLoaderTask;
1277 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001278 oldTask.stopLocked();
1279 }
Reena Lee93f824a2011-09-23 17:20:28 -07001280 }
1281
Adam Cohen1a85c582014-09-30 09:48:49 -07001282 public boolean isCurrentCallbacks(Callbacks callbacks) {
1283 return (mCallbacks != null && mCallbacks.get() == callbacks);
1284 }
1285
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001286 /**
1287 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
1288 * @return true if the page could be bound synchronously.
1289 */
1290 public boolean startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001291 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1292 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001293 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -04001294 // Don't bother to start the thread if we know it's not going to do anything
1295 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001296 final Callbacks oldCallbacks = mCallbacks.get();
1297 // Clear any pending bind-runnables from the synchronized load process.
1298 runOnMainThread(new Runnable() {
1299 public void run() {
1300 oldCallbacks.clearPendingBinds();
1301 }
1302 });
1303
Joe Onorato36115782010-06-17 13:28:48 -04001304 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001305 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001306 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Tony Wickham80f57872016-06-29 18:12:15 -07001307 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE && mAllAppsLoaded
1308 && mWorkspaceLoaded && mDeepShortcutsLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001309 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001310 return true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001311 } else {
1312 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1313 sWorker.post(mLoaderTask);
1314 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001315 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001316 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001317 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001318 }
1319
Joe Onorato36115782010-06-17 13:28:48 -04001320 public void stopLoader() {
1321 synchronized (mLock) {
1322 if (mLoaderTask != null) {
1323 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001324 }
1325 }
Joe Onorato36115782010-06-17 13:28:48 -04001326 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001327
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001328 /**
1329 * Loads the workspace screen ids in an ordered list.
1330 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001331 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001332 final ContentResolver contentResolver = context.getContentResolver();
1333 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001334
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001335 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001336 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
1337 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -07001338 }
1339
Joe Onorato36115782010-06-17 13:28:48 -04001340 /**
1341 * Runnable for the thread that loads the contents of the launcher:
1342 * - workspace icons
1343 * - widgets
1344 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001345 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -04001346 */
1347 private class LoaderTask implements Runnable {
1348 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001349 private int mPageToBindFirst;
1350
Adam Cohen091440a2015-03-18 14:16:05 -07001351 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001352 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001353 @Thunk boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001354
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001355 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -04001356 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001357 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001358 }
1359
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001360 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001361 mIsLoadingAndBindingWorkspace = true;
1362
Joe Onorato36115782010-06-17 13:28:48 -04001363 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001364 if (DEBUG_LOADERS) {
1365 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001366 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001367
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001368 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001369 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001370 synchronized (LoaderTask.this) {
1371 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001372 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001373 }
1374 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001375 }
1376 }
1377
Joe Onorato36115782010-06-17 13:28:48 -04001378 // Bind the workspace
Sunny Goyal93f878c2016-03-30 17:31:24 -07001379 bindWorkspace(mPageToBindFirst);
Joe Onorato36115782010-06-17 13:28:48 -04001380 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001381
Joe Onorato36115782010-06-17 13:28:48 -04001382 private void waitForIdle() {
1383 // Wait until the either we're stopped or the other threads are done.
1384 // This way we don't start loading all apps until the workspace has settled
1385 // down.
1386 synchronized (LoaderTask.this) {
1387 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001388
Joe Onorato36115782010-06-17 13:28:48 -04001389 mHandler.postIdle(new Runnable() {
1390 public void run() {
1391 synchronized (LoaderTask.this) {
1392 mLoadAndBindStepFinished = true;
1393 if (DEBUG_LOADERS) {
1394 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001395 }
Joe Onorato36115782010-06-17 13:28:48 -04001396 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001397 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001398 }
Joe Onorato36115782010-06-17 13:28:48 -04001399 });
1400
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001401 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001402 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001403 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1404 // wait no longer than 1sec at a time
1405 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001406 } catch (InterruptedException ex) {
1407 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001408 }
1409 }
Joe Onorato36115782010-06-17 13:28:48 -04001410 if (DEBUG_LOADERS) {
1411 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001412 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001413 + "ms for previous step to finish binding");
1414 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001415 }
Joe Onorato36115782010-06-17 13:28:48 -04001416 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001417
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001418 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001419 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001420 // Ensure that we have a valid page index to load synchronously
1421 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1422 "valid page index");
1423 }
1424 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1425 // Ensure that we don't try and bind a specified page when the pages have not been
1426 // loaded already (we should load everything asynchronously in that case)
1427 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1428 }
1429 synchronized (mLock) {
1430 if (mIsLoaderTaskRunning) {
1431 // Ensure that we are never running the background loading at this point since
1432 // we also touch the background collections
1433 throw new RuntimeException("Error! Background loading is already running");
1434 }
1435 }
1436
1437 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1438 // data structures, we can't allow any other thread to touch that data, but because
1439 // this call is synchronous, we can get away with not locking).
1440
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001441 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001442 // operations from the previous activity. We need to ensure that all queued operations
1443 // are executed before any synchronous binding work is done.
1444 mHandler.flush();
1445
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001446 // Divide the set of loaded items into those that we are binding synchronously, and
1447 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001448 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001449 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1450 // arise from that.
1451 onlyBindAllApps();
Tony Wickham80f57872016-06-29 18:12:15 -07001452
1453 bindDeepShortcuts();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001454 }
1455
Joe Onorato36115782010-06-17 13:28:48 -04001456 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001457 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001458 if (mStopped) {
1459 return;
1460 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001461 mIsLoaderTaskRunning = true;
1462 }
Joe Onorato36115782010-06-17 13:28:48 -04001463 // Optimize for end-user experience: if the Launcher is up and // running with the
1464 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1465 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001466 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001467 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001468 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001469
Joe Onorato36115782010-06-17 13:28:48 -04001470 if (mStopped) {
1471 break keep_running;
1472 }
1473
Joe Onorato36115782010-06-17 13:28:48 -04001474 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001475
1476 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001477 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1478 loadAndBindAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001479
1480 waitForIdle();
1481
1482 // third step
1483 if (DEBUG_LOADERS) Log.d(TAG, "step 3: loading deep shortcuts");
1484 loadAndBindDeepShortcuts();
Joe Onorato36115782010-06-17 13:28:48 -04001485 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001486
Joe Onorato36115782010-06-17 13:28:48 -04001487 // Clear out this reference, otherwise we end up holding it until all of the
1488 // callback runnables are done.
1489 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001490
Joe Onorato36115782010-06-17 13:28:48 -04001491 synchronized (mLock) {
1492 // If we are still the last one to be scheduled, remove ourselves.
1493 if (mLoaderTask == this) {
1494 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001495 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001496 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001497 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001498 }
Joe Onorato36115782010-06-17 13:28:48 -04001499 }
1500
1501 public void stopLocked() {
1502 synchronized (LoaderTask.this) {
1503 mStopped = true;
1504 this.notify();
1505 }
1506 }
1507
1508 /**
1509 * Gets the callbacks object. If we've been stopped, or if the launcher object
1510 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1511 * object that was around when the deferred message was scheduled, and if there's
1512 * a new Callbacks object around then also return null. This will save us from
1513 * calling onto it with data that will be ignored.
1514 */
1515 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1516 synchronized (mLock) {
1517 if (mStopped) {
1518 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001519 }
Joe Onorato36115782010-06-17 13:28:48 -04001520
1521 if (mCallbacks == null) {
1522 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001523 }
Joe Onorato36115782010-06-17 13:28:48 -04001524
1525 final Callbacks callbacks = mCallbacks.get();
1526 if (callbacks != oldCallbacks) {
1527 return null;
1528 }
1529 if (callbacks == null) {
1530 Log.w(TAG, "no mCallbacks");
1531 return null;
1532 }
1533
1534 return callbacks;
1535 }
1536 }
1537
1538 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001539 private boolean checkItemPlacement(LongArrayMap<GridOccupancy> occupied, ItemInfo item,
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001540 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001541 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001542 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung892c74d2013-08-22 16:15:50 -07001543
Adam Cohendcd297f2013-06-18 13:13:40 -07001544 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001545 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001546 // Return early if we detect that an item is under the hotseat button
Sunny Goyalbb011da2016-06-15 15:42:29 -07001547 if (!FeatureFlags.NO_ALL_APPS_ICON &&
1548 profile.isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001549 Log.e(TAG, "Error loading shortcut into hotseat " + item
1550 + " into position (" + item.screenId + ":" + item.cellX + ","
1551 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001552 return false;
1553 }
1554
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001555 final GridOccupancy hotseatOccupancy =
Dan Sandler295ae182013-12-10 16:05:47 -05001556 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1557
Adam Cohen2e6da152015-05-06 11:42:25 -07001558 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001559 Log.e(TAG, "Error loading shortcut " + item
1560 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001561 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001562 + ")");
1563 return false;
1564 }
1565
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001566 if (hotseatOccupancy != null) {
1567 if (hotseatOccupancy.cells[(int) item.screenId][0]) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001568 Log.e(TAG, "Error loading shortcut into hotseat " + item
1569 + " into position (" + item.screenId + ":" + item.cellX + ","
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001570 + item.cellY + ") already occupied");
Adam Cohendcd297f2013-06-18 13:13:40 -07001571 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001572 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001573 hotseatOccupancy.cells[(int) item.screenId][0] = true;
Dan Sandler295ae182013-12-10 16:05:47 -05001574 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001575 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001576 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001577 final GridOccupancy occupancy = new GridOccupancy(profile.numHotseatIcons, 1);
1578 occupancy.cells[(int) item.screenId][0] = true;
1579 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, occupancy);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001580 return true;
1581 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001582 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1583 if (!workspaceScreens.contains((Long) item.screenId)) {
1584 // The item has an invalid screen id.
1585 return false;
1586 }
1587 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001588 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001589 return true;
1590 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001591
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001592 final int countX = profile.numColumns;
1593 final int countY = profile.numRows;
Adam Cohenae4409d2013-11-26 10:34:59 -08001594 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1595 item.cellX < 0 || item.cellY < 0 ||
1596 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1597 Log.e(TAG, "Error loading shortcut " + item
1598 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1599 + item.cellX + "," + item.cellY
1600 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1601 return false;
1602 }
1603
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001604 if (!occupied.containsKey(item.screenId)) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001605 GridOccupancy screen = new GridOccupancy(countX + 1, countY + 1);
1606 if (item.screenId == Workspace.FIRST_SCREEN_ID) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001607 // Mark the first row as occupied (if the feature is enabled)
1608 // in order to account for the QSB.
1609 screen.markCells(0, 0, countX + 1, 1, FeatureFlags.QSB_ON_FIRST_SCREEN);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001610 }
1611 occupied.put(item.screenId, screen);
Joe Onorato36115782010-06-17 13:28:48 -04001612 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001613 final GridOccupancy occupancy = occupied.get(item.screenId);
Winson Chungf30ad5f2011-08-08 10:55:42 -07001614
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001615 // Check if any workspace icons overlap with each other
1616 if (occupancy.isRegionVacant(item.cellX, item.cellY, item.spanX, item.spanY)) {
1617 occupancy.markCells(item, true);
1618 return true;
1619 } else {
1620 Log.e(TAG, "Error loading shortcut " + item
1621 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1622 + item.cellX + "," + item.cellX + "," + item.spanX + "," + item.spanY
1623 + ") already occupied");
1624 return false;
1625 }
Joe Onorato36115782010-06-17 13:28:48 -04001626 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001627
Winson Chungba9c37f2013-08-30 14:11:37 -07001628 /** Clears all the sBg data structures */
1629 private void clearSBgDataStructures() {
1630 synchronized (sBgLock) {
1631 sBgWorkspaceItems.clear();
1632 sBgAppWidgets.clear();
1633 sBgFolders.clear();
1634 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001635 sBgWorkspaceScreens.clear();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001636 sBgPinnedShortcutCounts.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001637 }
1638 }
1639
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001640 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -07001641 if (LauncherAppState.PROFILE_STARTUP) {
1642 Trace.beginSection("Loading Workspace");
1643 }
Joe Onorato36115782010-06-17 13:28:48 -04001644 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001645
Joe Onorato36115782010-06-17 13:28:48 -04001646 final Context context = mContext;
1647 final ContentResolver contentResolver = context.getContentResolver();
1648 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001649 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001650 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001651 final boolean isSdCardReady = Utilities.isBootCompleted();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001652
Winson Chung892c74d2013-08-22 16:15:50 -07001653 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001654 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001655 int countX = profile.numColumns;
1656 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001657
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001658 boolean clearDb = false;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -07001659 try {
1660 ImportDataTask.performImportIfPossible(context);
1661 } catch (Exception e) {
1662 // Migration failed. Clear workspace.
1663 clearDb = true;
1664 }
1665
1666 if (!clearDb && GridSizeMigrationTask.ENABLED &&
Sunny Goyalf076eae2016-01-11 12:25:10 -08001667 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1668 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001669 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001670 }
1671
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001672 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -07001673 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001674 LauncherSettings.Settings.call(contentResolver,
1675 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001676 }
1677
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001678 Log.d(TAG, "loadWorkspace: loading default favorites");
1679 LauncherSettings.Settings.call(contentResolver,
1680 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -07001681
Winson Chung2abf94d2012-07-18 18:16:38 -07001682 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001683 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001684 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001685 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001686 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001687
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001688 final ArrayList<Long> itemsToRemove = new ArrayList<>();
1689 final ArrayList<Long> restoredRows = new ArrayList<>();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001690 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001691 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001692 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001693 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001694
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001695 // +1 for the hotseat (it can be larger than the workspace)
1696 // Load workspace in reverse order to ensure that latest items are loaded first (and
1697 // before any earlier duplicates)
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001698 final LongArrayMap<GridOccupancy> occupied = new LongArrayMap<>();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001699 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001700
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001701 try {
1702 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1703 final int intentIndex = c.getColumnIndexOrThrow
1704 (LauncherSettings.Favorites.INTENT);
1705 final int titleIndex = c.getColumnIndexOrThrow
1706 (LauncherSettings.Favorites.TITLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001707 final int containerIndex = c.getColumnIndexOrThrow(
1708 LauncherSettings.Favorites.CONTAINER);
1709 final int itemTypeIndex = c.getColumnIndexOrThrow(
1710 LauncherSettings.Favorites.ITEM_TYPE);
1711 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1712 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001713 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1714 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001715 final int screenIndex = c.getColumnIndexOrThrow(
1716 LauncherSettings.Favorites.SCREEN);
1717 final int cellXIndex = c.getColumnIndexOrThrow
1718 (LauncherSettings.Favorites.CELLX);
1719 final int cellYIndex = c.getColumnIndexOrThrow
1720 (LauncherSettings.Favorites.CELLY);
1721 final int spanXIndex = c.getColumnIndexOrThrow
1722 (LauncherSettings.Favorites.SPANX);
1723 final int spanYIndex = c.getColumnIndexOrThrow(
1724 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001725 final int rankIndex = c.getColumnIndexOrThrow(
1726 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001727 final int restoredIndex = c.getColumnIndexOrThrow(
1728 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001729 final int profileIdIndex = c.getColumnIndexOrThrow(
1730 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001731 final int optionsIndex = c.getColumnIndexOrThrow(
1732 LauncherSettings.Favorites.OPTIONS);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001733 final CursorIconInfo cursorIconInfo = new CursorIconInfo(c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001734
Sunny Goyal7f834d22015-04-21 10:10:23 -07001735 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
Kenny Guyff05f432016-01-22 17:48:29 +00001736 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyal7f834d22015-04-21 10:10:23 -07001737 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001738 long serialNo = mUserManager.getSerialNumberForUser(user);
1739 allUsers.put(serialNo, user);
1740 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001741
1742 List<ShortcutInfoCompat> pinnedShortcuts = mDeepShortcutManager
1743 .queryForPinnedShortcuts(null, user);
1744 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
1745 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
1746 shortcut);
1747 }
Sunny Goyal7f834d22015-04-21 10:10:23 -07001748 }
1749
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001750 ShortcutInfo info;
1751 String intentDescription;
1752 LauncherAppWidgetInfo appWidgetInfo;
1753 int container;
1754 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001755 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001756 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001757 UserHandleCompat user;
Sunny Goyald09c3702016-04-06 16:18:20 -07001758 String targetPackage;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001759
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001760 while (!mStopped && c.moveToNext()) {
1761 try {
1762 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001763 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001764 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001765 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001766
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001767 switch (itemType) {
1768 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1769 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001770 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001771 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001772 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001773 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001774 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001775 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001776 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001777 boolean itemReplaced = false;
Sunny Goyald09c3702016-04-06 16:18:20 -07001778 targetPackage = null;
Kenny Guyed131872014-04-30 03:02:21 +01001779 if (user == null) {
1780 // User has been deleted remove the item.
1781 itemsToRemove.add(id);
1782 continue;
1783 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001784 try {
1785 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001786 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001787 if (cn != null && cn.getPackageName() != null) {
1788 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1789 cn.getPackageName(), user);
1790 boolean validComponent = validPkg &&
1791 launcherApps.isActivityEnabledForProfile(cn, user);
Sunny Goyald09c3702016-04-06 16:18:20 -07001792 if (validPkg) {
1793 targetPackage = cn.getPackageName();
1794 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001795
1796 if (validComponent) {
1797 if (restored) {
1798 // no special handling necessary for this item
1799 restoredRows.add(id);
1800 restored = false;
1801 }
Kenny Guyff05f432016-01-22 17:48:29 +00001802 if (quietMode.get(serialNumber)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001803 disabledState = ShortcutInfo.FLAG_DISABLED_QUIET_USER;
Kenny Guyff05f432016-01-22 17:48:29 +00001804 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001805 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001806 intent = null;
1807 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1808 // We allow auto install apps to have their intent
1809 // updated after an install.
1810 intent = manager.getLaunchIntentForPackage(
1811 cn.getPackageName());
1812 if (intent != null) {
1813 ContentValues values = new ContentValues();
1814 values.put(LauncherSettings.Favorites.INTENT,
1815 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001816 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001817 }
1818 }
1819
1820 if (intent == null) {
1821 // The app is installed but the component is no
1822 // longer available.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001823 FileLog.d(TAG, "Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001824 itemsToRemove.add(id);
1825 continue;
1826 } else {
1827 // no special handling necessary for this item
1828 restoredRows.add(id);
1829 restored = false;
1830 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001831 } else if (restored) {
1832 // Package is not yet available but might be
1833 // installed later.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001834 FileLog.d(TAG, "package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001835
1836 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1837 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001838 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001839 // App restore has started. Update the flag
1840 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1841 ContentValues values = new ContentValues();
1842 values.put(LauncherSettings.Favorites.RESTORED,
1843 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001844 updateItem(id, values);
1845 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1846 // This is a common app. Try to replace this.
1847 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1848 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1849 if (parser.findDefaultApp()) {
1850 // Default app found. Replace it.
1851 intent = parser.parsedIntent;
1852 cn = intent.getComponent();
1853 ContentValues values = parser.parsedValues;
1854 values.put(LauncherSettings.Favorites.RESTORED, 0);
1855 updateItem(id, values);
1856 restored = false;
1857 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001858
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001859 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001860 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001861 itemsToRemove.add(id);
1862 continue;
1863 }
Sunny Goyal94485362014-09-18 16:13:58 -07001864 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001865 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001866 itemsToRemove.add(id);
1867 continue;
1868 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001869 } else if (PackageManagerHelper.isAppOnSdcard(
1870 manager, cn.getPackageName())) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07001871 // Package is present but not available.
1872 allowMissingTarget = true;
1873 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1874 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001875 // SdCard is not ready yet. Package might get available,
1876 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001877 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001878 HashSet<String> pkgs = sPendingPackages.get(user);
1879 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001880 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001881 sPendingPackages.put(user, pkgs);
1882 }
1883 pkgs.add(cn.getPackageName());
1884 allowMissingTarget = true;
1885 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001886
1887 } else {
1888 // Do not wait for external media load anymore.
1889 // Log the invalid package, and remove it
Sunny Goyal713edfc2016-05-06 09:58:34 -07001890 FileLog.d(TAG, "Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001891 itemsToRemove.add(id);
1892 continue;
Winson Chungee055712013-07-30 14:46:24 -07001893 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001894 } else if (cn == null) {
1895 // For shortcuts with no component, keep them as they are
1896 restoredRows.add(id);
1897 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001898 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001899 } catch (URISyntaxException e) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001900 FileLog.d(TAG, "Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001901 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001902 continue;
1903 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001904
Sunny Goyal34b65272015-03-11 16:56:52 -07001905 boolean useLowResIcon = container >= 0 &&
1906 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1907
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001908 if (itemReplaced) {
1909 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001910 info = getAppShortcutInfo(intent, user, context, null,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001911 cursorIconInfo.iconIndex, titleIndex,
1912 false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001913 } else {
1914 // Don't replace items for other profiles.
1915 itemsToRemove.add(id);
1916 continue;
1917 }
1918 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001919 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyal34b65272015-03-11 16:56:52 -07001920 info = getRestoredItemInfo(c, titleIndex, intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07001921 promiseType, itemType, cursorIconInfo, context);
Kenny Guyed131872014-04-30 03:02:21 +01001922 intent = getRestoredItemIntent(c, context, intent);
1923 } else {
1924 // Don't restore items for other profiles.
1925 itemsToRemove.add(id);
1926 continue;
1927 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001928 } else if (itemType ==
1929 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001930 info = getAppShortcutInfo(intent, user, context, c,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001931 cursorIconInfo.iconIndex, titleIndex,
1932 allowMissingTarget, useLowResIcon);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001933 } else if (itemType ==
1934 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
1935 String shortcutId = intent.getStringExtra(
1936 ShortcutInfoCompat.EXTRA_SHORTCUT_ID);
1937 String packageName = intent.getPackage();
1938 ShortcutKey key = new ShortcutKey(intent.getPackage(),
1939 user, shortcutId);
1940 ShortcutInfoCompat pinnedShortcut =
1941 shortcutKeyToPinnedShortcuts.get(key);
1942 boolean shouldPin = false; // It's already pinned.
1943 if (pinnedShortcut == null) {
1944 // It shouldn't be possible for a shortcut to be on the
1945 // workspace without being pinned, but if one somehow is,
1946 // we should pin it now to get back to a good state.
1947 Log.w(TAG, "Shortcut was on workspace but wasn't pinned");
1948 // Get full details; incrementing the count will pin it.
1949 List<ShortcutInfoCompat> fullDetails = mDeepShortcutManager
1950 .queryForFullDetails(packageName,
1951 Collections.singletonList(shortcutId), user);
Tony Wickhamd82a39d2016-07-01 15:44:13 -07001952 if (fullDetails == null || fullDetails.isEmpty()) {
1953 // There are no details for the shortcut. If this is due
1954 // to a SecurityException, keep it in the database so
1955 // we can restore the icon when the launcher regains
1956 // permission. Otherwise remove the icon from the db.
1957 if (!mDeepShortcutManager.wasLastCallSuccess()) {
1958 itemsToRemove.add(id);
1959 continue;
1960 }
1961 } else {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001962 pinnedShortcut = fullDetails.get(0);
1963 shouldPin = true;
1964 }
1965 }
1966 incrementPinnedShortcutCount(key, shouldPin);
Sunny Goyal10923b32016-07-20 15:42:44 -07001967 info = new ShortcutInfo(pinnedShortcut, context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001968 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyal4e5cc642015-06-25 16:37:44 -07001969 info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001970
Sunny Goyald09c3702016-04-06 16:18:20 -07001971 // Shortcuts are only available on the primary profile
1972 if (PackageManagerHelper.isAppSuspended(manager, targetPackage)) {
1973 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1974 }
1975
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001976 // App shortcuts that used to be automatically added to Launcher
1977 // didn't always have the correct intent flags set, so do that
1978 // here
1979 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001980 intent.getCategories() != null &&
1981 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001982 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001983 intent.addFlags(
1984 Intent.FLAG_ACTIVITY_NEW_TASK |
1985 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1986 }
Michael Jurka96879562012-03-22 05:54:33 -07001987 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001988
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001989 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001990 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001991 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001992 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001993 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001994 info.cellX = c.getInt(cellXIndex);
1995 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08001996 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001997 info.spanX = 1;
1998 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01001999 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002000 if (info.promisedIntent != null) {
2001 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
2002 }
Sunny Goyald09c3702016-04-06 16:18:20 -07002003 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002004 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2005 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2006 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002007
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002008 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002009 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002010 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002011 break;
2012 }
2013
Sunny Goyal756adbc2015-04-16 15:20:51 -07002014 if (restored) {
2015 ComponentName cn = info.getTargetComponent();
2016 if (cn != null) {
2017 Integer progress = installingPkgs.get(cn.getPackageName());
2018 if (progress != null) {
2019 info.setInstallProgress(progress);
2020 } else {
2021 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2022 }
2023 }
2024 }
2025
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002026 switch (container) {
2027 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2028 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2029 sBgWorkspaceItems.add(info);
2030 break;
2031 default:
2032 // Item is in a user folder
2033 FolderInfo folderInfo =
2034 findOrMakeFolder(sBgFolders, container);
Sunny Goyalc52ba712016-04-05 15:59:05 -07002035 folderInfo.add(info, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002036 break;
2037 }
2038 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002039 } else {
2040 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002041 }
2042 break;
2043
2044 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2045 id = c.getLong(idIndex);
2046 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2047
Sunny Goyala508e4f2015-05-21 09:33:57 -07002048 // Do not trim the folder label, as is was set by the user.
2049 folderInfo.title = c.getString(titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002050 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002051 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002052 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002053 folderInfo.cellX = c.getInt(cellXIndex);
2054 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002055 folderInfo.spanX = 1;
2056 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002057 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002058
Daniel Sandler8802e962010-05-26 16:28:16 -04002059 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002060 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002061 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002062 break;
2063 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002064
Joe Onorato9c1289c2009-08-17 11:03:03 -04002065 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002066 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2067 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2068 sBgWorkspaceItems.add(folderInfo);
2069 break;
Joe Onorato36115782010-06-17 13:28:48 -04002070 }
Joe Onorato17a89222011-02-08 17:26:11 -08002071
Chris Wrenf4d08112014-01-16 18:13:56 -05002072 if (restored) {
2073 // no special handling required for restored folders
2074 restoredRows.add(id);
2075 }
2076
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002077 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2078 sBgFolders.put(folderInfo.id, folderInfo);
2079 break;
2080
2081 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002082 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002083 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002084 boolean customWidget = itemType ==
2085 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2086
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002087 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002088 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002089 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002090 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002091 user = allUsers.get(serialNumber);
2092 if (user == null) {
2093 itemsToRemove.add(id);
2094 continue;
2095 }
2096
Sunny Goyalff572272014-07-23 13:58:07 -07002097 final ComponentName component =
2098 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002099
Sunny Goyal651077b2014-06-30 14:15:31 -07002100 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002101 final boolean isIdValid = (restoreStatus &
2102 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002103 final boolean wasProviderReady = (restoreStatus &
2104 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002105
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002106 if (widgetProvidersMap == null) {
2107 widgetProvidersMap = AppWidgetManagerCompat
2108 .getInstance(mContext).getAllProvidersMap();
2109 }
2110 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
2111 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00002112 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002113 user));
Sunny Goyalff572272014-07-23 13:58:07 -07002114
2115 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002116 if (!isSafeMode && !customWidget &&
2117 wasProviderReady && !isProviderReady) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002118 FileLog.d(TAG, "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07002119 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002120 itemsToRemove.add(id);
2121 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002122 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002123 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2124 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002125
Sunny Goyal53f96722015-07-13 19:54:53 -07002126 // The provider is available. So the widget is either
2127 // available or not available. We do not need to track
2128 // any future restore updates.
2129 int status = restoreStatus &
2130 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002131 if (!wasProviderReady) {
2132 // If provider was not previously ready, update the
2133 // status and UI flag.
2134
2135 // Id would be valid only if the widget restore broadcast was received.
2136 if (isIdValid) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002137 status = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002138 } else {
2139 status &= ~LauncherAppWidgetInfo
2140 .FLAG_PROVIDER_NOT_READY;
2141 }
2142 }
2143 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002144 } else {
2145 Log.v(TAG, "Widget restore pending id=" + id
2146 + " appWidgetId=" + appWidgetId
2147 + " status =" + restoreStatus);
2148 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002149 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002150 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002151 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002152
2153 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2154 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002155 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002156 // App restore has started. Update the flag
2157 appWidgetInfo.restoreStatus |=
2158 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002159 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002160 FileLog.d(TAG, "Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07002161 itemsToRemove.add(id);
2162 continue;
2163 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002164
2165 appWidgetInfo.installProgress =
2166 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002167 }
Sunny Goyalff572272014-07-23 13:58:07 -07002168
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002169 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002170 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002171 appWidgetInfo.cellX = c.getInt(cellXIndex);
2172 appWidgetInfo.cellY = c.getInt(cellYIndex);
2173 appWidgetInfo.spanX = c.getInt(spanXIndex);
2174 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002175 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002176
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002177 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2178 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2179 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002180 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2181 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002182 continue;
2183 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002184
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002185 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002186 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002187 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002188 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002189 break;
2190 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002191
Adam Cohen59400422014-03-05 18:07:04 -08002192 if (!customWidget) {
2193 String providerName =
2194 appWidgetInfo.providerName.flattenToString();
2195 if (!providerName.equals(savedProvider) ||
2196 (appWidgetInfo.restoreStatus != restoreStatus)) {
2197 ContentValues values = new ContentValues();
2198 values.put(
2199 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2200 providerName);
2201 values.put(LauncherSettings.Favorites.RESTORED,
2202 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002203 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002204 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002205 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002206 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2207 sBgAppWidgets.add(appWidgetInfo);
2208 }
Joe Onorato36115782010-06-17 13:28:48 -04002209 break;
2210 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002211 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002212 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002213 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002214 }
2215 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002216 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002217 }
2218
Winson Chungba9c37f2013-08-30 14:11:37 -07002219 // Break early if we've stopped loading
2220 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002221 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002222 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002223 }
2224
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002225 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002226 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002227 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2228 Utilities.createDbSelectionQuery(
2229 LauncherSettings.Favorites._ID, itemsToRemove), null);
2230 if (DEBUG_LOADERS) {
2231 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2232 LauncherSettings.Favorites._ID, itemsToRemove));
2233 }
2234
2235 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07002236 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
2237 .call(contentResolver,
2238 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
2239 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
2240 for (long folderId : deletedFolderIds) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002241 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2242 sBgFolders.remove(folderId);
2243 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002244 }
2245 }
2246
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002247 // Unpin shortcuts that don't exist on the workspace.
2248 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
2249 MutableInt numTimesPinned = sBgPinnedShortcutCounts.get(key);
2250 if (numTimesPinned == null || numTimesPinned.value == 0) {
2251 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
2252 mDeepShortcutManager.unpinShortcut(key);
2253 }
2254 }
2255
Sunny Goyal317698b2015-07-29 11:45:41 -07002256 // Sort all the folder items and make sure the first 3 items are high resolution.
2257 for (FolderInfo folder : sBgFolders) {
2258 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2259 int pos = 0;
2260 for (ShortcutInfo info : folder.contents) {
2261 if (info.usingLowResIcon) {
2262 info.updateIcon(mIconCache, false);
2263 }
2264 pos ++;
2265 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2266 break;
2267 }
2268 }
2269 }
2270
Chris Wrenf4d08112014-01-16 18:13:56 -05002271 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002272 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002273 ContentValues values = new ContentValues();
2274 values.put(LauncherSettings.Favorites.RESTORED, 0);
2275 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2276 Utilities.createDbSelectionQuery(
2277 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002278 }
2279
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002280 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2281 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002282 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002283 null, sWorker);
2284 }
2285
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002286 // Remove any empty screens
2287 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002288 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002289 long screenId = item.screenId;
2290 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2291 unusedScreens.contains(screenId)) {
2292 unusedScreens.remove(screenId);
2293 }
2294 }
2295
2296 // If there are any empty screens remove them, and update.
2297 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002298 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002299 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002300 }
2301
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002302 if (DEBUG_LOADERS) {
2303 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2304 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002305 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002306 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002307 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002308
Sunny Goyale2df0622015-04-24 11:27:00 -07002309 for (int i = 0; i < nScreens; i++) {
2310 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002311 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002312 line += " | ";
2313 }
Joe Onorato36115782010-06-17 13:28:48 -04002314 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002315 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002316 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002317 }
Joe Onorato36115782010-06-17 13:28:48 -04002318 }
Sunny Goyale26d1002016-06-20 14:52:14 -07002319 if (LauncherAppState.PROFILE_STARTUP) {
2320 Trace.endSection();
2321 }
Adam Cohene25af792013-06-06 23:08:25 -07002322 }
2323
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002324 /**
2325 * Partially updates the item without any notification. Must be called on the worker thread.
2326 */
2327 private void updateItem(long itemId, ContentValues update) {
2328 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002329 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002330 update,
2331 BaseColumns._ID + "= ?",
2332 new String[]{Long.toString(itemId)});
2333 }
2334
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002335 /** Filters the set of items who are directly or indirectly (via another container) on the
2336 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002337 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002338 ArrayList<ItemInfo> allWorkspaceItems,
2339 ArrayList<ItemInfo> currentScreenItems,
2340 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002341 // Purge any null ItemInfos
2342 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2343 while (iter.hasNext()) {
2344 ItemInfo i = iter.next();
2345 if (i == null) {
2346 iter.remove();
2347 }
2348 }
2349
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002350 // Order the set of items by their containers first, this allows use to walk through the
2351 // list sequentially, build up a list of containers that are in the specified screen,
2352 // as well as all items in those containers.
2353 Set<Long> itemsOnScreen = new HashSet<Long>();
2354 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2355 @Override
2356 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07002357 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002358 }
2359 });
2360 for (ItemInfo info : allWorkspaceItems) {
2361 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002362 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002363 currentScreenItems.add(info);
2364 itemsOnScreen.add(info.id);
2365 } else {
2366 otherScreenItems.add(info);
2367 }
2368 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2369 currentScreenItems.add(info);
2370 itemsOnScreen.add(info.id);
2371 } else {
2372 if (itemsOnScreen.contains(info.container)) {
2373 currentScreenItems.add(info);
2374 itemsOnScreen.add(info.id);
2375 } else {
2376 otherScreenItems.add(info);
2377 }
2378 }
2379 }
2380 }
2381
2382 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002383 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002384 ArrayList<LauncherAppWidgetInfo> appWidgets,
2385 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2386 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002387
2388 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002389 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002390 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002391 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002392 currentScreenWidgets.add(widget);
2393 } else {
2394 otherScreenWidgets.add(widget);
2395 }
2396 }
2397 }
2398
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002399 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2400 * right) */
2401 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002402 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002403 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002404 final int screenCols = profile.numColumns;
2405 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002406 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002407 @Override
2408 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002409 if (lhs.container == rhs.container) {
2410 // Within containers, order by their spatial position in that container
2411 switch ((int) lhs.container) {
2412 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2413 long lr = (lhs.screenId * screenCellCount +
2414 lhs.cellY * screenCols + lhs.cellX);
2415 long rr = (rhs.screenId * screenCellCount +
2416 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07002417 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07002418 }
2419 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2420 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07002421 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07002422 }
2423 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002424 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002425 throw new RuntimeException("Unexpected container type when " +
2426 "sorting workspace items.");
2427 }
2428 return 0;
2429 }
2430 } else {
2431 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07002432 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07002433 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002434 }
2435 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002436 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002437
Adam Cohendcd297f2013-06-18 13:13:40 -07002438 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2439 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002440 final Runnable r = new Runnable() {
2441 @Override
2442 public void run() {
2443 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2444 if (callbacks != null) {
2445 callbacks.bindScreens(orderedScreens);
2446 }
2447 }
2448 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002449 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002450 }
2451
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002452 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2453 final ArrayList<ItemInfo> workspaceItems,
2454 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002455 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002456
2457 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002458 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002459 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002460 final int start = i;
2461 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002462 final Runnable r = new Runnable() {
2463 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002464 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002465 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002466 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002467 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2468 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002469 }
2470 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002471 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002472 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002473 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002474
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002475 // Bind the widgets, one at a time
2476 N = appWidgets.size();
2477 for (int i = 0; i < N; i++) {
2478 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2479 final Runnable r = new Runnable() {
2480 public void run() {
2481 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2482 if (callbacks != null) {
2483 callbacks.bindAppWidget(widget);
2484 }
2485 }
2486 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002487 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002488 }
2489 }
2490
2491 /**
2492 * Binds all loaded data to actual views on the main thread.
2493 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002494 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002495 final long t = SystemClock.uptimeMillis();
2496 Runnable r;
2497
2498 // Don't use these two variables in any of the callback runnables.
2499 // Otherwise we hold a reference to them.
2500 final Callbacks oldCallbacks = mCallbacks.get();
2501 if (oldCallbacks == null) {
2502 // This launcher has exited and nobody bothered to tell us. Just bail.
2503 Log.w(TAG, "LoaderTask running with no launcher");
2504 return;
2505 }
2506
Winson Chung9b9fb962013-11-15 15:39:34 -08002507 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07002508 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
2509 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
2510 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002511
Winson Chung2abf94d2012-07-18 18:16:38 -07002512 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002513 workspaceItems.addAll(sBgWorkspaceItems);
2514 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002515 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002516 }
2517
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002518 final int currentScreen;
2519 {
2520 int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE
2521 ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen();
2522 if (currScreen >= orderedScreenIds.size()) {
2523 // There may be no workspace screens (just hotseat items and an empty page).
2524 currScreen = PagedView.INVALID_RESTORE_PAGE;
2525 }
2526 currentScreen = currScreen;
Winson Chung9b9fb962013-11-15 15:39:34 -08002527 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002528 final boolean validFirstPage = currentScreen >= 0;
2529 final long currentScreenId =
2530 validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID;
Winson Chung9b9fb962013-11-15 15:39:34 -08002531
Winson Chung9b9fb962013-11-15 15:39:34 -08002532 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07002533 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
2534 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
2535 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
2536 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002537
Winson Chung9b9fb962013-11-15 15:39:34 -08002538 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002539 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002540 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002541 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002542 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2543 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2544
2545 // Tell the workspace that we're about to start binding items
2546 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002547 public void run() {
2548 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2549 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002550 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002551 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002552 }
2553 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002554 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002555 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002556
Adam Cohendcd297f2013-06-18 13:13:40 -07002557 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2558
Sunny Goyal527c7d32015-08-28 15:19:36 -07002559 Executor mainExecutor = new DeferredMainThreadExecutor();
2560 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07002561 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002562
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002563 // In case of validFirstPage, only bind the first screen, and defer binding the
2564 // remaining screens after first onDraw (and an optional the fade animation whichever
2565 // happens later).
2566 // This ensures that the first screen is immediately visible (eg. during rotation)
2567 // In case of !validFirstPage, bind all pages one after other.
2568 final Executor deferredExecutor =
2569 validFirstPage ? new ViewOnDrawExecutor(mHandler) : mainExecutor;
2570
2571 mainExecutor.execute(new Runnable() {
2572 @Override
2573 public void run() {
2574 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2575 if (callbacks != null) {
2576 callbacks.finishFirstPageBind(
2577 validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null);
2578 }
2579 }
2580 });
Sunny Goyal527c7d32015-08-28 15:19:36 -07002581
Sunny Goyal44c06432016-04-02 10:56:02 -07002582 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002583
2584 // Tell the workspace that we're done binding items
2585 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002586 public void run() {
2587 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2588 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002589 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002590 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002591
Sunny Goyal639e9062015-08-19 19:17:06 -07002592 mIsLoadingAndBindingWorkspace = false;
2593
2594 // Run all the bind complete runnables after workspace is bound.
2595 if (!mBindCompleteRunnables.isEmpty()) {
2596 synchronized (mBindCompleteRunnables) {
2597 for (final Runnable r : mBindCompleteRunnables) {
2598 runOnWorkerThread(r);
2599 }
2600 mBindCompleteRunnables.clear();
2601 }
2602 }
2603
Winson Chung98e030b2012-05-07 16:01:11 -07002604 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002605 if (DEBUG_LOADERS) {
2606 Log.d(TAG, "bound workspace in "
2607 + (SystemClock.uptimeMillis()-t) + "ms");
2608 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002609
Joe Onorato36115782010-06-17 13:28:48 -04002610 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002611 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002612 deferredExecutor.execute(r);
2613
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002614 if (validFirstPage) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002615 r = new Runnable() {
2616 public void run() {
2617 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2618 if (callbacks != null) {
2619 // We are loading synchronously, which means, some of the pages will be
2620 // bound after first draw. Inform the callbacks that page binding is
2621 // not complete, and schedule the remaining pages.
2622 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2623 callbacks.onPageBoundSynchronously(currentScreen);
2624 }
2625 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2626 }
2627 }
2628 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002629 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002630 }
Joe Onorato36115782010-06-17 13:28:48 -04002631 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002632
Joe Onorato36115782010-06-17 13:28:48 -04002633 private void loadAndBindAllApps() {
2634 if (DEBUG_LOADERS) {
2635 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2636 }
2637 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002638 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002639 synchronized (LoaderTask.this) {
2640 if (mStopped) {
2641 return;
2642 }
2643 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002644 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002645 synchronized (LoaderTask.this) {
2646 if (mStopped) {
2647 return;
2648 }
2649 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002650 }
Joe Onorato36115782010-06-17 13:28:48 -04002651 } else {
2652 onlyBindAllApps();
2653 }
2654 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002655
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002656 private void updateIconCache() {
2657 // Ignore packages which have a promise icon.
2658 HashSet<String> packagesToIgnore = new HashSet<>();
2659 synchronized (sBgLock) {
2660 for (ItemInfo info : sBgItemsIdMap) {
2661 if (info instanceof ShortcutInfo) {
2662 ShortcutInfo si = (ShortcutInfo) info;
2663 if (si.isPromise() && si.getTargetComponent() != null) {
2664 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2665 }
2666 } else if (info instanceof LauncherAppWidgetInfo) {
2667 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2668 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2669 packagesToIgnore.add(lawi.providerName.getPackageName());
2670 }
2671 }
2672 }
2673 }
2674 mIconCache.updateDbIcons(packagesToIgnore);
2675 }
2676
Joe Onorato36115782010-06-17 13:28:48 -04002677 private void onlyBindAllApps() {
2678 final Callbacks oldCallbacks = mCallbacks.get();
2679 if (oldCallbacks == null) {
2680 // This launcher has exited and nobody bothered to tell us. Just bail.
2681 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2682 return;
2683 }
2684
2685 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002686 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002687 final ArrayList<AppInfo> list
2688 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002689 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002690 public void run() {
2691 final long t = SystemClock.uptimeMillis();
2692 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2693 if (callbacks != null) {
2694 callbacks.bindAllApplications(list);
2695 }
2696 if (DEBUG_LOADERS) {
2697 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002698 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04002699 }
2700 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002701 };
Tony Wickham80f57872016-06-29 18:12:15 -07002702 runOnMainThread(r);
Joe Onorato36115782010-06-17 13:28:48 -04002703 }
2704
Winson Chung64359a52013-07-08 17:17:08 -07002705 private void loadAllApps() {
2706 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002707
Joe Onorato36115782010-06-17 13:28:48 -04002708 final Callbacks oldCallbacks = mCallbacks.get();
2709 if (oldCallbacks == null) {
2710 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002711 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002712 return;
2713 }
2714
Kenny Guyed131872014-04-30 03:02:21 +01002715 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2716
Winson Chung64359a52013-07-08 17:17:08 -07002717 // Clear the list of apps
2718 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002719 for (UserHandleCompat user : profiles) {
2720 // Query for the set of apps
2721 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002722 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002723 if (DEBUG_LOADERS) {
2724 Log.d(TAG, "getActivityList took "
2725 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2726 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2727 }
2728 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002729 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002730 if (apps == null || apps.isEmpty()) {
2731 return;
2732 }
Kenny Guyff05f432016-01-22 17:48:29 +00002733 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002734 // Create the ApplicationInfos
2735 for (int i = 0; i < apps.size(); i++) {
2736 LauncherActivityInfoCompat app = apps.get(i);
2737 // This builds the icon bitmaps.
Kenny Guyff05f432016-01-22 17:48:29 +00002738 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode));
Kenny Guyed131872014-04-30 03:02:21 +01002739 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002740
Sunny Goyal756a28a2015-04-23 17:07:55 -07002741 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2742 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002743 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002744
2745 @Override
2746 public void run() {
2747 heuristic.processUserApps(apps);
2748 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002749 };
2750 runOnMainThread(new Runnable() {
2751
2752 @Override
2753 public void run() {
2754 // Check isLoadingWorkspace on the UI thread, as it is updated on
2755 // the UI thread.
2756 if (mIsLoadingAndBindingWorkspace) {
2757 synchronized (mBindCompleteRunnables) {
2758 mBindCompleteRunnables.add(r);
2759 }
2760 } else {
2761 runOnWorkerThread(r);
2762 }
2763 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002764 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002765 }
Winson Chung64359a52013-07-08 17:17:08 -07002766 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002767 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002768 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2769 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002770
2771 // Post callback on main thread
2772 mHandler.post(new Runnable() {
2773 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002774
Winson Chung64359a52013-07-08 17:17:08 -07002775 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002776 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002777 if (callbacks != null) {
2778 callbacks.bindAllApplications(added);
2779 if (DEBUG_LOADERS) {
2780 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002781 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07002782 }
2783 } else {
2784 Log.i(TAG, "not binding apps: no Launcher activity");
2785 }
2786 }
2787 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002788 // Cleanup any data stored for a deleted user.
2789 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04002790 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002791 Log.d(TAG, "Icons processed in "
2792 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002793 }
2794 }
2795
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002796 private void loadAndBindDeepShortcuts() {
2797 if (DEBUG_LOADERS) {
2798 Log.d(TAG, "loadAndBindDeepShortcuts mDeepShortcutsLoaded=" + mDeepShortcutsLoaded);
2799 }
2800 if (!mDeepShortcutsLoaded) {
2801 mBgDeepShortcutMap.clear();
2802 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
2803 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager
2804 .queryForAllShortcuts(user);
Sunny Goyal860538d2016-07-20 12:35:16 -07002805 updateDeepShortcutMap(null, user, shortcuts);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002806 }
2807 synchronized (LoaderTask.this) {
2808 if (mStopped) {
2809 return;
2810 }
2811 mDeepShortcutsLoaded = true;
2812 }
2813 }
Tony Wickham80f57872016-06-29 18:12:15 -07002814 bindDeepShortcuts();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002815 }
2816
Joe Onoratobe386092009-11-17 17:32:16 -08002817 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002818 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002819 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002820 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2821 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2822 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2823 }
Joe Onorato36115782010-06-17 13:28:48 -04002824 }
2825 }
2826
Sunny Goyal860538d2016-07-20 12:35:16 -07002827 /**
2828 * Clear all the shortcuts for the given package, and re-add the new shortcuts.
2829 */
2830 private void updateDeepShortcutMap(
2831 String packageName, UserHandleCompat user, List<ShortcutInfoCompat> shortcuts) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002832 if (packageName != null) {
2833 Iterator<ComponentKey> keysIter = mBgDeepShortcutMap.keySet().iterator();
2834 while (keysIter.hasNext()) {
Sunny Goyal860538d2016-07-20 12:35:16 -07002835 ComponentKey next = keysIter.next();
2836 if (next.componentName.getPackageName().equals(packageName)
2837 && next.user.equals(user)) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002838 keysIter.remove();
2839 }
2840 }
2841 }
2842
2843 // Now add the new shortcuts to the map.
2844 for (ShortcutInfoCompat shortcut : shortcuts) {
2845 ComponentKey targetComponent
2846 = new ComponentKey(shortcut.getActivity(), shortcut.getUserHandle());
2847 mBgDeepShortcutMap.addToList(targetComponent, shortcut.getId());
2848 }
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
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002906 private void bindUpdatedShortcuts(final ArrayList<ShortcutInfo> updatedShortcuts,
2907 UserHandleCompat user) {
2908 if (!updatedShortcuts.isEmpty()) {
2909 final Callbacks callbacks = getCallback();
2910 final UserHandleCompat userFinal = user;
2911 mHandler.post(new Runnable() {
2912
2913 public void run() {
2914 Callbacks cb = getCallback();
2915 if (cb != null && callbacks == cb) {
2916 cb.bindShortcutsChanged(updatedShortcuts,
2917 new ArrayList<ShortcutInfo>(), userFinal);
2918 }
2919 }
2920 });
2921 }
2922 }
2923
2924 void enqueueItemUpdatedTask(Runnable task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002925 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002926 }
2927
Adam Cohen091440a2015-03-18 14:16:05 -07002928 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002929
2930 @Override
2931 public void onReceive(Context context, Intent intent) {
2932 synchronized (sBgLock) {
2933 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2934 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002935 final PackageManager manager = context.getPackageManager();
2936 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2937 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002938 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2939 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002940 packagesRemoved.clear();
2941 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002942 for (String pkg : entry.getValue()) {
2943 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07002944 if (PackageManagerHelper.isAppOnSdcard(manager, pkg)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002945 packagesUnavailable.add(pkg);
2946 } else {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002947 packagesRemoved.add(pkg);
2948 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002949 }
2950 }
2951 if (!packagesRemoved.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002952 enqueueItemUpdatedTask(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002953 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2954 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002955 if (!packagesUnavailable.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002956 enqueueItemUpdatedTask(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
Sunny Goyal1a745e82014-10-02 15:58:31 -07002957 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2958 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002959 }
Sunny Goyal34942622014-08-29 17:20:55 -07002960 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002961 }
2962 }
2963 }
2964
Joe Onorato36115782010-06-17 13:28:48 -04002965 private class PackageUpdatedTask implements Runnable {
2966 int mOp;
2967 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002968 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002969
2970 public static final int OP_NONE = 0;
2971 public static final int OP_ADD = 1;
2972 public static final int OP_UPDATE = 2;
2973 public static final int OP_REMOVE = 3; // uninstlled
2974 public static final int OP_UNAVAILABLE = 4; // external media unmounted
Kenny Guy44cba692016-01-21 19:50:02 +00002975 public static final int OP_SUSPEND = 5; // package suspended
2976 public static final int OP_UNSUSPEND = 6; // package unsuspended
Sunny Goyalda891c12016-03-18 18:29:24 -07002977 public static final int OP_USER_AVAILABILITY_CHANGE = 7; // user available/unavailable
Joe Onorato36115782010-06-17 13:28:48 -04002978
Kenny Guyed131872014-04-30 03:02:21 +01002979 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002980 mOp = op;
2981 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002982 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002983 }
2984
2985 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002986 if (!mHasLoaderCompletedOnce) {
2987 // Loader has not yet run.
2988 return;
2989 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002990 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002991
2992 final String[] packages = mPackages;
2993 final int N = packages.length;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07002994 FlagOp flagOp = FlagOp.NO_OP;
Sunny Goyalda891c12016-03-18 18:29:24 -07002995 StringFilter pkgFilter = StringFilter.of(new HashSet<>(Arrays.asList(packages)));
Joe Onorato36115782010-06-17 13:28:48 -04002996 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002997 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04002998 for (int i=0; i<N; i++) {
2999 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003000 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003001 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003002 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003003
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003004 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3005 if (heuristic != null) {
3006 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003007 }
Joe Onorato36115782010-06-17 13:28:48 -04003008 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003009 }
Joe Onorato36115782010-06-17 13:28:48 -04003010 case OP_UPDATE:
3011 for (int i=0; i<N; i++) {
3012 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003013 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003014 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003015 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003016 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003017 // Since package was just updated, the target must be available now.
3018 flagOp = FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04003019 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003020 case OP_REMOVE: {
3021 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3022 if (heuristic != null) {
3023 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003024 }
Joe Onorato36115782010-06-17 13:28:48 -04003025 for (int i=0; i<N; i++) {
3026 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003027 mIconCache.removeIconsForPkg(packages[i], mUser);
3028 }
3029 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003030 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003031 case OP_UNAVAILABLE:
3032 for (int i=0; i<N; i++) {
3033 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3034 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003035 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003036 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003037 flagOp = FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04003038 break;
Kenny Guy44cba692016-01-21 19:50:02 +00003039 case OP_SUSPEND:
3040 case OP_UNSUSPEND:
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003041 flagOp = mOp == OP_SUSPEND ?
3042 FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED) :
3043 FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED);
Sunny Goyal17763cb2016-03-24 13:53:22 -07003044 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.(un)suspend " + N);
Sunny Goyalda891c12016-03-18 18:29:24 -07003045 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
3046 break;
3047 case OP_USER_AVAILABILITY_CHANGE:
3048 flagOp = UserManagerCompat.getInstance(context).isQuietModeEnabled(mUser)
3049 ? FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER)
3050 : FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER);
3051 // We want to update all packages for this user.
3052 pkgFilter = StringFilter.matchesAll();
3053 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
Kenny Guy44cba692016-01-21 19:50:02 +00003054 break;
Joe Onorato36115782010-06-17 13:28:48 -04003055 }
3056
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003057 ArrayList<AppInfo> added = null;
3058 ArrayList<AppInfo> modified = null;
3059 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003060
Adam Cohen487f7dd2012-06-28 18:12:10 -07003061 if (mBgAllAppsList.added.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003062 added = new ArrayList<>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003063 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003064 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003065 if (mBgAllAppsList.modified.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003066 modified = new ArrayList<>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003067 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003068 }
Winson Chung5d55f332012-07-16 20:45:03 -07003069 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003070 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003071 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003072 }
3073
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003074 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps = new HashMap<>();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003075
Joe Onorato36115782010-06-17 13:28:48 -04003076 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003077 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003078 for (AppInfo ai : added) {
3079 addedOrUpdatedApps.put(ai.componentName, ai);
3080 }
Joe Onorato36115782010-06-17 13:28:48 -04003081 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003082
Joe Onorato36115782010-06-17 13:28:48 -04003083 if (modified != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003084 final Callbacks callbacks = getCallback();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003085 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003086 for (AppInfo ai : modified) {
3087 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003088 }
3089
Joe Onorato36115782010-06-17 13:28:48 -04003090 mHandler.post(new Runnable() {
3091 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003092 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003093 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003094 callbacks.bindAppsUpdated(modifiedFinal);
3095 }
3096 }
3097 });
3098 }
Winson Chung83892cc2013-05-01 16:53:33 -07003099
Sunny Goyal4390ace2014-10-13 11:33:11 -07003100 // Update shortcut infos
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003101 if (mOp == OP_ADD || flagOp != FlagOp.NO_OP) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003102 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3103 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3104 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3105
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003106 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003107 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003108 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3109 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003110 boolean infoUpdated = false;
3111 boolean shortcutUpdated = false;
3112
3113 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003114 if ((si.iconResource != null)
Sunny Goyalda891c12016-03-18 18:29:24 -07003115 && pkgFilter.matches(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003116 Bitmap icon = Utilities.createIconBitmap(
3117 si.iconResource.packageName,
3118 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003119 if (icon != null) {
3120 si.setIcon(icon);
3121 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003122 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003123 }
3124 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003125
3126 ComponentName cn = si.getTargetComponent();
Sunny Goyalda891c12016-03-18 18:29:24 -07003127 if (cn != null && pkgFilter.matches(cn.getPackageName())) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003128 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3129
3130 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003131 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3132 // Auto install icon
3133 PackageManager pm = context.getPackageManager();
3134 ResolveInfo matched = pm.resolveActivity(
3135 new Intent(Intent.ACTION_MAIN)
3136 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3137 PackageManager.MATCH_DEFAULT_ONLY);
3138 if (matched == null) {
3139 // Try to find the best match activity.
3140 Intent intent = pm.getLaunchIntentForPackage(
3141 cn.getPackageName());
3142 if (intent != null) {
3143 cn = intent.getComponent();
3144 appInfo = addedOrUpdatedApps.get(cn);
3145 }
3146
3147 if ((intent == null) || (appInfo == null)) {
3148 removedShortcuts.add(si);
3149 continue;
3150 }
3151 si.promisedIntent = intent;
3152 }
3153 }
3154
3155 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003156 if (appInfo != null) {
3157 si.flags = appInfo.flags;
3158 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003159
Sunny Goyal756adbc2015-04-16 15:20:51 -07003160 si.intent = si.promisedIntent;
3161 si.promisedIntent = null;
3162 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003163 infoUpdated = true;
3164 si.updateIcon(mIconCache);
3165 }
3166
3167 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3168 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3169 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003170 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003171 si.contentDescription = appInfo.contentDescription;
3172 infoUpdated = true;
3173 }
3174
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003175 int oldDisabledFlags = si.isDisabled;
3176 si.isDisabled = flagOp.apply(si.isDisabled);
3177 if (si.isDisabled != oldDisabledFlags) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003178 shortcutUpdated = true;
3179 }
3180 }
3181
3182 if (infoUpdated || shortcutUpdated) {
3183 updatedShortcuts.add(si);
3184 }
3185 if (infoUpdated) {
3186 updateItemInDatabase(context, si);
3187 }
Sunny Goyalda891c12016-03-18 18:29:24 -07003188 } else if (info instanceof LauncherAppWidgetInfo && mOp == OP_ADD) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003189 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3190 if (mUser.equals(widgetInfo.user)
3191 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
Sunny Goyalda891c12016-03-18 18:29:24 -07003192 && pkgFilter.matches(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003193 widgetInfo.restoreStatus &=
3194 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3195 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003196
3197 // adding this flag ensures that launcher shows 'click to setup'
3198 // if the widget has a config activity. In case there is no config
3199 // activity, it will be marked as 'restored' during bind.
3200 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3201
Sunny Goyal4390ace2014-10-13 11:33:11 -07003202 widgets.add(widgetInfo);
3203 updateItemInDatabase(context, widgetInfo);
3204 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003205 }
3206 }
3207 }
3208
Sunny Goyal4390ace2014-10-13 11:33:11 -07003209 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003210 final Callbacks callbacks = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003211 mHandler.post(new Runnable() {
3212
3213 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003214 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003215 if (callbacks == cb && cb != null) {
3216 callbacks.bindShortcutsChanged(
3217 updatedShortcuts, removedShortcuts, mUser);
3218 }
3219 }
3220 });
3221 if (!removedShortcuts.isEmpty()) {
3222 deleteItemsFromDatabase(context, removedShortcuts);
3223 }
3224 }
3225 if (!widgets.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003226 final Callbacks callbacks = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003227 mHandler.post(new Runnable() {
3228 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003229 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003230 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003231 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003232 }
3233 }
3234 });
3235 }
3236 }
3237
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003238 final HashSet<String> removedPackages = new HashSet<>();
3239 final HashSet<ComponentName> removedComponents = new HashSet<>();
3240 if (mOp == OP_REMOVE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003241 // Mark all packages in the broadcast to be removed
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003242 Collections.addAll(removedPackages, packages);
3243
3244 // No need to update the removedComponents as
3245 // removedPackages is a super-set of removedComponents
Winson Chungdf95eb12013-10-16 14:57:07 -07003246 } else if (mOp == OP_UPDATE) {
3247 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003248 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003249 if (isPackageDisabled(context, packages[i], mUser)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003250 removedPackages.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003251 }
3252 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003253
3254 // Update removedComponents as some components can get removed during package update
3255 for (AppInfo info : removedApps) {
3256 removedComponents.add(info.componentName);
3257 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003258 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003259
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003260 if (!removedPackages.isEmpty() || !removedComponents.isEmpty()) {
3261 for (String pn : removedPackages) {
3262 deletePackageFromDatabase(context, pn, mUser);
3263 }
3264 for (ComponentName cn : removedComponents) {
3265 deleteItemsFromDatabase(context, getItemInfoForComponentName(cn, mUser));
Sunny Goyal1a745e82014-10-02 15:58:31 -07003266 }
3267
Winson Chungdf95eb12013-10-16 14:57:07 -07003268 // Remove any queued items from the install queue
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003269 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackages, mUser);
3270
Winson Chungdf95eb12013-10-16 14:57:07 -07003271 // Call the components-removed callback
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003272 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003273 mHandler.post(new Runnable() {
3274 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003275 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003276 if (callbacks == cb && cb != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003277 callbacks.bindWorkspaceComponentsRemoved(
3278 removedPackages, removedComponents, mUser);
3279 }
3280 }
3281 });
3282 }
3283
3284 if (!removedApps.isEmpty()) {
3285 // Remove corresponding apps from All-Apps
3286 final Callbacks callbacks = getCallback();
3287 mHandler.post(new Runnable() {
3288 public void run() {
3289 Callbacks cb = getCallback();
3290 if (callbacks == cb && cb != null) {
3291 callbacks.bindAppInfosRemoved(removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04003292 }
3293 }
3294 });
Joe Onoratobe386092009-11-17 17:32:16 -08003295 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003296
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003297 // Notify launcher of widget update. From marshmallow onwards we use AppWidgetHost to
3298 // get widget update signals.
3299 if (!Utilities.ATLEAST_MARSHMALLOW &&
3300 (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003301 final Callbacks callbacks = getCallback();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003302 mHandler.post(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003303 public void run() {
3304 Callbacks cb = getCallback();
3305 if (callbacks == cb && cb != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003306 callbacks.notifyWidgetProvidersChanged();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003307 }
3308 }
3309 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003310 }
3311 }
3312 }
3313
Sunny Goyal10923b32016-07-20 15:42:44 -07003314 /**
3315 * Repopulates the shortcut info, possibly updating any icon already on the workspace.
3316 */
3317 public void updateShortcutInfo(final ShortcutInfoCompat fullDetail, final ShortcutInfo info) {
3318 enqueueItemUpdatedTask(new Runnable() {
3319 @Override
3320 public void run() {
3321 info.updateFromDeepShortcutInfo(
3322 fullDetail, LauncherAppState.getInstance().getContext());
3323 ArrayList<ShortcutInfo> update = new ArrayList<ShortcutInfo>();
3324 update.add(info);
3325 bindUpdatedShortcuts(update, fullDetail.getUserHandle());
3326 }
3327 });
3328 }
3329
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003330 private class ShortcutsChangedTask implements Runnable {
3331 private String mPackageName;
3332 private List<ShortcutInfoCompat> mShortcuts;
3333 private UserHandleCompat mUser;
3334
3335 public ShortcutsChangedTask(String packageName, List<ShortcutInfoCompat> shortcuts,
3336 UserHandleCompat user) {
3337 mPackageName = packageName;
3338 mShortcuts = shortcuts;
3339 mUser = user;
3340 }
3341
3342 @Override
3343 public void run() {
3344 mDeepShortcutManager.onShortcutsChanged(mShortcuts);
3345
3346 Map<String, ShortcutInfoCompat> idsToShortcuts = new HashMap<>();
3347 for (ShortcutInfoCompat shortcut : mShortcuts) {
3348 idsToShortcuts.put(shortcut.getId(), shortcut);
3349 }
3350
3351 // Find ShortcutInfo's that have changed on the workspace.
3352 MultiHashMap<String, ShortcutInfo> idsToWorkspaceShortcutInfos = new MultiHashMap<>();
3353 for (ItemInfo itemInfo : sBgItemsIdMap) {
3354 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
3355 ShortcutInfo si = (ShortcutInfo) itemInfo;
3356 String shortcutId = si.getDeepShortcutId();
3357 if (idsToShortcuts.containsKey(shortcutId)) {
3358 idsToWorkspaceShortcutInfos.addToList(shortcutId, si);
3359 }
3360 }
3361 }
3362
3363 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3364 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager.queryForFullDetails(
3365 mPackageName, new ArrayList<>(idsToWorkspaceShortcutInfos.keySet()), mUser);
3366 ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3367 Context context = LauncherAppState.getInstance().getContext();
3368 for (ShortcutInfoCompat fullDetails : shortcuts) {
3369 List<ShortcutInfo> shortcutInfos = idsToWorkspaceShortcutInfos
3370 .get(fullDetails.getId());
3371 for (ShortcutInfo shortcutInfo : shortcutInfos) {
Sunny Goyal9994b2b2016-06-23 14:17:24 -07003372 shortcutInfo.updateFromDeepShortcutInfo(fullDetails, context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003373 updatedShortcutInfos.add(shortcutInfo);
3374 }
3375 }
3376 bindUpdatedShortcuts(updatedShortcutInfos, mUser);
3377
3378 // Update the deep shortcut map, in case the list of ids has changed for an activity.
Sunny Goyal860538d2016-07-20 12:35:16 -07003379 updateDeepShortcutMap(mPackageName, mUser, mShortcuts);
Tony Wickham80f57872016-06-29 18:12:15 -07003380 bindDeepShortcuts();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003381 }
3382 }
3383
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003384 private void bindWidgetsModel(final Callbacks callbacks, final WidgetsModel model) {
3385 mHandler.post(new Runnable() {
3386 @Override
3387 public void run() {
3388 Callbacks cb = getCallback();
3389 if (callbacks == cb && cb != null) {
3390 callbacks.bindWidgetsModel(model);
3391 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003392 }
3393 });
3394 }
3395
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003396 public void refreshAndBindWidgetsAndShortcuts(
3397 final Callbacks callbacks, final boolean bindFirst) {
3398 runOnWorkerThread(new Runnable() {
3399 @Override
3400 public void run() {
3401 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
3402 bindWidgetsModel(callbacks, mBgWidgetsModel.clone());
Sunny Goyal31860582015-09-18 08:38:57 -07003403 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003404 final WidgetsModel model = mBgWidgetsModel.updateAndClone(mApp.getContext());
3405 bindWidgetsModel(callbacks, model);
3406 // update the Widget entries inside DB on the worker thread.
3407 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3408 model.getRawList());
Sunny Goyal31860582015-09-18 08:38:57 -07003409 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003410 });
Michael Jurkac402cd92013-05-20 15:49:32 +02003411 }
3412
Adam Cohen091440a2015-03-18 14:16:05 -07003413 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003414 UserHandleCompat user) {
3415 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3416 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003417 }
Adam Cohen556f6132014-01-15 15:18:08 -08003418
Kenny Guyed131872014-04-30 03:02:21 +01003419 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3420 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003421 if (cn == null) {
3422 return false;
3423 }
Kenny Guyed131872014-04-30 03:02:21 +01003424 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3425 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003426 return false;
3427 }
Kenny Guyed131872014-04-30 03:02:21 +01003428 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003429 }
3430
Adam Cohena28b78e2014-05-20 17:03:04 -07003431 public static boolean isValidPackage(Context context, String packageName,
3432 UserHandleCompat user) {
3433 if (packageName == null) {
3434 return false;
3435 }
3436 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3437 return launcherApps.isPackageEnabledForProfile(packageName, user);
3438 }
3439
Joe Onorato9c1289c2009-08-17 11:03:03 -04003440 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003441 * Make an ShortcutInfo object for a restored application or shortcut item that points
3442 * to a package that is not yet installed on the system.
3443 */
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003444 public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent,
Sunny Goyalc22841b2015-07-13 19:59:50 -07003445 int promiseType, int itemType, CursorIconInfo iconInfo, Context context) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003446 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003447 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003448
3449 Bitmap icon = iconInfo.loadIcon(c, info, context);
3450 // the fallback icon
3451 if (icon == null) {
3452 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3453 } else {
3454 info.setIcon(icon);
3455 }
Sunny Goyal34942622014-08-29 17:20:55 -07003456
3457 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003458 String title = (c != null) ? c.getString(titleIndex) : null;
Sunny Goyal34942622014-08-29 17:20:55 -07003459 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003460 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003461 }
Sunny Goyal34942622014-08-29 17:20:55 -07003462 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3463 if (TextUtils.isEmpty(info.title)) {
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003464 info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : "";
Sunny Goyal34942622014-08-29 17:20:55 -07003465 }
Sunny Goyal34942622014-08-29 17:20:55 -07003466 } else {
3467 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3468 }
3469
Winson Chung82b016c2015-05-08 17:00:10 -07003470 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003471 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003472 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003473 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003474 return info;
3475 }
3476
3477 /**
3478 * Make an Intent object for a restored application or shortcut item that points
3479 * to the market page for the item.
3480 */
Adam Cohen091440a2015-03-18 14:16:05 -07003481 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003482 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003483 return getMarketIntent(componentName.getPackageName());
3484 }
3485
3486 static Intent getMarketIntent(String packageName) {
3487 return new Intent(Intent.ACTION_VIEW)
3488 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003489 .scheme("market")
3490 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003491 .appendQueryParameter("id", packageName)
3492 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003493 }
3494
3495 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003496 * Make an ShortcutInfo object for a shortcut that is an application.
3497 *
3498 * If c is not null, then it will be used to fill in missing data like the title and icon.
3499 */
Sunny Goyald09c3702016-04-06 16:18:20 -07003500 public ShortcutInfo getAppShortcutInfo(Intent intent,
Kenny Guyed131872014-04-30 03:02:21 +01003501 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyal34b65272015-03-11 16:56:52 -07003502 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003503 if (user == null) {
3504 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003505 return null;
3506 }
3507
Kenny Guyed131872014-04-30 03:02:21 +01003508 ComponentName componentName = intent.getComponent();
3509 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08003510 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01003511 return null;
3512 }
3513
3514 Intent newIntent = new Intent(intent.getAction(), null);
3515 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3516 newIntent.setComponent(componentName);
3517 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003518 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003519 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3520 return null;
3521 }
3522
3523 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003524 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003525 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
3526 Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context);
3527 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003528 }
3529
Sunny Goyald09c3702016-04-06 16:18:20 -07003530 if (lai != null && PackageManagerHelper.isAppSuspended(lai.getApplicationInfo())) {
3531 info.isDisabled = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
3532 }
3533
Joe Onorato56d82912010-03-07 14:32:10 -05003534 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003535 if (TextUtils.isEmpty(info.title) && c != null) {
Winson Chung82b016c2015-05-08 17:00:10 -07003536 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003537 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003538
Joe Onorato56d82912010-03-07 14:32:10 -05003539 // fall back to the class name of the activity
3540 if (info.title == null) {
3541 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003542 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003543
Joe Onorato9c1289c2009-08-17 11:03:03 -04003544 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003545 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003546 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003547 if (lai != null) {
3548 info.flags = AppInfo.initFlags(lai);
3549 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003550 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003551 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003552
Sunny Goyale2df0622015-04-24 11:27:00 -07003553 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003554 ItemInfoFilter f) {
3555 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3556 for (ItemInfo i : infos) {
3557 if (i instanceof ShortcutInfo) {
3558 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003559 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003560 if (cn != null && f.filterItem(null, info, cn)) {
3561 filtered.add(info);
3562 }
3563 } else if (i instanceof FolderInfo) {
3564 FolderInfo info = (FolderInfo) i;
3565 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003566 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003567 if (cn != null && f.filterItem(info, s, cn)) {
3568 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003569 }
3570 }
Winson Chung64359a52013-07-08 17:17:08 -07003571 } else if (i instanceof LauncherAppWidgetInfo) {
3572 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3573 ComponentName cn = info.providerName;
3574 if (cn != null && f.filterItem(null, info, cn)) {
3575 filtered.add(info);
3576 }
Winson Chung8a435102012-08-30 17:16:53 -07003577 }
3578 }
Winson Chung64359a52013-07-08 17:17:08 -07003579 return new ArrayList<ItemInfo>(filtered);
3580 }
3581
Adam Cohen091440a2015-03-18 14:16:05 -07003582 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003583 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003584 ItemInfoFilter filter = new ItemInfoFilter() {
3585 @Override
3586 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003587 if (info.user == null) {
3588 return cn.equals(cname);
3589 } else {
3590 return cn.equals(cname) && info.user.equals(user);
3591 }
Winson Chung64359a52013-07-08 17:17:08 -07003592 }
3593 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003594 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003595 }
3596
Sunny Goyal1a745e82014-10-02 15:58:31 -07003597 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003598 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003599 */
Adam Cohen091440a2015-03-18 14:16:05 -07003600 @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context,
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003601 int titleIndex, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003602 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003603 // Non-app shortcuts are only supported for current user.
3604 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003605 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003606
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003607 // TODO: If there's an explicit component and we can't install that, delete it.
3608
Winson Chung82b016c2015-05-08 17:00:10 -07003609 info.title = Utilities.trim(c.getString(titleIndex));
Joe Onorato56d82912010-03-07 14:32:10 -05003610
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003611 Bitmap icon = iconInfo.loadIcon(c, info, context);
3612 // the fallback icon
3613 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003614 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003615 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003616 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003617 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003618 return info;
3619 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003620
Sunny Goyal2350bc92014-10-14 16:42:54 -07003621 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003622 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3623 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3624 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3625
Adam Cohend9198822011-11-22 16:42:47 -08003626 if (intent == null) {
3627 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3628 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3629 return null;
3630 }
3631
Joe Onorato0589f0f2010-02-08 13:44:00 -08003632 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003633 boolean customIcon = false;
3634 ShortcutIconResource iconResource = null;
3635
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003636 if (bitmap instanceof Bitmap) {
3637 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003638 customIcon = true;
3639 } else {
3640 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003641 if (extra instanceof ShortcutIconResource) {
3642 iconResource = (ShortcutIconResource) extra;
3643 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003644 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003645 }
3646 }
3647
Michael Jurkac9d95c52011-08-29 14:03:34 -07003648 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003649
Kenny Guyed131872014-04-30 03:02:21 +01003650 // Only support intents for current user for now. Intents sent from other
3651 // users wouldn't get here without intent forwarding anyway.
3652 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003653 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003654 icon = mIconCache.getDefaultIcon(info.user);
3655 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003656 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003657 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003658
Winson Chung82b016c2015-05-08 17:00:10 -07003659 info.title = Utilities.trim(name);
3660 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003661 info.intent = intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003662 info.iconResource = iconResource;
3663
3664 return info;
3665 }
3666
Joe Onorato9c1289c2009-08-17 11:03:03 -04003667 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003668 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003669 * or make a new one.
3670 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003671 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003672 // See if a placeholder was created for us already
3673 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003674 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003675 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003676 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003677 folders.put(id, folderInfo);
3678 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003679 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003680 }
3681
Joe Onoratobe386092009-11-17 17:32:16 -08003682
Sunny Goyal651077b2014-06-30 14:15:31 -07003683 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3684 return (provider != null) && (provider.provider != null)
3685 && (provider.provider.getPackageName() != null);
3686 }
3687
Joe Onoratobe386092009-11-17 17:32:16 -08003688 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003689 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003690 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3691 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3692 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3693 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003694 if (mLoaderTask != null) {
3695 mLoaderTask.dumpState();
3696 } else {
3697 Log.d(TAG, "mLoaderTask=null");
3698 }
Joe Onoratobe386092009-11-17 17:32:16 -08003699 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003700
3701 public Callbacks getCallback() {
3702 return mCallbacks != null ? mCallbacks.get() : null;
3703 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003704
3705 /**
3706 * @return {@link FolderInfo} if its already loaded.
3707 */
3708 public FolderInfo findFolderById(Long folderId) {
3709 synchronized (sBgLock) {
3710 return sBgFolders.get(folderId);
3711 }
3712 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003713
Sunny Goyal527c7d32015-08-28 15:19:36 -07003714 @Thunk class DeferredMainThreadExecutor implements Executor {
3715
3716 @Override
3717 public void execute(Runnable command) {
3718 runOnMainThread(command);
3719 }
3720 }
3721
Sunny Goyal756adbc2015-04-16 15:20:51 -07003722 /**
3723 * @return the looper for the worker thread which can be used to start background tasks.
3724 */
3725 public static Looper getWorkerLooper() {
3726 return sWorkerThread.getLooper();
3727 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003728}