blob: c4987298a5624fabe973950a121d32d915585608 [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 Goyal10629b02016-09-01 12:50:11 -070060import com.android.launcher3.graphics.LauncherIcons;
Sunny Goyal1acb9e92016-05-16 12:41:09 -070061import com.android.launcher3.logging.FileLog;
Sunny Goyalf862a262015-12-14 14:27:38 -080062import com.android.launcher3.model.GridSizeMigrationTask;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070063import com.android.launcher3.model.WidgetsModel;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -070064import com.android.launcher3.provider.ImportDataTask;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070065import com.android.launcher3.provider.LauncherDbUtils;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070066import com.android.launcher3.shortcuts.DeepShortcutManager;
67import com.android.launcher3.shortcuts.ShortcutInfoCompat;
68import com.android.launcher3.shortcuts.ShortcutKey;
Robin Lee26ace122015-03-16 19:41:43 +000069import com.android.launcher3.util.ComponentKey;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070070import com.android.launcher3.util.CursorIconInfo;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -070071import com.android.launcher3.util.FlagOp;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070072import com.android.launcher3.util.GridOccupancy;
Sunny Goyale2df0622015-04-24 11:27:00 -070073import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070074import com.android.launcher3.util.ManagedProfileHeuristic;
Tony Wickhamd82a39d2016-07-01 15:44:13 -070075import com.android.launcher3.util.MultiHashMap;
Sunny Goyald09c3702016-04-06 16:18:20 -070076import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070077import com.android.launcher3.util.Preconditions;
Sunny Goyalda891c12016-03-18 18:29:24 -070078import com.android.launcher3.util.StringFilter;
Adam Cohen091440a2015-03-18 14:16:05 -070079import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070080import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080081
Michael Jurkac2f801e2011-07-12 14:19:46 -070082import java.lang.ref.WeakReference;
83import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070084import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070085import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070086import java.util.Arrays;
Michael Jurkac2f801e2011-07-12 14:19:46 -070087import java.util.Collections;
88import java.util.Comparator;
89import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070090import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070091import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070092import java.util.List;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070093import java.util.Map;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070094import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070095import java.util.Set;
Sunny Goyal527c7d32015-08-28 15:19:36 -070096import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -070097
The Android Open Source Project31dd5032009-03-03 19:32:27 -080098/**
99 * Maintains in-memory state of the Launcher. It is expected that there should be only one
100 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700101 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102 */
Kenny Guyed131872014-04-30 03:02:21 +0100103public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +0100104 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800105 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -0400106 private static final boolean DEBUG_RECEIVER = false;
Chris Wrenb358f812014-04-16 13:37:00 -0400107
Joe Onorato9c1289c2009-08-17 11:03:03 -0400108 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700109
Joe Onorato36115782010-06-17 13:28:48 -0400110 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500111 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800112
Adam Cohen091440a2015-03-18 14:16:05 -0700113 @Thunk final LauncherAppState mApp;
114 @Thunk final Object mLock = new Object();
115 @Thunk DeferredHandler mHandler = new DeferredHandler();
116 @Thunk LoaderTask mLoaderTask;
117 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700118 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119
Adam Cohen091440a2015-03-18 14:16:05 -0700120 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700121 static {
122 sWorkerThread.start();
123 }
Adam Cohen091440a2015-03-18 14:16:05 -0700124 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700125
Joe Onoratocc67f472010-06-08 10:54:30 -0700126 // We start off with everything not loaded. After that, we assume that
127 // our monitoring of the package manager provides all updates and we never
128 // need to do a requery. These are only ever touched from the loader thread.
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700129 private boolean mWorkspaceLoaded;
130 private boolean mAllAppsLoaded;
131 private boolean mDeepShortcutsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700132
Sunny Goyal756a28a2015-04-23 17:07:55 -0700133 /**
134 * Set of runnables to be called on the background thread after the workspace binding
135 * is complete.
136 */
137 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
138
Adam Cohen091440a2015-03-18 14:16:05 -0700139 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700141 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800142 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700143 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800144 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800145
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700146 // Maps all launcher activities to the id's of their shortcuts (if they have any).
147 private final MultiHashMap<ComponentKey, String> mBgDeepShortcutMap = new MultiHashMap<>();
148
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700149 private boolean mHasShortcutHostPermission;
150 // Runnable to check if the shortcuts permission has changed.
151 private final Runnable mShortcutPermissionCheckRunnable = new Runnable() {
152 @Override
153 public void run() {
154 if (mDeepShortcutsLoaded) {
155 boolean hasShortcutHostPermission = mDeepShortcutManager.hasHostPermission();
156 if (hasShortcutHostPermission != mHasShortcutHostPermission) {
157 mApp.reloadWorkspace();
158 }
159 }
160 }
161 };
162
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700163 // The lock that must be acquired before referencing any static bg data structures. Unlike
164 // other locks, this one can generally be held long-term because we never expect any of these
165 // static data structures to be referenced outside of the worker thread except on the first
166 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700167 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700168
Adam Cohen487f7dd2012-06-28 18:12:10 -0700169 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700170 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700171 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700172
Adam Cohen487f7dd2012-06-28 18:12:10 -0700173 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
174 // created by LauncherModel that are directly on the home screen (however, no widgets or
175 // shortcuts within folders).
176 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700177
Adam Cohen487f7dd2012-06-28 18:12:10 -0700178 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
179 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700180 new ArrayList<LauncherAppWidgetInfo>();
181
Adam Cohen487f7dd2012-06-28 18:12:10 -0700182 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700183 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700184
Adam Cohendcd297f2013-06-18 13:13:40 -0700185 // sBgWorkspaceScreens is the ordered set of workspace screens.
186 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
187
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700188 // sBgPinnedShortcutCounts is the ComponentKey representing a pinned shortcut to the number of
189 // times it is pinned.
190 static final Map<ShortcutKey, MutableInt> sBgPinnedShortcutCounts = new HashMap<>();
191
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(
Tony Wickhamfc02c1b2016-08-29 15:17:48 -0700934 ShortcutKey.fromShortcutInfo((ShortcutInfo) item),
935 true /* shouldPin */);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700936 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700937 break;
938 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
939 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
940 break;
941 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700942 }
943 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700944 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700945 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700946 }
947
Sunny Goyal34942622014-08-29 17:20:55 -0700948 private static ArrayList<ItemInfo> getItemsByPackageName(
949 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700950 ItemInfoFilter filter = new ItemInfoFilter() {
951 @Override
952 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
953 return cn.getPackageName().equals(pn) && info.user.equals(user);
954 }
955 };
Sunny Goyale2df0622015-04-24 11:27:00 -0700956 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -0700957 }
958
959 /**
960 * Removes all the items from the database corresponding to the specified package.
961 */
962 static void deletePackageFromDatabase(Context context, final String pn,
963 final UserHandleCompat user) {
964 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700965 }
966
967 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700968 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -0400969 */
Sunny Goyalfa401a12015-04-10 13:45:42 -0700970 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700971 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
972 items.add(item);
973 deleteItemsFromDatabase(context, items);
974 }
975
976 /**
977 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700978 */
Sunny Goyal4390ace2014-10-13 11:33:11 -0700979 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400980 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -0700981 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700982 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700983 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700984 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700985 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700986
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700987 // Lock on mBgLock *after* the db operation
988 synchronized (sBgLock) {
989 switch (item.itemType) {
990 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
991 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -0700992 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700993 if (info.container == item.id) {
994 // We are deleting a folder which still contains items that
995 // think they are contained by that folder.
996 String msg = "deleting a folder (" + item + ") which still " +
997 "contains items (" + info + ")";
998 Log.e(TAG, msg);
999 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001000 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001001 sBgWorkspaceItems.remove(item);
1002 break;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001003 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07001004 decrementPinnedShortcutCount(ShortcutKey.fromShortcutInfo(
1005 (ShortcutInfo) item));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001006 // Fall through.
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001007 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1008 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1009 sBgWorkspaceItems.remove(item);
1010 break;
1011 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1012 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1013 break;
1014 }
1015 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001016 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001017 }
1018 }
Michael Jurka83df1882011-08-31 20:59:26 -07001019 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001020 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001021 }
1022
1023 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001024 * Decrement the count for the given pinned shortcut, unpinning it if the count becomes 0.
1025 */
1026 private static void decrementPinnedShortcutCount(final ShortcutKey pinnedShortcut) {
1027 synchronized (sBgLock) {
1028 MutableInt count = sBgPinnedShortcutCounts.get(pinnedShortcut);
1029 if (count == null || --count.value == 0) {
1030 LauncherAppState.getInstance().getShortcutManager().unpinShortcut(pinnedShortcut);
1031 }
1032 }
1033 }
1034
1035 /**
1036 * Increment the count for the given shortcut, pinning it if the count becomes 1.
1037 *
1038 * As an optimization, the caller can pass shouldPin == false to avoid
1039 * unnecessary RPC's if the shortcut is already pinned.
1040 */
1041 private static void incrementPinnedShortcutCount(ShortcutKey pinnedShortcut, boolean shouldPin) {
1042 synchronized (sBgLock) {
1043 MutableInt count = sBgPinnedShortcutCounts.get(pinnedShortcut);
1044 if (count == null) {
1045 count = new MutableInt(1);
1046 sBgPinnedShortcutCounts.put(pinnedShortcut, count);
1047 } else {
1048 count.value++;
1049 }
1050 if (shouldPin && count.value == 1) {
1051 LauncherAppState.getInstance().getShortcutManager().pinShortcut(pinnedShortcut);
1052 }
1053 }
1054 }
1055
1056 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001057 * Update the order of the workspace screens in the database. The array list contains
1058 * a list of screen ids in the order that they should appear.
1059 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001060 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001061 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001062 final ContentResolver cr = context.getContentResolver();
1063 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1064
1065 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001066 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001067 while (iter.hasNext()) {
1068 long id = iter.next();
1069 if (id < 0) {
1070 iter.remove();
1071 }
1072 }
1073
1074 Runnable r = new Runnable() {
1075 @Override
1076 public void run() {
Yura085c8532014-02-11 15:15:29 +00001077 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001078 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001079 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001080 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001081 for (int i = 0; i < count; i++) {
1082 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001083 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001084 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1085 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001086 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001087 }
Yura085c8532014-02-11 15:15:29 +00001088
1089 try {
1090 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1091 } catch (Exception ex) {
1092 throw new RuntimeException(ex);
1093 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001094
Winson Chungba9c37f2013-08-30 14:11:37 -07001095 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001096 sBgWorkspaceScreens.clear();
1097 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001098 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001099 }
1100 };
1101 runOnWorkerThread(r);
1102 }
1103
1104 /**
Winsonc0b52fe2015-09-09 16:38:15 -07001105 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001106 */
Winsonc0b52fe2015-09-09 16:38:15 -07001107 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001108 final ContentResolver cr = context.getContentResolver();
1109
Michael Jurkac9d95c52011-08-29 14:03:34 -07001110 Runnable r = new Runnable() {
1111 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001112 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001113 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001114 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001115 sBgItemsIdMap.remove(info.id);
1116 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001117 sBgWorkspaceItems.remove(info);
1118 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001119
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001120 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001121 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001122 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001123 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001124 for (ItemInfo childInfo : info.contents) {
1125 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001126 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001127 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001128 }
1129 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001130 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001131 }
1132
1133 /**
1134 * Set this as the current Launcher activity object for the loader.
1135 */
1136 public void initialize(Callbacks callbacks) {
1137 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001138 Preconditions.assertUIThread();
1139 // Remove any queued UI runnables
1140 mHandler.cancelAll();
1141 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001142 }
1143 }
1144
Kenny Guyed131872014-04-30 03:02:21 +01001145 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001146 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001147 int op = PackageUpdatedTask.OP_UPDATE;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001148 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001149 user));
1150 }
1151
1152 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001153 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001154 int op = PackageUpdatedTask.OP_REMOVE;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001155 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001156 user));
1157 }
1158
1159 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001160 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001161 int op = PackageUpdatedTask.OP_ADD;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001162 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001163 user));
1164 }
1165
1166 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001167 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001168 boolean replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001169 enqueueItemUpdatedTask(
Sunny Goyal144154d2016-03-30 16:47:03 -07001170 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, packageNames, user));
Kenny Guyed131872014-04-30 03:02:21 +01001171 }
1172
1173 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001174 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001175 boolean replacing) {
1176 if (!replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001177 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guyed131872014-04-30 03:02:21 +01001178 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1179 user));
1180 }
Kenny Guyed131872014-04-30 03:02:21 +01001181 }
1182
Kenny Guy44cba692016-01-21 19:50:02 +00001183 @Override
1184 public void onPackagesSuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001185 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001186 PackageUpdatedTask.OP_SUSPEND, packageNames,
1187 user));
1188 }
1189
1190 @Override
1191 public void onPackagesUnsuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001192 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001193 PackageUpdatedTask.OP_UNSUSPEND, packageNames,
1194 user));
1195 }
1196
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001197 @Override
1198 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
1199 UserHandleCompat user) {
Sunny Goyal50941fb2016-08-04 12:03:52 -07001200 enqueueItemUpdatedTask(new ShortcutsChangedTask(packageName, shortcuts, user, true));
1201 }
1202
1203 public void updatePinnedShortcuts(String packageName, List<ShortcutInfoCompat> shortcuts,
1204 UserHandleCompat user) {
1205 enqueueItemUpdatedTask(new ShortcutsChangedTask(packageName, shortcuts, user, false));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001206 }
1207
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001208 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001209 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1210 * ACTION_PACKAGE_CHANGED.
1211 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001212 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001213 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001214 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001215
Joe Onorato36115782010-06-17 13:28:48 -04001216 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001217 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001218 // If we have changed locale we need to clear out the labels in all apps/workspace.
1219 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001220 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1221 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001222 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001223 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001224 } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1225 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
1226 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyalda891c12016-03-18 18:29:24 -07001227 UserHandleCompat user = UserHandleCompat.fromIntent(intent);
1228 if (user != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001229 if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1230 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
1231 enqueueItemUpdatedTask(new PackageUpdatedTask(
1232 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE,
1233 new String[0], user));
1234 }
1235
1236 // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so
1237 // we need to run the state change task again.
1238 if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
1239 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
1240 enqueueItemUpdatedTask(new UserLockStateChangedTask(user));
1241 }
Sunny Goyalda891c12016-03-18 18:29:24 -07001242 }
Tony Wickham827cef22016-03-17 15:39:39 -07001243 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
1244 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001245 }
1246 }
1247
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001248 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001249 resetLoadedState(true, true);
1250
Reena Lee93f824a2011-09-23 17:20:28 -07001251 // Do this here because if the launcher activity is running it will be restarted.
1252 // If it's not running startLoaderFromBackground will merely tell it that it needs
1253 // to reload.
1254 startLoaderFromBackground();
1255 }
1256
Winson Chungf0c6ae02012-03-21 16:10:31 -07001257 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1258 synchronized (mLock) {
1259 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1260 // mWorkspaceLoaded to true later
1261 stopLoaderLocked();
1262 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1263 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001264 // Always reset deep shortcuts loaded.
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001265 // TODO: why?
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001266 mDeepShortcutsLoaded = false;
Winson Chungf0c6ae02012-03-21 16:10:31 -07001267 }
1268 }
1269
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001270 /**
1271 * When the launcher is in the background, it's possible for it to miss paired
1272 * configuration changes. So whenever we trigger the loader from the background
1273 * tell the launcher that it needs to re-run the loader when it comes back instead
1274 * of doing it now.
1275 */
1276 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001277 Callbacks callbacks = getCallback();
1278 if (callbacks != null) {
1279 // Only actually run the loader if they're not paused.
1280 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -07001281 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001282 }
1283 }
Joe Onorato36115782010-06-17 13:28:48 -04001284 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001285
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001286 /**
1287 * If there is already a loader task running, tell it to stop.
1288 */
1289 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001290 LoaderTask oldTask = mLoaderTask;
1291 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001292 oldTask.stopLocked();
1293 }
Reena Lee93f824a2011-09-23 17:20:28 -07001294 }
1295
Adam Cohen1a85c582014-09-30 09:48:49 -07001296 public boolean isCurrentCallbacks(Callbacks callbacks) {
1297 return (mCallbacks != null && mCallbacks.get() == callbacks);
1298 }
1299
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001300 /**
1301 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
1302 * @return true if the page could be bound synchronously.
1303 */
1304 public boolean startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001305 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1306 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001307 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -04001308 // Don't bother to start the thread if we know it's not going to do anything
1309 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001310 final Callbacks oldCallbacks = mCallbacks.get();
1311 // Clear any pending bind-runnables from the synchronized load process.
1312 runOnMainThread(new Runnable() {
1313 public void run() {
1314 oldCallbacks.clearPendingBinds();
1315 }
1316 });
1317
Joe Onorato36115782010-06-17 13:28:48 -04001318 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001319 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001320 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001321 // TODO: mDeepShortcutsLoaded does not need to be true for synchronous bind.
Tony Wickham80f57872016-06-29 18:12:15 -07001322 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE && mAllAppsLoaded
1323 && mWorkspaceLoaded && mDeepShortcutsLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001324 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001325 return true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001326 } else {
1327 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1328 sWorker.post(mLoaderTask);
1329 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001330 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001331 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001332 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001333 }
1334
Joe Onorato36115782010-06-17 13:28:48 -04001335 public void stopLoader() {
1336 synchronized (mLock) {
1337 if (mLoaderTask != null) {
1338 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001339 }
1340 }
Joe Onorato36115782010-06-17 13:28:48 -04001341 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001342
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001343 /**
1344 * Loads the workspace screen ids in an ordered list.
1345 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001346 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001347 final ContentResolver contentResolver = context.getContentResolver();
1348 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001349
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001350 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001351 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
1352 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -07001353 }
1354
Joe Onorato36115782010-06-17 13:28:48 -04001355 /**
1356 * Runnable for the thread that loads the contents of the launcher:
1357 * - workspace icons
1358 * - widgets
1359 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001360 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -04001361 */
1362 private class LoaderTask implements Runnable {
1363 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001364 private int mPageToBindFirst;
1365
Adam Cohen091440a2015-03-18 14:16:05 -07001366 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001367 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001368 @Thunk boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001369
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001370 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -04001371 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001372 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001373 }
1374
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001375 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001376 mIsLoadingAndBindingWorkspace = true;
1377
Joe Onorato36115782010-06-17 13:28:48 -04001378 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001379 if (DEBUG_LOADERS) {
1380 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001381 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001382
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001383 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001384 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001385 synchronized (LoaderTask.this) {
1386 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001387 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001388 }
1389 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001390 }
1391 }
1392
Joe Onorato36115782010-06-17 13:28:48 -04001393 // Bind the workspace
Sunny Goyal93f878c2016-03-30 17:31:24 -07001394 bindWorkspace(mPageToBindFirst);
Joe Onorato36115782010-06-17 13:28:48 -04001395 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001396
Joe Onorato36115782010-06-17 13:28:48 -04001397 private void waitForIdle() {
1398 // Wait until the either we're stopped or the other threads are done.
1399 // This way we don't start loading all apps until the workspace has settled
1400 // down.
1401 synchronized (LoaderTask.this) {
1402 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001403
Joe Onorato36115782010-06-17 13:28:48 -04001404 mHandler.postIdle(new Runnable() {
1405 public void run() {
1406 synchronized (LoaderTask.this) {
1407 mLoadAndBindStepFinished = true;
1408 if (DEBUG_LOADERS) {
1409 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001410 }
Joe Onorato36115782010-06-17 13:28:48 -04001411 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001412 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001413 }
Joe Onorato36115782010-06-17 13:28:48 -04001414 });
1415
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001416 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001417 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001418 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1419 // wait no longer than 1sec at a time
1420 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001421 } catch (InterruptedException ex) {
1422 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001423 }
1424 }
Joe Onorato36115782010-06-17 13:28:48 -04001425 if (DEBUG_LOADERS) {
1426 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001427 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001428 + "ms for previous step to finish binding");
1429 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001430 }
Joe Onorato36115782010-06-17 13:28:48 -04001431 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001432
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001433 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001434 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001435 // Ensure that we have a valid page index to load synchronously
1436 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1437 "valid page index");
1438 }
1439 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1440 // Ensure that we don't try and bind a specified page when the pages have not been
1441 // loaded already (we should load everything asynchronously in that case)
1442 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1443 }
1444 synchronized (mLock) {
1445 if (mIsLoaderTaskRunning) {
1446 // Ensure that we are never running the background loading at this point since
1447 // we also touch the background collections
1448 throw new RuntimeException("Error! Background loading is already running");
1449 }
1450 }
1451
1452 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1453 // data structures, we can't allow any other thread to touch that data, but because
1454 // this call is synchronous, we can get away with not locking).
1455
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001456 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001457 // operations from the previous activity. We need to ensure that all queued operations
1458 // are executed before any synchronous binding work is done.
1459 mHandler.flush();
1460
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001461 // Divide the set of loaded items into those that we are binding synchronously, and
1462 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001463 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001464 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1465 // arise from that.
1466 onlyBindAllApps();
Tony Wickham80f57872016-06-29 18:12:15 -07001467
1468 bindDeepShortcuts();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001469 }
1470
Joe Onorato36115782010-06-17 13:28:48 -04001471 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001472 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001473 if (mStopped) {
1474 return;
1475 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001476 mIsLoaderTaskRunning = true;
1477 }
Joe Onorato36115782010-06-17 13:28:48 -04001478 // Optimize for end-user experience: if the Launcher is up and // running with the
1479 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1480 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001481 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001482 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001483 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001484
Joe Onorato36115782010-06-17 13:28:48 -04001485 if (mStopped) {
1486 break keep_running;
1487 }
1488
Joe Onorato36115782010-06-17 13:28:48 -04001489 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001490
1491 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001492 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1493 loadAndBindAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001494
1495 waitForIdle();
1496
1497 // third step
1498 if (DEBUG_LOADERS) Log.d(TAG, "step 3: loading deep shortcuts");
1499 loadAndBindDeepShortcuts();
Joe Onorato36115782010-06-17 13:28:48 -04001500 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001501
Joe Onorato36115782010-06-17 13:28:48 -04001502 // Clear out this reference, otherwise we end up holding it until all of the
1503 // callback runnables are done.
1504 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001505
Joe Onorato36115782010-06-17 13:28:48 -04001506 synchronized (mLock) {
1507 // If we are still the last one to be scheduled, remove ourselves.
1508 if (mLoaderTask == this) {
1509 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001510 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001511 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001512 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001513 }
Joe Onorato36115782010-06-17 13:28:48 -04001514 }
1515
1516 public void stopLocked() {
1517 synchronized (LoaderTask.this) {
1518 mStopped = true;
1519 this.notify();
1520 }
1521 }
1522
1523 /**
1524 * Gets the callbacks object. If we've been stopped, or if the launcher object
1525 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1526 * object that was around when the deferred message was scheduled, and if there's
1527 * a new Callbacks object around then also return null. This will save us from
1528 * calling onto it with data that will be ignored.
1529 */
1530 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1531 synchronized (mLock) {
1532 if (mStopped) {
1533 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001534 }
Joe Onorato36115782010-06-17 13:28:48 -04001535
1536 if (mCallbacks == null) {
1537 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001538 }
Joe Onorato36115782010-06-17 13:28:48 -04001539
1540 final Callbacks callbacks = mCallbacks.get();
1541 if (callbacks != oldCallbacks) {
1542 return null;
1543 }
1544 if (callbacks == null) {
1545 Log.w(TAG, "no mCallbacks");
1546 return null;
1547 }
1548
1549 return callbacks;
1550 }
1551 }
1552
1553 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001554 private boolean checkItemPlacement(LongArrayMap<GridOccupancy> occupied, ItemInfo item,
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001555 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001556 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001557 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung892c74d2013-08-22 16:15:50 -07001558
Adam Cohendcd297f2013-06-18 13:13:40 -07001559 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001560 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001561 // Return early if we detect that an item is under the hotseat button
Sunny Goyalbb011da2016-06-15 15:42:29 -07001562 if (!FeatureFlags.NO_ALL_APPS_ICON &&
1563 profile.isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001564 Log.e(TAG, "Error loading shortcut into hotseat " + item
1565 + " into position (" + item.screenId + ":" + item.cellX + ","
1566 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001567 return false;
1568 }
1569
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001570 final GridOccupancy hotseatOccupancy =
Dan Sandler295ae182013-12-10 16:05:47 -05001571 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1572
Adam Cohen2e6da152015-05-06 11:42:25 -07001573 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001574 Log.e(TAG, "Error loading shortcut " + item
1575 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001576 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001577 + ")");
1578 return false;
1579 }
1580
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001581 if (hotseatOccupancy != null) {
1582 if (hotseatOccupancy.cells[(int) item.screenId][0]) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001583 Log.e(TAG, "Error loading shortcut into hotseat " + item
1584 + " into position (" + item.screenId + ":" + item.cellX + ","
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001585 + item.cellY + ") already occupied");
Adam Cohendcd297f2013-06-18 13:13:40 -07001586 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001587 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001588 hotseatOccupancy.cells[(int) item.screenId][0] = true;
Dan Sandler295ae182013-12-10 16:05:47 -05001589 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001590 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001591 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001592 final GridOccupancy occupancy = new GridOccupancy(profile.numHotseatIcons, 1);
1593 occupancy.cells[(int) item.screenId][0] = true;
1594 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, occupancy);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001595 return true;
1596 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001597 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1598 if (!workspaceScreens.contains((Long) item.screenId)) {
1599 // The item has an invalid screen id.
1600 return false;
1601 }
1602 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001603 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001604 return true;
1605 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001606
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001607 final int countX = profile.numColumns;
1608 final int countY = profile.numRows;
Adam Cohenae4409d2013-11-26 10:34:59 -08001609 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1610 item.cellX < 0 || item.cellY < 0 ||
1611 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1612 Log.e(TAG, "Error loading shortcut " + item
1613 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1614 + item.cellX + "," + item.cellY
1615 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1616 return false;
1617 }
1618
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001619 if (!occupied.containsKey(item.screenId)) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001620 GridOccupancy screen = new GridOccupancy(countX + 1, countY + 1);
1621 if (item.screenId == Workspace.FIRST_SCREEN_ID) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001622 // Mark the first row as occupied (if the feature is enabled)
1623 // in order to account for the QSB.
1624 screen.markCells(0, 0, countX + 1, 1, FeatureFlags.QSB_ON_FIRST_SCREEN);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001625 }
1626 occupied.put(item.screenId, screen);
Joe Onorato36115782010-06-17 13:28:48 -04001627 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001628 final GridOccupancy occupancy = occupied.get(item.screenId);
Winson Chungf30ad5f2011-08-08 10:55:42 -07001629
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001630 // Check if any workspace icons overlap with each other
1631 if (occupancy.isRegionVacant(item.cellX, item.cellY, item.spanX, item.spanY)) {
1632 occupancy.markCells(item, true);
1633 return true;
1634 } else {
1635 Log.e(TAG, "Error loading shortcut " + item
1636 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1637 + item.cellX + "," + item.cellX + "," + item.spanX + "," + item.spanY
1638 + ") already occupied");
1639 return false;
1640 }
Joe Onorato36115782010-06-17 13:28:48 -04001641 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001642
Winson Chungba9c37f2013-08-30 14:11:37 -07001643 /** Clears all the sBg data structures */
1644 private void clearSBgDataStructures() {
1645 synchronized (sBgLock) {
1646 sBgWorkspaceItems.clear();
1647 sBgAppWidgets.clear();
1648 sBgFolders.clear();
1649 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001650 sBgWorkspaceScreens.clear();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001651 sBgPinnedShortcutCounts.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001652 }
1653 }
1654
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001655 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -07001656 if (LauncherAppState.PROFILE_STARTUP) {
1657 Trace.beginSection("Loading Workspace");
1658 }
Joe Onorato36115782010-06-17 13:28:48 -04001659 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001660
Joe Onorato36115782010-06-17 13:28:48 -04001661 final Context context = mContext;
1662 final ContentResolver contentResolver = context.getContentResolver();
1663 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001664 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001665 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001666 final boolean isSdCardReady = Utilities.isBootCompleted();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001667
Winson Chung892c74d2013-08-22 16:15:50 -07001668 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001669 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001670 int countX = profile.numColumns;
1671 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001672
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001673 boolean clearDb = false;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -07001674 try {
1675 ImportDataTask.performImportIfPossible(context);
1676 } catch (Exception e) {
1677 // Migration failed. Clear workspace.
1678 clearDb = true;
1679 }
1680
1681 if (!clearDb && GridSizeMigrationTask.ENABLED &&
Sunny Goyalf076eae2016-01-11 12:25:10 -08001682 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1683 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001684 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001685 }
1686
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001687 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -07001688 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001689 LauncherSettings.Settings.call(contentResolver,
1690 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001691 }
1692
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001693 Log.d(TAG, "loadWorkspace: loading default favorites");
1694 LauncherSettings.Settings.call(contentResolver,
1695 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -07001696
Winson Chung2abf94d2012-07-18 18:16:38 -07001697 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001698 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001699 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001700 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001701 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001702
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001703 final ArrayList<Long> itemsToRemove = new ArrayList<>();
1704 final ArrayList<Long> restoredRows = new ArrayList<>();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001705 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001706 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001707 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001708 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001709
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001710 // +1 for the hotseat (it can be larger than the workspace)
1711 // Load workspace in reverse order to ensure that latest items are loaded first (and
1712 // before any earlier duplicates)
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001713 final LongArrayMap<GridOccupancy> occupied = new LongArrayMap<>();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001714 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001715
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001716 try {
1717 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1718 final int intentIndex = c.getColumnIndexOrThrow
1719 (LauncherSettings.Favorites.INTENT);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001720 final int containerIndex = c.getColumnIndexOrThrow(
1721 LauncherSettings.Favorites.CONTAINER);
1722 final int itemTypeIndex = c.getColumnIndexOrThrow(
1723 LauncherSettings.Favorites.ITEM_TYPE);
1724 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1725 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001726 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1727 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001728 final int screenIndex = c.getColumnIndexOrThrow(
1729 LauncherSettings.Favorites.SCREEN);
1730 final int cellXIndex = c.getColumnIndexOrThrow
1731 (LauncherSettings.Favorites.CELLX);
1732 final int cellYIndex = c.getColumnIndexOrThrow
1733 (LauncherSettings.Favorites.CELLY);
1734 final int spanXIndex = c.getColumnIndexOrThrow
1735 (LauncherSettings.Favorites.SPANX);
1736 final int spanYIndex = c.getColumnIndexOrThrow(
1737 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001738 final int rankIndex = c.getColumnIndexOrThrow(
1739 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001740 final int restoredIndex = c.getColumnIndexOrThrow(
1741 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001742 final int profileIdIndex = c.getColumnIndexOrThrow(
1743 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001744 final int optionsIndex = c.getColumnIndexOrThrow(
1745 LauncherSettings.Favorites.OPTIONS);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001746 final CursorIconInfo cursorIconInfo = new CursorIconInfo(mContext, c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001747
Sunny Goyal7f834d22015-04-21 10:10:23 -07001748 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
Kenny Guyff05f432016-01-22 17:48:29 +00001749 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001750 final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>();
Sunny Goyal7f834d22015-04-21 10:10:23 -07001751 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001752 long serialNo = mUserManager.getSerialNumberForUser(user);
1753 allUsers.put(serialNo, user);
1754 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001755
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001756 boolean userUnlocked = mUserManager.isUserUnlocked(user);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001757
1758 // We can only query for shortcuts when the user is unlocked.
1759 if (userUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07001760 List<ShortcutInfoCompat> pinnedShortcuts =
1761 mDeepShortcutManager.queryForPinnedShortcuts(null, user);
1762 if (mDeepShortcutManager.wasLastCallSuccess()) {
1763 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
1764 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
1765 shortcut);
1766 }
1767 } else {
1768 // Shortcut manager can fail due to some race condition when the
1769 // lock state changes too frequently. For the purpose of the loading
1770 // shortcuts, consider the user is still locked.
1771 userUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001772 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001773 }
Sunny Goyal49f4f032016-08-01 15:45:49 -07001774 unlockedUsers.put(serialNo, userUnlocked);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001775 }
1776
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001777 ShortcutInfo info;
1778 String intentDescription;
1779 LauncherAppWidgetInfo appWidgetInfo;
1780 int container;
1781 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001782 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001783 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001784 UserHandleCompat user;
Sunny Goyald09c3702016-04-06 16:18:20 -07001785 String targetPackage;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001786
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001787 while (!mStopped && c.moveToNext()) {
1788 try {
1789 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001790 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001791 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001792 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001793
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001794 switch (itemType) {
1795 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1796 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001797 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001798 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001799 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001800 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001801 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001802 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001803 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001804 boolean itemReplaced = false;
Sunny Goyald09c3702016-04-06 16:18:20 -07001805 targetPackage = null;
Kenny Guyed131872014-04-30 03:02:21 +01001806 if (user == null) {
1807 // User has been deleted remove the item.
1808 itemsToRemove.add(id);
1809 continue;
1810 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001811 try {
1812 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001813 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001814 if (cn != null && cn.getPackageName() != null) {
1815 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1816 cn.getPackageName(), user);
1817 boolean validComponent = validPkg &&
1818 launcherApps.isActivityEnabledForProfile(cn, user);
Sunny Goyald09c3702016-04-06 16:18:20 -07001819 if (validPkg) {
1820 targetPackage = cn.getPackageName();
1821 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001822
1823 if (validComponent) {
1824 if (restored) {
1825 // no special handling necessary for this item
1826 restoredRows.add(id);
1827 restored = false;
1828 }
Kenny Guyff05f432016-01-22 17:48:29 +00001829 if (quietMode.get(serialNumber)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001830 disabledState = ShortcutInfo.FLAG_DISABLED_QUIET_USER;
Kenny Guyff05f432016-01-22 17:48:29 +00001831 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001832 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001833 intent = null;
1834 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1835 // We allow auto install apps to have their intent
1836 // updated after an install.
1837 intent = manager.getLaunchIntentForPackage(
1838 cn.getPackageName());
1839 if (intent != null) {
1840 ContentValues values = new ContentValues();
1841 values.put(LauncherSettings.Favorites.INTENT,
1842 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001843 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001844 }
1845 }
1846
1847 if (intent == null) {
1848 // The app is installed but the component is no
1849 // longer available.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001850 FileLog.d(TAG, "Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001851 itemsToRemove.add(id);
1852 continue;
1853 } else {
1854 // no special handling necessary for this item
1855 restoredRows.add(id);
1856 restored = false;
1857 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001858 } else if (restored) {
1859 // Package is not yet available but might be
1860 // installed later.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001861 FileLog.d(TAG, "package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001862
1863 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1864 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001865 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001866 // App restore has started. Update the flag
1867 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1868 ContentValues values = new ContentValues();
1869 values.put(LauncherSettings.Favorites.RESTORED,
1870 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001871 updateItem(id, values);
1872 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1873 // This is a common app. Try to replace this.
1874 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1875 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1876 if (parser.findDefaultApp()) {
1877 // Default app found. Replace it.
1878 intent = parser.parsedIntent;
1879 cn = intent.getComponent();
1880 ContentValues values = parser.parsedValues;
1881 values.put(LauncherSettings.Favorites.RESTORED, 0);
1882 updateItem(id, values);
1883 restored = false;
1884 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001885
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001886 } else {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001887 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001888 itemsToRemove.add(id);
1889 continue;
1890 }
Sunny Goyalb05a00a2016-08-29 10:06:57 -07001891 } else {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001892 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001893 itemsToRemove.add(id);
1894 continue;
1895 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001896 } else if (PackageManagerHelper.isAppOnSdcard(
1897 manager, cn.getPackageName())) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07001898 // Package is present but not available.
1899 allowMissingTarget = true;
1900 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1901 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001902 // SdCard is not ready yet. Package might get available,
1903 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001904 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001905 HashSet<String> pkgs = sPendingPackages.get(user);
1906 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001907 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001908 sPendingPackages.put(user, pkgs);
1909 }
1910 pkgs.add(cn.getPackageName());
1911 allowMissingTarget = true;
1912 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001913
1914 } else {
1915 // Do not wait for external media load anymore.
1916 // Log the invalid package, and remove it
Sunny Goyal713edfc2016-05-06 09:58:34 -07001917 FileLog.d(TAG, "Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001918 itemsToRemove.add(id);
1919 continue;
Winson Chungee055712013-07-30 14:46:24 -07001920 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001921 } else if (cn == null) {
1922 // For shortcuts with no component, keep them as they are
1923 restoredRows.add(id);
1924 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001925 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001926 } catch (URISyntaxException e) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001927 FileLog.d(TAG, "Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001928 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001929 continue;
1930 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001931
Sunny Goyal34b65272015-03-11 16:56:52 -07001932 boolean useLowResIcon = container >= 0 &&
1933 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1934
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001935 if (itemReplaced) {
1936 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001937 info = getAppShortcutInfo(intent, user, null,
1938 cursorIconInfo, false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001939 } else {
1940 // Don't replace items for other profiles.
1941 itemsToRemove.add(id);
1942 continue;
1943 }
1944 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001945 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001946 info = getRestoredItemInfo(c, intent,
1947 promiseType, itemType, cursorIconInfo);
Kenny Guyed131872014-04-30 03:02:21 +01001948 intent = getRestoredItemIntent(c, context, intent);
1949 } else {
1950 // Don't restore items for other profiles.
1951 itemsToRemove.add(id);
1952 continue;
1953 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001954 } else if (itemType ==
1955 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001956 info = getAppShortcutInfo(intent, user, c,
1957 cursorIconInfo, allowMissingTarget, useLowResIcon);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001958 } else if (itemType ==
1959 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001960
1961 ShortcutKey key = ShortcutKey.fromIntent(intent, user);
1962 if (unlockedUsers.get(serialNumber)) {
1963 ShortcutInfoCompat pinnedShortcut =
1964 shortcutKeyToPinnedShortcuts.get(key);
1965 if (pinnedShortcut == null) {
1966 // The shortcut is no longer valid.
1967 itemsToRemove.add(id);
1968 continue;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001969 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001970 info = new ShortcutInfo(pinnedShortcut, context);
1971 intent = info.intent;
1972 } else {
1973 // Create a shortcut info in disabled mode for now.
1974 info = new ShortcutInfo();
1975 info.user = user;
1976 info.itemType = itemType;
1977 loadInfoFromCursor(info, c, cursorIconInfo);
1978
1979 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001980 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001981 incrementPinnedShortcutCount(key, false /* shouldPin */);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001982 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001983 info = getShortcutInfo(c, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001984
Sunny Goyald09c3702016-04-06 16:18:20 -07001985 // Shortcuts are only available on the primary profile
1986 if (PackageManagerHelper.isAppSuspended(manager, targetPackage)) {
1987 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1988 }
1989
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001990 // App shortcuts that used to be automatically added to Launcher
1991 // didn't always have the correct intent flags set, so do that
1992 // here
1993 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001994 intent.getCategories() != null &&
1995 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001996 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001997 intent.addFlags(
1998 Intent.FLAG_ACTIVITY_NEW_TASK |
1999 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
2000 }
Michael Jurka96879562012-03-22 05:54:33 -07002001 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002002
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002003 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07002004 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002005 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002006 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002007 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002008 info.cellX = c.getInt(cellXIndex);
2009 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08002010 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002011 info.spanX = 1;
2012 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002013 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002014 if (info.promisedIntent != null) {
2015 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
2016 }
Sunny Goyald09c3702016-04-06 16:18:20 -07002017 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002018 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2019 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2020 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002021
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002022 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002023 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002024 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002025 break;
2026 }
2027
Sunny Goyal756adbc2015-04-16 15:20:51 -07002028 if (restored) {
2029 ComponentName cn = info.getTargetComponent();
2030 if (cn != null) {
2031 Integer progress = installingPkgs.get(cn.getPackageName());
2032 if (progress != null) {
2033 info.setInstallProgress(progress);
2034 } else {
2035 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2036 }
2037 }
2038 }
2039
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002040 switch (container) {
2041 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2042 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2043 sBgWorkspaceItems.add(info);
2044 break;
2045 default:
2046 // Item is in a user folder
2047 FolderInfo folderInfo =
2048 findOrMakeFolder(sBgFolders, container);
Sunny Goyalc52ba712016-04-05 15:59:05 -07002049 folderInfo.add(info, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002050 break;
2051 }
2052 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002053 } else {
2054 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002055 }
2056 break;
2057
2058 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2059 id = c.getLong(idIndex);
2060 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2061
Sunny Goyala508e4f2015-05-21 09:33:57 -07002062 // Do not trim the folder label, as is was set by the user.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002063 folderInfo.title = c.getString(cursorIconInfo.titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002064 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002065 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002066 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002067 folderInfo.cellX = c.getInt(cellXIndex);
2068 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002069 folderInfo.spanX = 1;
2070 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002071 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002072
Daniel Sandler8802e962010-05-26 16:28:16 -04002073 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002074 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002075 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002076 break;
2077 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002078
Joe Onorato9c1289c2009-08-17 11:03:03 -04002079 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002080 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2081 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2082 sBgWorkspaceItems.add(folderInfo);
2083 break;
Joe Onorato36115782010-06-17 13:28:48 -04002084 }
Joe Onorato17a89222011-02-08 17:26:11 -08002085
Chris Wrenf4d08112014-01-16 18:13:56 -05002086 if (restored) {
2087 // no special handling required for restored folders
2088 restoredRows.add(id);
2089 }
2090
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002091 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2092 sBgFolders.put(folderInfo.id, folderInfo);
2093 break;
2094
2095 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002096 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002097 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002098 boolean customWidget = itemType ==
2099 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2100
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002101 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002102 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002103 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002104 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002105 user = allUsers.get(serialNumber);
2106 if (user == null) {
2107 itemsToRemove.add(id);
2108 continue;
2109 }
2110
Sunny Goyalff572272014-07-23 13:58:07 -07002111 final ComponentName component =
2112 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002113
Sunny Goyal651077b2014-06-30 14:15:31 -07002114 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002115 final boolean isIdValid = (restoreStatus &
2116 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002117 final boolean wasProviderReady = (restoreStatus &
2118 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002119
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002120 if (widgetProvidersMap == null) {
2121 widgetProvidersMap = AppWidgetManagerCompat
2122 .getInstance(mContext).getAllProvidersMap();
2123 }
2124 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
2125 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00002126 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002127 user));
Sunny Goyalff572272014-07-23 13:58:07 -07002128
2129 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002130 if (!isSafeMode && !customWidget &&
2131 wasProviderReady && !isProviderReady) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002132 FileLog.d(TAG, "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07002133 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002134 itemsToRemove.add(id);
2135 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002136 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002137 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2138 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002139
Sunny Goyal53f96722015-07-13 19:54:53 -07002140 // The provider is available. So the widget is either
2141 // available or not available. We do not need to track
2142 // any future restore updates.
2143 int status = restoreStatus &
2144 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002145 if (!wasProviderReady) {
2146 // If provider was not previously ready, update the
2147 // status and UI flag.
2148
2149 // Id would be valid only if the widget restore broadcast was received.
2150 if (isIdValid) {
Sunny Goyal86df1382016-08-10 15:03:22 -07002151 status |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002152 } else {
2153 status &= ~LauncherAppWidgetInfo
2154 .FLAG_PROVIDER_NOT_READY;
2155 }
2156 }
2157 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002158 } else {
2159 Log.v(TAG, "Widget restore pending id=" + id
2160 + " appWidgetId=" + appWidgetId
2161 + " status =" + restoreStatus);
2162 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002163 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002164 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002165 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002166
2167 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2168 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002169 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002170 // App restore has started. Update the flag
2171 appWidgetInfo.restoreStatus |=
2172 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalb05a00a2016-08-29 10:06:57 -07002173 } else if (!isSafeMode) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002174 FileLog.d(TAG, "Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07002175 itemsToRemove.add(id);
2176 continue;
2177 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002178
2179 appWidgetInfo.installProgress =
2180 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002181 }
Sunny Goyal86df1382016-08-10 15:03:22 -07002182 if (appWidgetInfo.hasRestoreFlag(
2183 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG)) {
2184 intentDescription = c.getString(intentIndex);
2185 if (!TextUtils.isEmpty(intentDescription)) {
2186 appWidgetInfo.bindOptions =
2187 Intent.parseUri(intentDescription, 0);
2188 }
2189 }
Sunny Goyalff572272014-07-23 13:58:07 -07002190
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002191 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002192 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002193 appWidgetInfo.cellX = c.getInt(cellXIndex);
2194 appWidgetInfo.cellY = c.getInt(cellYIndex);
2195 appWidgetInfo.spanX = c.getInt(spanXIndex);
2196 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002197 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002198
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002199 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2200 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2201 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002202 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2203 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002204 continue;
2205 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002206
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002207 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002208 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002209 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002210 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002211 break;
2212 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002213
Adam Cohen59400422014-03-05 18:07:04 -08002214 if (!customWidget) {
2215 String providerName =
2216 appWidgetInfo.providerName.flattenToString();
2217 if (!providerName.equals(savedProvider) ||
2218 (appWidgetInfo.restoreStatus != restoreStatus)) {
2219 ContentValues values = new ContentValues();
2220 values.put(
2221 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2222 providerName);
2223 values.put(LauncherSettings.Favorites.RESTORED,
2224 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002225 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002226 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002227 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002228 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2229 sBgAppWidgets.add(appWidgetInfo);
2230 }
Joe Onorato36115782010-06-17 13:28:48 -04002231 break;
2232 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002233 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002234 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002235 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002236 }
2237 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002238 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002239 }
2240
Winson Chungba9c37f2013-08-30 14:11:37 -07002241 // Break early if we've stopped loading
2242 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002243 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002244 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002245 }
2246
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002247 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002248 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002249 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2250 Utilities.createDbSelectionQuery(
2251 LauncherSettings.Favorites._ID, itemsToRemove), null);
2252 if (DEBUG_LOADERS) {
2253 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2254 LauncherSettings.Favorites._ID, itemsToRemove));
2255 }
2256
2257 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07002258 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
2259 .call(contentResolver,
2260 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
2261 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
2262 for (long folderId : deletedFolderIds) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002263 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2264 sBgFolders.remove(folderId);
2265 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002266 }
2267 }
2268
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002269 // Unpin shortcuts that don't exist on the workspace.
2270 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
2271 MutableInt numTimesPinned = sBgPinnedShortcutCounts.get(key);
2272 if (numTimesPinned == null || numTimesPinned.value == 0) {
2273 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
2274 mDeepShortcutManager.unpinShortcut(key);
2275 }
2276 }
2277
Sunny Goyal317698b2015-07-29 11:45:41 -07002278 // Sort all the folder items and make sure the first 3 items are high resolution.
2279 for (FolderInfo folder : sBgFolders) {
2280 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2281 int pos = 0;
2282 for (ShortcutInfo info : folder.contents) {
2283 if (info.usingLowResIcon) {
2284 info.updateIcon(mIconCache, false);
2285 }
2286 pos ++;
2287 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2288 break;
2289 }
2290 }
2291 }
2292
Chris Wrenf4d08112014-01-16 18:13:56 -05002293 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002294 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002295 ContentValues values = new ContentValues();
2296 values.put(LauncherSettings.Favorites.RESTORED, 0);
2297 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2298 Utilities.createDbSelectionQuery(
2299 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002300 }
2301
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002302 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2303 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002304 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002305 null, sWorker);
2306 }
2307
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002308 // Remove any empty screens
2309 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002310 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002311 long screenId = item.screenId;
2312 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2313 unusedScreens.contains(screenId)) {
2314 unusedScreens.remove(screenId);
2315 }
2316 }
2317
2318 // If there are any empty screens remove them, and update.
2319 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002320 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002321 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002322 }
2323
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002324 if (DEBUG_LOADERS) {
2325 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2326 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002327 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002328 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002329 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002330
Sunny Goyale2df0622015-04-24 11:27:00 -07002331 for (int i = 0; i < nScreens; i++) {
2332 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002333 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002334 line += " | ";
2335 }
Joe Onorato36115782010-06-17 13:28:48 -04002336 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002337 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002338 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002339 }
Joe Onorato36115782010-06-17 13:28:48 -04002340 }
Sunny Goyale26d1002016-06-20 14:52:14 -07002341 if (LauncherAppState.PROFILE_STARTUP) {
2342 Trace.endSection();
2343 }
Adam Cohene25af792013-06-06 23:08:25 -07002344 }
2345
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002346 /**
2347 * Partially updates the item without any notification. Must be called on the worker thread.
2348 */
2349 private void updateItem(long itemId, ContentValues update) {
2350 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002351 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002352 update,
2353 BaseColumns._ID + "= ?",
2354 new String[]{Long.toString(itemId)});
2355 }
2356
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002357 /** Filters the set of items who are directly or indirectly (via another container) on the
2358 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002359 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002360 ArrayList<ItemInfo> allWorkspaceItems,
2361 ArrayList<ItemInfo> currentScreenItems,
2362 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002363 // Purge any null ItemInfos
2364 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2365 while (iter.hasNext()) {
2366 ItemInfo i = iter.next();
2367 if (i == null) {
2368 iter.remove();
2369 }
2370 }
2371
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002372 // Order the set of items by their containers first, this allows use to walk through the
2373 // list sequentially, build up a list of containers that are in the specified screen,
2374 // as well as all items in those containers.
2375 Set<Long> itemsOnScreen = new HashSet<Long>();
2376 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2377 @Override
2378 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07002379 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002380 }
2381 });
2382 for (ItemInfo info : allWorkspaceItems) {
2383 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002384 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002385 currentScreenItems.add(info);
2386 itemsOnScreen.add(info.id);
2387 } else {
2388 otherScreenItems.add(info);
2389 }
2390 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2391 currentScreenItems.add(info);
2392 itemsOnScreen.add(info.id);
2393 } else {
2394 if (itemsOnScreen.contains(info.container)) {
2395 currentScreenItems.add(info);
2396 itemsOnScreen.add(info.id);
2397 } else {
2398 otherScreenItems.add(info);
2399 }
2400 }
2401 }
2402 }
2403
2404 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002405 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002406 ArrayList<LauncherAppWidgetInfo> appWidgets,
2407 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2408 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002409
2410 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002411 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002412 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002413 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002414 currentScreenWidgets.add(widget);
2415 } else {
2416 otherScreenWidgets.add(widget);
2417 }
2418 }
2419 }
2420
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002421 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2422 * right) */
2423 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002424 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002425 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002426 final int screenCols = profile.numColumns;
2427 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002428 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002429 @Override
2430 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002431 if (lhs.container == rhs.container) {
2432 // Within containers, order by their spatial position in that container
2433 switch ((int) lhs.container) {
2434 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2435 long lr = (lhs.screenId * screenCellCount +
2436 lhs.cellY * screenCols + lhs.cellX);
2437 long rr = (rhs.screenId * screenCellCount +
2438 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07002439 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07002440 }
2441 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2442 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07002443 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07002444 }
2445 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002446 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002447 throw new RuntimeException("Unexpected container type when " +
2448 "sorting workspace items.");
2449 }
2450 return 0;
2451 }
2452 } else {
2453 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07002454 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07002455 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002456 }
2457 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002458 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002459
Adam Cohendcd297f2013-06-18 13:13:40 -07002460 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2461 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002462 final Runnable r = new Runnable() {
2463 @Override
2464 public void run() {
2465 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2466 if (callbacks != null) {
2467 callbacks.bindScreens(orderedScreens);
2468 }
2469 }
2470 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002471 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002472 }
2473
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002474 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2475 final ArrayList<ItemInfo> workspaceItems,
2476 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002477 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002478
2479 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002480 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002481 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002482 final int start = i;
2483 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002484 final Runnable r = new Runnable() {
2485 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002486 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002487 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002488 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002489 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2490 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002491 }
2492 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002493 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002494 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002495 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002496
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002497 // Bind the widgets, one at a time
2498 N = appWidgets.size();
2499 for (int i = 0; i < N; i++) {
2500 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2501 final Runnable r = new Runnable() {
2502 public void run() {
2503 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2504 if (callbacks != null) {
2505 callbacks.bindAppWidget(widget);
2506 }
2507 }
2508 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002509 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002510 }
2511 }
2512
2513 /**
2514 * Binds all loaded data to actual views on the main thread.
2515 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002516 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002517 final long t = SystemClock.uptimeMillis();
2518 Runnable r;
2519
2520 // Don't use these two variables in any of the callback runnables.
2521 // Otherwise we hold a reference to them.
2522 final Callbacks oldCallbacks = mCallbacks.get();
2523 if (oldCallbacks == null) {
2524 // This launcher has exited and nobody bothered to tell us. Just bail.
2525 Log.w(TAG, "LoaderTask running with no launcher");
2526 return;
2527 }
2528
Winson Chung9b9fb962013-11-15 15:39:34 -08002529 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07002530 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
2531 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
2532 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002533
Winson Chung2abf94d2012-07-18 18:16:38 -07002534 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002535 workspaceItems.addAll(sBgWorkspaceItems);
2536 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002537 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002538 }
2539
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002540 final int currentScreen;
2541 {
2542 int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE
2543 ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen();
2544 if (currScreen >= orderedScreenIds.size()) {
2545 // There may be no workspace screens (just hotseat items and an empty page).
2546 currScreen = PagedView.INVALID_RESTORE_PAGE;
2547 }
2548 currentScreen = currScreen;
Winson Chung9b9fb962013-11-15 15:39:34 -08002549 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002550 final boolean validFirstPage = currentScreen >= 0;
2551 final long currentScreenId =
2552 validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID;
Winson Chung9b9fb962013-11-15 15:39:34 -08002553
Winson Chung9b9fb962013-11-15 15:39:34 -08002554 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07002555 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
2556 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
2557 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
2558 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002559
Winson Chung9b9fb962013-11-15 15:39:34 -08002560 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002561 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002562 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002563 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002564 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2565 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2566
2567 // Tell the workspace that we're about to start binding items
2568 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002569 public void run() {
2570 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2571 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002572 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002573 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002574 }
2575 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002576 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002577 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002578
Adam Cohendcd297f2013-06-18 13:13:40 -07002579 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2580
Sunny Goyal527c7d32015-08-28 15:19:36 -07002581 Executor mainExecutor = new DeferredMainThreadExecutor();
2582 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07002583 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002584
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002585 // In case of validFirstPage, only bind the first screen, and defer binding the
2586 // remaining screens after first onDraw (and an optional the fade animation whichever
2587 // happens later).
2588 // This ensures that the first screen is immediately visible (eg. during rotation)
2589 // In case of !validFirstPage, bind all pages one after other.
2590 final Executor deferredExecutor =
2591 validFirstPage ? new ViewOnDrawExecutor(mHandler) : mainExecutor;
2592
2593 mainExecutor.execute(new Runnable() {
2594 @Override
2595 public void run() {
2596 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2597 if (callbacks != null) {
2598 callbacks.finishFirstPageBind(
2599 validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null);
2600 }
2601 }
2602 });
Sunny Goyal527c7d32015-08-28 15:19:36 -07002603
Sunny Goyal44c06432016-04-02 10:56:02 -07002604 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002605
2606 // Tell the workspace that we're done binding items
2607 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002608 public void run() {
2609 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2610 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002611 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002612 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002613
Sunny Goyal639e9062015-08-19 19:17:06 -07002614 mIsLoadingAndBindingWorkspace = false;
2615
2616 // Run all the bind complete runnables after workspace is bound.
2617 if (!mBindCompleteRunnables.isEmpty()) {
2618 synchronized (mBindCompleteRunnables) {
2619 for (final Runnable r : mBindCompleteRunnables) {
2620 runOnWorkerThread(r);
2621 }
2622 mBindCompleteRunnables.clear();
2623 }
2624 }
2625
Winson Chung98e030b2012-05-07 16:01:11 -07002626 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002627 if (DEBUG_LOADERS) {
2628 Log.d(TAG, "bound workspace in "
2629 + (SystemClock.uptimeMillis()-t) + "ms");
2630 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002631
Joe Onorato36115782010-06-17 13:28:48 -04002632 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002633 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002634 deferredExecutor.execute(r);
2635
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002636 if (validFirstPage) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002637 r = new Runnable() {
2638 public void run() {
2639 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2640 if (callbacks != null) {
2641 // We are loading synchronously, which means, some of the pages will be
2642 // bound after first draw. Inform the callbacks that page binding is
2643 // not complete, and schedule the remaining pages.
2644 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2645 callbacks.onPageBoundSynchronously(currentScreen);
2646 }
2647 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2648 }
2649 }
2650 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002651 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002652 }
Joe Onorato36115782010-06-17 13:28:48 -04002653 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002654
Joe Onorato36115782010-06-17 13:28:48 -04002655 private void loadAndBindAllApps() {
2656 if (DEBUG_LOADERS) {
2657 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2658 }
2659 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002660 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002661 synchronized (LoaderTask.this) {
2662 if (mStopped) {
2663 return;
2664 }
2665 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002666 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002667 synchronized (LoaderTask.this) {
2668 if (mStopped) {
2669 return;
2670 }
2671 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002672 }
Joe Onorato36115782010-06-17 13:28:48 -04002673 } else {
2674 onlyBindAllApps();
2675 }
2676 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002677
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002678 private void updateIconCache() {
2679 // Ignore packages which have a promise icon.
2680 HashSet<String> packagesToIgnore = new HashSet<>();
2681 synchronized (sBgLock) {
2682 for (ItemInfo info : sBgItemsIdMap) {
2683 if (info instanceof ShortcutInfo) {
2684 ShortcutInfo si = (ShortcutInfo) info;
2685 if (si.isPromise() && si.getTargetComponent() != null) {
2686 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2687 }
2688 } else if (info instanceof LauncherAppWidgetInfo) {
2689 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2690 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2691 packagesToIgnore.add(lawi.providerName.getPackageName());
2692 }
2693 }
2694 }
2695 }
2696 mIconCache.updateDbIcons(packagesToIgnore);
2697 }
2698
Joe Onorato36115782010-06-17 13:28:48 -04002699 private void onlyBindAllApps() {
2700 final Callbacks oldCallbacks = mCallbacks.get();
2701 if (oldCallbacks == null) {
2702 // This launcher has exited and nobody bothered to tell us. Just bail.
2703 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2704 return;
2705 }
2706
2707 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002708 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002709 final ArrayList<AppInfo> list
2710 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002711 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002712 public void run() {
2713 final long t = SystemClock.uptimeMillis();
2714 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2715 if (callbacks != null) {
2716 callbacks.bindAllApplications(list);
2717 }
2718 if (DEBUG_LOADERS) {
2719 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002720 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04002721 }
2722 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002723 };
Tony Wickham80f57872016-06-29 18:12:15 -07002724 runOnMainThread(r);
Joe Onorato36115782010-06-17 13:28:48 -04002725 }
2726
Winson Chung64359a52013-07-08 17:17:08 -07002727 private void loadAllApps() {
2728 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002729
Joe Onorato36115782010-06-17 13:28:48 -04002730 final Callbacks oldCallbacks = mCallbacks.get();
2731 if (oldCallbacks == null) {
2732 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002733 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002734 return;
2735 }
2736
Kenny Guyed131872014-04-30 03:02:21 +01002737 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2738
Winson Chung64359a52013-07-08 17:17:08 -07002739 // Clear the list of apps
2740 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002741 for (UserHandleCompat user : profiles) {
2742 // Query for the set of apps
2743 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002744 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002745 if (DEBUG_LOADERS) {
2746 Log.d(TAG, "getActivityList took "
2747 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2748 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2749 }
2750 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002751 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002752 if (apps == null || apps.isEmpty()) {
2753 return;
2754 }
Kenny Guyff05f432016-01-22 17:48:29 +00002755 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002756 // Create the ApplicationInfos
2757 for (int i = 0; i < apps.size(); i++) {
2758 LauncherActivityInfoCompat app = apps.get(i);
2759 // This builds the icon bitmaps.
Kenny Guyff05f432016-01-22 17:48:29 +00002760 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode));
Kenny Guyed131872014-04-30 03:02:21 +01002761 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002762
Sunny Goyal756a28a2015-04-23 17:07:55 -07002763 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2764 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002765 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002766
2767 @Override
2768 public void run() {
2769 heuristic.processUserApps(apps);
2770 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002771 };
2772 runOnMainThread(new Runnable() {
2773
2774 @Override
2775 public void run() {
2776 // Check isLoadingWorkspace on the UI thread, as it is updated on
2777 // the UI thread.
2778 if (mIsLoadingAndBindingWorkspace) {
2779 synchronized (mBindCompleteRunnables) {
2780 mBindCompleteRunnables.add(r);
2781 }
2782 } else {
2783 runOnWorkerThread(r);
2784 }
2785 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002786 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002787 }
Winson Chung64359a52013-07-08 17:17:08 -07002788 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002789 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002790 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2791 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002792
2793 // Post callback on main thread
2794 mHandler.post(new Runnable() {
2795 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002796
Winson Chung64359a52013-07-08 17:17:08 -07002797 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002798 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002799 if (callbacks != null) {
2800 callbacks.bindAllApplications(added);
2801 if (DEBUG_LOADERS) {
2802 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002803 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07002804 }
2805 } else {
2806 Log.i(TAG, "not binding apps: no Launcher activity");
2807 }
2808 }
2809 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002810 // Cleanup any data stored for a deleted user.
2811 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04002812 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002813 Log.d(TAG, "Icons processed in "
2814 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002815 }
2816 }
2817
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002818 private void loadAndBindDeepShortcuts() {
2819 if (DEBUG_LOADERS) {
2820 Log.d(TAG, "loadAndBindDeepShortcuts mDeepShortcutsLoaded=" + mDeepShortcutsLoaded);
2821 }
2822 if (!mDeepShortcutsLoaded) {
2823 mBgDeepShortcutMap.clear();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002824 mHasShortcutHostPermission = mDeepShortcutManager.hasHostPermission();
2825 if (mHasShortcutHostPermission) {
2826 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
2827 if (mUserManager.isUserUnlocked(user)) {
2828 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager
2829 .queryForAllShortcuts(user);
2830 updateDeepShortcutMap(null, user, shortcuts);
2831 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002832 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002833 }
2834 synchronized (LoaderTask.this) {
2835 if (mStopped) {
2836 return;
2837 }
2838 mDeepShortcutsLoaded = true;
2839 }
2840 }
Tony Wickham80f57872016-06-29 18:12:15 -07002841 bindDeepShortcuts();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002842 }
2843
Joe Onoratobe386092009-11-17 17:32:16 -08002844 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002845 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002846 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002847 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2848 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2849 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2850 }
Joe Onorato36115782010-06-17 13:28:48 -04002851 }
2852 }
2853
Sunny Goyal860538d2016-07-20 12:35:16 -07002854 /**
2855 * Clear all the shortcuts for the given package, and re-add the new shortcuts.
2856 */
2857 private void updateDeepShortcutMap(
2858 String packageName, UserHandleCompat user, List<ShortcutInfoCompat> shortcuts) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002859 if (packageName != null) {
2860 Iterator<ComponentKey> keysIter = mBgDeepShortcutMap.keySet().iterator();
2861 while (keysIter.hasNext()) {
Sunny Goyal860538d2016-07-20 12:35:16 -07002862 ComponentKey next = keysIter.next();
2863 if (next.componentName.getPackageName().equals(packageName)
2864 && next.user.equals(user)) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002865 keysIter.remove();
2866 }
2867 }
2868 }
2869
2870 // Now add the new shortcuts to the map.
2871 for (ShortcutInfoCompat shortcut : shortcuts) {
Tony Wickhamca258b32016-07-28 12:31:28 -07002872 boolean shouldShowInContainer = shortcut.isEnabled()
2873 && (shortcut.isDeclaredInManifest() || shortcut.isDynamic());
2874 if (shouldShowInContainer) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002875 ComponentKey targetComponent
2876 = new ComponentKey(shortcut.getActivity(), shortcut.getUserHandle());
2877 mBgDeepShortcutMap.addToList(targetComponent, shortcut.getId());
2878 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002879 }
2880 }
2881
Tony Wickham80f57872016-06-29 18:12:15 -07002882 public void bindDeepShortcuts() {
Sunny Goyal5f064012016-08-08 10:10:01 -07002883 final MultiHashMap<ComponentKey, String> shortcutMapCopy = mBgDeepShortcutMap.clone();
Tony Wickham80f57872016-06-29 18:12:15 -07002884 Runnable r = new Runnable() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002885 @Override
2886 public void run() {
2887 Callbacks callbacks = getCallback();
2888 if (callbacks != null) {
2889 callbacks.bindDeepShortcutMap(shortcutMapCopy);
2890 }
2891 }
Tony Wickham80f57872016-06-29 18:12:15 -07002892 };
2893 runOnMainThread(r);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002894 }
2895
Sunny Goyal75b0f552015-05-20 21:57:06 -07002896 /**
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002897 * Refreshes the cached shortcuts if the shortcut permission has changed.
2898 * Current implementation simply reloads the workspace, but it can be optimized to
2899 * use partial updates similar to {@link UserManagerCompat}
2900 */
2901 public void refreshShortcutsIfRequired() {
2902 if (Utilities.isNycMR1OrAbove()) {
2903 sWorker.removeCallbacks(mShortcutPermissionCheckRunnable);
2904 sWorker.post(mShortcutPermissionCheckRunnable);
2905 }
2906 }
2907
2908 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -07002909 * Called when the icons for packages have been updated in the icon cache.
2910 */
2911 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2912 final Callbacks callbacks = getCallback();
2913 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2914 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2915
2916 // If any package icon has changed (app was updated while launcher was dead),
2917 // update the corresponding shortcuts.
2918 synchronized (sBgLock) {
2919 for (ItemInfo info : sBgItemsIdMap) {
2920 if (info instanceof ShortcutInfo && user.equals(info.user)
2921 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2922 ShortcutInfo si = (ShortcutInfo) info;
2923 ComponentName cn = si.getTargetComponent();
2924 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2925 si.updateIcon(mIconCache);
2926 updatedShortcuts.add(si);
2927 }
2928 }
2929 }
2930 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2931 }
2932
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002933 bindUpdatedShortcuts(updatedShortcuts, user);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002934
2935 if (!updatedApps.isEmpty()) {
2936 mHandler.post(new Runnable() {
2937
2938 public void run() {
2939 Callbacks cb = getCallback();
2940 if (cb != null && callbacks == cb) {
2941 cb.bindAppsUpdated(updatedApps);
2942 }
2943 }
2944 });
2945 }
2946 }
2947
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002948 private void bindUpdatedShortcuts(
2949 ArrayList<ShortcutInfo> updatedShortcuts, UserHandleCompat user) {
2950 bindUpdatedShortcuts(updatedShortcuts, new ArrayList<ShortcutInfo>(), user);
2951 }
2952
2953 private void bindUpdatedShortcuts(
2954 final ArrayList<ShortcutInfo> updatedShortcuts,
2955 final ArrayList<ShortcutInfo> removedShortcuts,
2956 final UserHandleCompat user) {
2957 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002958 final Callbacks callbacks = getCallback();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002959 mHandler.post(new Runnable() {
2960
2961 public void run() {
2962 Callbacks cb = getCallback();
2963 if (cb != null && callbacks == cb) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002964 cb.bindShortcutsChanged(updatedShortcuts, removedShortcuts, user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002965 }
2966 }
2967 });
2968 }
2969 }
2970
2971 void enqueueItemUpdatedTask(Runnable task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002972 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002973 }
2974
Adam Cohen091440a2015-03-18 14:16:05 -07002975 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002976
2977 @Override
2978 public void onReceive(Context context, Intent intent) {
2979 synchronized (sBgLock) {
2980 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2981 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002982 final PackageManager manager = context.getPackageManager();
2983 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2984 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002985 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2986 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002987 packagesRemoved.clear();
2988 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002989 for (String pkg : entry.getValue()) {
2990 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07002991 if (PackageManagerHelper.isAppOnSdcard(manager, pkg)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002992 packagesUnavailable.add(pkg);
2993 } else {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002994 packagesRemoved.add(pkg);
2995 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002996 }
2997 }
2998 if (!packagesRemoved.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002999 enqueueItemUpdatedTask(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003000 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
3001 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003002 if (!packagesUnavailable.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003003 enqueueItemUpdatedTask(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
Sunny Goyal1a745e82014-10-02 15:58:31 -07003004 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
3005 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003006 }
Sunny Goyal34942622014-08-29 17:20:55 -07003007 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003008 }
3009 }
3010 }
3011
Joe Onorato36115782010-06-17 13:28:48 -04003012 private class PackageUpdatedTask implements Runnable {
3013 int mOp;
3014 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01003015 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04003016
3017 public static final int OP_NONE = 0;
3018 public static final int OP_ADD = 1;
3019 public static final int OP_UPDATE = 2;
3020 public static final int OP_REMOVE = 3; // uninstlled
3021 public static final int OP_UNAVAILABLE = 4; // external media unmounted
Kenny Guy44cba692016-01-21 19:50:02 +00003022 public static final int OP_SUSPEND = 5; // package suspended
3023 public static final int OP_UNSUSPEND = 6; // package unsuspended
Sunny Goyalda891c12016-03-18 18:29:24 -07003024 public static final int OP_USER_AVAILABILITY_CHANGE = 7; // user available/unavailable
Joe Onorato36115782010-06-17 13:28:48 -04003025
Kenny Guyed131872014-04-30 03:02:21 +01003026 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04003027 mOp = op;
3028 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01003029 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04003030 }
3031
3032 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07003033 if (!mHasLoaderCompletedOnce) {
3034 // Loader has not yet run.
3035 return;
3036 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04003037 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04003038
3039 final String[] packages = mPackages;
3040 final int N = packages.length;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003041 FlagOp flagOp = FlagOp.NO_OP;
Sunny Goyalda891c12016-03-18 18:29:24 -07003042 StringFilter pkgFilter = StringFilter.of(new HashSet<>(Arrays.asList(packages)));
Joe Onorato36115782010-06-17 13:28:48 -04003043 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003044 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04003045 for (int i=0; i<N; i++) {
3046 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003047 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003048 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003049 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003050
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003051 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3052 if (heuristic != null) {
3053 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003054 }
Joe Onorato36115782010-06-17 13:28:48 -04003055 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003056 }
Joe Onorato36115782010-06-17 13:28:48 -04003057 case OP_UPDATE:
3058 for (int i=0; i<N; i++) {
3059 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003060 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003061 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003062 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003063 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003064 // Since package was just updated, the target must be available now.
3065 flagOp = FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04003066 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003067 case OP_REMOVE: {
3068 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3069 if (heuristic != null) {
3070 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003071 }
Joe Onorato36115782010-06-17 13:28:48 -04003072 for (int i=0; i<N; i++) {
3073 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003074 mIconCache.removeIconsForPkg(packages[i], mUser);
3075 }
3076 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003077 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003078 case OP_UNAVAILABLE:
3079 for (int i=0; i<N; i++) {
3080 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3081 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003082 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003083 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003084 flagOp = FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04003085 break;
Kenny Guy44cba692016-01-21 19:50:02 +00003086 case OP_SUSPEND:
3087 case OP_UNSUSPEND:
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003088 flagOp = mOp == OP_SUSPEND ?
3089 FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED) :
3090 FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED);
Sunny Goyal17763cb2016-03-24 13:53:22 -07003091 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.(un)suspend " + N);
Sunny Goyalda891c12016-03-18 18:29:24 -07003092 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
3093 break;
3094 case OP_USER_AVAILABILITY_CHANGE:
3095 flagOp = UserManagerCompat.getInstance(context).isQuietModeEnabled(mUser)
3096 ? FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER)
3097 : FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER);
3098 // We want to update all packages for this user.
3099 pkgFilter = StringFilter.matchesAll();
3100 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
Kenny Guy44cba692016-01-21 19:50:02 +00003101 break;
Joe Onorato36115782010-06-17 13:28:48 -04003102 }
3103
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003104 ArrayList<AppInfo> added = null;
3105 ArrayList<AppInfo> modified = null;
3106 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003107
Adam Cohen487f7dd2012-06-28 18:12:10 -07003108 if (mBgAllAppsList.added.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003109 added = new ArrayList<>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003110 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003111 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003112 if (mBgAllAppsList.modified.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003113 modified = new ArrayList<>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003114 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003115 }
Winson Chung5d55f332012-07-16 20:45:03 -07003116 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003117 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003118 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003119 }
3120
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003121 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps = new HashMap<>();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003122
Joe Onorato36115782010-06-17 13:28:48 -04003123 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003124 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003125 for (AppInfo ai : added) {
3126 addedOrUpdatedApps.put(ai.componentName, ai);
3127 }
Joe Onorato36115782010-06-17 13:28:48 -04003128 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003129
Joe Onorato36115782010-06-17 13:28:48 -04003130 if (modified != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003131 final Callbacks callbacks = getCallback();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003132 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003133 for (AppInfo ai : modified) {
3134 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003135 }
3136
Joe Onorato36115782010-06-17 13:28:48 -04003137 mHandler.post(new Runnable() {
3138 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003139 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003140 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003141 callbacks.bindAppsUpdated(modifiedFinal);
3142 }
3143 }
3144 });
3145 }
Winson Chung83892cc2013-05-01 16:53:33 -07003146
Sunny Goyal4390ace2014-10-13 11:33:11 -07003147 // Update shortcut infos
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003148 if (mOp == OP_ADD || flagOp != FlagOp.NO_OP) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003149 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3150 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3151 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3152
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003153 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003154 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003155 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3156 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003157 boolean infoUpdated = false;
3158 boolean shortcutUpdated = false;
3159
3160 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003161 if ((si.iconResource != null)
Sunny Goyalda891c12016-03-18 18:29:24 -07003162 && pkgFilter.matches(si.iconResource.packageName)) {
Sunny Goyal10629b02016-09-01 12:50:11 -07003163 Bitmap icon = LauncherIcons.createIconBitmap(
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003164 si.iconResource.packageName,
3165 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003166 if (icon != null) {
3167 si.setIcon(icon);
3168 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003169 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003170 }
3171 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003172
3173 ComponentName cn = si.getTargetComponent();
Sunny Goyalda891c12016-03-18 18:29:24 -07003174 if (cn != null && pkgFilter.matches(cn.getPackageName())) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003175 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3176
3177 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003178 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3179 // Auto install icon
3180 PackageManager pm = context.getPackageManager();
3181 ResolveInfo matched = pm.resolveActivity(
3182 new Intent(Intent.ACTION_MAIN)
3183 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3184 PackageManager.MATCH_DEFAULT_ONLY);
3185 if (matched == null) {
3186 // Try to find the best match activity.
3187 Intent intent = pm.getLaunchIntentForPackage(
3188 cn.getPackageName());
3189 if (intent != null) {
3190 cn = intent.getComponent();
3191 appInfo = addedOrUpdatedApps.get(cn);
3192 }
3193
3194 if ((intent == null) || (appInfo == null)) {
3195 removedShortcuts.add(si);
3196 continue;
3197 }
3198 si.promisedIntent = intent;
3199 }
3200 }
3201
3202 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003203 if (appInfo != null) {
3204 si.flags = appInfo.flags;
3205 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003206
Sunny Goyal756adbc2015-04-16 15:20:51 -07003207 si.intent = si.promisedIntent;
3208 si.promisedIntent = null;
3209 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003210 infoUpdated = true;
3211 si.updateIcon(mIconCache);
3212 }
3213
3214 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3215 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3216 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003217 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003218 si.contentDescription = appInfo.contentDescription;
3219 infoUpdated = true;
3220 }
3221
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003222 int oldDisabledFlags = si.isDisabled;
3223 si.isDisabled = flagOp.apply(si.isDisabled);
3224 if (si.isDisabled != oldDisabledFlags) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003225 shortcutUpdated = true;
3226 }
3227 }
3228
3229 if (infoUpdated || shortcutUpdated) {
3230 updatedShortcuts.add(si);
3231 }
3232 if (infoUpdated) {
3233 updateItemInDatabase(context, si);
3234 }
Sunny Goyalda891c12016-03-18 18:29:24 -07003235 } else if (info instanceof LauncherAppWidgetInfo && mOp == OP_ADD) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003236 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3237 if (mUser.equals(widgetInfo.user)
3238 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
Sunny Goyalda891c12016-03-18 18:29:24 -07003239 && pkgFilter.matches(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003240 widgetInfo.restoreStatus &=
3241 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3242 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003243
3244 // adding this flag ensures that launcher shows 'click to setup'
3245 // if the widget has a config activity. In case there is no config
3246 // activity, it will be marked as 'restored' during bind.
3247 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3248
Sunny Goyal4390ace2014-10-13 11:33:11 -07003249 widgets.add(widgetInfo);
3250 updateItemInDatabase(context, widgetInfo);
3251 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003252 }
3253 }
3254 }
3255
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003256 bindUpdatedShortcuts(updatedShortcuts, removedShortcuts, mUser);
3257 if (!removedShortcuts.isEmpty()) {
3258 deleteItemsFromDatabase(context, removedShortcuts);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003259 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003260
Sunny Goyal4390ace2014-10-13 11:33:11 -07003261 if (!widgets.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003262 final Callbacks callbacks = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003263 mHandler.post(new Runnable() {
3264 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003265 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003266 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003267 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003268 }
3269 }
3270 });
3271 }
3272 }
3273
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003274 final HashSet<String> removedPackages = new HashSet<>();
3275 final HashSet<ComponentName> removedComponents = new HashSet<>();
3276 if (mOp == OP_REMOVE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003277 // Mark all packages in the broadcast to be removed
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003278 Collections.addAll(removedPackages, packages);
3279
3280 // No need to update the removedComponents as
3281 // removedPackages is a super-set of removedComponents
Winson Chungdf95eb12013-10-16 14:57:07 -07003282 } else if (mOp == OP_UPDATE) {
3283 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003284 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003285 if (isPackageDisabled(context, packages[i], mUser)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003286 removedPackages.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003287 }
3288 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003289
3290 // Update removedComponents as some components can get removed during package update
3291 for (AppInfo info : removedApps) {
3292 removedComponents.add(info.componentName);
3293 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003294 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003295
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003296 if (!removedPackages.isEmpty() || !removedComponents.isEmpty()) {
3297 for (String pn : removedPackages) {
3298 deletePackageFromDatabase(context, pn, mUser);
3299 }
3300 for (ComponentName cn : removedComponents) {
3301 deleteItemsFromDatabase(context, getItemInfoForComponentName(cn, mUser));
Sunny Goyal1a745e82014-10-02 15:58:31 -07003302 }
3303
Winson Chungdf95eb12013-10-16 14:57:07 -07003304 // Remove any queued items from the install queue
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003305 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackages, mUser);
3306
Winson Chungdf95eb12013-10-16 14:57:07 -07003307 // Call the components-removed callback
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003308 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003309 mHandler.post(new Runnable() {
3310 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003311 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003312 if (callbacks == cb && cb != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003313 callbacks.bindWorkspaceComponentsRemoved(
3314 removedPackages, removedComponents, mUser);
3315 }
3316 }
3317 });
3318 }
3319
3320 if (!removedApps.isEmpty()) {
3321 // Remove corresponding apps from All-Apps
3322 final Callbacks callbacks = getCallback();
3323 mHandler.post(new Runnable() {
3324 public void run() {
3325 Callbacks cb = getCallback();
3326 if (callbacks == cb && cb != null) {
3327 callbacks.bindAppInfosRemoved(removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04003328 }
3329 }
3330 });
Joe Onoratobe386092009-11-17 17:32:16 -08003331 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003332
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003333 // Notify launcher of widget update. From marshmallow onwards we use AppWidgetHost to
3334 // get widget update signals.
3335 if (!Utilities.ATLEAST_MARSHMALLOW &&
3336 (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003337 final Callbacks callbacks = getCallback();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003338 mHandler.post(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003339 public void run() {
3340 Callbacks cb = getCallback();
3341 if (callbacks == cb && cb != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003342 callbacks.notifyWidgetProvidersChanged();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003343 }
3344 }
3345 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003346 }
3347 }
3348 }
3349
Sunny Goyal10923b32016-07-20 15:42:44 -07003350 /**
3351 * Repopulates the shortcut info, possibly updating any icon already on the workspace.
3352 */
3353 public void updateShortcutInfo(final ShortcutInfoCompat fullDetail, final ShortcutInfo info) {
3354 enqueueItemUpdatedTask(new Runnable() {
3355 @Override
3356 public void run() {
3357 info.updateFromDeepShortcutInfo(
3358 fullDetail, LauncherAppState.getInstance().getContext());
3359 ArrayList<ShortcutInfo> update = new ArrayList<ShortcutInfo>();
3360 update.add(info);
3361 bindUpdatedShortcuts(update, fullDetail.getUserHandle());
3362 }
3363 });
3364 }
3365
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003366 private class ShortcutsChangedTask implements Runnable {
Sunny Goyal50941fb2016-08-04 12:03:52 -07003367 private final String mPackageName;
3368 private final List<ShortcutInfoCompat> mShortcuts;
3369 private final UserHandleCompat mUser;
3370 private final boolean mUpdateIdMap;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003371
3372 public ShortcutsChangedTask(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal50941fb2016-08-04 12:03:52 -07003373 UserHandleCompat user, boolean updateIdMap) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003374 mPackageName = packageName;
3375 mShortcuts = shortcuts;
3376 mUser = user;
Sunny Goyal50941fb2016-08-04 12:03:52 -07003377 mUpdateIdMap = updateIdMap;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003378 }
3379
3380 @Override
3381 public void run() {
3382 mDeepShortcutManager.onShortcutsChanged(mShortcuts);
3383
3384 Map<String, ShortcutInfoCompat> idsToShortcuts = new HashMap<>();
3385 for (ShortcutInfoCompat shortcut : mShortcuts) {
3386 idsToShortcuts.put(shortcut.getId(), shortcut);
3387 }
3388
3389 // Find ShortcutInfo's that have changed on the workspace.
3390 MultiHashMap<String, ShortcutInfo> idsToWorkspaceShortcutInfos = new MultiHashMap<>();
3391 for (ItemInfo itemInfo : sBgItemsIdMap) {
3392 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
3393 ShortcutInfo si = (ShortcutInfo) itemInfo;
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07003394 if (si.getPromisedIntent().getPackage().equals(mPackageName)
3395 && si.user.equals(mUser)) {
Tony Wickham672d07f2016-07-27 18:22:41 -07003396 String shortcutId = si.getDeepShortcutId();
3397 if (idsToShortcuts.containsKey(shortcutId)) {
3398 idsToWorkspaceShortcutInfos.addToList(shortcutId, si);
3399 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003400 }
3401 }
3402 }
3403
3404 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3405 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager.queryForFullDetails(
3406 mPackageName, new ArrayList<>(idsToWorkspaceShortcutInfos.keySet()), mUser);
3407 ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3408 Context context = LauncherAppState.getInstance().getContext();
3409 for (ShortcutInfoCompat fullDetails : shortcuts) {
3410 List<ShortcutInfo> shortcutInfos = idsToWorkspaceShortcutInfos
3411 .get(fullDetails.getId());
3412 for (ShortcutInfo shortcutInfo : shortcutInfos) {
Sunny Goyal9994b2b2016-06-23 14:17:24 -07003413 shortcutInfo.updateFromDeepShortcutInfo(fullDetails, context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003414 updatedShortcutInfos.add(shortcutInfo);
3415 }
3416 }
3417 bindUpdatedShortcuts(updatedShortcutInfos, mUser);
3418
Sunny Goyal50941fb2016-08-04 12:03:52 -07003419 if (mUpdateIdMap) {
3420 // Update the deep shortcut map if the list of ids has changed for an activity.
3421 updateDeepShortcutMap(mPackageName, mUser, mShortcuts);
3422 bindDeepShortcuts();
3423 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003424 }
3425 }
3426
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003427 /**
3428 * Task to handle changing of lock state of the user
3429 */
3430 private class UserLockStateChangedTask implements Runnable {
3431
3432 private final UserHandleCompat mUser;
3433
3434 public UserLockStateChangedTask(UserHandleCompat user) {
3435 mUser = user;
3436 }
3437
3438 @Override
3439 public void run() {
3440 boolean isUserUnlocked = mUserManager.isUserUnlocked(mUser);
3441 Context context = mApp.getContext();
3442
3443 HashMap<ShortcutKey, ShortcutInfoCompat> pinnedShortcuts = new HashMap<>();
3444 if (isUserUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07003445 List<ShortcutInfoCompat> shortcuts =
3446 mDeepShortcutManager.queryForPinnedShortcuts(null, mUser);
3447 if (mDeepShortcutManager.wasLastCallSuccess()) {
3448 for (ShortcutInfoCompat shortcut : shortcuts) {
3449 pinnedShortcuts.put(ShortcutKey.fromInfo(shortcut), shortcut);
3450 }
3451 } else {
3452 // Shortcut manager can fail due to some race condition when the lock state
3453 // changes too frequently. For the purpose of the update,
3454 // consider it as still locked.
3455 isUserUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003456 }
3457 }
3458
3459 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3460 ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3461 ArrayList<ShortcutInfo> deletedShortcutInfos = new ArrayList<>();
3462 for (ItemInfo itemInfo : sBgItemsIdMap) {
3463 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT
3464 && mUser.equals(itemInfo.user)) {
3465 ShortcutInfo si = (ShortcutInfo) itemInfo;
3466 if (isUserUnlocked) {
3467 ShortcutInfoCompat shortcut =
Tony Wickhamfc02c1b2016-08-29 15:17:48 -07003468 pinnedShortcuts.get(ShortcutKey.fromShortcutInfo(si));
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003469 // We couldn't verify the shortcut during loader. If its no longer available
3470 // (probably due to clear data), delete the workspace item as well
3471 if (shortcut == null) {
3472 deletedShortcutInfos.add(si);
3473 continue;
3474 }
3475 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
3476 si.updateFromDeepShortcutInfo(shortcut, context);
3477 } else {
3478 si.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
3479 }
3480 updatedShortcutInfos.add(si);
3481 }
3482 }
3483 bindUpdatedShortcuts(updatedShortcutInfos, deletedShortcutInfos, mUser);
3484 if (!deletedShortcutInfos.isEmpty()) {
3485 deleteItemsFromDatabase(context, deletedShortcutInfos);
3486 }
3487
3488 // Remove shortcut id map for that user
3489 Iterator<ComponentKey> keysIter = mBgDeepShortcutMap.keySet().iterator();
3490 while (keysIter.hasNext()) {
3491 if (keysIter.next().user.equals(mUser)) {
3492 keysIter.remove();
3493 }
3494 }
3495
3496 if (isUserUnlocked) {
3497 updateDeepShortcutMap(null, mUser, mDeepShortcutManager.queryForAllShortcuts(mUser));
3498 }
3499 bindDeepShortcuts();
3500 }
3501 }
3502
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003503 private void bindWidgetsModel(final Callbacks callbacks, final WidgetsModel model) {
3504 mHandler.post(new Runnable() {
3505 @Override
3506 public void run() {
3507 Callbacks cb = getCallback();
3508 if (callbacks == cb && cb != null) {
3509 callbacks.bindWidgetsModel(model);
3510 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003511 }
3512 });
3513 }
3514
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003515 public void refreshAndBindWidgetsAndShortcuts(
3516 final Callbacks callbacks, final boolean bindFirst) {
3517 runOnWorkerThread(new Runnable() {
3518 @Override
3519 public void run() {
3520 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
3521 bindWidgetsModel(callbacks, mBgWidgetsModel.clone());
Sunny Goyal31860582015-09-18 08:38:57 -07003522 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003523 final WidgetsModel model = mBgWidgetsModel.updateAndClone(mApp.getContext());
3524 bindWidgetsModel(callbacks, model);
3525 // update the Widget entries inside DB on the worker thread.
3526 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3527 model.getRawList());
Sunny Goyal31860582015-09-18 08:38:57 -07003528 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003529 });
Michael Jurkac402cd92013-05-20 15:49:32 +02003530 }
3531
Adam Cohen091440a2015-03-18 14:16:05 -07003532 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003533 UserHandleCompat user) {
3534 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3535 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003536 }
Adam Cohen556f6132014-01-15 15:18:08 -08003537
Kenny Guyed131872014-04-30 03:02:21 +01003538 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3539 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003540 if (cn == null) {
3541 return false;
3542 }
Kenny Guyed131872014-04-30 03:02:21 +01003543 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3544 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003545 return false;
3546 }
Kenny Guyed131872014-04-30 03:02:21 +01003547 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003548 }
3549
Adam Cohena28b78e2014-05-20 17:03:04 -07003550 public static boolean isValidPackage(Context context, String packageName,
3551 UserHandleCompat user) {
3552 if (packageName == null) {
3553 return false;
3554 }
3555 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3556 return launcherApps.isPackageEnabledForProfile(packageName, user);
3557 }
3558
Joe Onorato9c1289c2009-08-17 11:03:03 -04003559 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003560 * Make an ShortcutInfo object for a restored application or shortcut item that points
3561 * to a package that is not yet installed on the system.
3562 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003563 public ShortcutInfo getRestoredItemInfo(Cursor c, Intent intent,
3564 int promiseType, int itemType, CursorIconInfo iconInfo) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003565 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003566 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003567
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003568 Bitmap icon = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003569 // the fallback icon
3570 if (icon == null) {
3571 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3572 } else {
3573 info.setIcon(icon);
3574 }
Sunny Goyal34942622014-08-29 17:20:55 -07003575
3576 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003577 String title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07003578 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003579 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003580 }
Sunny Goyal34942622014-08-29 17:20:55 -07003581 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3582 if (TextUtils.isEmpty(info.title)) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003583 info.title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07003584 }
Sunny Goyal34942622014-08-29 17:20:55 -07003585 } else {
3586 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3587 }
3588
Winson Chung82b016c2015-05-08 17:00:10 -07003589 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003590 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003591 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003592 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003593 return info;
3594 }
3595
3596 /**
3597 * Make an Intent object for a restored application or shortcut item that points
3598 * to the market page for the item.
3599 */
Adam Cohen091440a2015-03-18 14:16:05 -07003600 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003601 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003602 return getMarketIntent(componentName.getPackageName());
3603 }
3604
3605 static Intent getMarketIntent(String packageName) {
3606 return new Intent(Intent.ACTION_VIEW)
3607 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003608 .scheme("market")
3609 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003610 .appendQueryParameter("id", packageName)
3611 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003612 }
3613
3614 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003615 * Make an ShortcutInfo object for a shortcut that is an application.
3616 *
3617 * If c is not null, then it will be used to fill in missing data like the title and icon.
3618 */
Sunny Goyald09c3702016-04-06 16:18:20 -07003619 public ShortcutInfo getAppShortcutInfo(Intent intent,
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003620 UserHandleCompat user, Cursor c, CursorIconInfo iconInfo,
Sunny Goyal34b65272015-03-11 16:56:52 -07003621 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003622 if (user == null) {
3623 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003624 return null;
3625 }
3626
Kenny Guyed131872014-04-30 03:02:21 +01003627 ComponentName componentName = intent.getComponent();
3628 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08003629 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01003630 return null;
3631 }
3632
3633 Intent newIntent = new Intent(intent.getAction(), null);
3634 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3635 newIntent.setComponent(componentName);
3636 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003637 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003638 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3639 return null;
3640 }
3641
3642 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003643 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003644 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003645 Bitmap icon = iconInfo.loadIcon(c);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003646 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003647 }
3648
Sunny Goyald09c3702016-04-06 16:18:20 -07003649 if (lai != null && PackageManagerHelper.isAppSuspended(lai.getApplicationInfo())) {
3650 info.isDisabled = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
3651 }
3652
Joe Onorato56d82912010-03-07 14:32:10 -05003653 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003654 if (TextUtils.isEmpty(info.title) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003655 info.title = iconInfo.getTitle(c);
Joe Onorato56d82912010-03-07 14:32:10 -05003656 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003657
Joe Onorato56d82912010-03-07 14:32:10 -05003658 // fall back to the class name of the activity
3659 if (info.title == null) {
3660 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003661 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003662
Joe Onorato9c1289c2009-08-17 11:03:03 -04003663 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003664 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003665 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003666 if (lai != null) {
3667 info.flags = AppInfo.initFlags(lai);
3668 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003669 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003670 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003671
Sunny Goyale2df0622015-04-24 11:27:00 -07003672 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003673 ItemInfoFilter f) {
3674 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3675 for (ItemInfo i : infos) {
3676 if (i instanceof ShortcutInfo) {
3677 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003678 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003679 if (cn != null && f.filterItem(null, info, cn)) {
3680 filtered.add(info);
3681 }
3682 } else if (i instanceof FolderInfo) {
3683 FolderInfo info = (FolderInfo) i;
3684 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003685 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003686 if (cn != null && f.filterItem(info, s, cn)) {
3687 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003688 }
3689 }
Winson Chung64359a52013-07-08 17:17:08 -07003690 } else if (i instanceof LauncherAppWidgetInfo) {
3691 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3692 ComponentName cn = info.providerName;
3693 if (cn != null && f.filterItem(null, info, cn)) {
3694 filtered.add(info);
3695 }
Winson Chung8a435102012-08-30 17:16:53 -07003696 }
3697 }
Winson Chung64359a52013-07-08 17:17:08 -07003698 return new ArrayList<ItemInfo>(filtered);
3699 }
3700
Adam Cohen091440a2015-03-18 14:16:05 -07003701 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003702 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003703 ItemInfoFilter filter = new ItemInfoFilter() {
3704 @Override
3705 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003706 if (info.user == null) {
3707 return cn.equals(cname);
3708 } else {
3709 return cn.equals(cname) && info.user.equals(user);
3710 }
Winson Chung64359a52013-07-08 17:17:08 -07003711 }
3712 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003713 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003714 }
3715
Sunny Goyal1a745e82014-10-02 15:58:31 -07003716 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003717 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003718 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003719 @Thunk ShortcutInfo getShortcutInfo(Cursor c, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003720 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003721 // Non-app shortcuts are only supported for current user.
3722 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003723 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003724
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003725 // TODO: If there's an explicit component and we can't install that, delete it.
3726
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003727 loadInfoFromCursor(info, c, iconInfo);
3728 return info;
3729 }
Joe Onorato56d82912010-03-07 14:32:10 -05003730
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003731 /**
3732 * Make an ShortcutInfo object for a shortcut that isn't an application.
3733 */
3734 public void loadInfoFromCursor(ShortcutInfo info, Cursor c, CursorIconInfo iconInfo) {
3735 info.title = iconInfo.getTitle(c);
3736 Bitmap icon = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003737 // the fallback icon
3738 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003739 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003740 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003741 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003742 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003743 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003744
Sunny Goyal2350bc92014-10-14 16:42:54 -07003745 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003746 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3747 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3748 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3749
Adam Cohend9198822011-11-22 16:42:47 -08003750 if (intent == null) {
3751 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3752 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3753 return null;
3754 }
3755
Joe Onorato0589f0f2010-02-08 13:44:00 -08003756 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003757 ShortcutIconResource iconResource = null;
3758
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003759 if (bitmap instanceof Bitmap) {
Sunny Goyal10629b02016-09-01 12:50:11 -07003760 icon = LauncherIcons.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003761 } else {
3762 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003763 if (extra instanceof ShortcutIconResource) {
3764 iconResource = (ShortcutIconResource) extra;
Sunny Goyal10629b02016-09-01 12:50:11 -07003765 icon = LauncherIcons.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003766 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003767 }
3768 }
3769
Michael Jurkac9d95c52011-08-29 14:03:34 -07003770 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003771
Kenny Guyed131872014-04-30 03:02:21 +01003772 // Only support intents for current user for now. Intents sent from other
3773 // users wouldn't get here without intent forwarding anyway.
3774 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003775 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003776 icon = mIconCache.getDefaultIcon(info.user);
3777 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003778 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003779 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003780
Winson Chung82b016c2015-05-08 17:00:10 -07003781 info.title = Utilities.trim(name);
3782 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003783 info.intent = intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003784 info.iconResource = iconResource;
3785
3786 return info;
3787 }
3788
Joe Onorato9c1289c2009-08-17 11:03:03 -04003789 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003790 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003791 * or make a new one.
3792 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003793 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003794 // See if a placeholder was created for us already
3795 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003796 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003797 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003798 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003799 folders.put(id, folderInfo);
3800 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003801 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003802 }
3803
Joe Onoratobe386092009-11-17 17:32:16 -08003804
Sunny Goyal651077b2014-06-30 14:15:31 -07003805 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3806 return (provider != null) && (provider.provider != null)
3807 && (provider.provider.getPackageName() != null);
3808 }
3809
Joe Onoratobe386092009-11-17 17:32:16 -08003810 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003811 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003812 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3813 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3814 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3815 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003816 if (mLoaderTask != null) {
3817 mLoaderTask.dumpState();
3818 } else {
3819 Log.d(TAG, "mLoaderTask=null");
3820 }
Joe Onoratobe386092009-11-17 17:32:16 -08003821 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003822
3823 public Callbacks getCallback() {
3824 return mCallbacks != null ? mCallbacks.get() : null;
3825 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003826
3827 /**
3828 * @return {@link FolderInfo} if its already loaded.
3829 */
3830 public FolderInfo findFolderById(Long folderId) {
3831 synchronized (sBgLock) {
3832 return sBgFolders.get(folderId);
3833 }
3834 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003835
Sunny Goyal527c7d32015-08-28 15:19:36 -07003836 @Thunk class DeferredMainThreadExecutor implements Executor {
3837
3838 @Override
3839 public void execute(Runnable command) {
3840 runOnMainThread(command);
3841 }
3842 }
3843
Sunny Goyal756adbc2015-04-16 15:20:51 -07003844 /**
3845 * @return the looper for the worker thread which can be used to start background tasks.
3846 */
3847 public static Looper getWorkerLooper() {
3848 return sWorkerThread.getLooper();
3849 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003850}