blob: 1607a4a4012419d66d7df6ff5c4a63c4da33f401 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Romain Guy629de3e2010-01-13 12:20:59 -080019import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070020import android.content.BroadcastReceiver;
21import android.content.ComponentName;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070022import android.content.ContentProviderOperation;
23import android.content.ContentResolver;
24import android.content.ContentValues;
25import android.content.Context;
26import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080027import android.content.Intent.ShortcutIconResource;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070028import android.content.IntentFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.content.pm.PackageManager;
30import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.database.Cursor;
32import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.net.Uri;
Joe Onorato36115782010-06-17 13:28:48 -040034import android.os.Handler;
35import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070036import android.os.Looper;
Joe Onorato0589f0f2010-02-08 13:44:00 -080037import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040039import android.os.SystemClock;
Sunny Goyale26d1002016-06-20 14:52:14 -070040import android.os.Trace;
Chris Wrenc3919c02013-09-18 09:48:33 -040041import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080042import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070043import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080044import android.util.LongSparseArray;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070045import android.util.MutableInt;
Winson Chungc9168342013-06-26 14:54:55 -070046import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010047
Sunny Goyalffe83f12014-08-14 17:39:34 -070048import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010049import com.android.launcher3.compat.LauncherActivityInfoCompat;
50import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070051import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070052import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010053import com.android.launcher3.compat.UserHandleCompat;
54import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070055import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -070056import com.android.launcher3.config.ProviderConfig;
Tony Wickham827cef22016-03-17 15:39:39 -070057import com.android.launcher3.dynamicui.ExtractionUtils;
Sunny Goyal26119432016-02-18 22:09:23 +000058import com.android.launcher3.folder.Folder;
59import com.android.launcher3.folder.FolderIcon;
Sunny Goyal1acb9e92016-05-16 12:41:09 -070060import com.android.launcher3.logging.FileLog;
Sunny Goyalf862a262015-12-14 14:27:38 -080061import com.android.launcher3.model.GridSizeMigrationTask;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070062import com.android.launcher3.model.WidgetsModel;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -070063import com.android.launcher3.provider.ImportDataTask;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070064import com.android.launcher3.provider.LauncherDbUtils;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070065import com.android.launcher3.shortcuts.DeepShortcutManager;
66import com.android.launcher3.shortcuts.ShortcutInfoCompat;
67import com.android.launcher3.shortcuts.ShortcutKey;
Robin Lee26ace122015-03-16 19:41:43 +000068import com.android.launcher3.util.ComponentKey;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070069import com.android.launcher3.util.CursorIconInfo;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -070070import com.android.launcher3.util.FlagOp;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070071import com.android.launcher3.util.GridOccupancy;
Sunny Goyale2df0622015-04-24 11:27:00 -070072import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070073import com.android.launcher3.util.ManagedProfileHeuristic;
Tony Wickhamd82a39d2016-07-01 15:44:13 -070074import com.android.launcher3.util.MultiHashMap;
Sunny Goyald09c3702016-04-06 16:18:20 -070075import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070076import com.android.launcher3.util.Preconditions;
Sunny Goyalda891c12016-03-18 18:29:24 -070077import com.android.launcher3.util.StringFilter;
Adam Cohen091440a2015-03-18 14:16:05 -070078import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070079import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080080
Michael Jurkac2f801e2011-07-12 14:19:46 -070081import java.lang.ref.WeakReference;
82import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070083import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070084import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070085import java.util.Arrays;
Michael Jurkac2f801e2011-07-12 14:19:46 -070086import java.util.Collections;
87import java.util.Comparator;
88import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070089import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070090import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070091import java.util.List;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070092import java.util.Map;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070093import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070094import java.util.Set;
Sunny Goyal527c7d32015-08-28 15:19:36 -070095import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -070096
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097/**
98 * Maintains in-memory state of the Launcher. It is expected that there should be only one
99 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700100 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101 */
Kenny Guyed131872014-04-30 03:02:21 +0100102public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +0100103 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800104 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -0400105 private static final boolean DEBUG_RECEIVER = false;
Sunny Goyal94485362014-09-18 16:13:58 -0700106 private static final boolean REMOVE_UNRESTORED_ICONS = true;
Chris Wrenb358f812014-04-16 13:37:00 -0400107
Joe Onorato9c1289c2009-08-17 11:03:03 -0400108 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700109
Joe Onorato36115782010-06-17 13:28:48 -0400110 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500111 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800112
Adam Cohen091440a2015-03-18 14:16:05 -0700113 @Thunk final LauncherAppState mApp;
114 @Thunk final Object mLock = new Object();
115 @Thunk DeferredHandler mHandler = new DeferredHandler();
116 @Thunk LoaderTask mLoaderTask;
117 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700118 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119
Adam Cohen091440a2015-03-18 14:16:05 -0700120 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700121 static {
122 sWorkerThread.start();
123 }
Adam Cohen091440a2015-03-18 14:16:05 -0700124 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700125
Joe Onoratocc67f472010-06-08 10:54:30 -0700126 // We start off with everything not loaded. After that, we assume that
127 // our monitoring of the package manager provides all updates and we never
128 // need to do a requery. These are only ever touched from the loader thread.
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700129 private boolean mWorkspaceLoaded;
130 private boolean mAllAppsLoaded;
131 private boolean mDeepShortcutsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700132
Sunny Goyal756a28a2015-04-23 17:07:55 -0700133 /**
134 * Set of runnables to be called on the background thread after the workspace binding
135 * is complete.
136 */
137 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
138
Adam Cohen091440a2015-03-18 14:16:05 -0700139 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700141 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800142 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700143 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800144 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800145
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700146 // Maps all launcher activities to the id's of their shortcuts (if they have any).
147 private final MultiHashMap<ComponentKey, String> mBgDeepShortcutMap = new MultiHashMap<>();
148
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
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700192 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700193 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
194 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700195
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700196 // </ only access in worker thread >
197
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700198 private IconCache mIconCache;
199 private DeepShortcutManager mDeepShortcutManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700201 private final LauncherAppsCompat mLauncherApps;
202 private final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100203
Joe Onorato9c1289c2009-08-17 11:03:03 -0400204 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700205 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400206 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700207 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400208 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700209 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
210 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700211 public void bindScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700212 public void finishFirstPageBind(ViewOnDrawExecutor executor);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800213 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400214 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200215 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700216 public void bindAppsAdded(ArrayList<Long> newScreens,
217 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700218 ArrayList<ItemInfo> addAnimated,
219 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200220 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700221 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
222 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
223 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700224 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700225 public void bindWorkspaceComponentsRemoved(
226 HashSet<String> packageNames, HashSet<ComponentName> components,
227 UserHandleCompat user);
228 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800229 public void notifyWidgetProvidersChanged();
230 public void bindWidgetsModel(WidgetsModel model);
Adam Cohen1462de32012-07-24 22:34:36 -0700231 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700232 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700233 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMap);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400234 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800235
Winson Chung64359a52013-07-08 17:17:08 -0700236 public interface ItemInfoFilter {
237 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
238 }
239
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700240 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter,
241 DeepShortcutManager deepShortcutManager) {
Winson Chunga6945242014-01-08 14:04:34 -0800242 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400243 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100244 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700245 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800246 mIconCache = iconCache;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700247 mDeepShortcutManager = deepShortcutManager;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800248
Kenny Guyed131872014-04-30 03:02:21 +0100249 mLauncherApps = LauncherAppsCompat.getInstance(context);
250 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800251 }
252
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700253 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
254 * posted on the main thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700255 private void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700256 if (sWorkerThread.getThreadId() == Process.myTid()) {
257 // If we are on the worker thread, post onto the main handler
258 mHandler.post(r);
259 } else {
260 r.run();
261 }
262 }
263
264 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
265 * posted on the worker thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700266 private static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700267 if (sWorkerThread.getThreadId() == Process.myTid()) {
268 r.run();
269 } else {
270 // If we are not on the worker thread, then post to the worker handler
271 sWorker.post(r);
272 }
273 }
274
Sunny Goyal756adbc2015-04-16 15:20:51 -0700275 public void setPackageState(final PackageInstallInfo installInfo) {
276 Runnable updateRunnable = new Runnable() {
277
278 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500279 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700280 synchronized (sBgLock) {
281 final HashSet<ItemInfo> updates = new HashSet<>();
282
283 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
284 // Ignore install success events as they are handled by Package add events.
285 return;
286 }
287
Sunny Goyale2df0622015-04-24 11:27:00 -0700288 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700289 if (info instanceof ShortcutInfo) {
290 ShortcutInfo si = (ShortcutInfo) info;
291 ComponentName cn = si.getTargetComponent();
292 if (si.isPromise() && (cn != null)
293 && installInfo.packageName.equals(cn.getPackageName())) {
294 si.setInstallProgress(installInfo.progress);
295
296 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
297 // Mark this info as broken.
298 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
299 }
300 updates.add(si);
301 }
302 }
303 }
304
305 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
306 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
307 widget.installProgress = installInfo.progress;
308 updates.add(widget);
309 }
310 }
311
312 if (!updates.isEmpty()) {
313 // Push changes to the callback.
314 Runnable r = new Runnable() {
315 public void run() {
316 Callbacks callbacks = getCallback();
317 if (callbacks != null) {
318 callbacks.bindRestoreItemsChange(updates);
319 }
320 }
321 };
322 mHandler.post(r);
323 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500324 }
325 }
326 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700327 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500328 }
329
Sunny Goyal756adbc2015-04-16 15:20:51 -0700330 /**
331 * Updates the icons and label of all pending icons for the provided package name.
332 */
333 public void updateSessionDisplayInfo(final String packageName) {
334 Runnable updateRunnable = new Runnable() {
335
336 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700337 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700338 synchronized (sBgLock) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700339 ArrayList<ShortcutInfo> updates = new ArrayList<>();
340 UserHandleCompat user = UserHandleCompat.myUserHandle();
Sunny Goyal756adbc2015-04-16 15:20:51 -0700341
Sunny Goyale2df0622015-04-24 11:27:00 -0700342 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700343 if (info instanceof ShortcutInfo) {
344 ShortcutInfo si = (ShortcutInfo) info;
345 ComponentName cn = si.getTargetComponent();
346 if (si.isPromise() && (cn != null)
347 && packageName.equals(cn.getPackageName())) {
348 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
349 // For auto install apps update the icon as well as label.
350 mIconCache.getTitleAndIcon(si,
351 si.promisedIntent, user,
352 si.shouldUseLowResIcon());
353 } else {
354 // Only update the icon for restored apps.
355 si.updateIcon(mIconCache);
356 }
357 updates.add(si);
358 }
359 }
360 }
361
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700362 bindUpdatedShortcuts(updates, user);
Sunny Goyala22666f2014-09-18 13:25:15 -0700363 }
364 }
365 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700366 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700367 }
368
Adam Cohen76a47a12014-02-05 11:47:43 -0800369 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800370 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800371
372 if (allAppsApps == null) {
373 throw new RuntimeException("allAppsApps must not be null");
374 }
375 if (allAppsApps.isEmpty()) {
376 return;
377 }
378
379 // Process the newly added applications and add them to the database first
380 Runnable r = new Runnable() {
381 public void run() {
382 runOnMainThread(new Runnable() {
383 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800384 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800385 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500386 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800387 }
388 }
389 });
390 }
391 };
392 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700393 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800394
Sunny Goyala9116722015-04-29 13:55:58 -0700395 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800396 int[] xy, int spanX, int spanY) {
397 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700398 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700399
400 GridOccupancy occupied = new GridOccupancy(profile.numColumns, profile.numRows);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800401 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700402 for (ItemInfo r : occupiedPos) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700403 occupied.markCells(r, true);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800404 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800405 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700406 return occupied.findVacantCell(xy, spanX, spanY);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800407 }
408
409 /**
410 * Find a position on the screen for the given size or adds a new screen.
411 * @return screenId and the coordinates for the item.
412 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700413 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyalc3642d42015-10-30 10:27:08 -0700414 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800415 ArrayList<Long> workspaceScreens,
416 ArrayList<Long> addedWorkspaceScreensFinal,
417 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700418 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800419
Sunny Goyala9116722015-04-29 13:55:58 -0700420 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700421 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700422 synchronized (sBgLock) {
423 for (ItemInfo info : sBgItemsIdMap) {
424 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
425 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
426 if (items == null) {
427 items = new ArrayList<>();
428 screenItems.put(info.screenId, items);
429 }
430 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800431 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800432 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800433 }
434
435 // Find appropriate space for the item.
436 long screenId = 0;
437 int[] cordinates = new int[2];
438 boolean found = false;
439
440 int screenCount = workspaceScreens.size();
441 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700442 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800443 if (preferredScreenIndex < screenCount) {
444 screenId = workspaceScreens.get(preferredScreenIndex);
445 found = findNextAvailableIconSpaceInScreen(
446 screenItems.get(screenId), cordinates, spanX, spanY);
447 }
448
449 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700450 // Search on any of the screens starting from the first screen.
451 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800452 screenId = workspaceScreens.get(screen);
453 if (findNextAvailableIconSpaceInScreen(
454 screenItems.get(screenId), cordinates, spanX, spanY)) {
455 // We found a space for it
456 found = true;
457 break;
458 }
459 }
460 }
461
462 if (!found) {
463 // Still no position found. Add a new screen to the end.
Sunny Goyald2497482015-09-22 18:24:19 -0700464 screenId = LauncherSettings.Settings.call(context.getContentResolver(),
465 LauncherSettings.Settings.METHOD_NEW_SCREEN_ID)
466 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800467
468 // Save the screen id for binding in the workspace
469 workspaceScreens.add(screenId);
470 addedWorkspaceScreensFinal.add(screenId);
471
472 // If we still can't find an empty space, then God help us all!!!
473 if (!findNextAvailableIconSpaceInScreen(
474 screenItems.get(screenId), cordinates, spanX, spanY)) {
475 throw new RuntimeException("Can't find space to add the item");
476 }
477 }
478 return Pair.create(screenId, cordinates);
479 }
480
481 /**
482 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800483 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700484 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700485 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800486 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800487 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700488 return;
Winson Chung997a9232013-07-24 15:33:46 -0700489 }
Winson Chung64359a52013-07-08 17:17:08 -0700490 // Process the newly added applications and add them to the database first
491 Runnable r = new Runnable() {
492 public void run() {
493 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
494 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
495
Winson Chung76828c82013-08-19 15:43:29 -0700496 // Get the list of workspace screens. We need to append to this list and
497 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
498 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800499 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700500 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800501 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700502 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800503 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700504 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800505 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700506 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800507 }
Winson Chung76828c82013-08-19 15:43:29 -0700508
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800509 // Find appropriate space for the item.
Sunny Goyalc3642d42015-10-30 10:27:08 -0700510 Pair<Long, int[]> coords = findSpaceForItem(context,
Sunny Goyal9eba1fd2015-10-16 08:58:57 -0700511 workspaceScreens, addedWorkspaceScreensFinal, 1, 1);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800512 long screenId = coords.first;
513 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700514
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700515 ItemInfo itemInfo;
516 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
517 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800518 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700519 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700520 } else {
521 throw new RuntimeException("Unexpected info type");
522 }
Winson Chung94d67682013-09-25 16:29:40 -0700523
Winson Chung64359a52013-07-08 17:17:08 -0700524 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700525 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700526 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700527 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700528 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700529 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700530 }
531 }
532
Winson Chung76828c82013-08-19 15:43:29 -0700533 // Update the workspace screens
534 updateWorkspaceScreenOrder(context, workspaceScreens);
535
Adam Cohen76a47a12014-02-05 11:47:43 -0800536 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700537 runOnMainThread(new Runnable() {
538 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800539 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700540 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700541 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
542 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700543 if (!addedShortcutsFinal.isEmpty()) {
544 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
545 long lastScreenId = info.screenId;
546 for (ItemInfo i : addedShortcutsFinal) {
547 if (i.screenId == lastScreenId) {
548 addAnimated.add(i);
549 } else {
550 addNotAnimated.add(i);
551 }
Winson Chung997a9232013-07-24 15:33:46 -0700552 }
553 }
Winson Chungd64d1762013-08-20 14:37:16 -0700554 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800555 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700556 }
Winson Chung64359a52013-07-08 17:17:08 -0700557 }
Winson Chung997a9232013-07-24 15:33:46 -0700558 });
559 }
Winson Chung64359a52013-07-08 17:17:08 -0700560 }
561 };
562 runOnWorkerThread(r);
563 }
564
Joe Onorato9c1289c2009-08-17 11:03:03 -0400565 /**
566 * Adds an item to the DB if it was not created previously, or move it to a new
567 * <container, screen, cellX, cellY>
568 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800569 public static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700570 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400571 if (item.container == ItemInfo.NO_ID) {
572 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700573 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400574 } else {
575 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700576 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800577 }
578 }
579
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700580 static void checkItemInfoLocked(
581 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
582 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
583 if (modelItem != null && item != modelItem) {
584 // check all the data is consistent
585 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
586 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
587 ShortcutInfo shortcut = (ShortcutInfo) item;
588 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
589 modelShortcut.intent.filterEquals(shortcut.intent) &&
590 modelShortcut.id == shortcut.id &&
591 modelShortcut.itemType == shortcut.itemType &&
592 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700593 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700594 modelShortcut.cellX == shortcut.cellX &&
595 modelShortcut.cellY == shortcut.cellY &&
596 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700597 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700598 // For all intents and purposes, this is the same object
599 return;
600 }
601 }
602
603 // the modelItem needs to match up perfectly with item if our model is
604 // to be consistent with the database-- for now, just require
605 // modelItem == item or the equality check above
606 String msg = "item: " + ((item != null) ? item.toString() : "null") +
607 "modelItem: " +
608 ((modelItem != null) ? modelItem.toString() : "null") +
609 "Error: ItemInfo passed to checkItemInfo doesn't match original";
610 RuntimeException e = new RuntimeException(msg);
611 if (stackTrace != null) {
612 e.setStackTrace(stackTrace);
613 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800614 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700615 }
616 }
617
Michael Jurka816474f2012-06-25 14:49:02 -0700618 static void checkItemInfo(final ItemInfo item) {
619 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
620 final long itemId = item.id;
621 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700622 public void run() {
623 synchronized (sBgLock) {
624 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700625 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700626 }
627 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700628 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700629 }
630
Michael Jurkac9d95c52011-08-29 14:03:34 -0700631 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
632 final ItemInfo item, final String callingFunction) {
633 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700634 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700635 final ContentResolver cr = context.getContentResolver();
636
Adam Cohen487f7dd2012-06-28 18:12:10 -0700637 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700638 Runnable r = new Runnable() {
639 public void run() {
640 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700641 updateItemArrays(item, itemId, stackTrace);
642 }
643 };
644 runOnWorkerThread(r);
645 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700646
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700647 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
648 final ArrayList<ItemInfo> items, final String callingFunction) {
649 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700650
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700651 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
652 Runnable r = new Runnable() {
653 public void run() {
654 ArrayList<ContentProviderOperation> ops =
655 new ArrayList<ContentProviderOperation>();
656 int count = items.size();
657 for (int i = 0; i < count; i++) {
658 ItemInfo item = items.get(i);
659 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700660 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700661 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700662
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700663 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
664 updateItemArrays(item, itemId, stackTrace);
665
666 }
667 try {
668 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
669 } catch (Exception e) {
670 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700671 }
672 }
673 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700674 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700675 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700676
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700677 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
678 // Lock on mBgLock *after* the db operation
679 synchronized (sBgLock) {
680 checkItemInfoLocked(itemId, item, stackTrace);
681
682 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
683 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
684 // Item is in a folder, make sure this folder exists
685 if (!sBgFolders.containsKey(item.container)) {
686 // An items container is being set to a that of an item which is not in
687 // the list of Folders.
688 String msg = "item: " + item + " container being set to: " +
689 item.container + ", not in the list of folders";
690 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700691 }
692 }
693
694 // Items are added/removed from the corresponding FolderInfo elsewhere, such
695 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
696 // that are on the desktop, as appropriate
697 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800698 if (modelItem != null &&
699 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
700 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700701 switch (modelItem.itemType) {
702 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
703 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700704 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700705 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
706 if (!sBgWorkspaceItems.contains(modelItem)) {
707 sBgWorkspaceItems.add(modelItem);
708 }
709 break;
710 default:
711 break;
712 }
713 } else {
714 sBgWorkspaceItems.remove(modelItem);
715 }
716 }
717 }
718
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800719 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400720 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700721 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700722 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700723 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400724 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400725 item.cellX = cellX;
726 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700727
Winson Chung3d503fb2011-07-13 17:25:49 -0700728 // We store hotseat items in canonical form which is this orientation invariant position
729 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700730 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700731 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700732 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700733 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700734 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700735 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400736
737 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400738 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700739 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
740 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800741 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700742 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400743
Michael Jurkac9d95c52011-08-29 14:03:34 -0700744 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700745 }
746
747 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700748 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
749 * cellX, cellY have already been updated on the ItemInfos.
750 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800751 public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700752 final long container, final int screen) {
753
754 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
755 int count = items.size();
756
757 for (int i = 0; i < count; i++) {
758 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700759 item.container = container;
760
761 // We store hotseat items in canonical form which is this orientation invariant position
762 // in the hotseat
763 if (context instanceof Launcher && screen < 0 &&
764 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700765 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700766 item.cellY);
767 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700768 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700769 }
770
771 final ContentValues values = new ContentValues();
772 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
773 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
774 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800775 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700776 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700777
778 contentValues.add(values);
779 }
780 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
781 }
782
783 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700784 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800785 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700786 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700787 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700788 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800789 item.cellX = cellX;
790 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700791 item.spanX = spanX;
792 item.spanY = spanY;
793
794 // We store hotseat items in canonical form which is this orientation invariant position
795 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700796 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700797 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700798 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700799 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700800 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700801 }
Adam Cohend4844c32011-02-18 19:25:06 -0800802
Adam Cohend4844c32011-02-18 19:25:06 -0800803 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800804 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700805 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
806 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800807 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700808 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
809 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700810 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800811
Michael Jurka816474f2012-06-25 14:49:02 -0700812 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700813 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700814
815 /**
816 * Update an item to the database in a specified container.
817 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700818 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700819 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100820 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700821 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800822 }
823
Sunny Goyal756a28a2015-04-23 17:07:55 -0700824 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700825 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700826 synchronized (mLock) {
827 if (!mHasLoaderCompletedOnce ||
828 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
829 throw new RuntimeException("Trying to add shortcut while loader is running");
830 }
831 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700832 }
833 }
834
Adam Cohend4844c32011-02-18 19:25:06 -0800835 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700836 * Returns true if the shortcuts already exists on the workspace. This must be called after
837 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700839 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
840 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700841 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700842 if (intent.getComponent() != null) {
843 // If component is not null, an intent with null package will produce
844 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700845 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700846 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700847 intentWithPkg = intent.toUri(0);
848 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700849 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700850 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
851 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700852 }
853 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700854 intentWithPkg = intent.toUri(0);
855 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700856 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700857
858 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700859 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700860 if (item instanceof ShortcutInfo) {
861 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700862 Intent targetIntent = info.promisedIntent == null
863 ? info.intent : info.promisedIntent;
864 if (targetIntent != null && info.user.equals(user)) {
Sunny Goyalcbfe71d2016-08-23 13:25:58 -0700865 Intent copyIntent = new Intent(targetIntent);
866 copyIntent.setSourceBounds(intent.getSourceBounds());
867 String s = copyIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700868 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
869 return true;
870 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700871 }
872 }
873 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800874 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700875 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700876 }
877
Joe Onorato9c1289c2009-08-17 11:03:03 -0400878 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400879 * Add an item to the database in a specified container. Sets the container, screen, cellX and
880 * cellY fields of the item. Also assigns an ID to the item.
881 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700882 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700883 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400884 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400885 item.cellX = cellX;
886 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700887 // We store hotseat items in canonical form which is this orientation invariant position
888 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700889 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700890 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700891 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700892 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700893 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700894 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400895
896 final ContentValues values = new ContentValues();
897 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100898 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400899
Sunny Goyald2497482015-09-22 18:24:19 -0700900 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
901 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
902
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700903 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700904
Jason Monk8e19cf22014-03-20 15:06:57 -0400905 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700906 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700907 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700908 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400909
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700910 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700911 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400912 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700913 sBgItemsIdMap.put(item.id, item);
914 switch (item.itemType) {
915 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
916 sBgFolders.put(item.id, (FolderInfo) item);
917 // Fall through
918 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
919 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700920 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700921 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
922 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
923 sBgWorkspaceItems.add(item);
924 } else {
925 if (!sBgFolders.containsKey(item.container)) {
926 // Adding an item to a folder that doesn't exist.
927 String msg = "adding item: " + item + " to a folder that " +
928 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700929 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700930 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700931 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700932 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700933 incrementPinnedShortcutCount(
934 ShortcutKey.fromItemInfo(item), true /* shouldPin */);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700935 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700936 break;
937 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
938 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
939 break;
940 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700941 }
942 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700943 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700944 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700945 }
946
Sunny Goyal34942622014-08-29 17:20:55 -0700947 private static ArrayList<ItemInfo> getItemsByPackageName(
948 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700949 ItemInfoFilter filter = new ItemInfoFilter() {
950 @Override
951 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
952 return cn.getPackageName().equals(pn) && info.user.equals(user);
953 }
954 };
Sunny Goyale2df0622015-04-24 11:27:00 -0700955 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -0700956 }
957
958 /**
959 * Removes all the items from the database corresponding to the specified package.
960 */
961 static void deletePackageFromDatabase(Context context, final String pn,
962 final UserHandleCompat user) {
963 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700964 }
965
966 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700967 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -0400968 */
Sunny Goyalfa401a12015-04-10 13:45:42 -0700969 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700970 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
971 items.add(item);
972 deleteItemsFromDatabase(context, items);
973 }
974
975 /**
976 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700977 */
Sunny Goyal4390ace2014-10-13 11:33:11 -0700978 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400979 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -0700980 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700981 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700982 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700983 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700984 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700985
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700986 // Lock on mBgLock *after* the db operation
987 synchronized (sBgLock) {
988 switch (item.itemType) {
989 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
990 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -0700991 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700992 if (info.container == item.id) {
993 // We are deleting a folder which still contains items that
994 // think they are contained by that folder.
995 String msg = "deleting a folder (" + item + ") which still " +
996 "contains items (" + info + ")";
997 Log.e(TAG, msg);
998 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700999 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001000 sBgWorkspaceItems.remove(item);
1001 break;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001002 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001003 decrementPinnedShortcutCount(ShortcutKey.fromItemInfo(item));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001004 // Fall through.
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001005 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1006 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1007 sBgWorkspaceItems.remove(item);
1008 break;
1009 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1010 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1011 break;
1012 }
1013 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001014 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001015 }
1016 }
Michael Jurka83df1882011-08-31 20:59:26 -07001017 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001018 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001019 }
1020
1021 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001022 * Decrement the count for the given pinned shortcut, unpinning it if the count becomes 0.
1023 */
1024 private static void decrementPinnedShortcutCount(final ShortcutKey pinnedShortcut) {
1025 synchronized (sBgLock) {
1026 MutableInt count = sBgPinnedShortcutCounts.get(pinnedShortcut);
1027 if (count == null || --count.value == 0) {
1028 LauncherAppState.getInstance().getShortcutManager().unpinShortcut(pinnedShortcut);
1029 }
1030 }
1031 }
1032
1033 /**
1034 * Increment the count for the given shortcut, pinning it if the count becomes 1.
1035 *
1036 * As an optimization, the caller can pass shouldPin == false to avoid
1037 * unnecessary RPC's if the shortcut is already pinned.
1038 */
1039 private static void incrementPinnedShortcutCount(ShortcutKey pinnedShortcut, boolean shouldPin) {
1040 synchronized (sBgLock) {
1041 MutableInt count = sBgPinnedShortcutCounts.get(pinnedShortcut);
1042 if (count == null) {
1043 count = new MutableInt(1);
1044 sBgPinnedShortcutCounts.put(pinnedShortcut, count);
1045 } else {
1046 count.value++;
1047 }
1048 if (shouldPin && count.value == 1) {
1049 LauncherAppState.getInstance().getShortcutManager().pinShortcut(pinnedShortcut);
1050 }
1051 }
1052 }
1053
1054 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001055 * Update the order of the workspace screens in the database. The array list contains
1056 * a list of screen ids in the order that they should appear.
1057 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001058 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001059 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001060 final ContentResolver cr = context.getContentResolver();
1061 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1062
1063 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001064 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001065 while (iter.hasNext()) {
1066 long id = iter.next();
1067 if (id < 0) {
1068 iter.remove();
1069 }
1070 }
1071
1072 Runnable r = new Runnable() {
1073 @Override
1074 public void run() {
Yura085c8532014-02-11 15:15:29 +00001075 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001076 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001077 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001078 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001079 for (int i = 0; i < count; i++) {
1080 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001081 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001082 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1083 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001084 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001085 }
Yura085c8532014-02-11 15:15:29 +00001086
1087 try {
1088 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1089 } catch (Exception ex) {
1090 throw new RuntimeException(ex);
1091 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001092
Winson Chungba9c37f2013-08-30 14:11:37 -07001093 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001094 sBgWorkspaceScreens.clear();
1095 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001096 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001097 }
1098 };
1099 runOnWorkerThread(r);
1100 }
1101
1102 /**
Winsonc0b52fe2015-09-09 16:38:15 -07001103 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001104 */
Winsonc0b52fe2015-09-09 16:38:15 -07001105 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001106 final ContentResolver cr = context.getContentResolver();
1107
Michael Jurkac9d95c52011-08-29 14:03:34 -07001108 Runnable r = new Runnable() {
1109 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001110 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001111 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001112 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001113 sBgItemsIdMap.remove(info.id);
1114 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001115 sBgWorkspaceItems.remove(info);
1116 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001117
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001118 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001119 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001120 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001121 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001122 for (ItemInfo childInfo : info.contents) {
1123 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001124 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001125 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001126 }
1127 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001128 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001129 }
1130
1131 /**
1132 * Set this as the current Launcher activity object for the loader.
1133 */
1134 public void initialize(Callbacks callbacks) {
1135 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001136 Preconditions.assertUIThread();
1137 // Remove any queued UI runnables
1138 mHandler.cancelAll();
1139 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001140 }
1141 }
1142
Kenny Guyed131872014-04-30 03:02:21 +01001143 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001144 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001145 int op = PackageUpdatedTask.OP_UPDATE;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001146 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001147 user));
1148 }
1149
1150 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001151 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001152 int op = PackageUpdatedTask.OP_REMOVE;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001153 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001154 user));
1155 }
1156
1157 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001158 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001159 int op = PackageUpdatedTask.OP_ADD;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001160 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001161 user));
1162 }
1163
1164 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001165 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001166 boolean replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001167 enqueueItemUpdatedTask(
Sunny Goyal144154d2016-03-30 16:47:03 -07001168 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, packageNames, user));
Kenny Guyed131872014-04-30 03:02:21 +01001169 }
1170
1171 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001172 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001173 boolean replacing) {
1174 if (!replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001175 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guyed131872014-04-30 03:02:21 +01001176 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1177 user));
1178 }
Kenny Guyed131872014-04-30 03:02:21 +01001179 }
1180
Kenny Guy44cba692016-01-21 19:50:02 +00001181 @Override
1182 public void onPackagesSuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001183 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001184 PackageUpdatedTask.OP_SUSPEND, packageNames,
1185 user));
1186 }
1187
1188 @Override
1189 public void onPackagesUnsuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001190 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001191 PackageUpdatedTask.OP_UNSUSPEND, packageNames,
1192 user));
1193 }
1194
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001195 @Override
1196 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
1197 UserHandleCompat user) {
Sunny Goyal50941fb2016-08-04 12:03:52 -07001198 enqueueItemUpdatedTask(new ShortcutsChangedTask(packageName, shortcuts, user, true));
1199 }
1200
1201 public void updatePinnedShortcuts(String packageName, List<ShortcutInfoCompat> shortcuts,
1202 UserHandleCompat user) {
1203 enqueueItemUpdatedTask(new ShortcutsChangedTask(packageName, shortcuts, user, false));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001204 }
1205
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001206 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001207 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1208 * ACTION_PACKAGE_CHANGED.
1209 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001210 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001211 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001212 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001213
Joe Onorato36115782010-06-17 13:28:48 -04001214 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001215 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001216 // If we have changed locale we need to clear out the labels in all apps/workspace.
1217 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001218 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1219 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001220 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001221 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001222 } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1223 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
1224 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyalda891c12016-03-18 18:29:24 -07001225 UserHandleCompat user = UserHandleCompat.fromIntent(intent);
1226 if (user != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001227 if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1228 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
1229 enqueueItemUpdatedTask(new PackageUpdatedTask(
1230 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE,
1231 new String[0], user));
1232 }
1233
1234 // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so
1235 // we need to run the state change task again.
1236 if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
1237 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
1238 enqueueItemUpdatedTask(new UserLockStateChangedTask(user));
1239 }
Sunny Goyalda891c12016-03-18 18:29:24 -07001240 }
Tony Wickham827cef22016-03-17 15:39:39 -07001241 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
1242 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001243 }
1244 }
1245
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001246 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001247 resetLoadedState(true, true);
1248
Reena Lee93f824a2011-09-23 17:20:28 -07001249 // Do this here because if the launcher activity is running it will be restarted.
1250 // If it's not running startLoaderFromBackground will merely tell it that it needs
1251 // to reload.
1252 startLoaderFromBackground();
1253 }
1254
Winson Chungf0c6ae02012-03-21 16:10:31 -07001255 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1256 synchronized (mLock) {
1257 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1258 // mWorkspaceLoaded to true later
1259 stopLoaderLocked();
1260 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1261 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001262 // Always reset deep shortcuts loaded.
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001263 // TODO: why?
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001264 mDeepShortcutsLoaded = false;
Winson Chungf0c6ae02012-03-21 16:10:31 -07001265 }
1266 }
1267
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001268 /**
1269 * When the launcher is in the background, it's possible for it to miss paired
1270 * configuration changes. So whenever we trigger the loader from the background
1271 * tell the launcher that it needs to re-run the loader when it comes back instead
1272 * of doing it now.
1273 */
1274 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001275 Callbacks callbacks = getCallback();
1276 if (callbacks != null) {
1277 // Only actually run the loader if they're not paused.
1278 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -07001279 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001280 }
1281 }
Joe Onorato36115782010-06-17 13:28:48 -04001282 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001283
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001284 /**
1285 * If there is already a loader task running, tell it to stop.
1286 */
1287 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001288 LoaderTask oldTask = mLoaderTask;
1289 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001290 oldTask.stopLocked();
1291 }
Reena Lee93f824a2011-09-23 17:20:28 -07001292 }
1293
Adam Cohen1a85c582014-09-30 09:48:49 -07001294 public boolean isCurrentCallbacks(Callbacks callbacks) {
1295 return (mCallbacks != null && mCallbacks.get() == callbacks);
1296 }
1297
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001298 /**
1299 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
1300 * @return true if the page could be bound synchronously.
1301 */
1302 public boolean startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001303 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1304 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001305 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -04001306 // Don't bother to start the thread if we know it's not going to do anything
1307 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001308 final Callbacks oldCallbacks = mCallbacks.get();
1309 // Clear any pending bind-runnables from the synchronized load process.
1310 runOnMainThread(new Runnable() {
1311 public void run() {
1312 oldCallbacks.clearPendingBinds();
1313 }
1314 });
1315
Joe Onorato36115782010-06-17 13:28:48 -04001316 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001317 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001318 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001319 // TODO: mDeepShortcutsLoaded does not need to be true for synchronous bind.
Tony Wickham80f57872016-06-29 18:12:15 -07001320 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE && mAllAppsLoaded
1321 && mWorkspaceLoaded && mDeepShortcutsLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001322 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001323 return true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001324 } else {
1325 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1326 sWorker.post(mLoaderTask);
1327 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001328 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001329 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001330 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001331 }
1332
Joe Onorato36115782010-06-17 13:28:48 -04001333 public void stopLoader() {
1334 synchronized (mLock) {
1335 if (mLoaderTask != null) {
1336 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001337 }
1338 }
Joe Onorato36115782010-06-17 13:28:48 -04001339 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001340
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001341 /**
1342 * Loads the workspace screen ids in an ordered list.
1343 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001344 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001345 final ContentResolver contentResolver = context.getContentResolver();
1346 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001347
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001348 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001349 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
1350 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -07001351 }
1352
Joe Onorato36115782010-06-17 13:28:48 -04001353 /**
1354 * Runnable for the thread that loads the contents of the launcher:
1355 * - workspace icons
1356 * - widgets
1357 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001358 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -04001359 */
1360 private class LoaderTask implements Runnable {
1361 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001362 private int mPageToBindFirst;
1363
Adam Cohen091440a2015-03-18 14:16:05 -07001364 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001365 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001366 @Thunk boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001367
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001368 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -04001369 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001370 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001371 }
1372
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001373 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001374 mIsLoadingAndBindingWorkspace = true;
1375
Joe Onorato36115782010-06-17 13:28:48 -04001376 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001377 if (DEBUG_LOADERS) {
1378 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001379 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001380
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001381 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001382 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001383 synchronized (LoaderTask.this) {
1384 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001385 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001386 }
1387 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001388 }
1389 }
1390
Joe Onorato36115782010-06-17 13:28:48 -04001391 // Bind the workspace
Sunny Goyal93f878c2016-03-30 17:31:24 -07001392 bindWorkspace(mPageToBindFirst);
Joe Onorato36115782010-06-17 13:28:48 -04001393 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001394
Joe Onorato36115782010-06-17 13:28:48 -04001395 private void waitForIdle() {
1396 // Wait until the either we're stopped or the other threads are done.
1397 // This way we don't start loading all apps until the workspace has settled
1398 // down.
1399 synchronized (LoaderTask.this) {
1400 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001401
Joe Onorato36115782010-06-17 13:28:48 -04001402 mHandler.postIdle(new Runnable() {
1403 public void run() {
1404 synchronized (LoaderTask.this) {
1405 mLoadAndBindStepFinished = true;
1406 if (DEBUG_LOADERS) {
1407 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001408 }
Joe Onorato36115782010-06-17 13:28:48 -04001409 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001410 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001411 }
Joe Onorato36115782010-06-17 13:28:48 -04001412 });
1413
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001414 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001415 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001416 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1417 // wait no longer than 1sec at a time
1418 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001419 } catch (InterruptedException ex) {
1420 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001421 }
1422 }
Joe Onorato36115782010-06-17 13:28:48 -04001423 if (DEBUG_LOADERS) {
1424 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001425 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001426 + "ms for previous step to finish binding");
1427 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001428 }
Joe Onorato36115782010-06-17 13:28:48 -04001429 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001430
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001431 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001432 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001433 // Ensure that we have a valid page index to load synchronously
1434 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1435 "valid page index");
1436 }
1437 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1438 // Ensure that we don't try and bind a specified page when the pages have not been
1439 // loaded already (we should load everything asynchronously in that case)
1440 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1441 }
1442 synchronized (mLock) {
1443 if (mIsLoaderTaskRunning) {
1444 // Ensure that we are never running the background loading at this point since
1445 // we also touch the background collections
1446 throw new RuntimeException("Error! Background loading is already running");
1447 }
1448 }
1449
1450 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1451 // data structures, we can't allow any other thread to touch that data, but because
1452 // this call is synchronous, we can get away with not locking).
1453
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001454 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001455 // operations from the previous activity. We need to ensure that all queued operations
1456 // are executed before any synchronous binding work is done.
1457 mHandler.flush();
1458
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001459 // Divide the set of loaded items into those that we are binding synchronously, and
1460 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001461 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001462 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1463 // arise from that.
1464 onlyBindAllApps();
Tony Wickham80f57872016-06-29 18:12:15 -07001465
1466 bindDeepShortcuts();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001467 }
1468
Joe Onorato36115782010-06-17 13:28:48 -04001469 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001470 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001471 if (mStopped) {
1472 return;
1473 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001474 mIsLoaderTaskRunning = true;
1475 }
Joe Onorato36115782010-06-17 13:28:48 -04001476 // Optimize for end-user experience: if the Launcher is up and // running with the
1477 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1478 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001479 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001480 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001481 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001482
Joe Onorato36115782010-06-17 13:28:48 -04001483 if (mStopped) {
1484 break keep_running;
1485 }
1486
Joe Onorato36115782010-06-17 13:28:48 -04001487 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001488
1489 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001490 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1491 loadAndBindAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001492
1493 waitForIdle();
1494
1495 // third step
1496 if (DEBUG_LOADERS) Log.d(TAG, "step 3: loading deep shortcuts");
1497 loadAndBindDeepShortcuts();
Joe Onorato36115782010-06-17 13:28:48 -04001498 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001499
Joe Onorato36115782010-06-17 13:28:48 -04001500 // Clear out this reference, otherwise we end up holding it until all of the
1501 // callback runnables are done.
1502 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001503
Joe Onorato36115782010-06-17 13:28:48 -04001504 synchronized (mLock) {
1505 // If we are still the last one to be scheduled, remove ourselves.
1506 if (mLoaderTask == this) {
1507 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001508 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001509 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001510 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001511 }
Joe Onorato36115782010-06-17 13:28:48 -04001512 }
1513
1514 public void stopLocked() {
1515 synchronized (LoaderTask.this) {
1516 mStopped = true;
1517 this.notify();
1518 }
1519 }
1520
1521 /**
1522 * Gets the callbacks object. If we've been stopped, or if the launcher object
1523 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1524 * object that was around when the deferred message was scheduled, and if there's
1525 * a new Callbacks object around then also return null. This will save us from
1526 * calling onto it with data that will be ignored.
1527 */
1528 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1529 synchronized (mLock) {
1530 if (mStopped) {
1531 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001532 }
Joe Onorato36115782010-06-17 13:28:48 -04001533
1534 if (mCallbacks == null) {
1535 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001536 }
Joe Onorato36115782010-06-17 13:28:48 -04001537
1538 final Callbacks callbacks = mCallbacks.get();
1539 if (callbacks != oldCallbacks) {
1540 return null;
1541 }
1542 if (callbacks == null) {
1543 Log.w(TAG, "no mCallbacks");
1544 return null;
1545 }
1546
1547 return callbacks;
1548 }
1549 }
1550
1551 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001552 private boolean checkItemPlacement(LongArrayMap<GridOccupancy> occupied, ItemInfo item,
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001553 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001554 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001555 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung892c74d2013-08-22 16:15:50 -07001556
Adam Cohendcd297f2013-06-18 13:13:40 -07001557 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001558 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001559 // Return early if we detect that an item is under the hotseat button
Sunny Goyalbb011da2016-06-15 15:42:29 -07001560 if (!FeatureFlags.NO_ALL_APPS_ICON &&
1561 profile.isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001562 Log.e(TAG, "Error loading shortcut into hotseat " + item
1563 + " into position (" + item.screenId + ":" + item.cellX + ","
1564 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001565 return false;
1566 }
1567
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001568 final GridOccupancy hotseatOccupancy =
Dan Sandler295ae182013-12-10 16:05:47 -05001569 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1570
Adam Cohen2e6da152015-05-06 11:42:25 -07001571 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001572 Log.e(TAG, "Error loading shortcut " + item
1573 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001574 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001575 + ")");
1576 return false;
1577 }
1578
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001579 if (hotseatOccupancy != null) {
1580 if (hotseatOccupancy.cells[(int) item.screenId][0]) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001581 Log.e(TAG, "Error loading shortcut into hotseat " + item
1582 + " into position (" + item.screenId + ":" + item.cellX + ","
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001583 + item.cellY + ") already occupied");
Adam Cohendcd297f2013-06-18 13:13:40 -07001584 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001585 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001586 hotseatOccupancy.cells[(int) item.screenId][0] = true;
Dan Sandler295ae182013-12-10 16:05:47 -05001587 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001588 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001589 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001590 final GridOccupancy occupancy = new GridOccupancy(profile.numHotseatIcons, 1);
1591 occupancy.cells[(int) item.screenId][0] = true;
1592 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, occupancy);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001593 return true;
1594 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001595 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1596 if (!workspaceScreens.contains((Long) item.screenId)) {
1597 // The item has an invalid screen id.
1598 return false;
1599 }
1600 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001601 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001602 return true;
1603 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001604
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001605 final int countX = profile.numColumns;
1606 final int countY = profile.numRows;
Adam Cohenae4409d2013-11-26 10:34:59 -08001607 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1608 item.cellX < 0 || item.cellY < 0 ||
1609 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1610 Log.e(TAG, "Error loading shortcut " + item
1611 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1612 + item.cellX + "," + item.cellY
1613 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1614 return false;
1615 }
1616
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001617 if (!occupied.containsKey(item.screenId)) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001618 GridOccupancy screen = new GridOccupancy(countX + 1, countY + 1);
1619 if (item.screenId == Workspace.FIRST_SCREEN_ID) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001620 // Mark the first row as occupied (if the feature is enabled)
1621 // in order to account for the QSB.
1622 screen.markCells(0, 0, countX + 1, 1, FeatureFlags.QSB_ON_FIRST_SCREEN);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001623 }
1624 occupied.put(item.screenId, screen);
Joe Onorato36115782010-06-17 13:28:48 -04001625 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001626 final GridOccupancy occupancy = occupied.get(item.screenId);
Winson Chungf30ad5f2011-08-08 10:55:42 -07001627
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001628 // Check if any workspace icons overlap with each other
1629 if (occupancy.isRegionVacant(item.cellX, item.cellY, item.spanX, item.spanY)) {
1630 occupancy.markCells(item, true);
1631 return true;
1632 } else {
1633 Log.e(TAG, "Error loading shortcut " + item
1634 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1635 + item.cellX + "," + item.cellX + "," + item.spanX + "," + item.spanY
1636 + ") already occupied");
1637 return false;
1638 }
Joe Onorato36115782010-06-17 13:28:48 -04001639 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001640
Winson Chungba9c37f2013-08-30 14:11:37 -07001641 /** Clears all the sBg data structures */
1642 private void clearSBgDataStructures() {
1643 synchronized (sBgLock) {
1644 sBgWorkspaceItems.clear();
1645 sBgAppWidgets.clear();
1646 sBgFolders.clear();
1647 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001648 sBgWorkspaceScreens.clear();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001649 sBgPinnedShortcutCounts.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001650 }
1651 }
1652
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001653 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -07001654 if (LauncherAppState.PROFILE_STARTUP) {
1655 Trace.beginSection("Loading Workspace");
1656 }
Joe Onorato36115782010-06-17 13:28:48 -04001657 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001658
Joe Onorato36115782010-06-17 13:28:48 -04001659 final Context context = mContext;
1660 final ContentResolver contentResolver = context.getContentResolver();
1661 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001662 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001663 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001664 final boolean isSdCardReady = Utilities.isBootCompleted();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001665
Winson Chung892c74d2013-08-22 16:15:50 -07001666 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001667 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001668 int countX = profile.numColumns;
1669 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001670
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001671 boolean clearDb = false;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -07001672 try {
1673 ImportDataTask.performImportIfPossible(context);
1674 } catch (Exception e) {
1675 // Migration failed. Clear workspace.
1676 clearDb = true;
1677 }
1678
1679 if (!clearDb && GridSizeMigrationTask.ENABLED &&
Sunny Goyalf076eae2016-01-11 12:25:10 -08001680 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1681 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001682 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001683 }
1684
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001685 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -07001686 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001687 LauncherSettings.Settings.call(contentResolver,
1688 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001689 }
1690
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001691 Log.d(TAG, "loadWorkspace: loading default favorites");
1692 LauncherSettings.Settings.call(contentResolver,
1693 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -07001694
Winson Chung2abf94d2012-07-18 18:16:38 -07001695 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001696 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001697 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001698 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001699 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001700
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001701 final ArrayList<Long> itemsToRemove = new ArrayList<>();
1702 final ArrayList<Long> restoredRows = new ArrayList<>();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001703 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001704 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001705 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001706 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001707
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001708 // +1 for the hotseat (it can be larger than the workspace)
1709 // Load workspace in reverse order to ensure that latest items are loaded first (and
1710 // before any earlier duplicates)
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001711 final LongArrayMap<GridOccupancy> occupied = new LongArrayMap<>();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001712 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001713
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001714 try {
1715 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1716 final int intentIndex = c.getColumnIndexOrThrow
1717 (LauncherSettings.Favorites.INTENT);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001718 final int containerIndex = c.getColumnIndexOrThrow(
1719 LauncherSettings.Favorites.CONTAINER);
1720 final int itemTypeIndex = c.getColumnIndexOrThrow(
1721 LauncherSettings.Favorites.ITEM_TYPE);
1722 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1723 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001724 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1725 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001726 final int screenIndex = c.getColumnIndexOrThrow(
1727 LauncherSettings.Favorites.SCREEN);
1728 final int cellXIndex = c.getColumnIndexOrThrow
1729 (LauncherSettings.Favorites.CELLX);
1730 final int cellYIndex = c.getColumnIndexOrThrow
1731 (LauncherSettings.Favorites.CELLY);
1732 final int spanXIndex = c.getColumnIndexOrThrow
1733 (LauncherSettings.Favorites.SPANX);
1734 final int spanYIndex = c.getColumnIndexOrThrow(
1735 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001736 final int rankIndex = c.getColumnIndexOrThrow(
1737 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001738 final int restoredIndex = c.getColumnIndexOrThrow(
1739 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001740 final int profileIdIndex = c.getColumnIndexOrThrow(
1741 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001742 final int optionsIndex = c.getColumnIndexOrThrow(
1743 LauncherSettings.Favorites.OPTIONS);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001744 final CursorIconInfo cursorIconInfo = new CursorIconInfo(mContext, c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001745
Sunny Goyal7f834d22015-04-21 10:10:23 -07001746 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
Kenny Guyff05f432016-01-22 17:48:29 +00001747 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001748 final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>();
Sunny Goyal7f834d22015-04-21 10:10:23 -07001749 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001750 long serialNo = mUserManager.getSerialNumberForUser(user);
1751 allUsers.put(serialNo, user);
1752 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001753
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001754 boolean userUnlocked = mUserManager.isUserUnlocked(user);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001755
1756 // We can only query for shortcuts when the user is unlocked.
1757 if (userUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07001758 List<ShortcutInfoCompat> pinnedShortcuts =
1759 mDeepShortcutManager.queryForPinnedShortcuts(null, user);
1760 if (mDeepShortcutManager.wasLastCallSuccess()) {
1761 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
1762 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
1763 shortcut);
1764 }
1765 } else {
1766 // Shortcut manager can fail due to some race condition when the
1767 // lock state changes too frequently. For the purpose of the loading
1768 // shortcuts, consider the user is still locked.
1769 userUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001770 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001771 }
Sunny Goyal49f4f032016-08-01 15:45:49 -07001772 unlockedUsers.put(serialNo, userUnlocked);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001773 }
1774
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001775 ShortcutInfo info;
1776 String intentDescription;
1777 LauncherAppWidgetInfo appWidgetInfo;
1778 int container;
1779 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001780 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001781 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001782 UserHandleCompat user;
Sunny Goyald09c3702016-04-06 16:18:20 -07001783 String targetPackage;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001784
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001785 while (!mStopped && c.moveToNext()) {
1786 try {
1787 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001788 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001789 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001790 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001791
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001792 switch (itemType) {
1793 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1794 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001795 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001796 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001797 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001798 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001799 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001800 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001801 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001802 boolean itemReplaced = false;
Sunny Goyald09c3702016-04-06 16:18:20 -07001803 targetPackage = null;
Kenny Guyed131872014-04-30 03:02:21 +01001804 if (user == null) {
1805 // User has been deleted remove the item.
1806 itemsToRemove.add(id);
1807 continue;
1808 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001809 try {
1810 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001811 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001812 if (cn != null && cn.getPackageName() != null) {
1813 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1814 cn.getPackageName(), user);
1815 boolean validComponent = validPkg &&
1816 launcherApps.isActivityEnabledForProfile(cn, user);
Sunny Goyald09c3702016-04-06 16:18:20 -07001817 if (validPkg) {
1818 targetPackage = cn.getPackageName();
1819 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001820
1821 if (validComponent) {
1822 if (restored) {
1823 // no special handling necessary for this item
1824 restoredRows.add(id);
1825 restored = false;
1826 }
Kenny Guyff05f432016-01-22 17:48:29 +00001827 if (quietMode.get(serialNumber)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001828 disabledState = ShortcutInfo.FLAG_DISABLED_QUIET_USER;
Kenny Guyff05f432016-01-22 17:48:29 +00001829 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001830 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001831 intent = null;
1832 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1833 // We allow auto install apps to have their intent
1834 // updated after an install.
1835 intent = manager.getLaunchIntentForPackage(
1836 cn.getPackageName());
1837 if (intent != null) {
1838 ContentValues values = new ContentValues();
1839 values.put(LauncherSettings.Favorites.INTENT,
1840 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001841 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001842 }
1843 }
1844
1845 if (intent == null) {
1846 // The app is installed but the component is no
1847 // longer available.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001848 FileLog.d(TAG, "Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001849 itemsToRemove.add(id);
1850 continue;
1851 } else {
1852 // no special handling necessary for this item
1853 restoredRows.add(id);
1854 restored = false;
1855 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001856 } else if (restored) {
1857 // Package is not yet available but might be
1858 // installed later.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001859 FileLog.d(TAG, "package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001860
1861 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1862 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001863 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001864 // App restore has started. Update the flag
1865 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1866 ContentValues values = new ContentValues();
1867 values.put(LauncherSettings.Favorites.RESTORED,
1868 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001869 updateItem(id, values);
1870 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1871 // This is a common app. Try to replace this.
1872 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1873 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1874 if (parser.findDefaultApp()) {
1875 // Default app found. Replace it.
1876 intent = parser.parsedIntent;
1877 cn = intent.getComponent();
1878 ContentValues values = parser.parsedValues;
1879 values.put(LauncherSettings.Favorites.RESTORED, 0);
1880 updateItem(id, values);
1881 restored = false;
1882 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001883
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001884 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001885 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001886 itemsToRemove.add(id);
1887 continue;
1888 }
Sunny Goyal94485362014-09-18 16:13:58 -07001889 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001890 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001891 itemsToRemove.add(id);
1892 continue;
1893 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001894 } else if (PackageManagerHelper.isAppOnSdcard(
1895 manager, cn.getPackageName())) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07001896 // Package is present but not available.
1897 allowMissingTarget = true;
1898 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1899 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001900 // SdCard is not ready yet. Package might get available,
1901 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001902 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001903 HashSet<String> pkgs = sPendingPackages.get(user);
1904 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001905 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001906 sPendingPackages.put(user, pkgs);
1907 }
1908 pkgs.add(cn.getPackageName());
1909 allowMissingTarget = true;
1910 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001911
1912 } else {
1913 // Do not wait for external media load anymore.
1914 // Log the invalid package, and remove it
Sunny Goyal713edfc2016-05-06 09:58:34 -07001915 FileLog.d(TAG, "Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001916 itemsToRemove.add(id);
1917 continue;
Winson Chungee055712013-07-30 14:46:24 -07001918 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001919 } else if (cn == null) {
1920 // For shortcuts with no component, keep them as they are
1921 restoredRows.add(id);
1922 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001923 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001924 } catch (URISyntaxException e) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001925 FileLog.d(TAG, "Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001926 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001927 continue;
1928 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001929
Sunny Goyal34b65272015-03-11 16:56:52 -07001930 boolean useLowResIcon = container >= 0 &&
1931 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1932
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001933 if (itemReplaced) {
1934 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001935 info = getAppShortcutInfo(intent, user, null,
1936 cursorIconInfo, false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001937 } else {
1938 // Don't replace items for other profiles.
1939 itemsToRemove.add(id);
1940 continue;
1941 }
1942 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001943 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001944 info = getRestoredItemInfo(c, intent,
1945 promiseType, itemType, cursorIconInfo);
Kenny Guyed131872014-04-30 03:02:21 +01001946 intent = getRestoredItemIntent(c, context, intent);
1947 } else {
1948 // Don't restore items for other profiles.
1949 itemsToRemove.add(id);
1950 continue;
1951 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001952 } else if (itemType ==
1953 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001954 info = getAppShortcutInfo(intent, user, c,
1955 cursorIconInfo, allowMissingTarget, useLowResIcon);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001956 } else if (itemType ==
1957 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001958
1959 ShortcutKey key = ShortcutKey.fromIntent(intent, user);
1960 if (unlockedUsers.get(serialNumber)) {
1961 ShortcutInfoCompat pinnedShortcut =
1962 shortcutKeyToPinnedShortcuts.get(key);
1963 if (pinnedShortcut == null) {
1964 // The shortcut is no longer valid.
1965 itemsToRemove.add(id);
1966 continue;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001967 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001968 info = new ShortcutInfo(pinnedShortcut, context);
1969 intent = info.intent;
1970 } else {
1971 // Create a shortcut info in disabled mode for now.
1972 info = new ShortcutInfo();
1973 info.user = user;
1974 info.itemType = itemType;
1975 loadInfoFromCursor(info, c, cursorIconInfo);
1976
1977 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001978 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001979 incrementPinnedShortcutCount(key, false /* shouldPin */);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001980 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001981 info = getShortcutInfo(c, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001982
Sunny Goyald09c3702016-04-06 16:18:20 -07001983 // Shortcuts are only available on the primary profile
1984 if (PackageManagerHelper.isAppSuspended(manager, targetPackage)) {
1985 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1986 }
1987
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001988 // App shortcuts that used to be automatically added to Launcher
1989 // didn't always have the correct intent flags set, so do that
1990 // here
1991 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001992 intent.getCategories() != null &&
1993 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001994 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001995 intent.addFlags(
1996 Intent.FLAG_ACTIVITY_NEW_TASK |
1997 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1998 }
Michael Jurka96879562012-03-22 05:54:33 -07001999 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002000
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002001 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07002002 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002003 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002004 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002005 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002006 info.cellX = c.getInt(cellXIndex);
2007 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08002008 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002009 info.spanX = 1;
2010 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002011 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002012 if (info.promisedIntent != null) {
2013 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
2014 }
Sunny Goyald09c3702016-04-06 16:18:20 -07002015 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002016 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2017 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2018 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002019
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002020 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002021 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002022 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002023 break;
2024 }
2025
Sunny Goyal756adbc2015-04-16 15:20:51 -07002026 if (restored) {
2027 ComponentName cn = info.getTargetComponent();
2028 if (cn != null) {
2029 Integer progress = installingPkgs.get(cn.getPackageName());
2030 if (progress != null) {
2031 info.setInstallProgress(progress);
2032 } else {
2033 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2034 }
2035 }
2036 }
2037
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002038 switch (container) {
2039 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2040 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2041 sBgWorkspaceItems.add(info);
2042 break;
2043 default:
2044 // Item is in a user folder
2045 FolderInfo folderInfo =
2046 findOrMakeFolder(sBgFolders, container);
Sunny Goyalc52ba712016-04-05 15:59:05 -07002047 folderInfo.add(info, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002048 break;
2049 }
2050 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002051 } else {
2052 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002053 }
2054 break;
2055
2056 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2057 id = c.getLong(idIndex);
2058 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2059
Sunny Goyala508e4f2015-05-21 09:33:57 -07002060 // Do not trim the folder label, as is was set by the user.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002061 folderInfo.title = c.getString(cursorIconInfo.titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002062 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002063 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002064 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002065 folderInfo.cellX = c.getInt(cellXIndex);
2066 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002067 folderInfo.spanX = 1;
2068 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002069 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002070
Daniel Sandler8802e962010-05-26 16:28:16 -04002071 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002072 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002073 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002074 break;
2075 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002076
Joe Onorato9c1289c2009-08-17 11:03:03 -04002077 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002078 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2079 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2080 sBgWorkspaceItems.add(folderInfo);
2081 break;
Joe Onorato36115782010-06-17 13:28:48 -04002082 }
Joe Onorato17a89222011-02-08 17:26:11 -08002083
Chris Wrenf4d08112014-01-16 18:13:56 -05002084 if (restored) {
2085 // no special handling required for restored folders
2086 restoredRows.add(id);
2087 }
2088
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002089 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2090 sBgFolders.put(folderInfo.id, folderInfo);
2091 break;
2092
2093 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002094 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002095 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002096 boolean customWidget = itemType ==
2097 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2098
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002099 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002100 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002101 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002102 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002103 user = allUsers.get(serialNumber);
2104 if (user == null) {
2105 itemsToRemove.add(id);
2106 continue;
2107 }
2108
Sunny Goyalff572272014-07-23 13:58:07 -07002109 final ComponentName component =
2110 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002111
Sunny Goyal651077b2014-06-30 14:15:31 -07002112 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002113 final boolean isIdValid = (restoreStatus &
2114 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002115 final boolean wasProviderReady = (restoreStatus &
2116 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002117
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002118 if (widgetProvidersMap == null) {
2119 widgetProvidersMap = AppWidgetManagerCompat
2120 .getInstance(mContext).getAllProvidersMap();
2121 }
2122 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
2123 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00002124 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002125 user));
Sunny Goyalff572272014-07-23 13:58:07 -07002126
2127 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002128 if (!isSafeMode && !customWidget &&
2129 wasProviderReady && !isProviderReady) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002130 FileLog.d(TAG, "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07002131 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002132 itemsToRemove.add(id);
2133 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002134 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002135 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2136 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002137
Sunny Goyal53f96722015-07-13 19:54:53 -07002138 // The provider is available. So the widget is either
2139 // available or not available. We do not need to track
2140 // any future restore updates.
2141 int status = restoreStatus &
2142 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002143 if (!wasProviderReady) {
2144 // If provider was not previously ready, update the
2145 // status and UI flag.
2146
2147 // Id would be valid only if the widget restore broadcast was received.
2148 if (isIdValid) {
Sunny Goyal86df1382016-08-10 15:03:22 -07002149 status |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002150 } else {
2151 status &= ~LauncherAppWidgetInfo
2152 .FLAG_PROVIDER_NOT_READY;
2153 }
2154 }
2155 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002156 } else {
2157 Log.v(TAG, "Widget restore pending id=" + id
2158 + " appWidgetId=" + appWidgetId
2159 + " status =" + restoreStatus);
2160 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002161 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002162 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002163 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002164
2165 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2166 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002167 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002168 // App restore has started. Update the flag
2169 appWidgetInfo.restoreStatus |=
2170 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002171 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002172 FileLog.d(TAG, "Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07002173 itemsToRemove.add(id);
2174 continue;
2175 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002176
2177 appWidgetInfo.installProgress =
2178 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002179 }
Sunny Goyal86df1382016-08-10 15:03:22 -07002180 if (appWidgetInfo.hasRestoreFlag(
2181 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG)) {
2182 intentDescription = c.getString(intentIndex);
2183 if (!TextUtils.isEmpty(intentDescription)) {
2184 appWidgetInfo.bindOptions =
2185 Intent.parseUri(intentDescription, 0);
2186 }
2187 }
Sunny Goyalff572272014-07-23 13:58:07 -07002188
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002189 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002190 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002191 appWidgetInfo.cellX = c.getInt(cellXIndex);
2192 appWidgetInfo.cellY = c.getInt(cellYIndex);
2193 appWidgetInfo.spanX = c.getInt(spanXIndex);
2194 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002195 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002196
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002197 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2198 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2199 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002200 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2201 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002202 continue;
2203 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002204
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002205 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002206 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002207 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002208 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002209 break;
2210 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002211
Adam Cohen59400422014-03-05 18:07:04 -08002212 if (!customWidget) {
2213 String providerName =
2214 appWidgetInfo.providerName.flattenToString();
2215 if (!providerName.equals(savedProvider) ||
2216 (appWidgetInfo.restoreStatus != restoreStatus)) {
2217 ContentValues values = new ContentValues();
2218 values.put(
2219 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2220 providerName);
2221 values.put(LauncherSettings.Favorites.RESTORED,
2222 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002223 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002224 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002225 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002226 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2227 sBgAppWidgets.add(appWidgetInfo);
2228 }
Joe Onorato36115782010-06-17 13:28:48 -04002229 break;
2230 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002231 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002232 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002233 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002234 }
2235 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002236 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002237 }
2238
Winson Chungba9c37f2013-08-30 14:11:37 -07002239 // Break early if we've stopped loading
2240 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002241 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002242 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002243 }
2244
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002245 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002246 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002247 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2248 Utilities.createDbSelectionQuery(
2249 LauncherSettings.Favorites._ID, itemsToRemove), null);
2250 if (DEBUG_LOADERS) {
2251 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2252 LauncherSettings.Favorites._ID, itemsToRemove));
2253 }
2254
2255 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07002256 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
2257 .call(contentResolver,
2258 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
2259 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
2260 for (long folderId : deletedFolderIds) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002261 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2262 sBgFolders.remove(folderId);
2263 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002264 }
2265 }
2266
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002267 // Unpin shortcuts that don't exist on the workspace.
2268 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
2269 MutableInt numTimesPinned = sBgPinnedShortcutCounts.get(key);
2270 if (numTimesPinned == null || numTimesPinned.value == 0) {
2271 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
2272 mDeepShortcutManager.unpinShortcut(key);
2273 }
2274 }
2275
Sunny Goyal317698b2015-07-29 11:45:41 -07002276 // Sort all the folder items and make sure the first 3 items are high resolution.
2277 for (FolderInfo folder : sBgFolders) {
2278 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2279 int pos = 0;
2280 for (ShortcutInfo info : folder.contents) {
2281 if (info.usingLowResIcon) {
2282 info.updateIcon(mIconCache, false);
2283 }
2284 pos ++;
2285 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2286 break;
2287 }
2288 }
2289 }
2290
Chris Wrenf4d08112014-01-16 18:13:56 -05002291 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002292 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002293 ContentValues values = new ContentValues();
2294 values.put(LauncherSettings.Favorites.RESTORED, 0);
2295 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2296 Utilities.createDbSelectionQuery(
2297 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002298 }
2299
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002300 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2301 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002302 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002303 null, sWorker);
2304 }
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
Adam Cohen091440a2015-03-18 14:16:05 -07002973 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002974
2975 @Override
2976 public void onReceive(Context context, Intent intent) {
2977 synchronized (sBgLock) {
2978 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2979 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002980 final PackageManager manager = context.getPackageManager();
2981 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2982 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002983 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2984 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002985 packagesRemoved.clear();
2986 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002987 for (String pkg : entry.getValue()) {
2988 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07002989 if (PackageManagerHelper.isAppOnSdcard(manager, pkg)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002990 packagesUnavailable.add(pkg);
2991 } else {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002992 packagesRemoved.add(pkg);
2993 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002994 }
2995 }
2996 if (!packagesRemoved.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002997 enqueueItemUpdatedTask(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002998 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2999 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003000 if (!packagesUnavailable.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003001 enqueueItemUpdatedTask(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
Sunny Goyal1a745e82014-10-02 15:58:31 -07003002 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
3003 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003004 }
Sunny Goyal34942622014-08-29 17:20:55 -07003005 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003006 }
3007 }
3008 }
3009
Joe Onorato36115782010-06-17 13:28:48 -04003010 private class PackageUpdatedTask implements Runnable {
3011 int mOp;
3012 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01003013 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04003014
3015 public static final int OP_NONE = 0;
3016 public static final int OP_ADD = 1;
3017 public static final int OP_UPDATE = 2;
3018 public static final int OP_REMOVE = 3; // uninstlled
3019 public static final int OP_UNAVAILABLE = 4; // external media unmounted
Kenny Guy44cba692016-01-21 19:50:02 +00003020 public static final int OP_SUSPEND = 5; // package suspended
3021 public static final int OP_UNSUSPEND = 6; // package unsuspended
Sunny Goyalda891c12016-03-18 18:29:24 -07003022 public static final int OP_USER_AVAILABILITY_CHANGE = 7; // user available/unavailable
Joe Onorato36115782010-06-17 13:28:48 -04003023
Kenny Guyed131872014-04-30 03:02:21 +01003024 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04003025 mOp = op;
3026 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01003027 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04003028 }
3029
3030 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07003031 if (!mHasLoaderCompletedOnce) {
3032 // Loader has not yet run.
3033 return;
3034 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04003035 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04003036
3037 final String[] packages = mPackages;
3038 final int N = packages.length;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003039 FlagOp flagOp = FlagOp.NO_OP;
Sunny Goyalda891c12016-03-18 18:29:24 -07003040 StringFilter pkgFilter = StringFilter.of(new HashSet<>(Arrays.asList(packages)));
Joe Onorato36115782010-06-17 13:28:48 -04003041 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003042 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04003043 for (int i=0; i<N; i++) {
3044 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003045 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003046 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003047 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003048
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003049 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3050 if (heuristic != null) {
3051 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003052 }
Joe Onorato36115782010-06-17 13:28:48 -04003053 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003054 }
Joe Onorato36115782010-06-17 13:28:48 -04003055 case OP_UPDATE:
3056 for (int i=0; i<N; i++) {
3057 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003058 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003059 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003060 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003061 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003062 // Since package was just updated, the target must be available now.
3063 flagOp = FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04003064 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003065 case OP_REMOVE: {
3066 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3067 if (heuristic != null) {
3068 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003069 }
Joe Onorato36115782010-06-17 13:28:48 -04003070 for (int i=0; i<N; i++) {
3071 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003072 mIconCache.removeIconsForPkg(packages[i], mUser);
3073 }
3074 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003075 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003076 case OP_UNAVAILABLE:
3077 for (int i=0; i<N; i++) {
3078 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3079 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003080 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003081 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003082 flagOp = FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04003083 break;
Kenny Guy44cba692016-01-21 19:50:02 +00003084 case OP_SUSPEND:
3085 case OP_UNSUSPEND:
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003086 flagOp = mOp == OP_SUSPEND ?
3087 FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED) :
3088 FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED);
Sunny Goyal17763cb2016-03-24 13:53:22 -07003089 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.(un)suspend " + N);
Sunny Goyalda891c12016-03-18 18:29:24 -07003090 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
3091 break;
3092 case OP_USER_AVAILABILITY_CHANGE:
3093 flagOp = UserManagerCompat.getInstance(context).isQuietModeEnabled(mUser)
3094 ? FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER)
3095 : FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER);
3096 // We want to update all packages for this user.
3097 pkgFilter = StringFilter.matchesAll();
3098 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
Kenny Guy44cba692016-01-21 19:50:02 +00003099 break;
Joe Onorato36115782010-06-17 13:28:48 -04003100 }
3101
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003102 ArrayList<AppInfo> added = null;
3103 ArrayList<AppInfo> modified = null;
3104 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003105
Adam Cohen487f7dd2012-06-28 18:12:10 -07003106 if (mBgAllAppsList.added.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003107 added = new ArrayList<>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003108 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003109 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003110 if (mBgAllAppsList.modified.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003111 modified = new ArrayList<>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003112 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003113 }
Winson Chung5d55f332012-07-16 20:45:03 -07003114 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003115 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003116 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003117 }
3118
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003119 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps = new HashMap<>();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003120
Joe Onorato36115782010-06-17 13:28:48 -04003121 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003122 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003123 for (AppInfo ai : added) {
3124 addedOrUpdatedApps.put(ai.componentName, ai);
3125 }
Joe Onorato36115782010-06-17 13:28:48 -04003126 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003127
Joe Onorato36115782010-06-17 13:28:48 -04003128 if (modified != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003129 final Callbacks callbacks = getCallback();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003130 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003131 for (AppInfo ai : modified) {
3132 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003133 }
3134
Joe Onorato36115782010-06-17 13:28:48 -04003135 mHandler.post(new Runnable() {
3136 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003137 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003138 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003139 callbacks.bindAppsUpdated(modifiedFinal);
3140 }
3141 }
3142 });
3143 }
Winson Chung83892cc2013-05-01 16:53:33 -07003144
Sunny Goyal4390ace2014-10-13 11:33:11 -07003145 // Update shortcut infos
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003146 if (mOp == OP_ADD || flagOp != FlagOp.NO_OP) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003147 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3148 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3149 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3150
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003151 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003152 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003153 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3154 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003155 boolean infoUpdated = false;
3156 boolean shortcutUpdated = false;
3157
3158 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003159 if ((si.iconResource != null)
Sunny Goyalda891c12016-03-18 18:29:24 -07003160 && pkgFilter.matches(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003161 Bitmap icon = Utilities.createIconBitmap(
3162 si.iconResource.packageName,
3163 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003164 if (icon != null) {
3165 si.setIcon(icon);
3166 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003167 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003168 }
3169 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003170
3171 ComponentName cn = si.getTargetComponent();
Sunny Goyalda891c12016-03-18 18:29:24 -07003172 if (cn != null && pkgFilter.matches(cn.getPackageName())) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003173 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3174
3175 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003176 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3177 // Auto install icon
3178 PackageManager pm = context.getPackageManager();
3179 ResolveInfo matched = pm.resolveActivity(
3180 new Intent(Intent.ACTION_MAIN)
3181 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3182 PackageManager.MATCH_DEFAULT_ONLY);
3183 if (matched == null) {
3184 // Try to find the best match activity.
3185 Intent intent = pm.getLaunchIntentForPackage(
3186 cn.getPackageName());
3187 if (intent != null) {
3188 cn = intent.getComponent();
3189 appInfo = addedOrUpdatedApps.get(cn);
3190 }
3191
3192 if ((intent == null) || (appInfo == null)) {
3193 removedShortcuts.add(si);
3194 continue;
3195 }
3196 si.promisedIntent = intent;
3197 }
3198 }
3199
3200 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003201 if (appInfo != null) {
3202 si.flags = appInfo.flags;
3203 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003204
Sunny Goyal756adbc2015-04-16 15:20:51 -07003205 si.intent = si.promisedIntent;
3206 si.promisedIntent = null;
3207 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003208 infoUpdated = true;
3209 si.updateIcon(mIconCache);
3210 }
3211
3212 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3213 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3214 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003215 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003216 si.contentDescription = appInfo.contentDescription;
3217 infoUpdated = true;
3218 }
3219
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003220 int oldDisabledFlags = si.isDisabled;
3221 si.isDisabled = flagOp.apply(si.isDisabled);
3222 if (si.isDisabled != oldDisabledFlags) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003223 shortcutUpdated = true;
3224 }
3225 }
3226
3227 if (infoUpdated || shortcutUpdated) {
3228 updatedShortcuts.add(si);
3229 }
3230 if (infoUpdated) {
3231 updateItemInDatabase(context, si);
3232 }
Sunny Goyalda891c12016-03-18 18:29:24 -07003233 } else if (info instanceof LauncherAppWidgetInfo && mOp == OP_ADD) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003234 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3235 if (mUser.equals(widgetInfo.user)
3236 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
Sunny Goyalda891c12016-03-18 18:29:24 -07003237 && pkgFilter.matches(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003238 widgetInfo.restoreStatus &=
3239 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3240 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003241
3242 // adding this flag ensures that launcher shows 'click to setup'
3243 // if the widget has a config activity. In case there is no config
3244 // activity, it will be marked as 'restored' during bind.
3245 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3246
Sunny Goyal4390ace2014-10-13 11:33:11 -07003247 widgets.add(widgetInfo);
3248 updateItemInDatabase(context, widgetInfo);
3249 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003250 }
3251 }
3252 }
3253
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003254 bindUpdatedShortcuts(updatedShortcuts, removedShortcuts, mUser);
3255 if (!removedShortcuts.isEmpty()) {
3256 deleteItemsFromDatabase(context, removedShortcuts);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003257 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003258
Sunny Goyal4390ace2014-10-13 11:33:11 -07003259 if (!widgets.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003260 final Callbacks callbacks = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003261 mHandler.post(new Runnable() {
3262 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003263 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003264 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003265 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003266 }
3267 }
3268 });
3269 }
3270 }
3271
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003272 final HashSet<String> removedPackages = new HashSet<>();
3273 final HashSet<ComponentName> removedComponents = new HashSet<>();
3274 if (mOp == OP_REMOVE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003275 // Mark all packages in the broadcast to be removed
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003276 Collections.addAll(removedPackages, packages);
3277
3278 // No need to update the removedComponents as
3279 // removedPackages is a super-set of removedComponents
Winson Chungdf95eb12013-10-16 14:57:07 -07003280 } else if (mOp == OP_UPDATE) {
3281 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003282 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003283 if (isPackageDisabled(context, packages[i], mUser)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003284 removedPackages.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003285 }
3286 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003287
3288 // Update removedComponents as some components can get removed during package update
3289 for (AppInfo info : removedApps) {
3290 removedComponents.add(info.componentName);
3291 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003292 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003293
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003294 if (!removedPackages.isEmpty() || !removedComponents.isEmpty()) {
3295 for (String pn : removedPackages) {
3296 deletePackageFromDatabase(context, pn, mUser);
3297 }
3298 for (ComponentName cn : removedComponents) {
3299 deleteItemsFromDatabase(context, getItemInfoForComponentName(cn, mUser));
Sunny Goyal1a745e82014-10-02 15:58:31 -07003300 }
3301
Winson Chungdf95eb12013-10-16 14:57:07 -07003302 // Remove any queued items from the install queue
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003303 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackages, mUser);
3304
Winson Chungdf95eb12013-10-16 14:57:07 -07003305 // Call the components-removed callback
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003306 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003307 mHandler.post(new Runnable() {
3308 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003309 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003310 if (callbacks == cb && cb != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003311 callbacks.bindWorkspaceComponentsRemoved(
3312 removedPackages, removedComponents, mUser);
3313 }
3314 }
3315 });
3316 }
3317
3318 if (!removedApps.isEmpty()) {
3319 // Remove corresponding apps from All-Apps
3320 final Callbacks callbacks = getCallback();
3321 mHandler.post(new Runnable() {
3322 public void run() {
3323 Callbacks cb = getCallback();
3324 if (callbacks == cb && cb != null) {
3325 callbacks.bindAppInfosRemoved(removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04003326 }
3327 }
3328 });
Joe Onoratobe386092009-11-17 17:32:16 -08003329 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003330
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003331 // Notify launcher of widget update. From marshmallow onwards we use AppWidgetHost to
3332 // get widget update signals.
3333 if (!Utilities.ATLEAST_MARSHMALLOW &&
3334 (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003335 final Callbacks callbacks = getCallback();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003336 mHandler.post(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003337 public void run() {
3338 Callbacks cb = getCallback();
3339 if (callbacks == cb && cb != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003340 callbacks.notifyWidgetProvidersChanged();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003341 }
3342 }
3343 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003344 }
3345 }
3346 }
3347
Sunny Goyal10923b32016-07-20 15:42:44 -07003348 /**
3349 * Repopulates the shortcut info, possibly updating any icon already on the workspace.
3350 */
3351 public void updateShortcutInfo(final ShortcutInfoCompat fullDetail, final ShortcutInfo info) {
3352 enqueueItemUpdatedTask(new Runnable() {
3353 @Override
3354 public void run() {
3355 info.updateFromDeepShortcutInfo(
3356 fullDetail, LauncherAppState.getInstance().getContext());
3357 ArrayList<ShortcutInfo> update = new ArrayList<ShortcutInfo>();
3358 update.add(info);
3359 bindUpdatedShortcuts(update, fullDetail.getUserHandle());
3360 }
3361 });
3362 }
3363
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003364 private class ShortcutsChangedTask implements Runnable {
Sunny Goyal50941fb2016-08-04 12:03:52 -07003365 private final String mPackageName;
3366 private final List<ShortcutInfoCompat> mShortcuts;
3367 private final UserHandleCompat mUser;
3368 private final boolean mUpdateIdMap;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003369
3370 public ShortcutsChangedTask(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal50941fb2016-08-04 12:03:52 -07003371 UserHandleCompat user, boolean updateIdMap) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003372 mPackageName = packageName;
3373 mShortcuts = shortcuts;
3374 mUser = user;
Sunny Goyal50941fb2016-08-04 12:03:52 -07003375 mUpdateIdMap = updateIdMap;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003376 }
3377
3378 @Override
3379 public void run() {
3380 mDeepShortcutManager.onShortcutsChanged(mShortcuts);
3381
3382 Map<String, ShortcutInfoCompat> idsToShortcuts = new HashMap<>();
3383 for (ShortcutInfoCompat shortcut : mShortcuts) {
3384 idsToShortcuts.put(shortcut.getId(), shortcut);
3385 }
3386
3387 // Find ShortcutInfo's that have changed on the workspace.
3388 MultiHashMap<String, ShortcutInfo> idsToWorkspaceShortcutInfos = new MultiHashMap<>();
3389 for (ItemInfo itemInfo : sBgItemsIdMap) {
3390 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
3391 ShortcutInfo si = (ShortcutInfo) itemInfo;
Tony Wickham672d07f2016-07-27 18:22:41 -07003392 if (si.getIntent().getPackage().equals(mPackageName) && si.user.equals(mUser)) {
3393 String shortcutId = si.getDeepShortcutId();
3394 if (idsToShortcuts.containsKey(shortcutId)) {
3395 idsToWorkspaceShortcutInfos.addToList(shortcutId, si);
3396 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003397 }
3398 }
3399 }
3400
3401 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3402 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager.queryForFullDetails(
3403 mPackageName, new ArrayList<>(idsToWorkspaceShortcutInfos.keySet()), mUser);
3404 ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3405 Context context = LauncherAppState.getInstance().getContext();
3406 for (ShortcutInfoCompat fullDetails : shortcuts) {
3407 List<ShortcutInfo> shortcutInfos = idsToWorkspaceShortcutInfos
3408 .get(fullDetails.getId());
3409 for (ShortcutInfo shortcutInfo : shortcutInfos) {
Sunny Goyal9994b2b2016-06-23 14:17:24 -07003410 shortcutInfo.updateFromDeepShortcutInfo(fullDetails, context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003411 updatedShortcutInfos.add(shortcutInfo);
3412 }
3413 }
3414 bindUpdatedShortcuts(updatedShortcutInfos, mUser);
3415
Sunny Goyal50941fb2016-08-04 12:03:52 -07003416 if (mUpdateIdMap) {
3417 // Update the deep shortcut map if the list of ids has changed for an activity.
3418 updateDeepShortcutMap(mPackageName, mUser, mShortcuts);
3419 bindDeepShortcuts();
3420 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003421 }
3422 }
3423
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003424 /**
3425 * Task to handle changing of lock state of the user
3426 */
3427 private class UserLockStateChangedTask implements Runnable {
3428
3429 private final UserHandleCompat mUser;
3430
3431 public UserLockStateChangedTask(UserHandleCompat user) {
3432 mUser = user;
3433 }
3434
3435 @Override
3436 public void run() {
3437 boolean isUserUnlocked = mUserManager.isUserUnlocked(mUser);
3438 Context context = mApp.getContext();
3439
3440 HashMap<ShortcutKey, ShortcutInfoCompat> pinnedShortcuts = new HashMap<>();
3441 if (isUserUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07003442 List<ShortcutInfoCompat> shortcuts =
3443 mDeepShortcutManager.queryForPinnedShortcuts(null, mUser);
3444 if (mDeepShortcutManager.wasLastCallSuccess()) {
3445 for (ShortcutInfoCompat shortcut : shortcuts) {
3446 pinnedShortcuts.put(ShortcutKey.fromInfo(shortcut), shortcut);
3447 }
3448 } else {
3449 // Shortcut manager can fail due to some race condition when the lock state
3450 // changes too frequently. For the purpose of the update,
3451 // consider it as still locked.
3452 isUserUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003453 }
3454 }
3455
3456 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3457 ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3458 ArrayList<ShortcutInfo> deletedShortcutInfos = new ArrayList<>();
3459 for (ItemInfo itemInfo : sBgItemsIdMap) {
3460 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT
3461 && mUser.equals(itemInfo.user)) {
3462 ShortcutInfo si = (ShortcutInfo) itemInfo;
3463 if (isUserUnlocked) {
3464 ShortcutInfoCompat shortcut =
3465 pinnedShortcuts.get(ShortcutKey.fromItemInfo(si));
3466 // We couldn't verify the shortcut during loader. If its no longer available
3467 // (probably due to clear data), delete the workspace item as well
3468 if (shortcut == null) {
3469 deletedShortcutInfos.add(si);
3470 continue;
3471 }
3472 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
3473 si.updateFromDeepShortcutInfo(shortcut, context);
3474 } else {
3475 si.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
3476 }
3477 updatedShortcutInfos.add(si);
3478 }
3479 }
3480 bindUpdatedShortcuts(updatedShortcutInfos, deletedShortcutInfos, mUser);
3481 if (!deletedShortcutInfos.isEmpty()) {
3482 deleteItemsFromDatabase(context, deletedShortcutInfos);
3483 }
3484
3485 // Remove shortcut id map for that user
3486 Iterator<ComponentKey> keysIter = mBgDeepShortcutMap.keySet().iterator();
3487 while (keysIter.hasNext()) {
3488 if (keysIter.next().user.equals(mUser)) {
3489 keysIter.remove();
3490 }
3491 }
3492
3493 if (isUserUnlocked) {
3494 updateDeepShortcutMap(null, mUser, mDeepShortcutManager.queryForAllShortcuts(mUser));
3495 }
3496 bindDeepShortcuts();
3497 }
3498 }
3499
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003500 private void bindWidgetsModel(final Callbacks callbacks, final WidgetsModel model) {
3501 mHandler.post(new Runnable() {
3502 @Override
3503 public void run() {
3504 Callbacks cb = getCallback();
3505 if (callbacks == cb && cb != null) {
3506 callbacks.bindWidgetsModel(model);
3507 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003508 }
3509 });
3510 }
3511
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003512 public void refreshAndBindWidgetsAndShortcuts(
3513 final Callbacks callbacks, final boolean bindFirst) {
3514 runOnWorkerThread(new Runnable() {
3515 @Override
3516 public void run() {
3517 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
3518 bindWidgetsModel(callbacks, mBgWidgetsModel.clone());
Sunny Goyal31860582015-09-18 08:38:57 -07003519 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003520 final WidgetsModel model = mBgWidgetsModel.updateAndClone(mApp.getContext());
3521 bindWidgetsModel(callbacks, model);
3522 // update the Widget entries inside DB on the worker thread.
3523 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3524 model.getRawList());
Sunny Goyal31860582015-09-18 08:38:57 -07003525 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003526 });
Michael Jurkac402cd92013-05-20 15:49:32 +02003527 }
3528
Adam Cohen091440a2015-03-18 14:16:05 -07003529 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003530 UserHandleCompat user) {
3531 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3532 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003533 }
Adam Cohen556f6132014-01-15 15:18:08 -08003534
Kenny Guyed131872014-04-30 03:02:21 +01003535 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3536 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003537 if (cn == null) {
3538 return false;
3539 }
Kenny Guyed131872014-04-30 03:02:21 +01003540 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3541 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003542 return false;
3543 }
Kenny Guyed131872014-04-30 03:02:21 +01003544 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003545 }
3546
Adam Cohena28b78e2014-05-20 17:03:04 -07003547 public static boolean isValidPackage(Context context, String packageName,
3548 UserHandleCompat user) {
3549 if (packageName == null) {
3550 return false;
3551 }
3552 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3553 return launcherApps.isPackageEnabledForProfile(packageName, user);
3554 }
3555
Joe Onorato9c1289c2009-08-17 11:03:03 -04003556 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003557 * Make an ShortcutInfo object for a restored application or shortcut item that points
3558 * to a package that is not yet installed on the system.
3559 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003560 public ShortcutInfo getRestoredItemInfo(Cursor c, Intent intent,
3561 int promiseType, int itemType, CursorIconInfo iconInfo) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003562 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003563 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003564
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003565 Bitmap icon = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003566 // the fallback icon
3567 if (icon == null) {
3568 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3569 } else {
3570 info.setIcon(icon);
3571 }
Sunny Goyal34942622014-08-29 17:20:55 -07003572
3573 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003574 String title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07003575 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003576 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003577 }
Sunny Goyal34942622014-08-29 17:20:55 -07003578 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3579 if (TextUtils.isEmpty(info.title)) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003580 info.title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07003581 }
Sunny Goyal34942622014-08-29 17:20:55 -07003582 } else {
3583 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3584 }
3585
Winson Chung82b016c2015-05-08 17:00:10 -07003586 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003587 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003588 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003589 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003590 return info;
3591 }
3592
3593 /**
3594 * Make an Intent object for a restored application or shortcut item that points
3595 * to the market page for the item.
3596 */
Adam Cohen091440a2015-03-18 14:16:05 -07003597 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003598 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003599 return getMarketIntent(componentName.getPackageName());
3600 }
3601
3602 static Intent getMarketIntent(String packageName) {
3603 return new Intent(Intent.ACTION_VIEW)
3604 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003605 .scheme("market")
3606 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003607 .appendQueryParameter("id", packageName)
3608 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003609 }
3610
3611 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003612 * Make an ShortcutInfo object for a shortcut that is an application.
3613 *
3614 * If c is not null, then it will be used to fill in missing data like the title and icon.
3615 */
Sunny Goyald09c3702016-04-06 16:18:20 -07003616 public ShortcutInfo getAppShortcutInfo(Intent intent,
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003617 UserHandleCompat user, Cursor c, CursorIconInfo iconInfo,
Sunny Goyal34b65272015-03-11 16:56:52 -07003618 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003619 if (user == null) {
3620 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003621 return null;
3622 }
3623
Kenny Guyed131872014-04-30 03:02:21 +01003624 ComponentName componentName = intent.getComponent();
3625 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08003626 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01003627 return null;
3628 }
3629
3630 Intent newIntent = new Intent(intent.getAction(), null);
3631 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3632 newIntent.setComponent(componentName);
3633 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003634 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003635 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3636 return null;
3637 }
3638
3639 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003640 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003641 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003642 Bitmap icon = iconInfo.loadIcon(c);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003643 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003644 }
3645
Sunny Goyald09c3702016-04-06 16:18:20 -07003646 if (lai != null && PackageManagerHelper.isAppSuspended(lai.getApplicationInfo())) {
3647 info.isDisabled = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
3648 }
3649
Joe Onorato56d82912010-03-07 14:32:10 -05003650 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003651 if (TextUtils.isEmpty(info.title) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003652 info.title = iconInfo.getTitle(c);
Joe Onorato56d82912010-03-07 14:32:10 -05003653 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003654
Joe Onorato56d82912010-03-07 14:32:10 -05003655 // fall back to the class name of the activity
3656 if (info.title == null) {
3657 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003658 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003659
Joe Onorato9c1289c2009-08-17 11:03:03 -04003660 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003661 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003662 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003663 if (lai != null) {
3664 info.flags = AppInfo.initFlags(lai);
3665 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003666 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003667 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003668
Sunny Goyale2df0622015-04-24 11:27:00 -07003669 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003670 ItemInfoFilter f) {
3671 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3672 for (ItemInfo i : infos) {
3673 if (i instanceof ShortcutInfo) {
3674 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003675 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003676 if (cn != null && f.filterItem(null, info, cn)) {
3677 filtered.add(info);
3678 }
3679 } else if (i instanceof FolderInfo) {
3680 FolderInfo info = (FolderInfo) i;
3681 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003682 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003683 if (cn != null && f.filterItem(info, s, cn)) {
3684 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003685 }
3686 }
Winson Chung64359a52013-07-08 17:17:08 -07003687 } else if (i instanceof LauncherAppWidgetInfo) {
3688 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3689 ComponentName cn = info.providerName;
3690 if (cn != null && f.filterItem(null, info, cn)) {
3691 filtered.add(info);
3692 }
Winson Chung8a435102012-08-30 17:16:53 -07003693 }
3694 }
Winson Chung64359a52013-07-08 17:17:08 -07003695 return new ArrayList<ItemInfo>(filtered);
3696 }
3697
Adam Cohen091440a2015-03-18 14:16:05 -07003698 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003699 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003700 ItemInfoFilter filter = new ItemInfoFilter() {
3701 @Override
3702 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003703 if (info.user == null) {
3704 return cn.equals(cname);
3705 } else {
3706 return cn.equals(cname) && info.user.equals(user);
3707 }
Winson Chung64359a52013-07-08 17:17:08 -07003708 }
3709 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003710 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003711 }
3712
Sunny Goyal1a745e82014-10-02 15:58:31 -07003713 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003714 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003715 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003716 @Thunk ShortcutInfo getShortcutInfo(Cursor c, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003717 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003718 // Non-app shortcuts are only supported for current user.
3719 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003720 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003721
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003722 // TODO: If there's an explicit component and we can't install that, delete it.
3723
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003724 loadInfoFromCursor(info, c, iconInfo);
3725 return info;
3726 }
Joe Onorato56d82912010-03-07 14:32:10 -05003727
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003728 /**
3729 * Make an ShortcutInfo object for a shortcut that isn't an application.
3730 */
3731 public void loadInfoFromCursor(ShortcutInfo info, Cursor c, CursorIconInfo iconInfo) {
3732 info.title = iconInfo.getTitle(c);
3733 Bitmap icon = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003734 // the fallback icon
3735 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003736 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003737 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003738 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003739 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003740 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003741
Sunny Goyal2350bc92014-10-14 16:42:54 -07003742 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003743 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3744 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3745 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3746
Adam Cohend9198822011-11-22 16:42:47 -08003747 if (intent == null) {
3748 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3749 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3750 return null;
3751 }
3752
Joe Onorato0589f0f2010-02-08 13:44:00 -08003753 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003754 boolean customIcon = false;
3755 ShortcutIconResource iconResource = null;
3756
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003757 if (bitmap instanceof Bitmap) {
3758 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003759 customIcon = true;
3760 } else {
3761 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003762 if (extra instanceof ShortcutIconResource) {
3763 iconResource = (ShortcutIconResource) extra;
3764 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003765 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003766 }
3767 }
3768
Michael Jurkac9d95c52011-08-29 14:03:34 -07003769 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003770
Kenny Guyed131872014-04-30 03:02:21 +01003771 // Only support intents for current user for now. Intents sent from other
3772 // users wouldn't get here without intent forwarding anyway.
3773 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003774 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003775 icon = mIconCache.getDefaultIcon(info.user);
3776 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003777 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003778 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003779
Winson Chung82b016c2015-05-08 17:00:10 -07003780 info.title = Utilities.trim(name);
3781 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003782 info.intent = intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003783 info.iconResource = iconResource;
3784
3785 return info;
3786 }
3787
Joe Onorato9c1289c2009-08-17 11:03:03 -04003788 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003789 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003790 * or make a new one.
3791 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003792 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003793 // See if a placeholder was created for us already
3794 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003795 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003796 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003797 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003798 folders.put(id, folderInfo);
3799 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003800 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003801 }
3802
Joe Onoratobe386092009-11-17 17:32:16 -08003803
Sunny Goyal651077b2014-06-30 14:15:31 -07003804 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3805 return (provider != null) && (provider.provider != null)
3806 && (provider.provider.getPackageName() != null);
3807 }
3808
Joe Onoratobe386092009-11-17 17:32:16 -08003809 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003810 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003811 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3812 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3813 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3814 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003815 if (mLoaderTask != null) {
3816 mLoaderTask.dumpState();
3817 } else {
3818 Log.d(TAG, "mLoaderTask=null");
3819 }
Joe Onoratobe386092009-11-17 17:32:16 -08003820 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003821
3822 public Callbacks getCallback() {
3823 return mCallbacks != null ? mCallbacks.get() : null;
3824 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003825
3826 /**
3827 * @return {@link FolderInfo} if its already loaded.
3828 */
3829 public FolderInfo findFolderById(Long folderId) {
3830 synchronized (sBgLock) {
3831 return sBgFolders.get(folderId);
3832 }
3833 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003834
Sunny Goyal527c7d32015-08-28 15:19:36 -07003835 @Thunk class DeferredMainThreadExecutor implements Executor {
3836
3837 @Override
3838 public void execute(Runnable command) {
3839 runOnMainThread(command);
3840 }
3841 }
3842
Sunny Goyal756adbc2015-04-16 15:20:51 -07003843 /**
3844 * @return the looper for the worker thread which can be used to start background tasks.
3845 */
3846 public static Looper getWorkerLooper() {
3847 return sWorkerThread.getLooper();
3848 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003849}