blob: ccde0e9cf1682744579a4dca197020402b053f62 [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;
Sunny Goyalc2936bc2016-09-01 15:50:36 -070062import com.android.launcher3.model.SdCardAvailableReceiver;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070063import com.android.launcher3.model.WidgetsModel;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -070064import com.android.launcher3.provider.ImportDataTask;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070065import com.android.launcher3.provider.LauncherDbUtils;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070066import com.android.launcher3.shortcuts.DeepShortcutManager;
67import com.android.launcher3.shortcuts.ShortcutInfoCompat;
68import com.android.launcher3.shortcuts.ShortcutKey;
Robin Lee26ace122015-03-16 19:41:43 +000069import com.android.launcher3.util.ComponentKey;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070070import com.android.launcher3.util.CursorIconInfo;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -070071import com.android.launcher3.util.FlagOp;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070072import com.android.launcher3.util.GridOccupancy;
Sunny Goyale2df0622015-04-24 11:27:00 -070073import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070074import com.android.launcher3.util.ManagedProfileHeuristic;
Tony Wickhamd82a39d2016-07-01 15:44:13 -070075import com.android.launcher3.util.MultiHashMap;
Sunny Goyald09c3702016-04-06 16:18:20 -070076import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070077import com.android.launcher3.util.Preconditions;
Sunny Goyalda891c12016-03-18 18:29:24 -070078import com.android.launcher3.util.StringFilter;
Adam Cohen091440a2015-03-18 14:16:05 -070079import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070080import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080081
Michael Jurkac2f801e2011-07-12 14:19:46 -070082import java.lang.ref.WeakReference;
83import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070084import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070085import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070086import java.util.Arrays;
Michael Jurkac2f801e2011-07-12 14:19:46 -070087import java.util.Collections;
88import java.util.Comparator;
89import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070090import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070091import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070092import java.util.List;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070093import java.util.Map;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070094import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070095import java.util.Set;
Sunny Goyal527c7d32015-08-28 15:19:36 -070096import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -070097
The Android Open Source Project31dd5032009-03-03 19:32:27 -080098/**
99 * Maintains in-memory state of the Launcher. It is expected that there should be only one
100 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700101 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102 */
Kenny Guyed131872014-04-30 03:02:21 +0100103public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +0100104 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800105 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -0400106 private static final boolean DEBUG_RECEIVER = false;
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
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700149 private boolean mHasShortcutHostPermission;
150 // Runnable to check if the shortcuts permission has changed.
151 private final Runnable mShortcutPermissionCheckRunnable = new Runnable() {
152 @Override
153 public void run() {
154 if (mDeepShortcutsLoaded) {
155 boolean hasShortcutHostPermission = mDeepShortcutManager.hasHostPermission();
156 if (hasShortcutHostPermission != mHasShortcutHostPermission) {
157 mApp.reloadWorkspace();
158 }
159 }
160 }
161 };
162
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700163 // The lock that must be acquired before referencing any static bg data structures. Unlike
164 // other locks, this one can generally be held long-term because we never expect any of these
165 // static data structures to be referenced outside of the worker thread except on the first
166 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700167 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700168
Adam Cohen487f7dd2012-06-28 18:12:10 -0700169 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700170 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700171 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700172
Adam Cohen487f7dd2012-06-28 18:12:10 -0700173 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
174 // created by LauncherModel that are directly on the home screen (however, no widgets or
175 // shortcuts within folders).
176 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700177
Adam Cohen487f7dd2012-06-28 18:12:10 -0700178 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
179 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700180 new ArrayList<LauncherAppWidgetInfo>();
181
Adam Cohen487f7dd2012-06-28 18:12:10 -0700182 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700183 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700184
Adam Cohendcd297f2013-06-18 13:13:40 -0700185 // sBgWorkspaceScreens is the ordered set of workspace screens.
186 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
187
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700188 // sBgPinnedShortcutCounts is the ComponentKey representing a pinned shortcut to the number of
189 // times it is pinned.
190 static final Map<ShortcutKey, MutableInt> sBgPinnedShortcutCounts = new HashMap<>();
191
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700192 // </ only access in worker thread >
193
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700194 private IconCache mIconCache;
195 private DeepShortcutManager mDeepShortcutManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700197 private final LauncherAppsCompat mLauncherApps;
198 private final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100199
Joe Onorato9c1289c2009-08-17 11:03:03 -0400200 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700201 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400202 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700203 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400204 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700205 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
206 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700207 public void bindScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700208 public void finishFirstPageBind(ViewOnDrawExecutor executor);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800209 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400210 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200211 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700212 public void bindAppsAdded(ArrayList<Long> newScreens,
213 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700214 ArrayList<ItemInfo> addAnimated,
215 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200216 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700217 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
218 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
219 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700220 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700221 public void bindWorkspaceComponentsRemoved(
222 HashSet<String> packageNames, HashSet<ComponentName> components,
223 UserHandleCompat user);
224 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800225 public void notifyWidgetProvidersChanged();
226 public void bindWidgetsModel(WidgetsModel model);
Adam Cohen1462de32012-07-24 22:34:36 -0700227 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700228 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700229 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMap);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400230 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800231
Winson Chung64359a52013-07-08 17:17:08 -0700232 public interface ItemInfoFilter {
233 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
234 }
235
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700236 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter,
237 DeepShortcutManager deepShortcutManager) {
Winson Chunga6945242014-01-08 14:04:34 -0800238 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400239 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100240 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700241 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800242 mIconCache = iconCache;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700243 mDeepShortcutManager = deepShortcutManager;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800244
Kenny Guyed131872014-04-30 03:02:21 +0100245 mLauncherApps = LauncherAppsCompat.getInstance(context);
246 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800247 }
248
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700249 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
250 * posted on the main thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700251 private void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700252 if (sWorkerThread.getThreadId() == Process.myTid()) {
253 // If we are on the worker thread, post onto the main handler
254 mHandler.post(r);
255 } else {
256 r.run();
257 }
258 }
259
260 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
261 * posted on the worker thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700262 private static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700263 if (sWorkerThread.getThreadId() == Process.myTid()) {
264 r.run();
265 } else {
266 // If we are not on the worker thread, then post to the worker handler
267 sWorker.post(r);
268 }
269 }
270
Sunny Goyal756adbc2015-04-16 15:20:51 -0700271 public void setPackageState(final PackageInstallInfo installInfo) {
272 Runnable updateRunnable = new Runnable() {
273
274 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500275 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700276 synchronized (sBgLock) {
277 final HashSet<ItemInfo> updates = new HashSet<>();
278
279 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
280 // Ignore install success events as they are handled by Package add events.
281 return;
282 }
283
Sunny Goyale2df0622015-04-24 11:27:00 -0700284 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700285 if (info instanceof ShortcutInfo) {
286 ShortcutInfo si = (ShortcutInfo) info;
287 ComponentName cn = si.getTargetComponent();
288 if (si.isPromise() && (cn != null)
289 && installInfo.packageName.equals(cn.getPackageName())) {
290 si.setInstallProgress(installInfo.progress);
291
292 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
293 // Mark this info as broken.
294 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
295 }
296 updates.add(si);
297 }
298 }
299 }
300
301 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
302 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
303 widget.installProgress = installInfo.progress;
304 updates.add(widget);
305 }
306 }
307
308 if (!updates.isEmpty()) {
309 // Push changes to the callback.
310 Runnable r = new Runnable() {
311 public void run() {
312 Callbacks callbacks = getCallback();
313 if (callbacks != null) {
314 callbacks.bindRestoreItemsChange(updates);
315 }
316 }
317 };
318 mHandler.post(r);
319 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500320 }
321 }
322 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700323 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500324 }
325
Sunny Goyal756adbc2015-04-16 15:20:51 -0700326 /**
327 * Updates the icons and label of all pending icons for the provided package name.
328 */
329 public void updateSessionDisplayInfo(final String packageName) {
330 Runnable updateRunnable = new Runnable() {
331
332 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700333 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700334 synchronized (sBgLock) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700335 ArrayList<ShortcutInfo> updates = new ArrayList<>();
336 UserHandleCompat user = UserHandleCompat.myUserHandle();
Sunny Goyal756adbc2015-04-16 15:20:51 -0700337
Sunny Goyale2df0622015-04-24 11:27:00 -0700338 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700339 if (info instanceof ShortcutInfo) {
340 ShortcutInfo si = (ShortcutInfo) info;
341 ComponentName cn = si.getTargetComponent();
342 if (si.isPromise() && (cn != null)
343 && packageName.equals(cn.getPackageName())) {
344 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
345 // For auto install apps update the icon as well as label.
346 mIconCache.getTitleAndIcon(si,
347 si.promisedIntent, user,
348 si.shouldUseLowResIcon());
349 } else {
350 // Only update the icon for restored apps.
351 si.updateIcon(mIconCache);
352 }
353 updates.add(si);
354 }
355 }
356 }
357
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700358 bindUpdatedShortcuts(updates, user);
Sunny Goyala22666f2014-09-18 13:25:15 -0700359 }
360 }
361 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700362 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700363 }
364
Adam Cohen76a47a12014-02-05 11:47:43 -0800365 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800366 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800367
368 if (allAppsApps == null) {
369 throw new RuntimeException("allAppsApps must not be null");
370 }
371 if (allAppsApps.isEmpty()) {
372 return;
373 }
374
375 // Process the newly added applications and add them to the database first
376 Runnable r = new Runnable() {
377 public void run() {
378 runOnMainThread(new Runnable() {
379 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800380 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800381 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500382 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800383 }
384 }
385 });
386 }
387 };
388 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700389 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800390
Sunny Goyala9116722015-04-29 13:55:58 -0700391 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800392 int[] xy, int spanX, int spanY) {
393 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700394 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700395
396 GridOccupancy occupied = new GridOccupancy(profile.numColumns, profile.numRows);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800397 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700398 for (ItemInfo r : occupiedPos) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700399 occupied.markCells(r, true);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800400 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800401 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700402 return occupied.findVacantCell(xy, spanX, spanY);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800403 }
404
405 /**
406 * Find a position on the screen for the given size or adds a new screen.
407 * @return screenId and the coordinates for the item.
408 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700409 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyalc3642d42015-10-30 10:27:08 -0700410 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800411 ArrayList<Long> workspaceScreens,
412 ArrayList<Long> addedWorkspaceScreensFinal,
413 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700414 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800415
Sunny Goyala9116722015-04-29 13:55:58 -0700416 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700417 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700418 synchronized (sBgLock) {
419 for (ItemInfo info : sBgItemsIdMap) {
420 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
421 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
422 if (items == null) {
423 items = new ArrayList<>();
424 screenItems.put(info.screenId, items);
425 }
426 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800427 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800428 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800429 }
430
431 // Find appropriate space for the item.
432 long screenId = 0;
433 int[] cordinates = new int[2];
434 boolean found = false;
435
436 int screenCount = workspaceScreens.size();
437 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700438 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800439 if (preferredScreenIndex < screenCount) {
440 screenId = workspaceScreens.get(preferredScreenIndex);
441 found = findNextAvailableIconSpaceInScreen(
442 screenItems.get(screenId), cordinates, spanX, spanY);
443 }
444
445 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700446 // Search on any of the screens starting from the first screen.
447 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800448 screenId = workspaceScreens.get(screen);
449 if (findNextAvailableIconSpaceInScreen(
450 screenItems.get(screenId), cordinates, spanX, spanY)) {
451 // We found a space for it
452 found = true;
453 break;
454 }
455 }
456 }
457
458 if (!found) {
459 // Still no position found. Add a new screen to the end.
Sunny Goyald2497482015-09-22 18:24:19 -0700460 screenId = LauncherSettings.Settings.call(context.getContentResolver(),
461 LauncherSettings.Settings.METHOD_NEW_SCREEN_ID)
462 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800463
464 // Save the screen id for binding in the workspace
465 workspaceScreens.add(screenId);
466 addedWorkspaceScreensFinal.add(screenId);
467
468 // If we still can't find an empty space, then God help us all!!!
469 if (!findNextAvailableIconSpaceInScreen(
470 screenItems.get(screenId), cordinates, spanX, spanY)) {
471 throw new RuntimeException("Can't find space to add the item");
472 }
473 }
474 return Pair.create(screenId, cordinates);
475 }
476
477 /**
478 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800479 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700480 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700481 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800482 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800483 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700484 return;
Winson Chung997a9232013-07-24 15:33:46 -0700485 }
Winson Chung64359a52013-07-08 17:17:08 -0700486 // Process the newly added applications and add them to the database first
487 Runnable r = new Runnable() {
488 public void run() {
489 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
490 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
491
Winson Chung76828c82013-08-19 15:43:29 -0700492 // Get the list of workspace screens. We need to append to this list and
493 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
494 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800495 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700496 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800497 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700498 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800499 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700500 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800501 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700502 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800503 }
Winson Chung76828c82013-08-19 15:43:29 -0700504
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800505 // Find appropriate space for the item.
Sunny Goyalc3642d42015-10-30 10:27:08 -0700506 Pair<Long, int[]> coords = findSpaceForItem(context,
Sunny Goyal9eba1fd2015-10-16 08:58:57 -0700507 workspaceScreens, addedWorkspaceScreensFinal, 1, 1);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800508 long screenId = coords.first;
509 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700510
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700511 ItemInfo itemInfo;
512 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
513 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800514 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700515 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700516 } else {
517 throw new RuntimeException("Unexpected info type");
518 }
Winson Chung94d67682013-09-25 16:29:40 -0700519
Winson Chung64359a52013-07-08 17:17:08 -0700520 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700521 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700522 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700523 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700524 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700525 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700526 }
527 }
528
Winson Chung76828c82013-08-19 15:43:29 -0700529 // Update the workspace screens
530 updateWorkspaceScreenOrder(context, workspaceScreens);
531
Adam Cohen76a47a12014-02-05 11:47:43 -0800532 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700533 runOnMainThread(new Runnable() {
534 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800535 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700536 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700537 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
538 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700539 if (!addedShortcutsFinal.isEmpty()) {
540 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
541 long lastScreenId = info.screenId;
542 for (ItemInfo i : addedShortcutsFinal) {
543 if (i.screenId == lastScreenId) {
544 addAnimated.add(i);
545 } else {
546 addNotAnimated.add(i);
547 }
Winson Chung997a9232013-07-24 15:33:46 -0700548 }
549 }
Winson Chungd64d1762013-08-20 14:37:16 -0700550 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800551 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700552 }
Winson Chung64359a52013-07-08 17:17:08 -0700553 }
Winson Chung997a9232013-07-24 15:33:46 -0700554 });
555 }
Winson Chung64359a52013-07-08 17:17:08 -0700556 }
557 };
558 runOnWorkerThread(r);
559 }
560
Joe Onorato9c1289c2009-08-17 11:03:03 -0400561 /**
562 * Adds an item to the DB if it was not created previously, or move it to a new
563 * <container, screen, cellX, cellY>
564 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800565 public static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700566 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400567 if (item.container == ItemInfo.NO_ID) {
568 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700569 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400570 } else {
571 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700572 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800573 }
574 }
575
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700576 static void checkItemInfoLocked(
577 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
578 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
579 if (modelItem != null && item != modelItem) {
580 // check all the data is consistent
581 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
582 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
583 ShortcutInfo shortcut = (ShortcutInfo) item;
584 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
585 modelShortcut.intent.filterEquals(shortcut.intent) &&
586 modelShortcut.id == shortcut.id &&
587 modelShortcut.itemType == shortcut.itemType &&
588 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700589 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700590 modelShortcut.cellX == shortcut.cellX &&
591 modelShortcut.cellY == shortcut.cellY &&
592 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700593 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700594 // For all intents and purposes, this is the same object
595 return;
596 }
597 }
598
599 // the modelItem needs to match up perfectly with item if our model is
600 // to be consistent with the database-- for now, just require
601 // modelItem == item or the equality check above
602 String msg = "item: " + ((item != null) ? item.toString() : "null") +
603 "modelItem: " +
604 ((modelItem != null) ? modelItem.toString() : "null") +
605 "Error: ItemInfo passed to checkItemInfo doesn't match original";
606 RuntimeException e = new RuntimeException(msg);
607 if (stackTrace != null) {
608 e.setStackTrace(stackTrace);
609 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800610 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700611 }
612 }
613
Michael Jurka816474f2012-06-25 14:49:02 -0700614 static void checkItemInfo(final ItemInfo item) {
615 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
616 final long itemId = item.id;
617 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700618 public void run() {
619 synchronized (sBgLock) {
620 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700621 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700622 }
623 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700624 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700625 }
626
Michael Jurkac9d95c52011-08-29 14:03:34 -0700627 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
628 final ItemInfo item, final String callingFunction) {
629 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700630 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700631 final ContentResolver cr = context.getContentResolver();
632
Adam Cohen487f7dd2012-06-28 18:12:10 -0700633 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700634 Runnable r = new Runnable() {
635 public void run() {
636 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700637 updateItemArrays(item, itemId, stackTrace);
638 }
639 };
640 runOnWorkerThread(r);
641 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700642
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700643 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
644 final ArrayList<ItemInfo> items, final String callingFunction) {
645 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700646
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700647 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
648 Runnable r = new Runnable() {
649 public void run() {
650 ArrayList<ContentProviderOperation> ops =
651 new ArrayList<ContentProviderOperation>();
652 int count = items.size();
653 for (int i = 0; i < count; i++) {
654 ItemInfo item = items.get(i);
655 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700656 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700657 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700658
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700659 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
660 updateItemArrays(item, itemId, stackTrace);
661
662 }
663 try {
664 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
665 } catch (Exception e) {
666 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700667 }
668 }
669 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700670 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700671 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700672
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700673 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
674 // Lock on mBgLock *after* the db operation
675 synchronized (sBgLock) {
676 checkItemInfoLocked(itemId, item, stackTrace);
677
678 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
679 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
680 // Item is in a folder, make sure this folder exists
681 if (!sBgFolders.containsKey(item.container)) {
682 // An items container is being set to a that of an item which is not in
683 // the list of Folders.
684 String msg = "item: " + item + " container being set to: " +
685 item.container + ", not in the list of folders";
686 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700687 }
688 }
689
690 // Items are added/removed from the corresponding FolderInfo elsewhere, such
691 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
692 // that are on the desktop, as appropriate
693 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800694 if (modelItem != null &&
695 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
696 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700697 switch (modelItem.itemType) {
698 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
699 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700700 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700701 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
702 if (!sBgWorkspaceItems.contains(modelItem)) {
703 sBgWorkspaceItems.add(modelItem);
704 }
705 break;
706 default:
707 break;
708 }
709 } else {
710 sBgWorkspaceItems.remove(modelItem);
711 }
712 }
713 }
714
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400716 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700717 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700718 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700719 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400720 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400721 item.cellX = cellX;
722 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700723
Winson Chung3d503fb2011-07-13 17:25:49 -0700724 // We store hotseat items in canonical form which is this orientation invariant position
725 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700726 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700727 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700728 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700729 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700730 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700731 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400732
733 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400734 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700735 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
736 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800737 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700738 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400739
Michael Jurkac9d95c52011-08-29 14:03:34 -0700740 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700741 }
742
743 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700744 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
745 * cellX, cellY have already been updated on the ItemInfos.
746 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800747 public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700748 final long container, final int screen) {
749
750 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
751 int count = items.size();
752
753 for (int i = 0; i < count; i++) {
754 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700755 item.container = container;
756
757 // We store hotseat items in canonical form which is this orientation invariant position
758 // in the hotseat
759 if (context instanceof Launcher && screen < 0 &&
760 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700761 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700762 item.cellY);
763 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700764 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700765 }
766
767 final ContentValues values = new ContentValues();
768 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
769 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
770 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800771 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700772 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700773
774 contentValues.add(values);
775 }
776 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
777 }
778
779 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700780 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800781 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700782 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700783 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700784 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800785 item.cellX = cellX;
786 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700787 item.spanX = spanX;
788 item.spanY = spanY;
789
790 // We store hotseat items in canonical form which is this orientation invariant position
791 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700792 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700793 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700794 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700795 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700796 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700797 }
Adam Cohend4844c32011-02-18 19:25:06 -0800798
Adam Cohend4844c32011-02-18 19:25:06 -0800799 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800800 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700801 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
802 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800803 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700804 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
805 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700806 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800807
Michael Jurka816474f2012-06-25 14:49:02 -0700808 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700809 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700810
811 /**
812 * Update an item to the database in a specified container.
813 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700814 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700815 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100816 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700817 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800818 }
819
Sunny Goyal756a28a2015-04-23 17:07:55 -0700820 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700821 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700822 synchronized (mLock) {
823 if (!mHasLoaderCompletedOnce ||
824 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
825 throw new RuntimeException("Trying to add shortcut while loader is running");
826 }
827 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700828 }
829 }
830
Adam Cohend4844c32011-02-18 19:25:06 -0800831 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700832 * Returns true if the shortcuts already exists on the workspace. This must be called after
833 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800834 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700835 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
836 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700837 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700838 if (intent.getComponent() != null) {
839 // If component is not null, an intent with null package will produce
840 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700841 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700842 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700843 intentWithPkg = intent.toUri(0);
844 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700845 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700846 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
847 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700848 }
849 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700850 intentWithPkg = intent.toUri(0);
851 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700852 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700853
854 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700855 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700856 if (item instanceof ShortcutInfo) {
857 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700858 Intent targetIntent = info.promisedIntent == null
859 ? info.intent : info.promisedIntent;
860 if (targetIntent != null && info.user.equals(user)) {
Sunny Goyalcbfe71d2016-08-23 13:25:58 -0700861 Intent copyIntent = new Intent(targetIntent);
862 copyIntent.setSourceBounds(intent.getSourceBounds());
863 String s = copyIntent.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) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700929 incrementPinnedShortcutCount(
Tony Wickhamfc02c1b2016-08-29 15:17:48 -0700930 ShortcutKey.fromShortcutInfo((ShortcutInfo) item),
931 true /* shouldPin */);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700932 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700933 break;
934 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
935 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
936 break;
937 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700938 }
939 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700940 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700941 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700942 }
943
Sunny Goyal34942622014-08-29 17:20:55 -0700944 private static ArrayList<ItemInfo> getItemsByPackageName(
945 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700946 ItemInfoFilter filter = new ItemInfoFilter() {
947 @Override
948 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
949 return cn.getPackageName().equals(pn) && info.user.equals(user);
950 }
951 };
Sunny Goyale2df0622015-04-24 11:27:00 -0700952 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -0700953 }
954
955 /**
956 * Removes all the items from the database corresponding to the specified package.
957 */
958 static void deletePackageFromDatabase(Context context, final String pn,
959 final UserHandleCompat user) {
960 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700961 }
962
963 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700964 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -0400965 */
Sunny Goyalfa401a12015-04-10 13:45:42 -0700966 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700967 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
968 items.add(item);
969 deleteItemsFromDatabase(context, items);
970 }
971
972 /**
973 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700974 */
Sunny Goyal4390ace2014-10-13 11:33:11 -0700975 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400976 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -0700977 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700978 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700979 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700980 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700981 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700982
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700983 // Lock on mBgLock *after* the db operation
984 synchronized (sBgLock) {
985 switch (item.itemType) {
986 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
987 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -0700988 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700989 if (info.container == item.id) {
990 // We are deleting a folder which still contains items that
991 // think they are contained by that folder.
992 String msg = "deleting a folder (" + item + ") which still " +
993 "contains items (" + info + ")";
994 Log.e(TAG, msg);
995 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700996 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700997 sBgWorkspaceItems.remove(item);
998 break;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700999 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07001000 decrementPinnedShortcutCount(ShortcutKey.fromShortcutInfo(
1001 (ShortcutInfo) item));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001002 // Fall through.
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001003 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1004 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1005 sBgWorkspaceItems.remove(item);
1006 break;
1007 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1008 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1009 break;
1010 }
1011 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001012 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001013 }
1014 }
Michael Jurka83df1882011-08-31 20:59:26 -07001015 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001016 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001017 }
1018
1019 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001020 * Decrement the count for the given pinned shortcut, unpinning it if the count becomes 0.
1021 */
1022 private static void decrementPinnedShortcutCount(final ShortcutKey pinnedShortcut) {
1023 synchronized (sBgLock) {
1024 MutableInt count = sBgPinnedShortcutCounts.get(pinnedShortcut);
1025 if (count == null || --count.value == 0) {
1026 LauncherAppState.getInstance().getShortcutManager().unpinShortcut(pinnedShortcut);
1027 }
1028 }
1029 }
1030
1031 /**
1032 * Increment the count for the given shortcut, pinning it if the count becomes 1.
1033 *
1034 * As an optimization, the caller can pass shouldPin == false to avoid
1035 * unnecessary RPC's if the shortcut is already pinned.
1036 */
1037 private static void incrementPinnedShortcutCount(ShortcutKey pinnedShortcut, boolean shouldPin) {
1038 synchronized (sBgLock) {
1039 MutableInt count = sBgPinnedShortcutCounts.get(pinnedShortcut);
1040 if (count == null) {
1041 count = new MutableInt(1);
1042 sBgPinnedShortcutCounts.put(pinnedShortcut, count);
1043 } else {
1044 count.value++;
1045 }
1046 if (shouldPin && count.value == 1) {
1047 LauncherAppState.getInstance().getShortcutManager().pinShortcut(pinnedShortcut);
1048 }
1049 }
1050 }
1051
1052 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001053 * Update the order of the workspace screens in the database. The array list contains
1054 * a list of screen ids in the order that they should appear.
1055 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001056 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001057 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001058 final ContentResolver cr = context.getContentResolver();
1059 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1060
1061 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001062 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001063 while (iter.hasNext()) {
1064 long id = iter.next();
1065 if (id < 0) {
1066 iter.remove();
1067 }
1068 }
1069
1070 Runnable r = new Runnable() {
1071 @Override
1072 public void run() {
Yura085c8532014-02-11 15:15:29 +00001073 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001074 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001075 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001076 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001077 for (int i = 0; i < count; i++) {
1078 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001079 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001080 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1081 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001082 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001083 }
Yura085c8532014-02-11 15:15:29 +00001084
1085 try {
1086 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1087 } catch (Exception ex) {
1088 throw new RuntimeException(ex);
1089 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001090
Winson Chungba9c37f2013-08-30 14:11:37 -07001091 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001092 sBgWorkspaceScreens.clear();
1093 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001094 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001095 }
1096 };
1097 runOnWorkerThread(r);
1098 }
1099
1100 /**
Winsonc0b52fe2015-09-09 16:38:15 -07001101 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001102 */
Winsonc0b52fe2015-09-09 16:38:15 -07001103 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001104 final ContentResolver cr = context.getContentResolver();
1105
Michael Jurkac9d95c52011-08-29 14:03:34 -07001106 Runnable r = new Runnable() {
1107 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001108 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001109 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001110 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001111 sBgItemsIdMap.remove(info.id);
1112 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001113 sBgWorkspaceItems.remove(info);
1114 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001115
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001116 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001117 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001118 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001119 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001120 for (ItemInfo childInfo : info.contents) {
1121 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001122 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001123 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001124 }
1125 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001126 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001127 }
1128
1129 /**
1130 * Set this as the current Launcher activity object for the loader.
1131 */
1132 public void initialize(Callbacks callbacks) {
1133 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001134 Preconditions.assertUIThread();
1135 // Remove any queued UI runnables
1136 mHandler.cancelAll();
1137 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001138 }
1139 }
1140
Kenny Guyed131872014-04-30 03:02:21 +01001141 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001142 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001143 int op = PackageUpdatedTask.OP_UPDATE;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001144 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001145 user));
1146 }
1147
1148 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001149 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001150 onPackagesRemoved(user, packageName);
1151 }
1152
1153 public void onPackagesRemoved(UserHandleCompat user, String... packages) {
Kenny Guyed131872014-04-30 03:02:21 +01001154 int op = PackageUpdatedTask.OP_REMOVE;
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001155 enqueueItemUpdatedTask(new PackageUpdatedTask(op, packages, user));
Kenny Guyed131872014-04-30 03:02:21 +01001156 }
1157
1158 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001159 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001160 int op = PackageUpdatedTask.OP_ADD;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001161 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001162 user));
1163 }
1164
1165 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001166 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001167 boolean replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001168 enqueueItemUpdatedTask(
Sunny Goyal144154d2016-03-30 16:47:03 -07001169 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, packageNames, user));
Kenny Guyed131872014-04-30 03:02:21 +01001170 }
1171
1172 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001173 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001174 boolean replacing) {
1175 if (!replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001176 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guyed131872014-04-30 03:02:21 +01001177 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1178 user));
1179 }
Kenny Guyed131872014-04-30 03:02:21 +01001180 }
1181
Kenny Guy44cba692016-01-21 19:50:02 +00001182 @Override
1183 public void onPackagesSuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001184 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001185 PackageUpdatedTask.OP_SUSPEND, packageNames,
1186 user));
1187 }
1188
1189 @Override
1190 public void onPackagesUnsuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001191 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001192 PackageUpdatedTask.OP_UNSUSPEND, packageNames,
1193 user));
1194 }
1195
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001196 @Override
1197 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
1198 UserHandleCompat user) {
Sunny Goyal50941fb2016-08-04 12:03:52 -07001199 enqueueItemUpdatedTask(new ShortcutsChangedTask(packageName, shortcuts, user, true));
1200 }
1201
1202 public void updatePinnedShortcuts(String packageName, List<ShortcutInfoCompat> shortcuts,
1203 UserHandleCompat user) {
1204 enqueueItemUpdatedTask(new ShortcutsChangedTask(packageName, shortcuts, user, false));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001205 }
1206
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001207 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001208 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1209 * ACTION_PACKAGE_CHANGED.
1210 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001211 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001212 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001213 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001214
Joe Onorato36115782010-06-17 13:28:48 -04001215 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001216 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001217 // If we have changed locale we need to clear out the labels in all apps/workspace.
1218 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001219 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1220 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001221 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001222 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001223 } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1224 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
1225 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyalda891c12016-03-18 18:29:24 -07001226 UserHandleCompat user = UserHandleCompat.fromIntent(intent);
1227 if (user != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001228 if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1229 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
1230 enqueueItemUpdatedTask(new PackageUpdatedTask(
1231 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE,
1232 new String[0], user));
1233 }
1234
1235 // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so
1236 // we need to run the state change task again.
1237 if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
1238 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
1239 enqueueItemUpdatedTask(new UserLockStateChangedTask(user));
1240 }
Sunny Goyalda891c12016-03-18 18:29:24 -07001241 }
Tony Wickham827cef22016-03-17 15:39:39 -07001242 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
1243 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001244 }
1245 }
1246
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001247 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001248 resetLoadedState(true, true);
1249
Reena Lee93f824a2011-09-23 17:20:28 -07001250 // Do this here because if the launcher activity is running it will be restarted.
1251 // If it's not running startLoaderFromBackground will merely tell it that it needs
1252 // to reload.
1253 startLoaderFromBackground();
1254 }
1255
Winson Chungf0c6ae02012-03-21 16:10:31 -07001256 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1257 synchronized (mLock) {
1258 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1259 // mWorkspaceLoaded to true later
1260 stopLoaderLocked();
1261 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1262 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001263 // Always reset deep shortcuts loaded.
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001264 // TODO: why?
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001265 mDeepShortcutsLoaded = false;
Winson Chungf0c6ae02012-03-21 16:10:31 -07001266 }
1267 }
1268
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001269 /**
1270 * When the launcher is in the background, it's possible for it to miss paired
1271 * configuration changes. So whenever we trigger the loader from the background
1272 * tell the launcher that it needs to re-run the loader when it comes back instead
1273 * of doing it now.
1274 */
1275 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001276 Callbacks callbacks = getCallback();
1277 if (callbacks != null) {
1278 // Only actually run the loader if they're not paused.
1279 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -07001280 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001281 }
1282 }
Joe Onorato36115782010-06-17 13:28:48 -04001283 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001284
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001285 /**
1286 * If there is already a loader task running, tell it to stop.
1287 */
1288 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001289 LoaderTask oldTask = mLoaderTask;
1290 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001291 oldTask.stopLocked();
1292 }
Reena Lee93f824a2011-09-23 17:20:28 -07001293 }
1294
Adam Cohen1a85c582014-09-30 09:48:49 -07001295 public boolean isCurrentCallbacks(Callbacks callbacks) {
1296 return (mCallbacks != null && mCallbacks.get() == callbacks);
1297 }
1298
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001299 /**
1300 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
1301 * @return true if the page could be bound synchronously.
1302 */
1303 public boolean startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001304 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1305 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001306 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -04001307 // Don't bother to start the thread if we know it's not going to do anything
1308 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001309 final Callbacks oldCallbacks = mCallbacks.get();
1310 // Clear any pending bind-runnables from the synchronized load process.
1311 runOnMainThread(new Runnable() {
1312 public void run() {
1313 oldCallbacks.clearPendingBinds();
1314 }
1315 });
1316
Joe Onorato36115782010-06-17 13:28:48 -04001317 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001318 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001319 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001320 // TODO: mDeepShortcutsLoaded does not need to be true for synchronous bind.
Tony Wickham80f57872016-06-29 18:12:15 -07001321 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE && mAllAppsLoaded
1322 && mWorkspaceLoaded && mDeepShortcutsLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001323 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001324 return true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001325 } else {
1326 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1327 sWorker.post(mLoaderTask);
1328 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001329 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001330 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001331 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001332 }
1333
Joe Onorato36115782010-06-17 13:28:48 -04001334 public void stopLoader() {
1335 synchronized (mLock) {
1336 if (mLoaderTask != null) {
1337 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001338 }
1339 }
Joe Onorato36115782010-06-17 13:28:48 -04001340 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001341
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001342 /**
1343 * Loads the workspace screen ids in an ordered list.
1344 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001345 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001346 final ContentResolver contentResolver = context.getContentResolver();
1347 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001348
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001349 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001350 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
1351 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -07001352 }
1353
Joe Onorato36115782010-06-17 13:28:48 -04001354 /**
1355 * Runnable for the thread that loads the contents of the launcher:
1356 * - workspace icons
1357 * - widgets
1358 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001359 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -04001360 */
1361 private class LoaderTask implements Runnable {
1362 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001363 private int mPageToBindFirst;
1364
Adam Cohen091440a2015-03-18 14:16:05 -07001365 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001366 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001367 @Thunk boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001368
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001369 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -04001370 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001371 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001372 }
1373
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001374 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001375 mIsLoadingAndBindingWorkspace = true;
1376
Joe Onorato36115782010-06-17 13:28:48 -04001377 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001378 if (DEBUG_LOADERS) {
1379 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001380 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001381
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001382 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001383 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001384 synchronized (LoaderTask.this) {
1385 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001386 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001387 }
1388 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001389 }
1390 }
1391
Joe Onorato36115782010-06-17 13:28:48 -04001392 // Bind the workspace
Sunny Goyal93f878c2016-03-30 17:31:24 -07001393 bindWorkspace(mPageToBindFirst);
Joe Onorato36115782010-06-17 13:28:48 -04001394 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001395
Joe Onorato36115782010-06-17 13:28:48 -04001396 private void waitForIdle() {
1397 // Wait until the either we're stopped or the other threads are done.
1398 // This way we don't start loading all apps until the workspace has settled
1399 // down.
1400 synchronized (LoaderTask.this) {
1401 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001402
Joe Onorato36115782010-06-17 13:28:48 -04001403 mHandler.postIdle(new Runnable() {
1404 public void run() {
1405 synchronized (LoaderTask.this) {
1406 mLoadAndBindStepFinished = true;
1407 if (DEBUG_LOADERS) {
1408 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001409 }
Joe Onorato36115782010-06-17 13:28:48 -04001410 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001411 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001412 }
Joe Onorato36115782010-06-17 13:28:48 -04001413 });
1414
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001415 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001416 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001417 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1418 // wait no longer than 1sec at a time
1419 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001420 } catch (InterruptedException ex) {
1421 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001422 }
1423 }
Joe Onorato36115782010-06-17 13:28:48 -04001424 if (DEBUG_LOADERS) {
1425 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001426 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001427 + "ms for previous step to finish binding");
1428 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001429 }
Joe Onorato36115782010-06-17 13:28:48 -04001430 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001431
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001432 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001433 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001434 // Ensure that we have a valid page index to load synchronously
1435 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1436 "valid page index");
1437 }
1438 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1439 // Ensure that we don't try and bind a specified page when the pages have not been
1440 // loaded already (we should load everything asynchronously in that case)
1441 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1442 }
1443 synchronized (mLock) {
1444 if (mIsLoaderTaskRunning) {
1445 // Ensure that we are never running the background loading at this point since
1446 // we also touch the background collections
1447 throw new RuntimeException("Error! Background loading is already running");
1448 }
1449 }
1450
1451 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1452 // data structures, we can't allow any other thread to touch that data, but because
1453 // this call is synchronous, we can get away with not locking).
1454
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001455 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001456 // operations from the previous activity. We need to ensure that all queued operations
1457 // are executed before any synchronous binding work is done.
1458 mHandler.flush();
1459
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001460 // Divide the set of loaded items into those that we are binding synchronously, and
1461 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001462 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001463 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1464 // arise from that.
1465 onlyBindAllApps();
Tony Wickham80f57872016-06-29 18:12:15 -07001466
1467 bindDeepShortcuts();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001468 }
1469
Joe Onorato36115782010-06-17 13:28:48 -04001470 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001471 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001472 if (mStopped) {
1473 return;
1474 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001475 mIsLoaderTaskRunning = true;
1476 }
Joe Onorato36115782010-06-17 13:28:48 -04001477 // Optimize for end-user experience: if the Launcher is up and // running with the
1478 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1479 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001480 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001481 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001482 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001483
Joe Onorato36115782010-06-17 13:28:48 -04001484 if (mStopped) {
1485 break keep_running;
1486 }
1487
Joe Onorato36115782010-06-17 13:28:48 -04001488 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001489
1490 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001491 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1492 loadAndBindAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001493
1494 waitForIdle();
1495
1496 // third step
1497 if (DEBUG_LOADERS) Log.d(TAG, "step 3: loading deep shortcuts");
1498 loadAndBindDeepShortcuts();
Joe Onorato36115782010-06-17 13:28:48 -04001499 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001500
Joe Onorato36115782010-06-17 13:28:48 -04001501 // Clear out this reference, otherwise we end up holding it until all of the
1502 // callback runnables are done.
1503 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001504
Joe Onorato36115782010-06-17 13:28:48 -04001505 synchronized (mLock) {
1506 // If we are still the last one to be scheduled, remove ourselves.
1507 if (mLoaderTask == this) {
1508 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001509 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001510 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001511 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001512 }
Joe Onorato36115782010-06-17 13:28:48 -04001513 }
1514
1515 public void stopLocked() {
1516 synchronized (LoaderTask.this) {
1517 mStopped = true;
1518 this.notify();
1519 }
1520 }
1521
1522 /**
1523 * Gets the callbacks object. If we've been stopped, or if the launcher object
1524 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1525 * object that was around when the deferred message was scheduled, and if there's
1526 * a new Callbacks object around then also return null. This will save us from
1527 * calling onto it with data that will be ignored.
1528 */
1529 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1530 synchronized (mLock) {
1531 if (mStopped) {
1532 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001533 }
Joe Onorato36115782010-06-17 13:28:48 -04001534
1535 if (mCallbacks == null) {
1536 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001537 }
Joe Onorato36115782010-06-17 13:28:48 -04001538
1539 final Callbacks callbacks = mCallbacks.get();
1540 if (callbacks != oldCallbacks) {
1541 return null;
1542 }
1543 if (callbacks == null) {
1544 Log.w(TAG, "no mCallbacks");
1545 return null;
1546 }
1547
1548 return callbacks;
1549 }
1550 }
1551
1552 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001553 private boolean checkItemPlacement(LongArrayMap<GridOccupancy> occupied, ItemInfo item,
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001554 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001555 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001556 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung892c74d2013-08-22 16:15:50 -07001557
Adam Cohendcd297f2013-06-18 13:13:40 -07001558 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001559 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001560 // Return early if we detect that an item is under the hotseat button
Sunny Goyalbb011da2016-06-15 15:42:29 -07001561 if (!FeatureFlags.NO_ALL_APPS_ICON &&
1562 profile.isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001563 Log.e(TAG, "Error loading shortcut into hotseat " + item
1564 + " into position (" + item.screenId + ":" + item.cellX + ","
1565 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001566 return false;
1567 }
1568
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001569 final GridOccupancy hotseatOccupancy =
Dan Sandler295ae182013-12-10 16:05:47 -05001570 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1571
Adam Cohen2e6da152015-05-06 11:42:25 -07001572 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001573 Log.e(TAG, "Error loading shortcut " + item
1574 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001575 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001576 + ")");
1577 return false;
1578 }
1579
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001580 if (hotseatOccupancy != null) {
1581 if (hotseatOccupancy.cells[(int) item.screenId][0]) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001582 Log.e(TAG, "Error loading shortcut into hotseat " + item
1583 + " into position (" + item.screenId + ":" + item.cellX + ","
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001584 + item.cellY + ") already occupied");
Adam Cohendcd297f2013-06-18 13:13:40 -07001585 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001586 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001587 hotseatOccupancy.cells[(int) item.screenId][0] = true;
Dan Sandler295ae182013-12-10 16:05:47 -05001588 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001589 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001590 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001591 final GridOccupancy occupancy = new GridOccupancy(profile.numHotseatIcons, 1);
1592 occupancy.cells[(int) item.screenId][0] = true;
1593 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, occupancy);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001594 return true;
1595 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001596 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1597 if (!workspaceScreens.contains((Long) item.screenId)) {
1598 // The item has an invalid screen id.
1599 return false;
1600 }
1601 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001602 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001603 return true;
1604 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001605
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001606 final int countX = profile.numColumns;
1607 final int countY = profile.numRows;
Adam Cohenae4409d2013-11-26 10:34:59 -08001608 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1609 item.cellX < 0 || item.cellY < 0 ||
1610 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1611 Log.e(TAG, "Error loading shortcut " + item
1612 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1613 + item.cellX + "," + item.cellY
1614 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1615 return false;
1616 }
1617
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001618 if (!occupied.containsKey(item.screenId)) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001619 GridOccupancy screen = new GridOccupancy(countX + 1, countY + 1);
1620 if (item.screenId == Workspace.FIRST_SCREEN_ID) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001621 // Mark the first row as occupied (if the feature is enabled)
1622 // in order to account for the QSB.
1623 screen.markCells(0, 0, countX + 1, 1, FeatureFlags.QSB_ON_FIRST_SCREEN);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001624 }
1625 occupied.put(item.screenId, screen);
Joe Onorato36115782010-06-17 13:28:48 -04001626 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001627 final GridOccupancy occupancy = occupied.get(item.screenId);
Winson Chungf30ad5f2011-08-08 10:55:42 -07001628
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001629 // Check if any workspace icons overlap with each other
1630 if (occupancy.isRegionVacant(item.cellX, item.cellY, item.spanX, item.spanY)) {
1631 occupancy.markCells(item, true);
1632 return true;
1633 } else {
1634 Log.e(TAG, "Error loading shortcut " + item
1635 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1636 + item.cellX + "," + item.cellX + "," + item.spanX + "," + item.spanY
1637 + ") already occupied");
1638 return false;
1639 }
Joe Onorato36115782010-06-17 13:28:48 -04001640 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001641
Winson Chungba9c37f2013-08-30 14:11:37 -07001642 /** Clears all the sBg data structures */
1643 private void clearSBgDataStructures() {
1644 synchronized (sBgLock) {
1645 sBgWorkspaceItems.clear();
1646 sBgAppWidgets.clear();
1647 sBgFolders.clear();
1648 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001649 sBgWorkspaceScreens.clear();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001650 sBgPinnedShortcutCounts.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001651 }
1652 }
1653
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001654 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -07001655 if (LauncherAppState.PROFILE_STARTUP) {
1656 Trace.beginSection("Loading Workspace");
1657 }
Joe Onorato36115782010-06-17 13:28:48 -04001658 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001659
Joe Onorato36115782010-06-17 13:28:48 -04001660 final Context context = mContext;
1661 final ContentResolver contentResolver = context.getContentResolver();
1662 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001663 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001664 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001665 final boolean isSdCardReady = Utilities.isBootCompleted();
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001666 final MultiHashMap<UserHandleCompat, String> pendingPackages = new MultiHashMap<>();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001667
Winson Chung892c74d2013-08-22 16:15:50 -07001668 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001669 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001670 int countX = profile.numColumns;
1671 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001672
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001673 boolean clearDb = false;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -07001674 try {
1675 ImportDataTask.performImportIfPossible(context);
1676 } catch (Exception e) {
1677 // Migration failed. Clear workspace.
1678 clearDb = true;
1679 }
1680
1681 if (!clearDb && GridSizeMigrationTask.ENABLED &&
Sunny Goyalf076eae2016-01-11 12:25:10 -08001682 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1683 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001684 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001685 }
1686
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001687 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -07001688 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001689 LauncherSettings.Settings.call(contentResolver,
1690 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001691 }
1692
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001693 Log.d(TAG, "loadWorkspace: loading default favorites");
1694 LauncherSettings.Settings.call(contentResolver,
1695 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -07001696
Winson Chung2abf94d2012-07-18 18:16:38 -07001697 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001698 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001699 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001700 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001701 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001702
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001703 final ArrayList<Long> itemsToRemove = new ArrayList<>();
1704 final ArrayList<Long> restoredRows = new ArrayList<>();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001705 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001706 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001707 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001708 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001709
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001710 // +1 for the hotseat (it can be larger than the workspace)
1711 // Load workspace in reverse order to ensure that latest items are loaded first (and
1712 // before any earlier duplicates)
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001713 final LongArrayMap<GridOccupancy> occupied = new LongArrayMap<>();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001714 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001715
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001716 try {
1717 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1718 final int intentIndex = c.getColumnIndexOrThrow
1719 (LauncherSettings.Favorites.INTENT);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001720 final int containerIndex = c.getColumnIndexOrThrow(
1721 LauncherSettings.Favorites.CONTAINER);
1722 final int itemTypeIndex = c.getColumnIndexOrThrow(
1723 LauncherSettings.Favorites.ITEM_TYPE);
1724 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1725 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001726 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1727 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001728 final int screenIndex = c.getColumnIndexOrThrow(
1729 LauncherSettings.Favorites.SCREEN);
1730 final int cellXIndex = c.getColumnIndexOrThrow
1731 (LauncherSettings.Favorites.CELLX);
1732 final int cellYIndex = c.getColumnIndexOrThrow
1733 (LauncherSettings.Favorites.CELLY);
1734 final int spanXIndex = c.getColumnIndexOrThrow
1735 (LauncherSettings.Favorites.SPANX);
1736 final int spanYIndex = c.getColumnIndexOrThrow(
1737 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001738 final int rankIndex = c.getColumnIndexOrThrow(
1739 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001740 final int restoredIndex = c.getColumnIndexOrThrow(
1741 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001742 final int profileIdIndex = c.getColumnIndexOrThrow(
1743 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001744 final int optionsIndex = c.getColumnIndexOrThrow(
1745 LauncherSettings.Favorites.OPTIONS);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001746 final CursorIconInfo cursorIconInfo = new CursorIconInfo(mContext, c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001747
Sunny Goyal7f834d22015-04-21 10:10:23 -07001748 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
Kenny Guyff05f432016-01-22 17:48:29 +00001749 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001750 final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>();
Sunny Goyal7f834d22015-04-21 10:10:23 -07001751 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001752 long serialNo = mUserManager.getSerialNumberForUser(user);
1753 allUsers.put(serialNo, user);
1754 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001755
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001756 boolean userUnlocked = mUserManager.isUserUnlocked(user);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001757
1758 // We can only query for shortcuts when the user is unlocked.
1759 if (userUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07001760 List<ShortcutInfoCompat> pinnedShortcuts =
1761 mDeepShortcutManager.queryForPinnedShortcuts(null, user);
1762 if (mDeepShortcutManager.wasLastCallSuccess()) {
1763 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
1764 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
1765 shortcut);
1766 }
1767 } else {
1768 // Shortcut manager can fail due to some race condition when the
1769 // lock state changes too frequently. For the purpose of the loading
1770 // shortcuts, consider the user is still locked.
1771 userUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001772 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001773 }
Sunny Goyal49f4f032016-08-01 15:45:49 -07001774 unlockedUsers.put(serialNo, userUnlocked);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001775 }
1776
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001777 ShortcutInfo info;
1778 String intentDescription;
1779 LauncherAppWidgetInfo appWidgetInfo;
1780 int container;
1781 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001782 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001783 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001784 UserHandleCompat user;
Sunny Goyald09c3702016-04-06 16:18:20 -07001785 String targetPackage;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001786
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001787 while (!mStopped && c.moveToNext()) {
1788 try {
1789 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001790 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001791 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001792 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001793
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001794 switch (itemType) {
1795 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1796 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001797 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001798 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001799 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001800 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001801 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001802 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001803 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001804 boolean itemReplaced = false;
Sunny Goyald09c3702016-04-06 16:18:20 -07001805 targetPackage = null;
Kenny Guyed131872014-04-30 03:02:21 +01001806 if (user == null) {
1807 // User has been deleted remove the item.
1808 itemsToRemove.add(id);
1809 continue;
1810 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001811 try {
1812 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001813 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001814 if (cn != null && cn.getPackageName() != null) {
1815 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1816 cn.getPackageName(), user);
1817 boolean validComponent = validPkg &&
1818 launcherApps.isActivityEnabledForProfile(cn, user);
Sunny Goyald09c3702016-04-06 16:18:20 -07001819 if (validPkg) {
1820 targetPackage = cn.getPackageName();
1821 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001822
1823 if (validComponent) {
1824 if (restored) {
1825 // no special handling necessary for this item
1826 restoredRows.add(id);
1827 restored = false;
1828 }
Kenny Guyff05f432016-01-22 17:48:29 +00001829 if (quietMode.get(serialNumber)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001830 disabledState = ShortcutInfo.FLAG_DISABLED_QUIET_USER;
Kenny Guyff05f432016-01-22 17:48:29 +00001831 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001832 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001833 intent = null;
1834 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1835 // We allow auto install apps to have their intent
1836 // updated after an install.
1837 intent = manager.getLaunchIntentForPackage(
1838 cn.getPackageName());
1839 if (intent != null) {
1840 ContentValues values = new ContentValues();
1841 values.put(LauncherSettings.Favorites.INTENT,
1842 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001843 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001844 }
1845 }
1846
1847 if (intent == null) {
1848 // The app is installed but the component is no
1849 // longer available.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001850 FileLog.d(TAG, "Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001851 itemsToRemove.add(id);
1852 continue;
1853 } else {
1854 // no special handling necessary for this item
1855 restoredRows.add(id);
1856 restored = false;
1857 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001858 } else if (restored) {
1859 // Package is not yet available but might be
1860 // installed later.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001861 FileLog.d(TAG, "package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001862
1863 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1864 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001865 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001866 // App restore has started. Update the flag
1867 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1868 ContentValues values = new ContentValues();
1869 values.put(LauncherSettings.Favorites.RESTORED,
1870 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001871 updateItem(id, values);
1872 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1873 // This is a common app. Try to replace this.
1874 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1875 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1876 if (parser.findDefaultApp()) {
1877 // Default app found. Replace it.
1878 intent = parser.parsedIntent;
1879 cn = intent.getComponent();
1880 ContentValues values = parser.parsedValues;
1881 values.put(LauncherSettings.Favorites.RESTORED, 0);
1882 updateItem(id, values);
1883 restored = false;
1884 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001885
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001886 } else {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001887 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001888 itemsToRemove.add(id);
1889 continue;
1890 }
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001891 } else {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001892 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001893 itemsToRemove.add(id);
1894 continue;
1895 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001896 } else if (PackageManagerHelper.isAppOnSdcard(
1897 manager, cn.getPackageName())) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07001898 // Package is present but not available.
1899 allowMissingTarget = true;
1900 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1901 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001902 // SdCard is not ready yet. Package might get available,
1903 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001904 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalc2936bc2016-09-01 15:50:36 -07001905 pendingPackages.addToList(user, cn.getPackageName());
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001906 allowMissingTarget = true;
1907 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001908
1909 } else {
1910 // Do not wait for external media load anymore.
1911 // Log the invalid package, and remove it
Sunny Goyal713edfc2016-05-06 09:58:34 -07001912 FileLog.d(TAG, "Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001913 itemsToRemove.add(id);
1914 continue;
Winson Chungee055712013-07-30 14:46:24 -07001915 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001916 } else if (cn == null) {
1917 // For shortcuts with no component, keep them as they are
1918 restoredRows.add(id);
1919 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001920 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001921 } catch (URISyntaxException e) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001922 FileLog.d(TAG, "Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001923 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001924 continue;
1925 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001926
Sunny Goyal34b65272015-03-11 16:56:52 -07001927 boolean useLowResIcon = container >= 0 &&
1928 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1929
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001930 if (itemReplaced) {
1931 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001932 info = getAppShortcutInfo(intent, user, null,
1933 cursorIconInfo, false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001934 } else {
1935 // Don't replace items for other profiles.
1936 itemsToRemove.add(id);
1937 continue;
1938 }
1939 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001940 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001941 info = getRestoredItemInfo(c, intent,
1942 promiseType, itemType, cursorIconInfo);
Kenny Guyed131872014-04-30 03:02:21 +01001943 intent = getRestoredItemIntent(c, context, intent);
1944 } else {
1945 // Don't restore items for other profiles.
1946 itemsToRemove.add(id);
1947 continue;
1948 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001949 } else if (itemType ==
1950 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001951 info = getAppShortcutInfo(intent, user, c,
1952 cursorIconInfo, allowMissingTarget, useLowResIcon);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001953 } else if (itemType ==
1954 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001955
1956 ShortcutKey key = ShortcutKey.fromIntent(intent, user);
1957 if (unlockedUsers.get(serialNumber)) {
1958 ShortcutInfoCompat pinnedShortcut =
1959 shortcutKeyToPinnedShortcuts.get(key);
1960 if (pinnedShortcut == null) {
1961 // The shortcut is no longer valid.
1962 itemsToRemove.add(id);
1963 continue;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001964 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001965 info = new ShortcutInfo(pinnedShortcut, context);
1966 intent = info.intent;
1967 } else {
1968 // Create a shortcut info in disabled mode for now.
1969 info = new ShortcutInfo();
1970 info.user = user;
1971 info.itemType = itemType;
1972 loadInfoFromCursor(info, c, cursorIconInfo);
1973
1974 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001975 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001976 incrementPinnedShortcutCount(key, false /* shouldPin */);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001977 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001978 info = getShortcutInfo(c, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001979
Sunny Goyald09c3702016-04-06 16:18:20 -07001980 // Shortcuts are only available on the primary profile
1981 if (PackageManagerHelper.isAppSuspended(manager, targetPackage)) {
1982 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1983 }
1984
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001985 // App shortcuts that used to be automatically added to Launcher
1986 // didn't always have the correct intent flags set, so do that
1987 // here
1988 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001989 intent.getCategories() != null &&
1990 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001991 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001992 intent.addFlags(
1993 Intent.FLAG_ACTIVITY_NEW_TASK |
1994 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1995 }
Michael Jurka96879562012-03-22 05:54:33 -07001996 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001997
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001998 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001999 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002000 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002001 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002002 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002003 info.cellX = c.getInt(cellXIndex);
2004 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08002005 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002006 info.spanX = 1;
2007 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002008 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002009 if (info.promisedIntent != null) {
2010 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
2011 }
Sunny Goyald09c3702016-04-06 16:18:20 -07002012 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002013 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2014 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2015 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002016
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002017 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002018 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002019 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002020 break;
2021 }
2022
Sunny Goyal756adbc2015-04-16 15:20:51 -07002023 if (restored) {
2024 ComponentName cn = info.getTargetComponent();
2025 if (cn != null) {
2026 Integer progress = installingPkgs.get(cn.getPackageName());
2027 if (progress != null) {
2028 info.setInstallProgress(progress);
2029 } else {
2030 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2031 }
2032 }
2033 }
2034
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002035 switch (container) {
2036 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2037 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2038 sBgWorkspaceItems.add(info);
2039 break;
2040 default:
2041 // Item is in a user folder
2042 FolderInfo folderInfo =
2043 findOrMakeFolder(sBgFolders, container);
Sunny Goyalc52ba712016-04-05 15:59:05 -07002044 folderInfo.add(info, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002045 break;
2046 }
2047 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002048 } else {
2049 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002050 }
2051 break;
2052
2053 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2054 id = c.getLong(idIndex);
2055 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2056
Sunny Goyala508e4f2015-05-21 09:33:57 -07002057 // Do not trim the folder label, as is was set by the user.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002058 folderInfo.title = c.getString(cursorIconInfo.titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002059 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002060 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002061 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002062 folderInfo.cellX = c.getInt(cellXIndex);
2063 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002064 folderInfo.spanX = 1;
2065 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002066 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002067
Daniel Sandler8802e962010-05-26 16:28:16 -04002068 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002069 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002070 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002071 break;
2072 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002073
Joe Onorato9c1289c2009-08-17 11:03:03 -04002074 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002075 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2076 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2077 sBgWorkspaceItems.add(folderInfo);
2078 break;
Joe Onorato36115782010-06-17 13:28:48 -04002079 }
Joe Onorato17a89222011-02-08 17:26:11 -08002080
Chris Wrenf4d08112014-01-16 18:13:56 -05002081 if (restored) {
2082 // no special handling required for restored folders
2083 restoredRows.add(id);
2084 }
2085
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002086 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2087 sBgFolders.put(folderInfo.id, folderInfo);
2088 break;
2089
2090 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002091 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002092 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002093 boolean customWidget = itemType ==
2094 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2095
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002096 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002097 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002098 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002099 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002100 user = allUsers.get(serialNumber);
2101 if (user == null) {
2102 itemsToRemove.add(id);
2103 continue;
2104 }
2105
Sunny Goyalff572272014-07-23 13:58:07 -07002106 final ComponentName component =
2107 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002108
Sunny Goyal651077b2014-06-30 14:15:31 -07002109 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002110 final boolean isIdValid = (restoreStatus &
2111 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002112 final boolean wasProviderReady = (restoreStatus &
2113 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002114
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002115 if (widgetProvidersMap == null) {
2116 widgetProvidersMap = AppWidgetManagerCompat
2117 .getInstance(mContext).getAllProvidersMap();
2118 }
2119 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
2120 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00002121 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002122 user));
Sunny Goyalff572272014-07-23 13:58:07 -07002123
2124 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002125 if (!isSafeMode && !customWidget &&
2126 wasProviderReady && !isProviderReady) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002127 FileLog.d(TAG, "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07002128 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002129 itemsToRemove.add(id);
2130 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002131 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002132 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2133 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002134
Sunny Goyal53f96722015-07-13 19:54:53 -07002135 // The provider is available. So the widget is either
2136 // available or not available. We do not need to track
2137 // any future restore updates.
2138 int status = restoreStatus &
2139 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002140 if (!wasProviderReady) {
2141 // If provider was not previously ready, update the
2142 // status and UI flag.
2143
2144 // Id would be valid only if the widget restore broadcast was received.
2145 if (isIdValid) {
Sunny Goyal86df1382016-08-10 15:03:22 -07002146 status |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002147 } else {
2148 status &= ~LauncherAppWidgetInfo
2149 .FLAG_PROVIDER_NOT_READY;
2150 }
2151 }
2152 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002153 } else {
2154 Log.v(TAG, "Widget restore pending id=" + id
2155 + " appWidgetId=" + appWidgetId
2156 + " status =" + restoreStatus);
2157 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002158 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002159 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002160 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002161
2162 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2163 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002164 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002165 // App restore has started. Update the flag
2166 appWidgetInfo.restoreStatus |=
2167 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalb05a00a2016-08-29 10:06:57 -07002168 } else if (!isSafeMode) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002169 FileLog.d(TAG, "Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07002170 itemsToRemove.add(id);
2171 continue;
2172 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002173
2174 appWidgetInfo.installProgress =
2175 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002176 }
Sunny Goyal86df1382016-08-10 15:03:22 -07002177 if (appWidgetInfo.hasRestoreFlag(
2178 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG)) {
2179 intentDescription = c.getString(intentIndex);
2180 if (!TextUtils.isEmpty(intentDescription)) {
2181 appWidgetInfo.bindOptions =
2182 Intent.parseUri(intentDescription, 0);
2183 }
2184 }
Sunny Goyalff572272014-07-23 13:58:07 -07002185
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002186 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002187 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002188 appWidgetInfo.cellX = c.getInt(cellXIndex);
2189 appWidgetInfo.cellY = c.getInt(cellYIndex);
2190 appWidgetInfo.spanX = c.getInt(spanXIndex);
2191 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002192 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002193
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002194 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2195 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2196 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002197 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2198 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002199 continue;
2200 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002201
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002202 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002203 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002204 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002205 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002206 break;
2207 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002208
Adam Cohen59400422014-03-05 18:07:04 -08002209 if (!customWidget) {
2210 String providerName =
2211 appWidgetInfo.providerName.flattenToString();
2212 if (!providerName.equals(savedProvider) ||
2213 (appWidgetInfo.restoreStatus != restoreStatus)) {
2214 ContentValues values = new ContentValues();
2215 values.put(
2216 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2217 providerName);
2218 values.put(LauncherSettings.Favorites.RESTORED,
2219 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002220 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002221 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002222 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002223 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2224 sBgAppWidgets.add(appWidgetInfo);
2225 }
Joe Onorato36115782010-06-17 13:28:48 -04002226 break;
2227 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002228 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002229 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002230 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002231 }
2232 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002233 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002234 }
2235
Winson Chungba9c37f2013-08-30 14:11:37 -07002236 // Break early if we've stopped loading
2237 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002238 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002239 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002240 }
2241
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002242 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002243 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002244 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2245 Utilities.createDbSelectionQuery(
2246 LauncherSettings.Favorites._ID, itemsToRemove), null);
2247 if (DEBUG_LOADERS) {
2248 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2249 LauncherSettings.Favorites._ID, itemsToRemove));
2250 }
2251
2252 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07002253 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
2254 .call(contentResolver,
2255 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
2256 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
2257 for (long folderId : deletedFolderIds) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002258 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2259 sBgFolders.remove(folderId);
2260 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002261 }
2262 }
2263
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002264 // Unpin shortcuts that don't exist on the workspace.
2265 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
2266 MutableInt numTimesPinned = sBgPinnedShortcutCounts.get(key);
2267 if (numTimesPinned == null || numTimesPinned.value == 0) {
2268 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
2269 mDeepShortcutManager.unpinShortcut(key);
2270 }
2271 }
2272
Sunny Goyal317698b2015-07-29 11:45:41 -07002273 // Sort all the folder items and make sure the first 3 items are high resolution.
2274 for (FolderInfo folder : sBgFolders) {
2275 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2276 int pos = 0;
2277 for (ShortcutInfo info : folder.contents) {
2278 if (info.usingLowResIcon) {
2279 info.updateIcon(mIconCache, false);
2280 }
2281 pos ++;
2282 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2283 break;
2284 }
2285 }
2286 }
2287
Chris Wrenf4d08112014-01-16 18:13:56 -05002288 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002289 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002290 ContentValues values = new ContentValues();
2291 values.put(LauncherSettings.Favorites.RESTORED, 0);
2292 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2293 Utilities.createDbSelectionQuery(
2294 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002295 }
2296
Sunny Goyalc2936bc2016-09-01 15:50:36 -07002297 if (!isSdCardReady && !pendingPackages.isEmpty()) {
2298 context.registerReceiver(
2299 new SdCardAvailableReceiver(
2300 LauncherModel.this, mContext, pendingPackages),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002301 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalc2936bc2016-09-01 15:50:36 -07002302 null,
2303 sWorker);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002304 }
2305
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002306 // Remove any empty screens
2307 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002308 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002309 long screenId = item.screenId;
2310 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2311 unusedScreens.contains(screenId)) {
2312 unusedScreens.remove(screenId);
2313 }
2314 }
2315
2316 // If there are any empty screens remove them, and update.
2317 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002318 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002319 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002320 }
2321
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002322 if (DEBUG_LOADERS) {
2323 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2324 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002325 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002326 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002327 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002328
Sunny Goyale2df0622015-04-24 11:27:00 -07002329 for (int i = 0; i < nScreens; i++) {
2330 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002331 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002332 line += " | ";
2333 }
Joe Onorato36115782010-06-17 13:28:48 -04002334 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002335 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002336 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002337 }
Joe Onorato36115782010-06-17 13:28:48 -04002338 }
Sunny Goyale26d1002016-06-20 14:52:14 -07002339 if (LauncherAppState.PROFILE_STARTUP) {
2340 Trace.endSection();
2341 }
Adam Cohene25af792013-06-06 23:08:25 -07002342 }
2343
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002344 /**
2345 * Partially updates the item without any notification. Must be called on the worker thread.
2346 */
2347 private void updateItem(long itemId, ContentValues update) {
2348 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002349 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002350 update,
2351 BaseColumns._ID + "= ?",
2352 new String[]{Long.toString(itemId)});
2353 }
2354
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002355 /** Filters the set of items who are directly or indirectly (via another container) on the
2356 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002357 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002358 ArrayList<ItemInfo> allWorkspaceItems,
2359 ArrayList<ItemInfo> currentScreenItems,
2360 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002361 // Purge any null ItemInfos
2362 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2363 while (iter.hasNext()) {
2364 ItemInfo i = iter.next();
2365 if (i == null) {
2366 iter.remove();
2367 }
2368 }
2369
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002370 // Order the set of items by their containers first, this allows use to walk through the
2371 // list sequentially, build up a list of containers that are in the specified screen,
2372 // as well as all items in those containers.
2373 Set<Long> itemsOnScreen = new HashSet<Long>();
2374 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2375 @Override
2376 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07002377 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002378 }
2379 });
2380 for (ItemInfo info : allWorkspaceItems) {
2381 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002382 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002383 currentScreenItems.add(info);
2384 itemsOnScreen.add(info.id);
2385 } else {
2386 otherScreenItems.add(info);
2387 }
2388 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2389 currentScreenItems.add(info);
2390 itemsOnScreen.add(info.id);
2391 } else {
2392 if (itemsOnScreen.contains(info.container)) {
2393 currentScreenItems.add(info);
2394 itemsOnScreen.add(info.id);
2395 } else {
2396 otherScreenItems.add(info);
2397 }
2398 }
2399 }
2400 }
2401
2402 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002403 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002404 ArrayList<LauncherAppWidgetInfo> appWidgets,
2405 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2406 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002407
2408 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002409 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002410 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002411 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002412 currentScreenWidgets.add(widget);
2413 } else {
2414 otherScreenWidgets.add(widget);
2415 }
2416 }
2417 }
2418
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002419 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2420 * right) */
2421 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002422 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002423 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002424 final int screenCols = profile.numColumns;
2425 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002426 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002427 @Override
2428 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002429 if (lhs.container == rhs.container) {
2430 // Within containers, order by their spatial position in that container
2431 switch ((int) lhs.container) {
2432 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2433 long lr = (lhs.screenId * screenCellCount +
2434 lhs.cellY * screenCols + lhs.cellX);
2435 long rr = (rhs.screenId * screenCellCount +
2436 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07002437 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07002438 }
2439 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2440 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07002441 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07002442 }
2443 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002444 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002445 throw new RuntimeException("Unexpected container type when " +
2446 "sorting workspace items.");
2447 }
2448 return 0;
2449 }
2450 } else {
2451 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07002452 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07002453 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002454 }
2455 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002456 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002457
Adam Cohendcd297f2013-06-18 13:13:40 -07002458 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2459 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002460 final Runnable r = new Runnable() {
2461 @Override
2462 public void run() {
2463 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2464 if (callbacks != null) {
2465 callbacks.bindScreens(orderedScreens);
2466 }
2467 }
2468 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002469 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002470 }
2471
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002472 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2473 final ArrayList<ItemInfo> workspaceItems,
2474 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002475 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002476
2477 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002478 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002479 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002480 final int start = i;
2481 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002482 final Runnable r = new Runnable() {
2483 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002484 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002485 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002486 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002487 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2488 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002489 }
2490 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002491 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002492 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002493 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002494
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002495 // Bind the widgets, one at a time
2496 N = appWidgets.size();
2497 for (int i = 0; i < N; i++) {
2498 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2499 final Runnable r = new Runnable() {
2500 public void run() {
2501 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2502 if (callbacks != null) {
2503 callbacks.bindAppWidget(widget);
2504 }
2505 }
2506 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002507 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002508 }
2509 }
2510
2511 /**
2512 * Binds all loaded data to actual views on the main thread.
2513 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002514 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002515 final long t = SystemClock.uptimeMillis();
2516 Runnable r;
2517
2518 // Don't use these two variables in any of the callback runnables.
2519 // Otherwise we hold a reference to them.
2520 final Callbacks oldCallbacks = mCallbacks.get();
2521 if (oldCallbacks == null) {
2522 // This launcher has exited and nobody bothered to tell us. Just bail.
2523 Log.w(TAG, "LoaderTask running with no launcher");
2524 return;
2525 }
2526
Winson Chung9b9fb962013-11-15 15:39:34 -08002527 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07002528 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
2529 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
2530 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002531
Winson Chung2abf94d2012-07-18 18:16:38 -07002532 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002533 workspaceItems.addAll(sBgWorkspaceItems);
2534 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002535 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002536 }
2537
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002538 final int currentScreen;
2539 {
2540 int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE
2541 ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen();
2542 if (currScreen >= orderedScreenIds.size()) {
2543 // There may be no workspace screens (just hotseat items and an empty page).
2544 currScreen = PagedView.INVALID_RESTORE_PAGE;
2545 }
2546 currentScreen = currScreen;
Winson Chung9b9fb962013-11-15 15:39:34 -08002547 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002548 final boolean validFirstPage = currentScreen >= 0;
2549 final long currentScreenId =
2550 validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID;
Winson Chung9b9fb962013-11-15 15:39:34 -08002551
Winson Chung9b9fb962013-11-15 15:39:34 -08002552 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07002553 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
2554 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
2555 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
2556 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002557
Winson Chung9b9fb962013-11-15 15:39:34 -08002558 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002559 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002560 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002561 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002562 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2563 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2564
2565 // Tell the workspace that we're about to start binding items
2566 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002567 public void run() {
2568 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2569 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002570 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002571 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002572 }
2573 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002574 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002575 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002576
Adam Cohendcd297f2013-06-18 13:13:40 -07002577 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2578
Sunny Goyal527c7d32015-08-28 15:19:36 -07002579 Executor mainExecutor = new DeferredMainThreadExecutor();
2580 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07002581 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002582
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002583 // In case of validFirstPage, only bind the first screen, and defer binding the
2584 // remaining screens after first onDraw (and an optional the fade animation whichever
2585 // happens later).
2586 // This ensures that the first screen is immediately visible (eg. during rotation)
2587 // In case of !validFirstPage, bind all pages one after other.
2588 final Executor deferredExecutor =
2589 validFirstPage ? new ViewOnDrawExecutor(mHandler) : mainExecutor;
2590
2591 mainExecutor.execute(new Runnable() {
2592 @Override
2593 public void run() {
2594 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2595 if (callbacks != null) {
2596 callbacks.finishFirstPageBind(
2597 validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null);
2598 }
2599 }
2600 });
Sunny Goyal527c7d32015-08-28 15:19:36 -07002601
Sunny Goyal44c06432016-04-02 10:56:02 -07002602 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002603
2604 // Tell the workspace that we're done binding items
2605 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002606 public void run() {
2607 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2608 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002609 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002610 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002611
Sunny Goyal639e9062015-08-19 19:17:06 -07002612 mIsLoadingAndBindingWorkspace = false;
2613
2614 // Run all the bind complete runnables after workspace is bound.
2615 if (!mBindCompleteRunnables.isEmpty()) {
2616 synchronized (mBindCompleteRunnables) {
2617 for (final Runnable r : mBindCompleteRunnables) {
2618 runOnWorkerThread(r);
2619 }
2620 mBindCompleteRunnables.clear();
2621 }
2622 }
2623
Winson Chung98e030b2012-05-07 16:01:11 -07002624 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002625 if (DEBUG_LOADERS) {
2626 Log.d(TAG, "bound workspace in "
2627 + (SystemClock.uptimeMillis()-t) + "ms");
2628 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002629
Joe Onorato36115782010-06-17 13:28:48 -04002630 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002631 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002632 deferredExecutor.execute(r);
2633
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002634 if (validFirstPage) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002635 r = new Runnable() {
2636 public void run() {
2637 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2638 if (callbacks != null) {
2639 // We are loading synchronously, which means, some of the pages will be
2640 // bound after first draw. Inform the callbacks that page binding is
2641 // not complete, and schedule the remaining pages.
2642 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2643 callbacks.onPageBoundSynchronously(currentScreen);
2644 }
2645 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2646 }
2647 }
2648 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002649 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002650 }
Joe Onorato36115782010-06-17 13:28:48 -04002651 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002652
Joe Onorato36115782010-06-17 13:28:48 -04002653 private void loadAndBindAllApps() {
2654 if (DEBUG_LOADERS) {
2655 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2656 }
2657 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002658 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002659 synchronized (LoaderTask.this) {
2660 if (mStopped) {
2661 return;
2662 }
2663 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002664 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002665 synchronized (LoaderTask.this) {
2666 if (mStopped) {
2667 return;
2668 }
2669 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002670 }
Joe Onorato36115782010-06-17 13:28:48 -04002671 } else {
2672 onlyBindAllApps();
2673 }
2674 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002675
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002676 private void updateIconCache() {
2677 // Ignore packages which have a promise icon.
2678 HashSet<String> packagesToIgnore = new HashSet<>();
2679 synchronized (sBgLock) {
2680 for (ItemInfo info : sBgItemsIdMap) {
2681 if (info instanceof ShortcutInfo) {
2682 ShortcutInfo si = (ShortcutInfo) info;
2683 if (si.isPromise() && si.getTargetComponent() != null) {
2684 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2685 }
2686 } else if (info instanceof LauncherAppWidgetInfo) {
2687 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2688 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2689 packagesToIgnore.add(lawi.providerName.getPackageName());
2690 }
2691 }
2692 }
2693 }
2694 mIconCache.updateDbIcons(packagesToIgnore);
2695 }
2696
Joe Onorato36115782010-06-17 13:28:48 -04002697 private void onlyBindAllApps() {
2698 final Callbacks oldCallbacks = mCallbacks.get();
2699 if (oldCallbacks == null) {
2700 // This launcher has exited and nobody bothered to tell us. Just bail.
2701 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2702 return;
2703 }
2704
2705 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002706 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002707 final ArrayList<AppInfo> list
2708 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002709 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002710 public void run() {
2711 final long t = SystemClock.uptimeMillis();
2712 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2713 if (callbacks != null) {
2714 callbacks.bindAllApplications(list);
2715 }
2716 if (DEBUG_LOADERS) {
2717 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002718 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04002719 }
2720 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002721 };
Tony Wickham80f57872016-06-29 18:12:15 -07002722 runOnMainThread(r);
Joe Onorato36115782010-06-17 13:28:48 -04002723 }
2724
Winson Chung64359a52013-07-08 17:17:08 -07002725 private void loadAllApps() {
2726 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002727
Joe Onorato36115782010-06-17 13:28:48 -04002728 final Callbacks oldCallbacks = mCallbacks.get();
2729 if (oldCallbacks == null) {
2730 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002731 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002732 return;
2733 }
2734
Kenny Guyed131872014-04-30 03:02:21 +01002735 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2736
Winson Chung64359a52013-07-08 17:17:08 -07002737 // Clear the list of apps
2738 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002739 for (UserHandleCompat user : profiles) {
2740 // Query for the set of apps
2741 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002742 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002743 if (DEBUG_LOADERS) {
2744 Log.d(TAG, "getActivityList took "
2745 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2746 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2747 }
2748 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002749 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002750 if (apps == null || apps.isEmpty()) {
2751 return;
2752 }
Kenny Guyff05f432016-01-22 17:48:29 +00002753 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002754 // Create the ApplicationInfos
2755 for (int i = 0; i < apps.size(); i++) {
2756 LauncherActivityInfoCompat app = apps.get(i);
2757 // This builds the icon bitmaps.
Kenny Guyff05f432016-01-22 17:48:29 +00002758 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode));
Kenny Guyed131872014-04-30 03:02:21 +01002759 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002760
Sunny Goyal756a28a2015-04-23 17:07:55 -07002761 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2762 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002763 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002764
2765 @Override
2766 public void run() {
2767 heuristic.processUserApps(apps);
2768 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002769 };
2770 runOnMainThread(new Runnable() {
2771
2772 @Override
2773 public void run() {
2774 // Check isLoadingWorkspace on the UI thread, as it is updated on
2775 // the UI thread.
2776 if (mIsLoadingAndBindingWorkspace) {
2777 synchronized (mBindCompleteRunnables) {
2778 mBindCompleteRunnables.add(r);
2779 }
2780 } else {
2781 runOnWorkerThread(r);
2782 }
2783 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002784 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002785 }
Winson Chung64359a52013-07-08 17:17:08 -07002786 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002787 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002788 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2789 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002790
2791 // Post callback on main thread
2792 mHandler.post(new Runnable() {
2793 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002794
Winson Chung64359a52013-07-08 17:17:08 -07002795 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002796 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002797 if (callbacks != null) {
2798 callbacks.bindAllApplications(added);
2799 if (DEBUG_LOADERS) {
2800 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002801 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07002802 }
2803 } else {
2804 Log.i(TAG, "not binding apps: no Launcher activity");
2805 }
2806 }
2807 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002808 // Cleanup any data stored for a deleted user.
2809 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04002810 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002811 Log.d(TAG, "Icons processed in "
2812 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002813 }
2814 }
2815
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002816 private void loadAndBindDeepShortcuts() {
2817 if (DEBUG_LOADERS) {
2818 Log.d(TAG, "loadAndBindDeepShortcuts mDeepShortcutsLoaded=" + mDeepShortcutsLoaded);
2819 }
2820 if (!mDeepShortcutsLoaded) {
2821 mBgDeepShortcutMap.clear();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002822 mHasShortcutHostPermission = mDeepShortcutManager.hasHostPermission();
2823 if (mHasShortcutHostPermission) {
2824 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
2825 if (mUserManager.isUserUnlocked(user)) {
2826 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager
2827 .queryForAllShortcuts(user);
2828 updateDeepShortcutMap(null, user, shortcuts);
2829 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002830 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002831 }
2832 synchronized (LoaderTask.this) {
2833 if (mStopped) {
2834 return;
2835 }
2836 mDeepShortcutsLoaded = true;
2837 }
2838 }
Tony Wickham80f57872016-06-29 18:12:15 -07002839 bindDeepShortcuts();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002840 }
2841
Joe Onoratobe386092009-11-17 17:32:16 -08002842 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002843 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002844 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002845 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2846 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2847 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2848 }
Joe Onorato36115782010-06-17 13:28:48 -04002849 }
2850 }
2851
Sunny Goyal860538d2016-07-20 12:35:16 -07002852 /**
2853 * Clear all the shortcuts for the given package, and re-add the new shortcuts.
2854 */
2855 private void updateDeepShortcutMap(
2856 String packageName, UserHandleCompat user, List<ShortcutInfoCompat> shortcuts) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002857 if (packageName != null) {
2858 Iterator<ComponentKey> keysIter = mBgDeepShortcutMap.keySet().iterator();
2859 while (keysIter.hasNext()) {
Sunny Goyal860538d2016-07-20 12:35:16 -07002860 ComponentKey next = keysIter.next();
2861 if (next.componentName.getPackageName().equals(packageName)
2862 && next.user.equals(user)) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002863 keysIter.remove();
2864 }
2865 }
2866 }
2867
2868 // Now add the new shortcuts to the map.
2869 for (ShortcutInfoCompat shortcut : shortcuts) {
Tony Wickhamca258b32016-07-28 12:31:28 -07002870 boolean shouldShowInContainer = shortcut.isEnabled()
2871 && (shortcut.isDeclaredInManifest() || shortcut.isDynamic());
2872 if (shouldShowInContainer) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002873 ComponentKey targetComponent
2874 = new ComponentKey(shortcut.getActivity(), shortcut.getUserHandle());
2875 mBgDeepShortcutMap.addToList(targetComponent, shortcut.getId());
2876 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002877 }
2878 }
2879
Tony Wickham80f57872016-06-29 18:12:15 -07002880 public void bindDeepShortcuts() {
Sunny Goyal5f064012016-08-08 10:10:01 -07002881 final MultiHashMap<ComponentKey, String> shortcutMapCopy = mBgDeepShortcutMap.clone();
Tony Wickham80f57872016-06-29 18:12:15 -07002882 Runnable r = new Runnable() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002883 @Override
2884 public void run() {
2885 Callbacks callbacks = getCallback();
2886 if (callbacks != null) {
2887 callbacks.bindDeepShortcutMap(shortcutMapCopy);
2888 }
2889 }
Tony Wickham80f57872016-06-29 18:12:15 -07002890 };
2891 runOnMainThread(r);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002892 }
2893
Sunny Goyal75b0f552015-05-20 21:57:06 -07002894 /**
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002895 * Refreshes the cached shortcuts if the shortcut permission has changed.
2896 * Current implementation simply reloads the workspace, but it can be optimized to
2897 * use partial updates similar to {@link UserManagerCompat}
2898 */
2899 public void refreshShortcutsIfRequired() {
2900 if (Utilities.isNycMR1OrAbove()) {
2901 sWorker.removeCallbacks(mShortcutPermissionCheckRunnable);
2902 sWorker.post(mShortcutPermissionCheckRunnable);
2903 }
2904 }
2905
2906 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -07002907 * Called when the icons for packages have been updated in the icon cache.
2908 */
2909 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2910 final Callbacks callbacks = getCallback();
2911 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2912 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2913
2914 // If any package icon has changed (app was updated while launcher was dead),
2915 // update the corresponding shortcuts.
2916 synchronized (sBgLock) {
2917 for (ItemInfo info : sBgItemsIdMap) {
2918 if (info instanceof ShortcutInfo && user.equals(info.user)
2919 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2920 ShortcutInfo si = (ShortcutInfo) info;
2921 ComponentName cn = si.getTargetComponent();
2922 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2923 si.updateIcon(mIconCache);
2924 updatedShortcuts.add(si);
2925 }
2926 }
2927 }
2928 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2929 }
2930
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002931 bindUpdatedShortcuts(updatedShortcuts, user);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002932
2933 if (!updatedApps.isEmpty()) {
2934 mHandler.post(new Runnable() {
2935
2936 public void run() {
2937 Callbacks cb = getCallback();
2938 if (cb != null && callbacks == cb) {
2939 cb.bindAppsUpdated(updatedApps);
2940 }
2941 }
2942 });
2943 }
2944 }
2945
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002946 private void bindUpdatedShortcuts(
2947 ArrayList<ShortcutInfo> updatedShortcuts, UserHandleCompat user) {
2948 bindUpdatedShortcuts(updatedShortcuts, new ArrayList<ShortcutInfo>(), user);
2949 }
2950
2951 private void bindUpdatedShortcuts(
2952 final ArrayList<ShortcutInfo> updatedShortcuts,
2953 final ArrayList<ShortcutInfo> removedShortcuts,
2954 final UserHandleCompat user) {
2955 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002956 final Callbacks callbacks = getCallback();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002957 mHandler.post(new Runnable() {
2958
2959 public void run() {
2960 Callbacks cb = getCallback();
2961 if (cb != null && callbacks == cb) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002962 cb.bindShortcutsChanged(updatedShortcuts, removedShortcuts, user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002963 }
2964 }
2965 });
2966 }
2967 }
2968
2969 void enqueueItemUpdatedTask(Runnable task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002970 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002971 }
2972
2973 private class PackageUpdatedTask implements Runnable {
2974 int mOp;
2975 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002976 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002977
2978 public static final int OP_NONE = 0;
2979 public static final int OP_ADD = 1;
2980 public static final int OP_UPDATE = 2;
2981 public static final int OP_REMOVE = 3; // uninstlled
2982 public static final int OP_UNAVAILABLE = 4; // external media unmounted
Kenny Guy44cba692016-01-21 19:50:02 +00002983 public static final int OP_SUSPEND = 5; // package suspended
2984 public static final int OP_UNSUSPEND = 6; // package unsuspended
Sunny Goyalda891c12016-03-18 18:29:24 -07002985 public static final int OP_USER_AVAILABILITY_CHANGE = 7; // user available/unavailable
Joe Onorato36115782010-06-17 13:28:48 -04002986
Kenny Guyed131872014-04-30 03:02:21 +01002987 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002988 mOp = op;
2989 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002990 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002991 }
2992
2993 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07002994 if (!mHasLoaderCompletedOnce) {
2995 // Loader has not yet run.
2996 return;
2997 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002998 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002999
3000 final String[] packages = mPackages;
3001 final int N = packages.length;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003002 FlagOp flagOp = FlagOp.NO_OP;
Sunny Goyalda891c12016-03-18 18:29:24 -07003003 StringFilter pkgFilter = StringFilter.of(new HashSet<>(Arrays.asList(packages)));
Joe Onorato36115782010-06-17 13:28:48 -04003004 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003005 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04003006 for (int i=0; i<N; i++) {
3007 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003008 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003009 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003010 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003011
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003012 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3013 if (heuristic != null) {
3014 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003015 }
Joe Onorato36115782010-06-17 13:28:48 -04003016 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003017 }
Joe Onorato36115782010-06-17 13:28:48 -04003018 case OP_UPDATE:
3019 for (int i=0; i<N; i++) {
3020 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003021 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003022 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003023 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003024 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003025 // Since package was just updated, the target must be available now.
3026 flagOp = FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04003027 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003028 case OP_REMOVE: {
3029 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3030 if (heuristic != null) {
3031 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003032 }
Joe Onorato36115782010-06-17 13:28:48 -04003033 for (int i=0; i<N; i++) {
3034 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003035 mIconCache.removeIconsForPkg(packages[i], mUser);
3036 }
3037 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003038 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003039 case OP_UNAVAILABLE:
3040 for (int i=0; i<N; i++) {
3041 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3042 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003043 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003044 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003045 flagOp = FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04003046 break;
Kenny Guy44cba692016-01-21 19:50:02 +00003047 case OP_SUSPEND:
3048 case OP_UNSUSPEND:
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003049 flagOp = mOp == OP_SUSPEND ?
3050 FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED) :
3051 FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED);
Sunny Goyal17763cb2016-03-24 13:53:22 -07003052 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.(un)suspend " + N);
Sunny Goyalda891c12016-03-18 18:29:24 -07003053 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
3054 break;
3055 case OP_USER_AVAILABILITY_CHANGE:
3056 flagOp = UserManagerCompat.getInstance(context).isQuietModeEnabled(mUser)
3057 ? FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER)
3058 : FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER);
3059 // We want to update all packages for this user.
3060 pkgFilter = StringFilter.matchesAll();
3061 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
Kenny Guy44cba692016-01-21 19:50:02 +00003062 break;
Joe Onorato36115782010-06-17 13:28:48 -04003063 }
3064
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003065 ArrayList<AppInfo> added = null;
3066 ArrayList<AppInfo> modified = null;
3067 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003068
Adam Cohen487f7dd2012-06-28 18:12:10 -07003069 if (mBgAllAppsList.added.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003070 added = new ArrayList<>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003071 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003072 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003073 if (mBgAllAppsList.modified.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003074 modified = new ArrayList<>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003075 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003076 }
Winson Chung5d55f332012-07-16 20:45:03 -07003077 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003078 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003079 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003080 }
3081
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003082 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps = new HashMap<>();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003083
Joe Onorato36115782010-06-17 13:28:48 -04003084 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003085 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003086 for (AppInfo ai : added) {
3087 addedOrUpdatedApps.put(ai.componentName, ai);
3088 }
Joe Onorato36115782010-06-17 13:28:48 -04003089 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003090
Joe Onorato36115782010-06-17 13:28:48 -04003091 if (modified != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003092 final Callbacks callbacks = getCallback();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003093 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003094 for (AppInfo ai : modified) {
3095 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003096 }
3097
Joe Onorato36115782010-06-17 13:28:48 -04003098 mHandler.post(new Runnable() {
3099 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003100 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003101 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003102 callbacks.bindAppsUpdated(modifiedFinal);
3103 }
3104 }
3105 });
3106 }
Winson Chung83892cc2013-05-01 16:53:33 -07003107
Sunny Goyal4390ace2014-10-13 11:33:11 -07003108 // Update shortcut infos
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003109 if (mOp == OP_ADD || flagOp != FlagOp.NO_OP) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003110 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3111 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3112 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3113
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003114 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003115 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003116 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3117 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003118 boolean infoUpdated = false;
3119 boolean shortcutUpdated = false;
3120
3121 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003122 if ((si.iconResource != null)
Sunny Goyalda891c12016-03-18 18:29:24 -07003123 && pkgFilter.matches(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003124 Bitmap icon = Utilities.createIconBitmap(
3125 si.iconResource.packageName,
3126 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003127 if (icon != null) {
3128 si.setIcon(icon);
3129 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003130 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003131 }
3132 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003133
3134 ComponentName cn = si.getTargetComponent();
Sunny Goyalda891c12016-03-18 18:29:24 -07003135 if (cn != null && pkgFilter.matches(cn.getPackageName())) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003136 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3137
3138 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003139 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3140 // Auto install icon
3141 PackageManager pm = context.getPackageManager();
3142 ResolveInfo matched = pm.resolveActivity(
3143 new Intent(Intent.ACTION_MAIN)
3144 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3145 PackageManager.MATCH_DEFAULT_ONLY);
3146 if (matched == null) {
3147 // Try to find the best match activity.
3148 Intent intent = pm.getLaunchIntentForPackage(
3149 cn.getPackageName());
3150 if (intent != null) {
3151 cn = intent.getComponent();
3152 appInfo = addedOrUpdatedApps.get(cn);
3153 }
3154
3155 if ((intent == null) || (appInfo == null)) {
3156 removedShortcuts.add(si);
3157 continue;
3158 }
3159 si.promisedIntent = intent;
3160 }
3161 }
3162
3163 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003164 if (appInfo != null) {
3165 si.flags = appInfo.flags;
3166 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003167
Sunny Goyal756adbc2015-04-16 15:20:51 -07003168 si.intent = si.promisedIntent;
3169 si.promisedIntent = null;
3170 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003171 infoUpdated = true;
3172 si.updateIcon(mIconCache);
3173 }
3174
3175 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3176 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3177 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003178 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003179 si.contentDescription = appInfo.contentDescription;
3180 infoUpdated = true;
3181 }
3182
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003183 int oldDisabledFlags = si.isDisabled;
3184 si.isDisabled = flagOp.apply(si.isDisabled);
3185 if (si.isDisabled != oldDisabledFlags) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003186 shortcutUpdated = true;
3187 }
3188 }
3189
3190 if (infoUpdated || shortcutUpdated) {
3191 updatedShortcuts.add(si);
3192 }
3193 if (infoUpdated) {
3194 updateItemInDatabase(context, si);
3195 }
Sunny Goyalda891c12016-03-18 18:29:24 -07003196 } else if (info instanceof LauncherAppWidgetInfo && mOp == OP_ADD) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003197 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3198 if (mUser.equals(widgetInfo.user)
3199 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
Sunny Goyalda891c12016-03-18 18:29:24 -07003200 && pkgFilter.matches(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003201 widgetInfo.restoreStatus &=
3202 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3203 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003204
3205 // adding this flag ensures that launcher shows 'click to setup'
3206 // if the widget has a config activity. In case there is no config
3207 // activity, it will be marked as 'restored' during bind.
3208 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3209
Sunny Goyal4390ace2014-10-13 11:33:11 -07003210 widgets.add(widgetInfo);
3211 updateItemInDatabase(context, widgetInfo);
3212 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003213 }
3214 }
3215 }
3216
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003217 bindUpdatedShortcuts(updatedShortcuts, removedShortcuts, mUser);
3218 if (!removedShortcuts.isEmpty()) {
3219 deleteItemsFromDatabase(context, removedShortcuts);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003220 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003221
Sunny Goyal4390ace2014-10-13 11:33:11 -07003222 if (!widgets.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003223 final Callbacks callbacks = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003224 mHandler.post(new Runnable() {
3225 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003226 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003227 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003228 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003229 }
3230 }
3231 });
3232 }
3233 }
3234
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003235 final HashSet<String> removedPackages = new HashSet<>();
3236 final HashSet<ComponentName> removedComponents = new HashSet<>();
3237 if (mOp == OP_REMOVE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003238 // Mark all packages in the broadcast to be removed
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003239 Collections.addAll(removedPackages, packages);
3240
3241 // No need to update the removedComponents as
3242 // removedPackages is a super-set of removedComponents
Winson Chungdf95eb12013-10-16 14:57:07 -07003243 } else if (mOp == OP_UPDATE) {
3244 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003245 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003246 if (isPackageDisabled(context, packages[i], mUser)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003247 removedPackages.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003248 }
3249 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003250
3251 // Update removedComponents as some components can get removed during package update
3252 for (AppInfo info : removedApps) {
3253 removedComponents.add(info.componentName);
3254 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003255 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003256
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003257 if (!removedPackages.isEmpty() || !removedComponents.isEmpty()) {
3258 for (String pn : removedPackages) {
3259 deletePackageFromDatabase(context, pn, mUser);
3260 }
3261 for (ComponentName cn : removedComponents) {
3262 deleteItemsFromDatabase(context, getItemInfoForComponentName(cn, mUser));
Sunny Goyal1a745e82014-10-02 15:58:31 -07003263 }
3264
Winson Chungdf95eb12013-10-16 14:57:07 -07003265 // Remove any queued items from the install queue
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003266 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackages, mUser);
3267
Winson Chungdf95eb12013-10-16 14:57:07 -07003268 // Call the components-removed callback
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003269 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003270 mHandler.post(new Runnable() {
3271 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003272 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003273 if (callbacks == cb && cb != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003274 callbacks.bindWorkspaceComponentsRemoved(
3275 removedPackages, removedComponents, mUser);
3276 }
3277 }
3278 });
3279 }
3280
3281 if (!removedApps.isEmpty()) {
3282 // Remove corresponding apps from All-Apps
3283 final Callbacks callbacks = getCallback();
3284 mHandler.post(new Runnable() {
3285 public void run() {
3286 Callbacks cb = getCallback();
3287 if (callbacks == cb && cb != null) {
3288 callbacks.bindAppInfosRemoved(removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04003289 }
3290 }
3291 });
Joe Onoratobe386092009-11-17 17:32:16 -08003292 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003293
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003294 // Notify launcher of widget update. From marshmallow onwards we use AppWidgetHost to
3295 // get widget update signals.
3296 if (!Utilities.ATLEAST_MARSHMALLOW &&
3297 (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003298 final Callbacks callbacks = getCallback();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003299 mHandler.post(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003300 public void run() {
3301 Callbacks cb = getCallback();
3302 if (callbacks == cb && cb != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003303 callbacks.notifyWidgetProvidersChanged();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003304 }
3305 }
3306 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003307 }
3308 }
3309 }
3310
Sunny Goyal10923b32016-07-20 15:42:44 -07003311 /**
3312 * Repopulates the shortcut info, possibly updating any icon already on the workspace.
3313 */
3314 public void updateShortcutInfo(final ShortcutInfoCompat fullDetail, final ShortcutInfo info) {
3315 enqueueItemUpdatedTask(new Runnable() {
3316 @Override
3317 public void run() {
3318 info.updateFromDeepShortcutInfo(
3319 fullDetail, LauncherAppState.getInstance().getContext());
3320 ArrayList<ShortcutInfo> update = new ArrayList<ShortcutInfo>();
3321 update.add(info);
3322 bindUpdatedShortcuts(update, fullDetail.getUserHandle());
3323 }
3324 });
3325 }
3326
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003327 private class ShortcutsChangedTask implements Runnable {
Sunny Goyal50941fb2016-08-04 12:03:52 -07003328 private final String mPackageName;
3329 private final List<ShortcutInfoCompat> mShortcuts;
3330 private final UserHandleCompat mUser;
3331 private final boolean mUpdateIdMap;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003332
3333 public ShortcutsChangedTask(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal50941fb2016-08-04 12:03:52 -07003334 UserHandleCompat user, boolean updateIdMap) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003335 mPackageName = packageName;
3336 mShortcuts = shortcuts;
3337 mUser = user;
Sunny Goyal50941fb2016-08-04 12:03:52 -07003338 mUpdateIdMap = updateIdMap;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003339 }
3340
3341 @Override
3342 public void run() {
3343 mDeepShortcutManager.onShortcutsChanged(mShortcuts);
3344
3345 Map<String, ShortcutInfoCompat> idsToShortcuts = new HashMap<>();
3346 for (ShortcutInfoCompat shortcut : mShortcuts) {
3347 idsToShortcuts.put(shortcut.getId(), shortcut);
3348 }
3349
3350 // Find ShortcutInfo's that have changed on the workspace.
3351 MultiHashMap<String, ShortcutInfo> idsToWorkspaceShortcutInfos = new MultiHashMap<>();
3352 for (ItemInfo itemInfo : sBgItemsIdMap) {
3353 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
3354 ShortcutInfo si = (ShortcutInfo) itemInfo;
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07003355 if (si.getPromisedIntent().getPackage().equals(mPackageName)
3356 && si.user.equals(mUser)) {
Tony Wickham672d07f2016-07-27 18:22:41 -07003357 String shortcutId = si.getDeepShortcutId();
3358 if (idsToShortcuts.containsKey(shortcutId)) {
3359 idsToWorkspaceShortcutInfos.addToList(shortcutId, si);
3360 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003361 }
3362 }
3363 }
3364
3365 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3366 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager.queryForFullDetails(
3367 mPackageName, new ArrayList<>(idsToWorkspaceShortcutInfos.keySet()), mUser);
3368 ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3369 Context context = LauncherAppState.getInstance().getContext();
3370 for (ShortcutInfoCompat fullDetails : shortcuts) {
3371 List<ShortcutInfo> shortcutInfos = idsToWorkspaceShortcutInfos
3372 .get(fullDetails.getId());
3373 for (ShortcutInfo shortcutInfo : shortcutInfos) {
Sunny Goyal9994b2b2016-06-23 14:17:24 -07003374 shortcutInfo.updateFromDeepShortcutInfo(fullDetails, context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003375 updatedShortcutInfos.add(shortcutInfo);
3376 }
3377 }
3378 bindUpdatedShortcuts(updatedShortcutInfos, mUser);
3379
Sunny Goyal50941fb2016-08-04 12:03:52 -07003380 if (mUpdateIdMap) {
3381 // Update the deep shortcut map if the list of ids has changed for an activity.
3382 updateDeepShortcutMap(mPackageName, mUser, mShortcuts);
3383 bindDeepShortcuts();
3384 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003385 }
3386 }
3387
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003388 /**
3389 * Task to handle changing of lock state of the user
3390 */
3391 private class UserLockStateChangedTask implements Runnable {
3392
3393 private final UserHandleCompat mUser;
3394
3395 public UserLockStateChangedTask(UserHandleCompat user) {
3396 mUser = user;
3397 }
3398
3399 @Override
3400 public void run() {
3401 boolean isUserUnlocked = mUserManager.isUserUnlocked(mUser);
3402 Context context = mApp.getContext();
3403
3404 HashMap<ShortcutKey, ShortcutInfoCompat> pinnedShortcuts = new HashMap<>();
3405 if (isUserUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07003406 List<ShortcutInfoCompat> shortcuts =
3407 mDeepShortcutManager.queryForPinnedShortcuts(null, mUser);
3408 if (mDeepShortcutManager.wasLastCallSuccess()) {
3409 for (ShortcutInfoCompat shortcut : shortcuts) {
3410 pinnedShortcuts.put(ShortcutKey.fromInfo(shortcut), shortcut);
3411 }
3412 } else {
3413 // Shortcut manager can fail due to some race condition when the lock state
3414 // changes too frequently. For the purpose of the update,
3415 // consider it as still locked.
3416 isUserUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003417 }
3418 }
3419
3420 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3421 ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3422 ArrayList<ShortcutInfo> deletedShortcutInfos = new ArrayList<>();
3423 for (ItemInfo itemInfo : sBgItemsIdMap) {
3424 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT
3425 && mUser.equals(itemInfo.user)) {
3426 ShortcutInfo si = (ShortcutInfo) itemInfo;
3427 if (isUserUnlocked) {
3428 ShortcutInfoCompat shortcut =
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07003429 pinnedShortcuts.get(ShortcutKey.fromShortcutInfo(si));
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003430 // We couldn't verify the shortcut during loader. If its no longer available
3431 // (probably due to clear data), delete the workspace item as well
3432 if (shortcut == null) {
3433 deletedShortcutInfos.add(si);
3434 continue;
3435 }
3436 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
3437 si.updateFromDeepShortcutInfo(shortcut, context);
3438 } else {
3439 si.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
3440 }
3441 updatedShortcutInfos.add(si);
3442 }
3443 }
3444 bindUpdatedShortcuts(updatedShortcutInfos, deletedShortcutInfos, mUser);
3445 if (!deletedShortcutInfos.isEmpty()) {
3446 deleteItemsFromDatabase(context, deletedShortcutInfos);
3447 }
3448
3449 // Remove shortcut id map for that user
3450 Iterator<ComponentKey> keysIter = mBgDeepShortcutMap.keySet().iterator();
3451 while (keysIter.hasNext()) {
3452 if (keysIter.next().user.equals(mUser)) {
3453 keysIter.remove();
3454 }
3455 }
3456
3457 if (isUserUnlocked) {
3458 updateDeepShortcutMap(null, mUser, mDeepShortcutManager.queryForAllShortcuts(mUser));
3459 }
3460 bindDeepShortcuts();
3461 }
3462 }
3463
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003464 private void bindWidgetsModel(final Callbacks callbacks, final WidgetsModel model) {
3465 mHandler.post(new Runnable() {
3466 @Override
3467 public void run() {
3468 Callbacks cb = getCallback();
3469 if (callbacks == cb && cb != null) {
3470 callbacks.bindWidgetsModel(model);
3471 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003472 }
3473 });
3474 }
3475
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003476 public void refreshAndBindWidgetsAndShortcuts(
3477 final Callbacks callbacks, final boolean bindFirst) {
3478 runOnWorkerThread(new Runnable() {
3479 @Override
3480 public void run() {
3481 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
3482 bindWidgetsModel(callbacks, mBgWidgetsModel.clone());
Sunny Goyal31860582015-09-18 08:38:57 -07003483 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003484 final WidgetsModel model = mBgWidgetsModel.updateAndClone(mApp.getContext());
3485 bindWidgetsModel(callbacks, model);
3486 // update the Widget entries inside DB on the worker thread.
3487 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3488 model.getRawList());
Sunny Goyal31860582015-09-18 08:38:57 -07003489 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003490 });
Michael Jurkac402cd92013-05-20 15:49:32 +02003491 }
3492
Adam Cohen091440a2015-03-18 14:16:05 -07003493 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003494 UserHandleCompat user) {
3495 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3496 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003497 }
Adam Cohen556f6132014-01-15 15:18:08 -08003498
Kenny Guyed131872014-04-30 03:02:21 +01003499 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3500 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003501 if (cn == null) {
3502 return false;
3503 }
Kenny Guyed131872014-04-30 03:02:21 +01003504 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3505 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003506 return false;
3507 }
Kenny Guyed131872014-04-30 03:02:21 +01003508 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003509 }
3510
Adam Cohena28b78e2014-05-20 17:03:04 -07003511 public static boolean isValidPackage(Context context, String packageName,
3512 UserHandleCompat user) {
3513 if (packageName == null) {
3514 return false;
3515 }
3516 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3517 return launcherApps.isPackageEnabledForProfile(packageName, user);
3518 }
3519
Joe Onorato9c1289c2009-08-17 11:03:03 -04003520 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003521 * Make an ShortcutInfo object for a restored application or shortcut item that points
3522 * to a package that is not yet installed on the system.
3523 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003524 public ShortcutInfo getRestoredItemInfo(Cursor c, Intent intent,
3525 int promiseType, int itemType, CursorIconInfo iconInfo) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003526 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003527 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003528
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003529 Bitmap icon = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003530 // the fallback icon
3531 if (icon == null) {
3532 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3533 } else {
3534 info.setIcon(icon);
3535 }
Sunny Goyal34942622014-08-29 17:20:55 -07003536
3537 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003538 String title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07003539 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003540 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003541 }
Sunny Goyal34942622014-08-29 17:20:55 -07003542 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3543 if (TextUtils.isEmpty(info.title)) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003544 info.title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07003545 }
Sunny Goyal34942622014-08-29 17:20:55 -07003546 } else {
3547 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3548 }
3549
Winson Chung82b016c2015-05-08 17:00:10 -07003550 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003551 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003552 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003553 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003554 return info;
3555 }
3556
3557 /**
3558 * Make an Intent object for a restored application or shortcut item that points
3559 * to the market page for the item.
3560 */
Adam Cohen091440a2015-03-18 14:16:05 -07003561 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003562 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003563 return getMarketIntent(componentName.getPackageName());
3564 }
3565
3566 static Intent getMarketIntent(String packageName) {
3567 return new Intent(Intent.ACTION_VIEW)
3568 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003569 .scheme("market")
3570 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003571 .appendQueryParameter("id", packageName)
3572 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003573 }
3574
3575 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003576 * Make an ShortcutInfo object for a shortcut that is an application.
3577 *
3578 * If c is not null, then it will be used to fill in missing data like the title and icon.
3579 */
Sunny Goyald09c3702016-04-06 16:18:20 -07003580 public ShortcutInfo getAppShortcutInfo(Intent intent,
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003581 UserHandleCompat user, Cursor c, CursorIconInfo iconInfo,
Sunny Goyal34b65272015-03-11 16:56:52 -07003582 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003583 if (user == null) {
3584 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003585 return null;
3586 }
3587
Kenny Guyed131872014-04-30 03:02:21 +01003588 ComponentName componentName = intent.getComponent();
3589 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08003590 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01003591 return null;
3592 }
3593
3594 Intent newIntent = new Intent(intent.getAction(), null);
3595 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3596 newIntent.setComponent(componentName);
3597 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003598 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003599 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3600 return null;
3601 }
3602
3603 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003604 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003605 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003606 Bitmap icon = iconInfo.loadIcon(c);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003607 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003608 }
3609
Sunny Goyald09c3702016-04-06 16:18:20 -07003610 if (lai != null && PackageManagerHelper.isAppSuspended(lai.getApplicationInfo())) {
3611 info.isDisabled = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
3612 }
3613
Joe Onorato56d82912010-03-07 14:32:10 -05003614 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003615 if (TextUtils.isEmpty(info.title) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003616 info.title = iconInfo.getTitle(c);
Joe Onorato56d82912010-03-07 14:32:10 -05003617 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003618
Joe Onorato56d82912010-03-07 14:32:10 -05003619 // fall back to the class name of the activity
3620 if (info.title == null) {
3621 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003622 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003623
Joe Onorato9c1289c2009-08-17 11:03:03 -04003624 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003625 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003626 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003627 if (lai != null) {
3628 info.flags = AppInfo.initFlags(lai);
3629 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003630 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003631 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003632
Sunny Goyale2df0622015-04-24 11:27:00 -07003633 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003634 ItemInfoFilter f) {
3635 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3636 for (ItemInfo i : infos) {
3637 if (i instanceof ShortcutInfo) {
3638 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003639 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003640 if (cn != null && f.filterItem(null, info, cn)) {
3641 filtered.add(info);
3642 }
3643 } else if (i instanceof FolderInfo) {
3644 FolderInfo info = (FolderInfo) i;
3645 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003646 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003647 if (cn != null && f.filterItem(info, s, cn)) {
3648 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003649 }
3650 }
Winson Chung64359a52013-07-08 17:17:08 -07003651 } else if (i instanceof LauncherAppWidgetInfo) {
3652 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3653 ComponentName cn = info.providerName;
3654 if (cn != null && f.filterItem(null, info, cn)) {
3655 filtered.add(info);
3656 }
Winson Chung8a435102012-08-30 17:16:53 -07003657 }
3658 }
Winson Chung64359a52013-07-08 17:17:08 -07003659 return new ArrayList<ItemInfo>(filtered);
3660 }
3661
Adam Cohen091440a2015-03-18 14:16:05 -07003662 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003663 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003664 ItemInfoFilter filter = new ItemInfoFilter() {
3665 @Override
3666 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003667 if (info.user == null) {
3668 return cn.equals(cname);
3669 } else {
3670 return cn.equals(cname) && info.user.equals(user);
3671 }
Winson Chung64359a52013-07-08 17:17:08 -07003672 }
3673 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003674 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003675 }
3676
Sunny Goyal1a745e82014-10-02 15:58:31 -07003677 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003678 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003679 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003680 @Thunk ShortcutInfo getShortcutInfo(Cursor c, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003681 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003682 // Non-app shortcuts are only supported for current user.
3683 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003684 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003685
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003686 // TODO: If there's an explicit component and we can't install that, delete it.
3687
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003688 loadInfoFromCursor(info, c, iconInfo);
3689 return info;
3690 }
Joe Onorato56d82912010-03-07 14:32:10 -05003691
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003692 /**
3693 * Make an ShortcutInfo object for a shortcut that isn't an application.
3694 */
3695 public void loadInfoFromCursor(ShortcutInfo info, Cursor c, CursorIconInfo iconInfo) {
3696 info.title = iconInfo.getTitle(c);
3697 Bitmap icon = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003698 // the fallback icon
3699 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003700 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003701 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003702 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003703 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003704 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003705
Sunny Goyal2350bc92014-10-14 16:42:54 -07003706 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003707 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3708 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3709 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3710
Adam Cohend9198822011-11-22 16:42:47 -08003711 if (intent == null) {
3712 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3713 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3714 return null;
3715 }
3716
Joe Onorato0589f0f2010-02-08 13:44:00 -08003717 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003718 boolean customIcon = false;
3719 ShortcutIconResource iconResource = null;
3720
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003721 if (bitmap instanceof Bitmap) {
3722 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003723 customIcon = true;
3724 } else {
3725 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003726 if (extra instanceof ShortcutIconResource) {
3727 iconResource = (ShortcutIconResource) extra;
3728 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003729 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003730 }
3731 }
3732
Michael Jurkac9d95c52011-08-29 14:03:34 -07003733 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003734
Kenny Guyed131872014-04-30 03:02:21 +01003735 // Only support intents for current user for now. Intents sent from other
3736 // users wouldn't get here without intent forwarding anyway.
3737 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003738 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003739 icon = mIconCache.getDefaultIcon(info.user);
3740 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003741 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003742 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003743
Winson Chung82b016c2015-05-08 17:00:10 -07003744 info.title = Utilities.trim(name);
3745 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003746 info.intent = intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003747 info.iconResource = iconResource;
3748
3749 return info;
3750 }
3751
Joe Onorato9c1289c2009-08-17 11:03:03 -04003752 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003753 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003754 * or make a new one.
3755 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003756 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003757 // See if a placeholder was created for us already
3758 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003759 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003760 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003761 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003762 folders.put(id, folderInfo);
3763 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003764 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003765 }
3766
Joe Onoratobe386092009-11-17 17:32:16 -08003767
Sunny Goyal651077b2014-06-30 14:15:31 -07003768 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3769 return (provider != null) && (provider.provider != null)
3770 && (provider.provider.getPackageName() != null);
3771 }
3772
Joe Onoratobe386092009-11-17 17:32:16 -08003773 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003774 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003775 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3776 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3777 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3778 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003779 if (mLoaderTask != null) {
3780 mLoaderTask.dumpState();
3781 } else {
3782 Log.d(TAG, "mLoaderTask=null");
3783 }
Joe Onoratobe386092009-11-17 17:32:16 -08003784 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003785
3786 public Callbacks getCallback() {
3787 return mCallbacks != null ? mCallbacks.get() : null;
3788 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003789
3790 /**
3791 * @return {@link FolderInfo} if its already loaded.
3792 */
3793 public FolderInfo findFolderById(Long folderId) {
3794 synchronized (sBgLock) {
3795 return sBgFolders.get(folderId);
3796 }
3797 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003798
Sunny Goyal527c7d32015-08-28 15:19:36 -07003799 @Thunk class DeferredMainThreadExecutor implements Executor {
3800
3801 @Override
3802 public void execute(Runnable command) {
3803 runOnMainThread(command);
3804 }
3805 }
3806
Sunny Goyal756adbc2015-04-16 15:20:51 -07003807 /**
3808 * @return the looper for the worker thread which can be used to start background tasks.
3809 */
3810 public static Looper getWorkerLooper() {
3811 return sWorkerThread.getLooper();
3812 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003813}