blob: 6a63110b11502cc2ec2891a69921d23deab0b291 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Romain Guy629de3e2010-01-13 12:20:59 -080019import android.appwidget.AppWidgetProviderInfo;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070020import android.content.BroadcastReceiver;
21import android.content.ComponentName;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070022import android.content.ContentProviderOperation;
23import android.content.ContentResolver;
24import android.content.ContentValues;
25import android.content.Context;
26import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080027import android.content.Intent.ShortcutIconResource;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070028import android.content.IntentFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.content.pm.PackageManager;
30import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.database.Cursor;
32import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.net.Uri;
Joe Onorato36115782010-06-17 13:28:48 -040034import android.os.Handler;
35import android.os.HandlerThread;
Sunny Goyal756adbc2015-04-16 15:20:51 -070036import android.os.Looper;
Joe Onorato0589f0f2010-02-08 13:44:00 -080037import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.os.Process;
Joe Onorato9c1289c2009-08-17 11:03:03 -040039import android.os.SystemClock;
Sunny Goyale26d1002016-06-20 14:52:14 -070040import android.os.Trace;
Chris Wrenc3919c02013-09-18 09:48:33 -040041import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080042import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070043import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080044import android.util.LongSparseArray;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070045import android.util.MutableInt;
Winson Chungc9168342013-06-26 14:54:55 -070046import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010047
Sunny Goyalffe83f12014-08-14 17:39:34 -070048import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010049import com.android.launcher3.compat.LauncherActivityInfoCompat;
50import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070051import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070052import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010053import com.android.launcher3.compat.UserHandleCompat;
54import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070055import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -070056import com.android.launcher3.config.ProviderConfig;
Tony Wickham827cef22016-03-17 15:39:39 -070057import com.android.launcher3.dynamicui.ExtractionUtils;
Sunny Goyal26119432016-02-18 22:09:23 +000058import com.android.launcher3.folder.Folder;
59import com.android.launcher3.folder.FolderIcon;
Sunny Goyal1acb9e92016-05-16 12:41:09 -070060import com.android.launcher3.logging.FileLog;
Sunny Goyalf862a262015-12-14 14:27:38 -080061import com.android.launcher3.model.GridSizeMigrationTask;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -070062import com.android.launcher3.model.WidgetsModel;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -070063import com.android.launcher3.provider.ImportDataTask;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070064import com.android.launcher3.provider.LauncherDbUtils;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070065import com.android.launcher3.shortcuts.DeepShortcutManager;
66import com.android.launcher3.shortcuts.ShortcutInfoCompat;
67import com.android.launcher3.shortcuts.ShortcutKey;
Robin Lee26ace122015-03-16 19:41:43 +000068import com.android.launcher3.util.ComponentKey;
Sunny Goyal4e5cc642015-06-25 16:37:44 -070069import com.android.launcher3.util.CursorIconInfo;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -070070import com.android.launcher3.util.FlagOp;
Sunny Goyalff4ba2d2016-04-02 14:12:34 -070071import com.android.launcher3.util.GridOccupancy;
Sunny Goyale2df0622015-04-24 11:27:00 -070072import com.android.launcher3.util.LongArrayMap;
Sunny Goyal18bf8e22015-04-08 18:13:46 -070073import com.android.launcher3.util.ManagedProfileHeuristic;
Tony Wickhamd82a39d2016-07-01 15:44:13 -070074import com.android.launcher3.util.MultiHashMap;
Sunny Goyald09c3702016-04-06 16:18:20 -070075import com.android.launcher3.util.PackageManagerHelper;
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -070076import com.android.launcher3.util.Preconditions;
Sunny Goyalda891c12016-03-18 18:29:24 -070077import com.android.launcher3.util.StringFilter;
Adam Cohen091440a2015-03-18 14:16:05 -070078import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -070079import com.android.launcher3.util.ViewOnDrawExecutor;
Romain Guyedcce092010-03-04 13:03:17 -080080
Michael Jurkac2f801e2011-07-12 14:19:46 -070081import java.lang.ref.WeakReference;
82import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070083import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070084import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070085import java.util.Arrays;
Michael Jurkac2f801e2011-07-12 14:19:46 -070086import java.util.Collections;
87import java.util.Comparator;
88import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070089import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070090import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070091import java.util.List;
Tony Wickhambfbf7f92016-05-19 11:19:39 -070092import java.util.Map;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070093import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070094import java.util.Set;
Sunny Goyal527c7d32015-08-28 15:19:36 -070095import java.util.concurrent.Executor;
Michael Jurkac2f801e2011-07-12 14:19:46 -070096
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097/**
98 * Maintains in-memory state of the Launcher. It is expected that there should be only one
99 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700100 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101 */
Kenny Guyed131872014-04-30 03:02:21 +0100102public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +0100103 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800104 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -0400105 private static final boolean DEBUG_RECEIVER = false;
Sunny Goyal94485362014-09-18 16:13:58 -0700106 private static final boolean REMOVE_UNRESTORED_ICONS = true;
Chris Wrenb358f812014-04-16 13:37:00 -0400107
Joe Onorato9c1289c2009-08-17 11:03:03 -0400108 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700109
Joe Onorato36115782010-06-17 13:28:48 -0400110 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -0500111 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -0800112
Adam Cohen091440a2015-03-18 14:16:05 -0700113 @Thunk final LauncherAppState mApp;
114 @Thunk final Object mLock = new Object();
115 @Thunk DeferredHandler mHandler = new DeferredHandler();
116 @Thunk LoaderTask mLoaderTask;
117 @Thunk boolean mIsLoaderTaskRunning;
Sunny Goyal756a28a2015-04-23 17:07:55 -0700118 @Thunk boolean mHasLoaderCompletedOnce;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119
Adam Cohen091440a2015-03-18 14:16:05 -0700120 @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700121 static {
122 sWorkerThread.start();
123 }
Adam Cohen091440a2015-03-18 14:16:05 -0700124 @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700125
Joe Onoratocc67f472010-06-08 10:54:30 -0700126 // We start off with everything not loaded. After that, we assume that
127 // our monitoring of the package manager provides all updates and we never
128 // need to do a requery. These are only ever touched from the loader thread.
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700129 private boolean mWorkspaceLoaded;
130 private boolean mAllAppsLoaded;
131 private boolean mDeepShortcutsLoaded;
Joe Onoratocc67f472010-06-08 10:54:30 -0700132
Sunny Goyal756a28a2015-04-23 17:07:55 -0700133 /**
134 * Set of runnables to be called on the background thread after the workspace binding
135 * is complete.
136 */
137 static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>();
138
Adam Cohen091440a2015-03-18 14:16:05 -0700139 @Thunk WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700141 // < only access in worker thread >
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800142 private final AllAppsList mBgAllAppsList;
Hyunyoung Song9110d482015-05-22 14:49:23 -0700143 // Entire list of widgets.
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800144 private final WidgetsModel mBgWidgetsModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800145
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700146 // Maps all launcher activities to the id's of their shortcuts (if they have any).
147 private final MultiHashMap<ComponentKey, String> mBgDeepShortcutMap = new MultiHashMap<>();
148
Sunny Goyal95f3d6b2016-08-10 16:09:29 -0700149 private boolean mHasShortcutHostPermission;
150 // Runnable to check if the shortcuts permission has changed.
151 private final Runnable mShortcutPermissionCheckRunnable = new Runnable() {
152 @Override
153 public void run() {
154 if (mDeepShortcutsLoaded) {
155 boolean hasShortcutHostPermission = mDeepShortcutManager.hasHostPermission();
156 if (hasShortcutHostPermission != mHasShortcutHostPermission) {
157 mApp.reloadWorkspace();
158 }
159 }
160 }
161 };
162
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700163 // The lock that must be acquired before referencing any static bg data structures. Unlike
164 // other locks, this one can generally be held long-term because we never expect any of these
165 // static data structures to be referenced outside of the worker thread except on the first
166 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700167 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700168
Adam Cohen487f7dd2012-06-28 18:12:10 -0700169 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700170 // LauncherModel to their ids
Sunny Goyale2df0622015-04-24 11:27:00 -0700171 static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700172
Adam Cohen487f7dd2012-06-28 18:12:10 -0700173 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
174 // created by LauncherModel that are directly on the home screen (however, no widgets or
175 // shortcuts within folders).
176 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700177
Adam Cohen487f7dd2012-06-28 18:12:10 -0700178 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
179 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700180 new ArrayList<LauncherAppWidgetInfo>();
181
Adam Cohen487f7dd2012-06-28 18:12:10 -0700182 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
Sunny Goyale2df0622015-04-24 11:27:00 -0700183 static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700184
Adam Cohendcd297f2013-06-18 13:13:40 -0700185 // sBgWorkspaceScreens is the ordered set of workspace screens.
186 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
187
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700188 // sBgPinnedShortcutCounts is the ComponentKey representing a pinned shortcut to the number of
189 // times it is pinned.
190 static final Map<ShortcutKey, MutableInt> sBgPinnedShortcutCounts = new HashMap<>();
191
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700192 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700193 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
194 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700195
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700196 // </ only access in worker thread >
197
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700198 private IconCache mIconCache;
199 private DeepShortcutManager mDeepShortcutManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700201 private final LauncherAppsCompat mLauncherApps;
202 private final UserManagerCompat mUserManager;
Kenny Guyed131872014-04-30 03:02:21 +0100203
Joe Onorato9c1289c2009-08-17 11:03:03 -0400204 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700205 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400206 public int getCurrentWorkspaceScreen();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700207 public void clearPendingBinds();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400208 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700209 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
210 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700211 public void bindScreens(ArrayList<Long> orderedScreenIds);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -0700212 public void finishFirstPageBind(ViewOnDrawExecutor executor);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800213 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400214 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200215 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700216 public void bindAppsAdded(ArrayList<Long> newScreens,
217 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700218 ArrayList<ItemInfo> addAnimated,
219 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200220 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700221 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
222 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
223 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyal756adbc2015-04-16 15:20:51 -0700224 public void bindRestoreItemsChange(HashSet<ItemInfo> updates);
Sunny Goyal3bbbabc2016-03-15 09:16:30 -0700225 public void bindWorkspaceComponentsRemoved(
226 HashSet<String> packageNames, HashSet<ComponentName> components,
227 UserHandleCompat user);
228 public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos);
Sunny Goyal2e1efb42016-03-03 16:58:55 -0800229 public void notifyWidgetProvidersChanged();
230 public void bindWidgetsModel(WidgetsModel model);
Adam Cohen1462de32012-07-24 22:34:36 -0700231 public void onPageBoundSynchronously(int page);
Sunny Goyal527c7d32015-08-28 15:19:36 -0700232 public void executeOnNextDraw(ViewOnDrawExecutor executor);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700233 public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMap);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400234 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800235
Winson Chung64359a52013-07-08 17:17:08 -0700236 public interface ItemInfoFilter {
237 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
238 }
239
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700240 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter,
241 DeepShortcutManager deepShortcutManager) {
Winson Chunga6945242014-01-08 14:04:34 -0800242 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400243 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100244 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Hyunyoung Songeaf291b2015-06-17 21:12:44 -0700245 mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800246 mIconCache = iconCache;
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700247 mDeepShortcutManager = deepShortcutManager;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800248
Kenny Guyed131872014-04-30 03:02:21 +0100249 mLauncherApps = LauncherAppsCompat.getInstance(context);
250 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800251 }
252
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700253 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
254 * posted on the main thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700255 private void runOnMainThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700256 if (sWorkerThread.getThreadId() == Process.myTid()) {
257 // If we are on the worker thread, post onto the main handler
258 mHandler.post(r);
259 } else {
260 r.run();
261 }
262 }
263
264 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
265 * posted on the worker thread handler. */
Tony Wickham80f57872016-06-29 18:12:15 -0700266 private static void runOnWorkerThread(Runnable r) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700267 if (sWorkerThread.getThreadId() == Process.myTid()) {
268 r.run();
269 } else {
270 // If we are not on the worker thread, then post to the worker handler
271 sWorker.post(r);
272 }
273 }
274
Sunny Goyal756adbc2015-04-16 15:20:51 -0700275 public void setPackageState(final PackageInstallInfo installInfo) {
276 Runnable updateRunnable = new Runnable() {
277
278 @Override
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500279 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700280 synchronized (sBgLock) {
281 final HashSet<ItemInfo> updates = new HashSet<>();
282
283 if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
284 // Ignore install success events as they are handled by Package add events.
285 return;
286 }
287
Sunny Goyale2df0622015-04-24 11:27:00 -0700288 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700289 if (info instanceof ShortcutInfo) {
290 ShortcutInfo si = (ShortcutInfo) info;
291 ComponentName cn = si.getTargetComponent();
292 if (si.isPromise() && (cn != null)
293 && installInfo.packageName.equals(cn.getPackageName())) {
294 si.setInstallProgress(installInfo.progress);
295
296 if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) {
297 // Mark this info as broken.
298 si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
299 }
300 updates.add(si);
301 }
302 }
303 }
304
305 for (LauncherAppWidgetInfo widget : sBgAppWidgets) {
306 if (widget.providerName.getPackageName().equals(installInfo.packageName)) {
307 widget.installProgress = installInfo.progress;
308 updates.add(widget);
309 }
310 }
311
312 if (!updates.isEmpty()) {
313 // Push changes to the callback.
314 Runnable r = new Runnable() {
315 public void run() {
316 Callbacks callbacks = getCallback();
317 if (callbacks != null) {
318 callbacks.bindRestoreItemsChange(updates);
319 }
320 }
321 };
322 mHandler.post(r);
323 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500324 }
325 }
326 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700327 runOnWorkerThread(updateRunnable);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500328 }
329
Sunny Goyal756adbc2015-04-16 15:20:51 -0700330 /**
331 * Updates the icons and label of all pending icons for the provided package name.
332 */
333 public void updateSessionDisplayInfo(final String packageName) {
334 Runnable updateRunnable = new Runnable() {
335
336 @Override
Sunny Goyala22666f2014-09-18 13:25:15 -0700337 public void run() {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700338 synchronized (sBgLock) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700339 ArrayList<ShortcutInfo> updates = new ArrayList<>();
340 UserHandleCompat user = UserHandleCompat.myUserHandle();
Sunny Goyal756adbc2015-04-16 15:20:51 -0700341
Sunny Goyale2df0622015-04-24 11:27:00 -0700342 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700343 if (info instanceof ShortcutInfo) {
344 ShortcutInfo si = (ShortcutInfo) info;
345 ComponentName cn = si.getTargetComponent();
346 if (si.isPromise() && (cn != null)
347 && packageName.equals(cn.getPackageName())) {
348 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
349 // For auto install apps update the icon as well as label.
350 mIconCache.getTitleAndIcon(si,
351 si.promisedIntent, user,
352 si.shouldUseLowResIcon());
353 } else {
354 // Only update the icon for restored apps.
355 si.updateIcon(mIconCache);
356 }
357 updates.add(si);
358 }
359 }
360 }
361
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700362 bindUpdatedShortcuts(updates, user);
Sunny Goyala22666f2014-09-18 13:25:15 -0700363 }
364 }
365 };
Sunny Goyal756adbc2015-04-16 15:20:51 -0700366 runOnWorkerThread(updateRunnable);
Sunny Goyala22666f2014-09-18 13:25:15 -0700367 }
368
Adam Cohen76a47a12014-02-05 11:47:43 -0800369 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800370 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800371
372 if (allAppsApps == null) {
373 throw new RuntimeException("allAppsApps must not be null");
374 }
375 if (allAppsApps.isEmpty()) {
376 return;
377 }
378
379 // Process the newly added applications and add them to the database first
380 Runnable r = new Runnable() {
381 public void run() {
382 runOnMainThread(new Runnable() {
383 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800384 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800385 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500386 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800387 }
388 }
389 });
390 }
391 };
392 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700393 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800394
Sunny Goyala9116722015-04-29 13:55:58 -0700395 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800396 int[] xy, int spanX, int spanY) {
397 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -0700398 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700399
400 GridOccupancy occupied = new GridOccupancy(profile.numColumns, profile.numRows);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800401 if (occupiedPos != null) {
Sunny Goyala9116722015-04-29 13:55:58 -0700402 for (ItemInfo r : occupiedPos) {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700403 occupied.markCells(r, true);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800404 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800405 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -0700406 return occupied.findVacantCell(xy, spanX, spanY);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800407 }
408
409 /**
410 * Find a position on the screen for the given size or adds a new screen.
411 * @return screenId and the coordinates for the item.
412 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700413 @Thunk Pair<Long, int[]> findSpaceForItem(
Sunny Goyalc3642d42015-10-30 10:27:08 -0700414 Context context,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800415 ArrayList<Long> workspaceScreens,
416 ArrayList<Long> addedWorkspaceScreensFinal,
417 int spanX, int spanY) {
Sunny Goyala9116722015-04-29 13:55:58 -0700418 LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800419
Sunny Goyala9116722015-04-29 13:55:58 -0700420 // Use sBgItemsIdMap as all the items are already loaded.
Sunny Goyal756a28a2015-04-23 17:07:55 -0700421 assertWorkspaceLoaded();
Sunny Goyala9116722015-04-29 13:55:58 -0700422 synchronized (sBgLock) {
423 for (ItemInfo info : sBgItemsIdMap) {
424 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
425 ArrayList<ItemInfo> items = screenItems.get(info.screenId);
426 if (items == null) {
427 items = new ArrayList<>();
428 screenItems.put(info.screenId, items);
429 }
430 items.add(info);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800431 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800432 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800433 }
434
435 // Find appropriate space for the item.
436 long screenId = 0;
437 int[] cordinates = new int[2];
438 boolean found = false;
439
440 int screenCount = workspaceScreens.size();
441 // First check the preferred screen.
Sunny Goyala9116722015-04-29 13:55:58 -0700442 int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800443 if (preferredScreenIndex < screenCount) {
444 screenId = workspaceScreens.get(preferredScreenIndex);
445 found = findNextAvailableIconSpaceInScreen(
446 screenItems.get(screenId), cordinates, spanX, spanY);
447 }
448
449 if (!found) {
Sunny Goyala9116722015-04-29 13:55:58 -0700450 // Search on any of the screens starting from the first screen.
451 for (int screen = 1; screen < screenCount; screen++) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800452 screenId = workspaceScreens.get(screen);
453 if (findNextAvailableIconSpaceInScreen(
454 screenItems.get(screenId), cordinates, spanX, spanY)) {
455 // We found a space for it
456 found = true;
457 break;
458 }
459 }
460 }
461
462 if (!found) {
463 // Still no position found. Add a new screen to the end.
Sunny Goyald2497482015-09-22 18:24:19 -0700464 screenId = LauncherSettings.Settings.call(context.getContentResolver(),
465 LauncherSettings.Settings.METHOD_NEW_SCREEN_ID)
466 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800467
468 // Save the screen id for binding in the workspace
469 workspaceScreens.add(screenId);
470 addedWorkspaceScreensFinal.add(screenId);
471
472 // If we still can't find an empty space, then God help us all!!!
473 if (!findNextAvailableIconSpaceInScreen(
474 screenItems.get(screenId), cordinates, spanX, spanY)) {
475 throw new RuntimeException("Can't find space to add the item");
476 }
477 }
478 return Pair.create(screenId, cordinates);
479 }
480
481 /**
482 * Adds the provided items to the workspace.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800483 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700484 public void addAndBindAddedWorkspaceItems(final Context context,
Sunny Goyala214a632015-05-06 12:23:34 -0700485 final ArrayList<? extends ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800486 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800487 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700488 return;
Winson Chung997a9232013-07-24 15:33:46 -0700489 }
Winson Chung64359a52013-07-08 17:17:08 -0700490 // Process the newly added applications and add them to the database first
491 Runnable r = new Runnable() {
492 public void run() {
493 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
494 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
495
Winson Chung76828c82013-08-19 15:43:29 -0700496 // Get the list of workspace screens. We need to append to this list and
497 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
498 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800499 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700500 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800501 for (ItemInfo item : workspaceApps) {
Sunny Goyala9116722015-04-29 13:55:58 -0700502 if (item instanceof ShortcutInfo) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800503 // Short-circuit this logic if the icon exists somewhere on the workspace
Sunny Goyal756adbc2015-04-16 15:20:51 -0700504 if (shortcutExists(context, item.getIntent(), item.user)) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800505 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700506 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800507 }
Winson Chung76828c82013-08-19 15:43:29 -0700508
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800509 // Find appropriate space for the item.
Sunny Goyalc3642d42015-10-30 10:27:08 -0700510 Pair<Long, int[]> coords = findSpaceForItem(context,
Sunny Goyal9eba1fd2015-10-16 08:58:57 -0700511 workspaceScreens, addedWorkspaceScreensFinal, 1, 1);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800512 long screenId = coords.first;
513 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700514
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700515 ItemInfo itemInfo;
516 if (item instanceof ShortcutInfo || item instanceof FolderInfo) {
517 itemInfo = item;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800518 } else if (item instanceof AppInfo) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700519 itemInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700520 } else {
521 throw new RuntimeException("Unexpected info type");
522 }
Winson Chung94d67682013-09-25 16:29:40 -0700523
Winson Chung64359a52013-07-08 17:17:08 -0700524 // Add the shortcut to the db
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700525 addItemToDatabase(context, itemInfo,
Winson Chung64359a52013-07-08 17:17:08 -0700526 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700527 screenId, cordinates[0], cordinates[1]);
Winson Chung64359a52013-07-08 17:17:08 -0700528 // Save the ShortcutInfo for binding in the workspace
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700529 addedShortcutsFinal.add(itemInfo);
Winson Chung64359a52013-07-08 17:17:08 -0700530 }
531 }
532
Winson Chung76828c82013-08-19 15:43:29 -0700533 // Update the workspace screens
534 updateWorkspaceScreenOrder(context, workspaceScreens);
535
Adam Cohen76a47a12014-02-05 11:47:43 -0800536 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700537 runOnMainThread(new Runnable() {
538 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800539 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700540 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700541 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
542 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700543 if (!addedShortcutsFinal.isEmpty()) {
544 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
545 long lastScreenId = info.screenId;
546 for (ItemInfo i : addedShortcutsFinal) {
547 if (i.screenId == lastScreenId) {
548 addAnimated.add(i);
549 } else {
550 addNotAnimated.add(i);
551 }
Winson Chung997a9232013-07-24 15:33:46 -0700552 }
553 }
Winson Chungd64d1762013-08-20 14:37:16 -0700554 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800555 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700556 }
Winson Chung64359a52013-07-08 17:17:08 -0700557 }
Winson Chung997a9232013-07-24 15:33:46 -0700558 });
559 }
Winson Chung64359a52013-07-08 17:17:08 -0700560 }
561 };
562 runOnWorkerThread(r);
563 }
564
Joe Onorato9c1289c2009-08-17 11:03:03 -0400565 /**
566 * Adds an item to the DB if it was not created previously, or move it to a new
567 * <container, screen, cellX, cellY>
568 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800569 public static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700570 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400571 if (item.container == ItemInfo.NO_ID) {
572 // From all apps
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700573 addItemToDatabase(context, item, container, screenId, cellX, cellY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400574 } else {
575 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700576 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800577 }
578 }
579
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700580 static void checkItemInfoLocked(
581 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
582 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
583 if (modelItem != null && item != modelItem) {
584 // check all the data is consistent
585 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
586 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
587 ShortcutInfo shortcut = (ShortcutInfo) item;
588 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
589 modelShortcut.intent.filterEquals(shortcut.intent) &&
590 modelShortcut.id == shortcut.id &&
591 modelShortcut.itemType == shortcut.itemType &&
592 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700593 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700594 modelShortcut.cellX == shortcut.cellX &&
595 modelShortcut.cellY == shortcut.cellY &&
596 modelShortcut.spanX == shortcut.spanX &&
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700597 modelShortcut.spanY == shortcut.spanY) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700598 // For all intents and purposes, this is the same object
599 return;
600 }
601 }
602
603 // the modelItem needs to match up perfectly with item if our model is
604 // to be consistent with the database-- for now, just require
605 // modelItem == item or the equality check above
606 String msg = "item: " + ((item != null) ? item.toString() : "null") +
607 "modelItem: " +
608 ((modelItem != null) ? modelItem.toString() : "null") +
609 "Error: ItemInfo passed to checkItemInfo doesn't match original";
610 RuntimeException e = new RuntimeException(msg);
611 if (stackTrace != null) {
612 e.setStackTrace(stackTrace);
613 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800614 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700615 }
616 }
617
Michael Jurka816474f2012-06-25 14:49:02 -0700618 static void checkItemInfo(final ItemInfo item) {
619 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
620 final long itemId = item.id;
621 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700622 public void run() {
623 synchronized (sBgLock) {
624 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700625 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700626 }
627 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700628 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700629 }
630
Michael Jurkac9d95c52011-08-29 14:03:34 -0700631 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
632 final ItemInfo item, final String callingFunction) {
633 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700634 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700635 final ContentResolver cr = context.getContentResolver();
636
Adam Cohen487f7dd2012-06-28 18:12:10 -0700637 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700638 Runnable r = new Runnable() {
639 public void run() {
640 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700641 updateItemArrays(item, itemId, stackTrace);
642 }
643 };
644 runOnWorkerThread(r);
645 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700646
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700647 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
648 final ArrayList<ItemInfo> items, final String callingFunction) {
649 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700650
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700651 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
652 Runnable r = new Runnable() {
653 public void run() {
654 ArrayList<ContentProviderOperation> ops =
655 new ArrayList<ContentProviderOperation>();
656 int count = items.size();
657 for (int i = 0; i < count; i++) {
658 ItemInfo item = items.get(i);
659 final long itemId = item.id;
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700660 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700661 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700662
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700663 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
664 updateItemArrays(item, itemId, stackTrace);
665
666 }
667 try {
668 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
669 } catch (Exception e) {
670 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700671 }
672 }
673 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700674 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700675 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700676
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700677 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
678 // Lock on mBgLock *after* the db operation
679 synchronized (sBgLock) {
680 checkItemInfoLocked(itemId, item, stackTrace);
681
682 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
683 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
684 // Item is in a folder, make sure this folder exists
685 if (!sBgFolders.containsKey(item.container)) {
686 // An items container is being set to a that of an item which is not in
687 // the list of Folders.
688 String msg = "item: " + item + " container being set to: " +
689 item.container + ", not in the list of folders";
690 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700691 }
692 }
693
694 // Items are added/removed from the corresponding FolderInfo elsewhere, such
695 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
696 // that are on the desktop, as appropriate
697 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800698 if (modelItem != null &&
699 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
700 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700701 switch (modelItem.itemType) {
702 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
703 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700704 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700705 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
706 if (!sBgWorkspaceItems.contains(modelItem)) {
707 sBgWorkspaceItems.add(modelItem);
708 }
709 break;
710 default:
711 break;
712 }
713 } else {
714 sBgWorkspaceItems.remove(modelItem);
715 }
716 }
717 }
718
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800719 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400720 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700721 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700722 public static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700723 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400724 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400725 item.cellX = cellX;
726 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700727
Winson Chung3d503fb2011-07-13 17:25:49 -0700728 // We store hotseat items in canonical form which is this orientation invariant position
729 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700730 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700731 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700732 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700733 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700734 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700735 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400736
737 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400738 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700739 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
740 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800741 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700742 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400743
Michael Jurkac9d95c52011-08-29 14:03:34 -0700744 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700745 }
746
747 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700748 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
749 * cellX, cellY have already been updated on the ItemInfos.
750 */
Adam Cohenf9c184a2016-01-15 16:47:43 -0800751 public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700752 final long container, final int screen) {
753
754 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
755 int count = items.size();
756
757 for (int i = 0; i < count; i++) {
758 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700759 item.container = container;
760
761 // We store hotseat items in canonical form which is this orientation invariant position
762 // in the hotseat
763 if (context instanceof Launcher && screen < 0 &&
764 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700765 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700766 item.cellY);
767 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700768 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700769 }
770
771 final ContentValues values = new ContentValues();
772 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
773 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
774 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800775 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700776 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700777
778 contentValues.add(values);
779 }
780 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
781 }
782
783 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700784 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800785 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700786 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700787 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700788 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800789 item.cellX = cellX;
790 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700791 item.spanX = spanX;
792 item.spanY = spanY;
793
794 // We store hotseat items in canonical form which is this orientation invariant position
795 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700796 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700797 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700798 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700799 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700800 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700801 }
Adam Cohend4844c32011-02-18 19:25:06 -0800802
Adam Cohend4844c32011-02-18 19:25:06 -0800803 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800804 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700805 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
806 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800807 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700808 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
809 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700810 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800811
Michael Jurka816474f2012-06-25 14:49:02 -0700812 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700813 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700814
815 /**
816 * Update an item to the database in a specified container.
817 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700818 public static void updateItemInDatabase(Context context, final ItemInfo item) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700819 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100820 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700821 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800822 }
823
Sunny Goyal756a28a2015-04-23 17:07:55 -0700824 private void assertWorkspaceLoaded() {
Sunny Goyal8f531dd2015-08-25 17:08:57 -0700825 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Sunny Goyal639e9062015-08-19 19:17:06 -0700826 synchronized (mLock) {
827 if (!mHasLoaderCompletedOnce ||
828 (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) {
829 throw new RuntimeException("Trying to add shortcut while loader is running");
830 }
831 }
Sunny Goyal756a28a2015-04-23 17:07:55 -0700832 }
833 }
834
Adam Cohend4844c32011-02-18 19:25:06 -0800835 /**
Sunny Goyal756adbc2015-04-16 15:20:51 -0700836 * Returns true if the shortcuts already exists on the workspace. This must be called after
837 * the workspace has been loaded. We identify a shortcut by its intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838 */
Sunny Goyal756a28a2015-04-23 17:07:55 -0700839 @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) {
840 assertWorkspaceLoaded();
Sunny Goyaldfde9992015-05-01 12:31:32 -0700841 final String intentWithPkg, intentWithoutPkg;
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700842 if (intent.getComponent() != null) {
843 // If component is not null, an intent with null package will produce
844 // the same result and should also be a match.
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700845 String packageName = intent.getComponent().getPackageName();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700846 if (intent.getPackage() != null) {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700847 intentWithPkg = intent.toUri(0);
848 intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700849 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700850 intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0);
851 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700852 }
853 } else {
Sunny Goyaldfde9992015-05-01 12:31:32 -0700854 intentWithPkg = intent.toUri(0);
855 intentWithoutPkg = intent.toUri(0);
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700856 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700857
858 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -0700859 for (ItemInfo item : sBgItemsIdMap) {
Sunny Goyal756adbc2015-04-16 15:20:51 -0700860 if (item instanceof ShortcutInfo) {
861 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700862 Intent targetIntent = info.promisedIntent == null
863 ? info.intent : info.promisedIntent;
864 if (targetIntent != null && info.user.equals(user)) {
865 String s = targetIntent.toUri(0);
Sunny Goyaldfde9992015-05-01 12:31:32 -0700866 if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
867 return true;
868 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700869 }
870 }
871 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800872 }
Sunny Goyal756adbc2015-04-16 15:20:51 -0700873 return false;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700874 }
875
Joe Onorato9c1289c2009-08-17 11:03:03 -0400876 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400877 * Add an item to the database in a specified container. Sets the container, screen, cellX and
878 * cellY fields of the item. Also assigns an ID to the item.
879 */
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700880 public static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700881 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400882 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400883 item.cellX = cellX;
884 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700885 // We store hotseat items in canonical form which is this orientation invariant position
886 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700887 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700888 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700889 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700890 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700891 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700892 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400893
894 final ContentValues values = new ContentValues();
895 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100896 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400897
Sunny Goyald2497482015-09-22 18:24:19 -0700898 item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
899 .getLong(LauncherSettings.Settings.EXTRA_VALUE);
900
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700901 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -0700902
Jason Monk8e19cf22014-03-20 15:06:57 -0400903 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700904 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700905 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700906 cr.insert(LauncherSettings.Favorites.CONTENT_URI, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400907
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700908 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700909 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400910 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700911 sBgItemsIdMap.put(item.id, item);
912 switch (item.itemType) {
913 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
914 sBgFolders.put(item.id, (FolderInfo) item);
915 // Fall through
916 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
917 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700918 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700919 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
920 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
921 sBgWorkspaceItems.add(item);
922 } else {
923 if (!sBgFolders.containsKey(item.container)) {
924 // Adding an item to a folder that doesn't exist.
925 String msg = "adding item: " + item + " to a folder that " +
926 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700927 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700928 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700929 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700930 if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700931 incrementPinnedShortcutCount(
932 ShortcutKey.fromItemInfo(item), true /* shouldPin */);
Tony Wickhambfbf7f92016-05-19 11:19:39 -0700933 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700934 break;
935 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
936 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
937 break;
938 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700939 }
940 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700941 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700942 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700943 }
944
Sunny Goyal34942622014-08-29 17:20:55 -0700945 private static ArrayList<ItemInfo> getItemsByPackageName(
946 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700947 ItemInfoFilter filter = new ItemInfoFilter() {
948 @Override
949 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
950 return cn.getPackageName().equals(pn) && info.user.equals(user);
951 }
952 };
Sunny Goyale2df0622015-04-24 11:27:00 -0700953 return filterItemInfos(sBgItemsIdMap, filter);
Sunny Goyal34942622014-08-29 17:20:55 -0700954 }
955
956 /**
957 * Removes all the items from the database corresponding to the specified package.
958 */
959 static void deletePackageFromDatabase(Context context, final String pn,
960 final UserHandleCompat user) {
961 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700962 }
963
964 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700965 * Removes the specified item from the database
Joe Onorato9c1289c2009-08-17 11:03:03 -0400966 */
Sunny Goyalfa401a12015-04-10 13:45:42 -0700967 public static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700968 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
969 items.add(item);
970 deleteItemsFromDatabase(context, items);
971 }
972
973 /**
974 * Removes the specified items from the database
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700975 */
Sunny Goyal4390ace2014-10-13 11:33:11 -0700976 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400977 final ContentResolver cr = context.getContentResolver();
Michael Jurka83df1882011-08-31 20:59:26 -0700978 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700979 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700980 for (ItemInfo item : items) {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700981 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id);
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700982 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700983
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700984 // Lock on mBgLock *after* the db operation
985 synchronized (sBgLock) {
986 switch (item.itemType) {
987 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
988 sBgFolders.remove(item.id);
Sunny Goyale2df0622015-04-24 11:27:00 -0700989 for (ItemInfo info: sBgItemsIdMap) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700990 if (info.container == item.id) {
991 // We are deleting a folder which still contains items that
992 // think they are contained by that folder.
993 String msg = "deleting a folder (" + item + ") which still " +
994 "contains items (" + info + ")";
995 Log.e(TAG, msg);
996 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700997 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -0700998 sBgWorkspaceItems.remove(item);
999 break;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001000 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001001 decrementPinnedShortcutCount(ShortcutKey.fromItemInfo(item));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001002 // Fall through.
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001003 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1004 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1005 sBgWorkspaceItems.remove(item);
1006 break;
1007 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1008 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1009 break;
1010 }
1011 sBgItemsIdMap.remove(item.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001012 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001013 }
1014 }
Michael Jurka83df1882011-08-31 20:59:26 -07001015 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001016 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001017 }
1018
1019 /**
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001020 * Decrement the count for the given pinned shortcut, unpinning it if the count becomes 0.
1021 */
1022 private static void decrementPinnedShortcutCount(final ShortcutKey pinnedShortcut) {
1023 synchronized (sBgLock) {
1024 MutableInt count = sBgPinnedShortcutCounts.get(pinnedShortcut);
1025 if (count == null || --count.value == 0) {
1026 LauncherAppState.getInstance().getShortcutManager().unpinShortcut(pinnedShortcut);
1027 }
1028 }
1029 }
1030
1031 /**
1032 * Increment the count for the given shortcut, pinning it if the count becomes 1.
1033 *
1034 * As an optimization, the caller can pass shouldPin == false to avoid
1035 * unnecessary RPC's if the shortcut is already pinned.
1036 */
1037 private static void incrementPinnedShortcutCount(ShortcutKey pinnedShortcut, boolean shouldPin) {
1038 synchronized (sBgLock) {
1039 MutableInt count = sBgPinnedShortcutCounts.get(pinnedShortcut);
1040 if (count == null) {
1041 count = new MutableInt(1);
1042 sBgPinnedShortcutCounts.put(pinnedShortcut, count);
1043 } else {
1044 count.value++;
1045 }
1046 if (shouldPin && count.value == 1) {
1047 LauncherAppState.getInstance().getShortcutManager().pinShortcut(pinnedShortcut);
1048 }
1049 }
1050 }
1051
1052 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001053 * Update the order of the workspace screens in the database. The array list contains
1054 * a list of screen ids in the order that they should appear.
1055 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001056 public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001057 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001058 final ContentResolver cr = context.getContentResolver();
1059 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1060
1061 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001062 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001063 while (iter.hasNext()) {
1064 long id = iter.next();
1065 if (id < 0) {
1066 iter.remove();
1067 }
1068 }
1069
1070 Runnable r = new Runnable() {
1071 @Override
1072 public void run() {
Yura085c8532014-02-11 15:15:29 +00001073 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001074 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001075 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001076 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001077 for (int i = 0; i < count; i++) {
1078 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001079 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001080 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1081 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001082 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001083 }
Yura085c8532014-02-11 15:15:29 +00001084
1085 try {
1086 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1087 } catch (Exception ex) {
1088 throw new RuntimeException(ex);
1089 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001090
Winson Chungba9c37f2013-08-30 14:11:37 -07001091 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001092 sBgWorkspaceScreens.clear();
1093 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001094 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001095 }
1096 };
1097 runOnWorkerThread(r);
1098 }
1099
1100 /**
Winsonc0b52fe2015-09-09 16:38:15 -07001101 * Remove the specified folder and all its contents from the database.
Joe Onorato9c1289c2009-08-17 11:03:03 -04001102 */
Winsonc0b52fe2015-09-09 16:38:15 -07001103 public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001104 final ContentResolver cr = context.getContentResolver();
1105
Michael Jurkac9d95c52011-08-29 14:03:34 -07001106 Runnable r = new Runnable() {
1107 public void run() {
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001108 cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001109 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001110 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001111 sBgItemsIdMap.remove(info.id);
1112 sBgFolders.remove(info.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001113 sBgWorkspaceItems.remove(info);
1114 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001115
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001116 cr.delete(LauncherSettings.Favorites.CONTENT_URI,
Michael Jurkac9d95c52011-08-29 14:03:34 -07001117 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001118 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001119 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001120 for (ItemInfo childInfo : info.contents) {
1121 sBgItemsIdMap.remove(childInfo.id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001122 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001123 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001124 }
1125 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001126 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001127 }
1128
1129 /**
1130 * Set this as the current Launcher activity object for the loader.
1131 */
1132 public void initialize(Callbacks callbacks) {
1133 synchronized (mLock) {
Sunny Goyalaaf7d1d2016-05-17 13:38:54 -07001134 Preconditions.assertUIThread();
1135 // Remove any queued UI runnables
1136 mHandler.cancelAll();
1137 mCallbacks = new WeakReference<>(callbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001138 }
1139 }
1140
Kenny Guyed131872014-04-30 03:02:21 +01001141 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001142 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001143 int op = PackageUpdatedTask.OP_UPDATE;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001144 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001145 user));
1146 }
1147
1148 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001149 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001150 int op = PackageUpdatedTask.OP_REMOVE;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001151 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001152 user));
1153 }
1154
1155 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001156 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001157 int op = PackageUpdatedTask.OP_ADD;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001158 enqueueItemUpdatedTask(new PackageUpdatedTask(op, new String[] { packageName },
Kenny Guyed131872014-04-30 03:02:21 +01001159 user));
1160 }
1161
1162 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001163 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001164 boolean replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001165 enqueueItemUpdatedTask(
Sunny Goyal144154d2016-03-30 16:47:03 -07001166 new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, packageNames, user));
Kenny Guyed131872014-04-30 03:02:21 +01001167 }
1168
1169 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001170 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001171 boolean replacing) {
1172 if (!replacing) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001173 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guyed131872014-04-30 03:02:21 +01001174 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1175 user));
1176 }
Kenny Guyed131872014-04-30 03:02:21 +01001177 }
1178
Kenny Guy44cba692016-01-21 19:50:02 +00001179 @Override
1180 public void onPackagesSuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001181 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001182 PackageUpdatedTask.OP_SUSPEND, packageNames,
1183 user));
1184 }
1185
1186 @Override
1187 public void onPackagesUnsuspended(String[] packageNames, UserHandleCompat user) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001188 enqueueItemUpdatedTask(new PackageUpdatedTask(
Kenny Guy44cba692016-01-21 19:50:02 +00001189 PackageUpdatedTask.OP_UNSUSPEND, packageNames,
1190 user));
1191 }
1192
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001193 @Override
1194 public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
1195 UserHandleCompat user) {
Sunny Goyal50941fb2016-08-04 12:03:52 -07001196 enqueueItemUpdatedTask(new ShortcutsChangedTask(packageName, shortcuts, user, true));
1197 }
1198
1199 public void updatePinnedShortcuts(String packageName, List<ShortcutInfoCompat> shortcuts,
1200 UserHandleCompat user) {
1201 enqueueItemUpdatedTask(new ShortcutsChangedTask(packageName, shortcuts, user, false));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001202 }
1203
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001204 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001205 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1206 * ACTION_PACKAGE_CHANGED.
1207 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001208 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001209 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001210 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001211
Joe Onorato36115782010-06-17 13:28:48 -04001212 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001213 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001214 // If we have changed locale we need to clear out the labels in all apps/workspace.
1215 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001216 } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1217 || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
Sunny Goyal823fd502015-08-04 11:40:13 -07001218 UserManagerCompat.getInstance(context).enableAndResetCache();
Sunny Goyal957c13f2015-05-01 13:02:20 -07001219 forceReload();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001220 } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1221 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
1222 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
Sunny Goyalda891c12016-03-18 18:29:24 -07001223 UserHandleCompat user = UserHandleCompat.fromIntent(intent);
1224 if (user != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001225 if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
1226 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
1227 enqueueItemUpdatedTask(new PackageUpdatedTask(
1228 PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE,
1229 new String[0], user));
1230 }
1231
1232 // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so
1233 // we need to run the state change task again.
1234 if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
1235 Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
1236 enqueueItemUpdatedTask(new UserLockStateChangedTask(user));
1237 }
Sunny Goyalda891c12016-03-18 18:29:24 -07001238 }
Tony Wickham827cef22016-03-17 15:39:39 -07001239 } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) {
1240 ExtractionUtils.startColorExtractionServiceIfNecessary(context);
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001241 }
1242 }
1243
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001244 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001245 resetLoadedState(true, true);
1246
Reena Lee93f824a2011-09-23 17:20:28 -07001247 // Do this here because if the launcher activity is running it will be restarted.
1248 // If it's not running startLoaderFromBackground will merely tell it that it needs
1249 // to reload.
1250 startLoaderFromBackground();
1251 }
1252
Winson Chungf0c6ae02012-03-21 16:10:31 -07001253 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1254 synchronized (mLock) {
1255 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1256 // mWorkspaceLoaded to true later
1257 stopLoaderLocked();
1258 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1259 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001260 // Always reset deep shortcuts loaded.
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001261 // TODO: why?
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001262 mDeepShortcutsLoaded = false;
Winson Chungf0c6ae02012-03-21 16:10:31 -07001263 }
1264 }
1265
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001266 /**
1267 * When the launcher is in the background, it's possible for it to miss paired
1268 * configuration changes. So whenever we trigger the loader from the background
1269 * tell the launcher that it needs to re-run the loader when it comes back instead
1270 * of doing it now.
1271 */
1272 public void startLoaderFromBackground() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001273 Callbacks callbacks = getCallback();
1274 if (callbacks != null) {
1275 // Only actually run the loader if they're not paused.
1276 if (!callbacks.setLoadOnResume()) {
Sunny Goyal93f878c2016-03-30 17:31:24 -07001277 startLoader(callbacks.getCurrentWorkspaceScreen());
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001278 }
1279 }
Joe Onorato36115782010-06-17 13:28:48 -04001280 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001281
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001282 /**
1283 * If there is already a loader task running, tell it to stop.
1284 */
1285 private void stopLoaderLocked() {
Reena Lee93f824a2011-09-23 17:20:28 -07001286 LoaderTask oldTask = mLoaderTask;
1287 if (oldTask != null) {
Reena Lee93f824a2011-09-23 17:20:28 -07001288 oldTask.stopLocked();
1289 }
Reena Lee93f824a2011-09-23 17:20:28 -07001290 }
1291
Adam Cohen1a85c582014-09-30 09:48:49 -07001292 public boolean isCurrentCallbacks(Callbacks callbacks) {
1293 return (mCallbacks != null && mCallbacks.get() == callbacks);
1294 }
1295
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001296 /**
1297 * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
1298 * @return true if the page could be bound synchronously.
1299 */
1300 public boolean startLoader(int synchronousBindPage) {
Sunny Goyal756adbc2015-04-16 15:20:51 -07001301 // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
1302 InstallShortcutReceiver.enableInstallQueue();
Joe Onorato36115782010-06-17 13:28:48 -04001303 synchronized (mLock) {
Joe Onorato36115782010-06-17 13:28:48 -04001304 // Don't bother to start the thread if we know it's not going to do anything
1305 if (mCallbacks != null && mCallbacks.get() != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07001306 final Callbacks oldCallbacks = mCallbacks.get();
1307 // Clear any pending bind-runnables from the synchronized load process.
1308 runOnMainThread(new Runnable() {
1309 public void run() {
1310 oldCallbacks.clearPendingBinds();
1311 }
1312 });
1313
Joe Onorato36115782010-06-17 13:28:48 -04001314 // If there is already one running, tell it to stop.
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001315 stopLoaderLocked();
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001316 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07001317 // TODO: mDeepShortcutsLoaded does not need to be true for synchronous bind.
Tony Wickham80f57872016-06-29 18:12:15 -07001318 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE && mAllAppsLoaded
1319 && mWorkspaceLoaded && mDeepShortcutsLoaded && !mIsLoaderTaskRunning) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001320 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001321 return true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001322 } else {
1323 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1324 sWorker.post(mLoaderTask);
1325 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001326 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001327 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07001328 return false;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001329 }
1330
Joe Onorato36115782010-06-17 13:28:48 -04001331 public void stopLoader() {
1332 synchronized (mLock) {
1333 if (mLoaderTask != null) {
1334 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001335 }
1336 }
Joe Onorato36115782010-06-17 13:28:48 -04001337 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001338
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001339 /**
1340 * Loads the workspace screen ids in an ordered list.
1341 */
Sunny Goyale5bb7052015-07-27 14:36:07 -07001342 public static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001343 final ContentResolver contentResolver = context.getContentResolver();
1344 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001345
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001346 // Get screens ordered by rank.
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001347 return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query(
1348 screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK));
Winson Chung76828c82013-08-19 15:43:29 -07001349 }
1350
Joe Onorato36115782010-06-17 13:28:48 -04001351 /**
1352 * Runnable for the thread that loads the contents of the launcher:
1353 * - workspace icons
1354 * - widgets
1355 * - all apps icons
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001356 * - deep shortcuts within apps
Joe Onorato36115782010-06-17 13:28:48 -04001357 */
1358 private class LoaderTask implements Runnable {
1359 private Context mContext;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001360 private int mPageToBindFirst;
1361
Adam Cohen091440a2015-03-18 14:16:05 -07001362 @Thunk boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001363 private boolean mStopped;
Adam Cohen091440a2015-03-18 14:16:05 -07001364 @Thunk boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001365
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001366 LoaderTask(Context context, int pageToBindFirst) {
Joe Onorato36115782010-06-17 13:28:48 -04001367 mContext = context;
Sunny Goyal93f878c2016-03-30 17:31:24 -07001368 mPageToBindFirst = pageToBindFirst;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001369 }
1370
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001371 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001372 mIsLoadingAndBindingWorkspace = true;
1373
Joe Onorato36115782010-06-17 13:28:48 -04001374 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001375 if (DEBUG_LOADERS) {
1376 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001377 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001378
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001379 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001380 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001381 synchronized (LoaderTask.this) {
1382 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001383 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001384 }
1385 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001386 }
1387 }
1388
Joe Onorato36115782010-06-17 13:28:48 -04001389 // Bind the workspace
Sunny Goyal93f878c2016-03-30 17:31:24 -07001390 bindWorkspace(mPageToBindFirst);
Joe Onorato36115782010-06-17 13:28:48 -04001391 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001392
Joe Onorato36115782010-06-17 13:28:48 -04001393 private void waitForIdle() {
1394 // Wait until the either we're stopped or the other threads are done.
1395 // This way we don't start loading all apps until the workspace has settled
1396 // down.
1397 synchronized (LoaderTask.this) {
1398 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001399
Joe Onorato36115782010-06-17 13:28:48 -04001400 mHandler.postIdle(new Runnable() {
1401 public void run() {
1402 synchronized (LoaderTask.this) {
1403 mLoadAndBindStepFinished = true;
1404 if (DEBUG_LOADERS) {
1405 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001406 }
Joe Onorato36115782010-06-17 13:28:48 -04001407 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001408 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001409 }
Joe Onorato36115782010-06-17 13:28:48 -04001410 });
1411
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001412 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001413 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001414 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1415 // wait no longer than 1sec at a time
1416 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001417 } catch (InterruptedException ex) {
1418 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001419 }
1420 }
Joe Onorato36115782010-06-17 13:28:48 -04001421 if (DEBUG_LOADERS) {
1422 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001423 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001424 + "ms for previous step to finish binding");
1425 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001426 }
Joe Onorato36115782010-06-17 13:28:48 -04001427 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001428
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001429 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001430 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001431 // Ensure that we have a valid page index to load synchronously
1432 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1433 "valid page index");
1434 }
1435 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1436 // Ensure that we don't try and bind a specified page when the pages have not been
1437 // loaded already (we should load everything asynchronously in that case)
1438 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1439 }
1440 synchronized (mLock) {
1441 if (mIsLoaderTaskRunning) {
1442 // Ensure that we are never running the background loading at this point since
1443 // we also touch the background collections
1444 throw new RuntimeException("Error! Background loading is already running");
1445 }
1446 }
1447
1448 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1449 // data structures, we can't allow any other thread to touch that data, but because
1450 // this call is synchronous, we can get away with not locking).
1451
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001452 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001453 // operations from the previous activity. We need to ensure that all queued operations
1454 // are executed before any synchronous binding work is done.
1455 mHandler.flush();
1456
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001457 // Divide the set of loaded items into those that we are binding synchronously, and
1458 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001459 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001460 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1461 // arise from that.
1462 onlyBindAllApps();
Tony Wickham80f57872016-06-29 18:12:15 -07001463
1464 bindDeepShortcuts();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001465 }
1466
Joe Onorato36115782010-06-17 13:28:48 -04001467 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001468 synchronized (mLock) {
Sunny Goyalf5cd9982015-05-18 15:19:29 -07001469 if (mStopped) {
1470 return;
1471 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001472 mIsLoaderTaskRunning = true;
1473 }
Joe Onorato36115782010-06-17 13:28:48 -04001474 // Optimize for end-user experience: if the Launcher is up and // running with the
1475 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1476 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001477 keep_running: {
Winson Chung64359a52013-07-08 17:17:08 -07001478 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001479 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001480
Joe Onorato36115782010-06-17 13:28:48 -04001481 if (mStopped) {
1482 break keep_running;
1483 }
1484
Joe Onorato36115782010-06-17 13:28:48 -04001485 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001486
1487 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001488 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1489 loadAndBindAllApps();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001490
1491 waitForIdle();
1492
1493 // third step
1494 if (DEBUG_LOADERS) Log.d(TAG, "step 3: loading deep shortcuts");
1495 loadAndBindDeepShortcuts();
Joe Onorato36115782010-06-17 13:28:48 -04001496 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001497
Joe Onorato36115782010-06-17 13:28:48 -04001498 // Clear out this reference, otherwise we end up holding it until all of the
1499 // callback runnables are done.
1500 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001501
Joe Onorato36115782010-06-17 13:28:48 -04001502 synchronized (mLock) {
1503 // If we are still the last one to be scheduled, remove ourselves.
1504 if (mLoaderTask == this) {
1505 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001506 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001507 mIsLoaderTaskRunning = false;
Sunny Goyal756a28a2015-04-23 17:07:55 -07001508 mHasLoaderCompletedOnce = true;
Joe Onorato36115782010-06-17 13:28:48 -04001509 }
Joe Onorato36115782010-06-17 13:28:48 -04001510 }
1511
1512 public void stopLocked() {
1513 synchronized (LoaderTask.this) {
1514 mStopped = true;
1515 this.notify();
1516 }
1517 }
1518
1519 /**
1520 * Gets the callbacks object. If we've been stopped, or if the launcher object
1521 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1522 * object that was around when the deferred message was scheduled, and if there's
1523 * a new Callbacks object around then also return null. This will save us from
1524 * calling onto it with data that will be ignored.
1525 */
1526 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1527 synchronized (mLock) {
1528 if (mStopped) {
1529 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001530 }
Joe Onorato36115782010-06-17 13:28:48 -04001531
1532 if (mCallbacks == null) {
1533 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001534 }
Joe Onorato36115782010-06-17 13:28:48 -04001535
1536 final Callbacks callbacks = mCallbacks.get();
1537 if (callbacks != oldCallbacks) {
1538 return null;
1539 }
1540 if (callbacks == null) {
1541 Log.w(TAG, "no mCallbacks");
1542 return null;
1543 }
1544
1545 return callbacks;
1546 }
1547 }
1548
1549 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001550 private boolean checkItemPlacement(LongArrayMap<GridOccupancy> occupied, ItemInfo item,
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001551 ArrayList<Long> workspaceScreens) {
Winson Chung892c74d2013-08-22 16:15:50 -07001552 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001553 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung892c74d2013-08-22 16:15:50 -07001554
Adam Cohendcd297f2013-06-18 13:13:40 -07001555 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001556 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001557 // Return early if we detect that an item is under the hotseat button
Sunny Goyalbb011da2016-06-15 15:42:29 -07001558 if (!FeatureFlags.NO_ALL_APPS_ICON &&
1559 profile.isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001560 Log.e(TAG, "Error loading shortcut into hotseat " + item
1561 + " into position (" + item.screenId + ":" + item.cellX + ","
1562 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001563 return false;
1564 }
1565
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001566 final GridOccupancy hotseatOccupancy =
Dan Sandler295ae182013-12-10 16:05:47 -05001567 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1568
Adam Cohen2e6da152015-05-06 11:42:25 -07001569 if (item.screenId >= profile.numHotseatIcons) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001570 Log.e(TAG, "Error loading shortcut " + item
1571 + " into hotseat position " + item.screenId
Adam Cohen2e6da152015-05-06 11:42:25 -07001572 + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1)
Adam Cohenae4409d2013-11-26 10:34:59 -08001573 + ")");
1574 return false;
1575 }
1576
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001577 if (hotseatOccupancy != null) {
1578 if (hotseatOccupancy.cells[(int) item.screenId][0]) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001579 Log.e(TAG, "Error loading shortcut into hotseat " + item
1580 + " into position (" + item.screenId + ":" + item.cellX + ","
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001581 + item.cellY + ") already occupied");
Adam Cohendcd297f2013-06-18 13:13:40 -07001582 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001583 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001584 hotseatOccupancy.cells[(int) item.screenId][0] = true;
Dan Sandler295ae182013-12-10 16:05:47 -05001585 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001586 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001587 } else {
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001588 final GridOccupancy occupancy = new GridOccupancy(profile.numHotseatIcons, 1);
1589 occupancy.cells[(int) item.screenId][0] = true;
1590 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, occupancy);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001591 return true;
1592 }
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001593 } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1594 if (!workspaceScreens.contains((Long) item.screenId)) {
1595 // The item has an invalid screen id.
1596 return false;
1597 }
1598 } else {
Winson Chungf30ad5f2011-08-08 10:55:42 -07001599 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001600 return true;
1601 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001602
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001603 final int countX = profile.numColumns;
1604 final int countY = profile.numRows;
Adam Cohenae4409d2013-11-26 10:34:59 -08001605 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1606 item.cellX < 0 || item.cellY < 0 ||
1607 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1608 Log.e(TAG, "Error loading shortcut " + item
1609 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1610 + item.cellX + "," + item.cellY
1611 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1612 return false;
1613 }
1614
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001615 if (!occupied.containsKey(item.screenId)) {
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001616 GridOccupancy screen = new GridOccupancy(countX + 1, countY + 1);
1617 if (item.screenId == Workspace.FIRST_SCREEN_ID) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -07001618 // Mark the first row as occupied (if the feature is enabled)
1619 // in order to account for the QSB.
1620 screen.markCells(0, 0, countX + 1, 1, FeatureFlags.QSB_ON_FIRST_SCREEN);
Sunny Goyalda4fe1a2016-05-26 16:05:17 -07001621 }
1622 occupied.put(item.screenId, screen);
Joe Onorato36115782010-06-17 13:28:48 -04001623 }
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001624 final GridOccupancy occupancy = occupied.get(item.screenId);
Winson Chungf30ad5f2011-08-08 10:55:42 -07001625
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001626 // Check if any workspace icons overlap with each other
1627 if (occupancy.isRegionVacant(item.cellX, item.cellY, item.spanX, item.spanY)) {
1628 occupancy.markCells(item, true);
1629 return true;
1630 } else {
1631 Log.e(TAG, "Error loading shortcut " + item
1632 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1633 + item.cellX + "," + item.cellX + "," + item.spanX + "," + item.spanY
1634 + ") already occupied");
1635 return false;
1636 }
Joe Onorato36115782010-06-17 13:28:48 -04001637 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001638
Winson Chungba9c37f2013-08-30 14:11:37 -07001639 /** Clears all the sBg data structures */
1640 private void clearSBgDataStructures() {
1641 synchronized (sBgLock) {
1642 sBgWorkspaceItems.clear();
1643 sBgAppWidgets.clear();
1644 sBgFolders.clear();
1645 sBgItemsIdMap.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001646 sBgWorkspaceScreens.clear();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001647 sBgPinnedShortcutCounts.clear();
Winson Chungba9c37f2013-08-30 14:11:37 -07001648 }
1649 }
1650
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001651 private void loadWorkspace() {
Sunny Goyale26d1002016-06-20 14:52:14 -07001652 if (LauncherAppState.PROFILE_STARTUP) {
1653 Trace.beginSection("Loading Workspace");
1654 }
Joe Onorato36115782010-06-17 13:28:48 -04001655 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001656
Joe Onorato36115782010-06-17 13:28:48 -04001657 final Context context = mContext;
1658 final ContentResolver contentResolver = context.getContentResolver();
1659 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001660 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001661 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
Sunny Goyal25aba0a2015-07-16 15:07:47 -07001662 final boolean isSdCardReady = Utilities.isBootCompleted();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001663
Winson Chung892c74d2013-08-22 16:15:50 -07001664 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07001665 InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001666 int countX = profile.numColumns;
1667 int countY = profile.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001668
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001669 boolean clearDb = false;
Sunny Goyala5c8a9e2016-07-08 08:32:44 -07001670 try {
1671 ImportDataTask.performImportIfPossible(context);
1672 } catch (Exception e) {
1673 // Migration failed. Clear workspace.
1674 clearDb = true;
1675 }
1676
1677 if (!clearDb && GridSizeMigrationTask.ENABLED &&
Sunny Goyalf076eae2016-01-11 12:25:10 -08001678 !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) {
1679 // Migration failed. Clear workspace.
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001680 clearDb = true;
Sunny Goyale5bb7052015-07-27 14:36:07 -07001681 }
1682
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001683 if (clearDb) {
Sunny Goyala1365452015-10-01 15:46:24 -07001684 Log.d(TAG, "loadWorkspace: resetting launcher database");
Sunny Goyald2497482015-09-22 18:24:19 -07001685 LauncherSettings.Settings.call(contentResolver,
1686 LauncherSettings.Settings.METHOD_DELETE_DB);
Dan Sandlerd5024042014-01-09 15:01:33 -05001687 }
1688
Sunny Goyalded0fdb2016-05-23 15:55:41 -07001689 Log.d(TAG, "loadWorkspace: loading default favorites");
1690 LauncherSettings.Settings.call(contentResolver,
1691 LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES);
Adam Cohene25af792013-06-06 23:08:25 -07001692
Winson Chung2abf94d2012-07-18 18:16:38 -07001693 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001694 clearSBgDataStructures();
Sunny Goyal756adbc2015-04-16 15:20:51 -07001695 final HashMap<String, Integer> installingPkgs = PackageInstallerCompat
Sunny Goyal94485362014-09-18 16:13:58 -07001696 .getInstance(mContext).updateAndGetActiveSessionCache();
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001697 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
Romain Guy5c16f3e2010-01-12 17:24:58 -08001698
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001699 final ArrayList<Long> itemsToRemove = new ArrayList<>();
1700 final ArrayList<Long> restoredRows = new ArrayList<>();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001701 Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>();
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001702 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001703 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001704 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001705
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001706 // +1 for the hotseat (it can be larger than the workspace)
1707 // Load workspace in reverse order to ensure that latest items are loaded first (and
1708 // before any earlier duplicates)
Sunny Goyalff4ba2d2016-04-02 14:12:34 -07001709 final LongArrayMap<GridOccupancy> occupied = new LongArrayMap<>();
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001710 HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001711
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001712 try {
1713 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1714 final int intentIndex = c.getColumnIndexOrThrow
1715 (LauncherSettings.Favorites.INTENT);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001716 final int containerIndex = c.getColumnIndexOrThrow(
1717 LauncherSettings.Favorites.CONTAINER);
1718 final int itemTypeIndex = c.getColumnIndexOrThrow(
1719 LauncherSettings.Favorites.ITEM_TYPE);
1720 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1721 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001722 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1723 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001724 final int screenIndex = c.getColumnIndexOrThrow(
1725 LauncherSettings.Favorites.SCREEN);
1726 final int cellXIndex = c.getColumnIndexOrThrow
1727 (LauncherSettings.Favorites.CELLX);
1728 final int cellYIndex = c.getColumnIndexOrThrow
1729 (LauncherSettings.Favorites.CELLY);
1730 final int spanXIndex = c.getColumnIndexOrThrow
1731 (LauncherSettings.Favorites.SPANX);
1732 final int spanYIndex = c.getColumnIndexOrThrow(
1733 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001734 final int rankIndex = c.getColumnIndexOrThrow(
1735 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001736 final int restoredIndex = c.getColumnIndexOrThrow(
1737 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001738 final int profileIdIndex = c.getColumnIndexOrThrow(
1739 LauncherSettings.Favorites.PROFILE_ID);
Sunny Goyal5d85c442015-03-10 13:14:47 -07001740 final int optionsIndex = c.getColumnIndexOrThrow(
1741 LauncherSettings.Favorites.OPTIONS);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001742 final CursorIconInfo cursorIconInfo = new CursorIconInfo(mContext, c);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001743
Sunny Goyal7f834d22015-04-21 10:10:23 -07001744 final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>();
Kenny Guyff05f432016-01-22 17:48:29 +00001745 final LongSparseArray<Boolean> quietMode = new LongSparseArray<>();
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001746 final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>();
Sunny Goyal7f834d22015-04-21 10:10:23 -07001747 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
Kenny Guyff05f432016-01-22 17:48:29 +00001748 long serialNo = mUserManager.getSerialNumberForUser(user);
1749 allUsers.put(serialNo, user);
1750 quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user));
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001751
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001752 boolean userUnlocked = mUserManager.isUserUnlocked(user);
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001753
1754 // We can only query for shortcuts when the user is unlocked.
1755 if (userUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07001756 List<ShortcutInfoCompat> pinnedShortcuts =
1757 mDeepShortcutManager.queryForPinnedShortcuts(null, user);
1758 if (mDeepShortcutManager.wasLastCallSuccess()) {
1759 for (ShortcutInfoCompat shortcut : pinnedShortcuts) {
1760 shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut),
1761 shortcut);
1762 }
1763 } else {
1764 // Shortcut manager can fail due to some race condition when the
1765 // lock state changes too frequently. For the purpose of the loading
1766 // shortcuts, consider the user is still locked.
1767 userUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001768 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001769 }
Sunny Goyal49f4f032016-08-01 15:45:49 -07001770 unlockedUsers.put(serialNo, userUnlocked);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001771 }
1772
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001773 ShortcutInfo info;
1774 String intentDescription;
1775 LauncherAppWidgetInfo appWidgetInfo;
1776 int container;
1777 long id;
Robin Lee26ace122015-03-16 19:41:43 +00001778 long serialNumber;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001779 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001780 UserHandleCompat user;
Sunny Goyald09c3702016-04-06 16:18:20 -07001781 String targetPackage;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001782
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001783 while (!mStopped && c.moveToNext()) {
1784 try {
1785 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001786 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001787 boolean allowMissingTarget = false;
Sunny Goyalb1622cc2015-06-10 16:00:42 -07001788 container = c.getInt(containerIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001789
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001790 switch (itemType) {
1791 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1792 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001793 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001794 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001795 intentDescription = c.getString(intentIndex);
Robin Lee26ace122015-03-16 19:41:43 +00001796 serialNumber = c.getInt(profileIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07001797 user = allUsers.get(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001798 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001799 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001800 boolean itemReplaced = false;
Sunny Goyald09c3702016-04-06 16:18:20 -07001801 targetPackage = null;
Kenny Guyed131872014-04-30 03:02:21 +01001802 if (user == null) {
1803 // User has been deleted remove the item.
1804 itemsToRemove.add(id);
1805 continue;
1806 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001807 try {
1808 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001809 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001810 if (cn != null && cn.getPackageName() != null) {
1811 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1812 cn.getPackageName(), user);
1813 boolean validComponent = validPkg &&
1814 launcherApps.isActivityEnabledForProfile(cn, user);
Sunny Goyald09c3702016-04-06 16:18:20 -07001815 if (validPkg) {
1816 targetPackage = cn.getPackageName();
1817 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001818
1819 if (validComponent) {
1820 if (restored) {
1821 // no special handling necessary for this item
1822 restoredRows.add(id);
1823 restored = false;
1824 }
Kenny Guyff05f432016-01-22 17:48:29 +00001825 if (quietMode.get(serialNumber)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07001826 disabledState = ShortcutInfo.FLAG_DISABLED_QUIET_USER;
Kenny Guyff05f432016-01-22 17:48:29 +00001827 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001828 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001829 intent = null;
1830 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1831 // We allow auto install apps to have their intent
1832 // updated after an install.
1833 intent = manager.getLaunchIntentForPackage(
1834 cn.getPackageName());
1835 if (intent != null) {
1836 ContentValues values = new ContentValues();
1837 values.put(LauncherSettings.Favorites.INTENT,
1838 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001839 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001840 }
1841 }
1842
1843 if (intent == null) {
1844 // The app is installed but the component is no
1845 // longer available.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001846 FileLog.d(TAG, "Invalid component removed: " + cn);
Sunny Goyal34942622014-08-29 17:20:55 -07001847 itemsToRemove.add(id);
1848 continue;
1849 } else {
1850 // no special handling necessary for this item
1851 restoredRows.add(id);
1852 restored = false;
1853 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001854 } else if (restored) {
1855 // Package is not yet available but might be
1856 // installed later.
Sunny Goyal713edfc2016-05-06 09:58:34 -07001857 FileLog.d(TAG, "package not yet restored: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001858
1859 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1860 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07001861 } else if (installingPkgs.containsKey(cn.getPackageName())) {
Sunny Goyal94485362014-09-18 16:13:58 -07001862 // App restore has started. Update the flag
1863 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1864 ContentValues values = new ContentValues();
1865 values.put(LauncherSettings.Favorites.RESTORED,
1866 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001867 updateItem(id, values);
1868 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1869 // This is a common app. Try to replace this.
1870 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1871 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1872 if (parser.findDefaultApp()) {
1873 // Default app found. Replace it.
1874 intent = parser.parsedIntent;
1875 cn = intent.getComponent();
1876 ContentValues values = parser.parsedValues;
1877 values.put(LauncherSettings.Favorites.RESTORED, 0);
1878 updateItem(id, values);
1879 restored = false;
1880 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001881
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001882 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001883 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001884 itemsToRemove.add(id);
1885 continue;
1886 }
Sunny Goyal94485362014-09-18 16:13:58 -07001887 } else if (REMOVE_UNRESTORED_ICONS) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001888 FileLog.d(TAG, "Unrestored package removed: " + cn);
Sunny Goyal94485362014-09-18 16:13:58 -07001889 itemsToRemove.add(id);
1890 continue;
1891 }
Sunny Goyald09c3702016-04-06 16:18:20 -07001892 } else if (PackageManagerHelper.isAppOnSdcard(
1893 manager, cn.getPackageName())) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07001894 // Package is present but not available.
1895 allowMissingTarget = true;
1896 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
1897 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001898 // SdCard is not ready yet. Package might get available,
1899 // once it is ready.
Sunny Goyala1365452015-10-01 15:46:24 -07001900 Log.d(TAG, "Invalid package: " + cn + " (check again later)");
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001901 HashSet<String> pkgs = sPendingPackages.get(user);
1902 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07001903 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001904 sPendingPackages.put(user, pkgs);
1905 }
1906 pkgs.add(cn.getPackageName());
1907 allowMissingTarget = true;
1908 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07001909
1910 } else {
1911 // Do not wait for external media load anymore.
1912 // Log the invalid package, and remove it
Sunny Goyal713edfc2016-05-06 09:58:34 -07001913 FileLog.d(TAG, "Invalid package removed: " + cn);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001914 itemsToRemove.add(id);
1915 continue;
Winson Chungee055712013-07-30 14:46:24 -07001916 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07001917 } else if (cn == null) {
1918 // For shortcuts with no component, keep them as they are
1919 restoredRows.add(id);
1920 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001921 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001922 } catch (URISyntaxException e) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07001923 FileLog.d(TAG, "Invalid uri: " + intentDescription);
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07001924 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001925 continue;
1926 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001927
Sunny Goyal34b65272015-03-11 16:56:52 -07001928 boolean useLowResIcon = container >= 0 &&
1929 c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW;
1930
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001931 if (itemReplaced) {
1932 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001933 info = getAppShortcutInfo(intent, user, null,
1934 cursorIconInfo, false, useLowResIcon);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001935 } else {
1936 // Don't replace items for other profiles.
1937 itemsToRemove.add(id);
1938 continue;
1939 }
1940 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001941 if (user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001942 info = getRestoredItemInfo(c, intent,
1943 promiseType, itemType, cursorIconInfo);
Kenny Guyed131872014-04-30 03:02:21 +01001944 intent = getRestoredItemIntent(c, context, intent);
1945 } else {
1946 // Don't restore items for other profiles.
1947 itemsToRemove.add(id);
1948 continue;
1949 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001950 } else if (itemType ==
1951 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001952 info = getAppShortcutInfo(intent, user, c,
1953 cursorIconInfo, allowMissingTarget, useLowResIcon);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001954 } else if (itemType ==
1955 LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001956
1957 ShortcutKey key = ShortcutKey.fromIntent(intent, user);
1958 if (unlockedUsers.get(serialNumber)) {
1959 ShortcutInfoCompat pinnedShortcut =
1960 shortcutKeyToPinnedShortcuts.get(key);
1961 if (pinnedShortcut == null) {
1962 // The shortcut is no longer valid.
1963 itemsToRemove.add(id);
1964 continue;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001965 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001966 info = new ShortcutInfo(pinnedShortcut, context);
1967 intent = info.intent;
1968 } else {
1969 // Create a shortcut info in disabled mode for now.
1970 info = new ShortcutInfo();
1971 info.user = user;
1972 info.itemType = itemType;
1973 loadInfoFromCursor(info, c, cursorIconInfo);
1974
1975 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001976 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001977 incrementPinnedShortcutCount(key, false /* shouldPin */);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07001978 } else { // item type == ITEM_TYPE_SHORTCUT
Sunny Goyald3b87ef2016-07-28 12:11:54 -07001979 info = getShortcutInfo(c, cursorIconInfo);
Michael Jurka96879562012-03-22 05:54:33 -07001980
Sunny Goyald09c3702016-04-06 16:18:20 -07001981 // Shortcuts are only available on the primary profile
1982 if (PackageManagerHelper.isAppSuspended(manager, targetPackage)) {
1983 disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED;
1984 }
1985
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001986 // App shortcuts that used to be automatically added to Launcher
1987 // didn't always have the correct intent flags set, so do that
1988 // here
1989 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001990 intent.getCategories() != null &&
1991 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001992 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001993 intent.addFlags(
1994 Intent.FLAG_ACTIVITY_NEW_TASK |
1995 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1996 }
Michael Jurka96879562012-03-22 05:54:33 -07001997 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001998
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001999 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07002000 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002001 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002002 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002003 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002004 info.cellX = c.getInt(cellXIndex);
2005 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08002006 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002007 info.spanX = 1;
2008 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002009 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002010 if (info.promisedIntent != null) {
2011 info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
2012 }
Sunny Goyald09c3702016-04-06 16:18:20 -07002013 info.isDisabled |= disabledState;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002014 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2015 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2016 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002017
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002018 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002019 if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002020 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002021 break;
2022 }
2023
Sunny Goyal756adbc2015-04-16 15:20:51 -07002024 if (restored) {
2025 ComponentName cn = info.getTargetComponent();
2026 if (cn != null) {
2027 Integer progress = installingPkgs.get(cn.getPackageName());
2028 if (progress != null) {
2029 info.setInstallProgress(progress);
2030 } else {
2031 info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
2032 }
2033 }
2034 }
2035
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002036 switch (container) {
2037 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2038 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2039 sBgWorkspaceItems.add(info);
2040 break;
2041 default:
2042 // Item is in a user folder
2043 FolderInfo folderInfo =
2044 findOrMakeFolder(sBgFolders, container);
Sunny Goyalc52ba712016-04-05 15:59:05 -07002045 folderInfo.add(info, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002046 break;
2047 }
2048 sBgItemsIdMap.put(info.id, info);
Winson Chung1323b482013-08-05 12:41:55 -07002049 } else {
2050 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002051 }
2052 break;
2053
2054 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2055 id = c.getLong(idIndex);
2056 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2057
Sunny Goyala508e4f2015-05-21 09:33:57 -07002058 // Do not trim the folder label, as is was set by the user.
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002059 folderInfo.title = c.getString(cursorIconInfo.titleIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002060 folderInfo.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002061 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002062 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002063 folderInfo.cellX = c.getInt(cellXIndex);
2064 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002065 folderInfo.spanX = 1;
2066 folderInfo.spanY = 1;
Sunny Goyal5d85c442015-03-10 13:14:47 -07002067 folderInfo.options = c.getInt(optionsIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002068
Daniel Sandler8802e962010-05-26 16:28:16 -04002069 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002070 if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002071 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002072 break;
2073 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002074
Joe Onorato9c1289c2009-08-17 11:03:03 -04002075 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002076 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2077 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2078 sBgWorkspaceItems.add(folderInfo);
2079 break;
Joe Onorato36115782010-06-17 13:28:48 -04002080 }
Joe Onorato17a89222011-02-08 17:26:11 -08002081
Chris Wrenf4d08112014-01-16 18:13:56 -05002082 if (restored) {
2083 // no special handling required for restored folders
2084 restoredRows.add(id);
2085 }
2086
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002087 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2088 sBgFolders.put(folderInfo.id, folderInfo);
2089 break;
2090
2091 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002092 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002093 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002094 boolean customWidget = itemType ==
2095 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2096
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002097 int appWidgetId = c.getInt(appWidgetIdIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002098 serialNumber = c.getLong(profileIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002099 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002100 id = c.getLong(idIndex);
Sunny Goyal7f834d22015-04-21 10:10:23 -07002101 user = allUsers.get(serialNumber);
2102 if (user == null) {
2103 itemsToRemove.add(id);
2104 continue;
2105 }
2106
Sunny Goyalff572272014-07-23 13:58:07 -07002107 final ComponentName component =
2108 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002109
Sunny Goyal651077b2014-06-30 14:15:31 -07002110 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002111 final boolean isIdValid = (restoreStatus &
2112 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Sunny Goyalff572272014-07-23 13:58:07 -07002113 final boolean wasProviderReady = (restoreStatus &
2114 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002115
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002116 if (widgetProvidersMap == null) {
2117 widgetProvidersMap = AppWidgetManagerCompat
2118 .getInstance(mContext).getAllProvidersMap();
2119 }
2120 final AppWidgetProviderInfo provider = widgetProvidersMap.get(
2121 new ComponentKey(
Robin Lee26ace122015-03-16 19:41:43 +00002122 ComponentName.unflattenFromString(savedProvider),
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002123 user));
Sunny Goyalff572272014-07-23 13:58:07 -07002124
2125 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002126 if (!isSafeMode && !customWidget &&
2127 wasProviderReady && !isProviderReady) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002128 FileLog.d(TAG, "Deleting widget that isn't installed anymore: "
Sunny Goyala1365452015-10-01 15:46:24 -07002129 + provider);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002130 itemsToRemove.add(id);
2131 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002132 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002133 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2134 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002135
Sunny Goyal53f96722015-07-13 19:54:53 -07002136 // The provider is available. So the widget is either
2137 // available or not available. We do not need to track
2138 // any future restore updates.
2139 int status = restoreStatus &
2140 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyalff572272014-07-23 13:58:07 -07002141 if (!wasProviderReady) {
2142 // If provider was not previously ready, update the
2143 // status and UI flag.
2144
2145 // Id would be valid only if the widget restore broadcast was received.
2146 if (isIdValid) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07002147 status = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
Sunny Goyalff572272014-07-23 13:58:07 -07002148 } else {
2149 status &= ~LauncherAppWidgetInfo
2150 .FLAG_PROVIDER_NOT_READY;
2151 }
2152 }
2153 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002154 } else {
2155 Log.v(TAG, "Widget restore pending id=" + id
2156 + " appWidgetId=" + appWidgetId
2157 + " status =" + restoreStatus);
2158 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002159 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002160 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal756adbc2015-04-16 15:20:51 -07002161 Integer installProgress = installingPkgs.get(component.getPackageName());
Sunny Goyal94485362014-09-18 16:13:58 -07002162
2163 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2164 // Restore has started once.
Sunny Goyal756adbc2015-04-16 15:20:51 -07002165 } else if (installProgress != null) {
Sunny Goyal94485362014-09-18 16:13:58 -07002166 // App restore has started. Update the flag
2167 appWidgetInfo.restoreStatus |=
2168 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002169 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002170 FileLog.d(TAG, "Unrestored widget removed: " + component);
Sunny Goyal94485362014-09-18 16:13:58 -07002171 itemsToRemove.add(id);
2172 continue;
2173 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07002174
2175 appWidgetInfo.installProgress =
2176 installProgress == null ? 0 : installProgress;
Sunny Goyal651077b2014-06-30 14:15:31 -07002177 }
Sunny Goyalff572272014-07-23 13:58:07 -07002178
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002179 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002180 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002181 appWidgetInfo.cellX = c.getInt(cellXIndex);
2182 appWidgetInfo.cellY = c.getInt(cellYIndex);
2183 appWidgetInfo.spanX = c.getInt(spanXIndex);
2184 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002185 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002186
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002187 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2188 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2189 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002190 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2191 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002192 continue;
2193 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002194
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002195 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002196 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002197 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002198 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002199 break;
2200 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002201
Adam Cohen59400422014-03-05 18:07:04 -08002202 if (!customWidget) {
2203 String providerName =
2204 appWidgetInfo.providerName.flattenToString();
2205 if (!providerName.equals(savedProvider) ||
2206 (appWidgetInfo.restoreStatus != restoreStatus)) {
2207 ContentValues values = new ContentValues();
2208 values.put(
2209 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2210 providerName);
2211 values.put(LauncherSettings.Favorites.RESTORED,
2212 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002213 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002214 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002215 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002216 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2217 sBgAppWidgets.add(appWidgetInfo);
2218 }
Joe Onorato36115782010-06-17 13:28:48 -04002219 break;
2220 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002221 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002222 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002223 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002224 }
2225 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002226 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002227 }
2228
Winson Chungba9c37f2013-08-30 14:11:37 -07002229 // Break early if we've stopped loading
2230 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002231 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002232 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002233 }
2234
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002235 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002236 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002237 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2238 Utilities.createDbSelectionQuery(
2239 LauncherSettings.Favorites._ID, itemsToRemove), null);
2240 if (DEBUG_LOADERS) {
2241 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2242 LauncherSettings.Favorites._ID, itemsToRemove));
2243 }
2244
2245 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07002246 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
2247 .call(contentResolver,
2248 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
2249 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
2250 for (long folderId : deletedFolderIds) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002251 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2252 sBgFolders.remove(folderId);
2253 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002254 }
2255 }
2256
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002257 // Unpin shortcuts that don't exist on the workspace.
2258 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
2259 MutableInt numTimesPinned = sBgPinnedShortcutCounts.get(key);
2260 if (numTimesPinned == null || numTimesPinned.value == 0) {
2261 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
2262 mDeepShortcutManager.unpinShortcut(key);
2263 }
2264 }
2265
Sunny Goyal317698b2015-07-29 11:45:41 -07002266 // Sort all the folder items and make sure the first 3 items are high resolution.
2267 for (FolderInfo folder : sBgFolders) {
2268 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2269 int pos = 0;
2270 for (ShortcutInfo info : folder.contents) {
2271 if (info.usingLowResIcon) {
2272 info.updateIcon(mIconCache, false);
2273 }
2274 pos ++;
2275 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2276 break;
2277 }
2278 }
2279 }
2280
Chris Wrenf4d08112014-01-16 18:13:56 -05002281 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002282 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002283 ContentValues values = new ContentValues();
2284 values.put(LauncherSettings.Favorites.RESTORED, 0);
2285 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2286 Utilities.createDbSelectionQuery(
2287 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002288 }
2289
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002290 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2291 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002292 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002293 null, sWorker);
2294 }
2295
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002296 // Remove any empty screens
2297 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002298 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002299 long screenId = item.screenId;
2300 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2301 unusedScreens.contains(screenId)) {
2302 unusedScreens.remove(screenId);
2303 }
2304 }
2305
2306 // If there are any empty screens remove them, and update.
2307 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002308 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002309 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002310 }
2311
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002312 if (DEBUG_LOADERS) {
2313 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2314 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002315 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002316 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002317 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002318
Sunny Goyale2df0622015-04-24 11:27:00 -07002319 for (int i = 0; i < nScreens; i++) {
2320 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002321 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002322 line += " | ";
2323 }
Joe Onorato36115782010-06-17 13:28:48 -04002324 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002325 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002326 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002327 }
Joe Onorato36115782010-06-17 13:28:48 -04002328 }
Sunny Goyale26d1002016-06-20 14:52:14 -07002329 if (LauncherAppState.PROFILE_STARTUP) {
2330 Trace.endSection();
2331 }
Adam Cohene25af792013-06-06 23:08:25 -07002332 }
2333
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002334 /**
2335 * Partially updates the item without any notification. Must be called on the worker thread.
2336 */
2337 private void updateItem(long itemId, ContentValues update) {
2338 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002339 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002340 update,
2341 BaseColumns._ID + "= ?",
2342 new String[]{Long.toString(itemId)});
2343 }
2344
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002345 /** Filters the set of items who are directly or indirectly (via another container) on the
2346 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002347 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002348 ArrayList<ItemInfo> allWorkspaceItems,
2349 ArrayList<ItemInfo> currentScreenItems,
2350 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002351 // Purge any null ItemInfos
2352 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2353 while (iter.hasNext()) {
2354 ItemInfo i = iter.next();
2355 if (i == null) {
2356 iter.remove();
2357 }
2358 }
2359
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002360 // Order the set of items by their containers first, this allows use to walk through the
2361 // list sequentially, build up a list of containers that are in the specified screen,
2362 // as well as all items in those containers.
2363 Set<Long> itemsOnScreen = new HashSet<Long>();
2364 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2365 @Override
2366 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07002367 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002368 }
2369 });
2370 for (ItemInfo info : allWorkspaceItems) {
2371 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002372 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002373 currentScreenItems.add(info);
2374 itemsOnScreen.add(info.id);
2375 } else {
2376 otherScreenItems.add(info);
2377 }
2378 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2379 currentScreenItems.add(info);
2380 itemsOnScreen.add(info.id);
2381 } else {
2382 if (itemsOnScreen.contains(info.container)) {
2383 currentScreenItems.add(info);
2384 itemsOnScreen.add(info.id);
2385 } else {
2386 otherScreenItems.add(info);
2387 }
2388 }
2389 }
2390 }
2391
2392 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002393 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002394 ArrayList<LauncherAppWidgetInfo> appWidgets,
2395 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2396 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002397
2398 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002399 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002400 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002401 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002402 currentScreenWidgets.add(widget);
2403 } else {
2404 otherScreenWidgets.add(widget);
2405 }
2406 }
2407 }
2408
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002409 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2410 * right) */
2411 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002412 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002413 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002414 final int screenCols = profile.numColumns;
2415 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002416 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002417 @Override
2418 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002419 if (lhs.container == rhs.container) {
2420 // Within containers, order by their spatial position in that container
2421 switch ((int) lhs.container) {
2422 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2423 long lr = (lhs.screenId * screenCellCount +
2424 lhs.cellY * screenCols + lhs.cellX);
2425 long rr = (rhs.screenId * screenCellCount +
2426 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07002427 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07002428 }
2429 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2430 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07002431 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07002432 }
2433 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002434 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002435 throw new RuntimeException("Unexpected container type when " +
2436 "sorting workspace items.");
2437 }
2438 return 0;
2439 }
2440 } else {
2441 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07002442 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07002443 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002444 }
2445 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002446 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002447
Adam Cohendcd297f2013-06-18 13:13:40 -07002448 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2449 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002450 final Runnable r = new Runnable() {
2451 @Override
2452 public void run() {
2453 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2454 if (callbacks != null) {
2455 callbacks.bindScreens(orderedScreens);
2456 }
2457 }
2458 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002459 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002460 }
2461
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002462 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2463 final ArrayList<ItemInfo> workspaceItems,
2464 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002465 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002466
2467 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002468 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002469 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002470 final int start = i;
2471 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002472 final Runnable r = new Runnable() {
2473 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002474 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002475 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002476 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002477 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2478 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002479 }
2480 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002481 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002482 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002483 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002484
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002485 // Bind the widgets, one at a time
2486 N = appWidgets.size();
2487 for (int i = 0; i < N; i++) {
2488 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2489 final Runnable r = new Runnable() {
2490 public void run() {
2491 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2492 if (callbacks != null) {
2493 callbacks.bindAppWidget(widget);
2494 }
2495 }
2496 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002497 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002498 }
2499 }
2500
2501 /**
2502 * Binds all loaded data to actual views on the main thread.
2503 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002504 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002505 final long t = SystemClock.uptimeMillis();
2506 Runnable r;
2507
2508 // Don't use these two variables in any of the callback runnables.
2509 // Otherwise we hold a reference to them.
2510 final Callbacks oldCallbacks = mCallbacks.get();
2511 if (oldCallbacks == null) {
2512 // This launcher has exited and nobody bothered to tell us. Just bail.
2513 Log.w(TAG, "LoaderTask running with no launcher");
2514 return;
2515 }
2516
Winson Chung9b9fb962013-11-15 15:39:34 -08002517 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07002518 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
2519 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
2520 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002521
Winson Chung2abf94d2012-07-18 18:16:38 -07002522 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002523 workspaceItems.addAll(sBgWorkspaceItems);
2524 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002525 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002526 }
2527
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002528 final int currentScreen;
2529 {
2530 int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE
2531 ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen();
2532 if (currScreen >= orderedScreenIds.size()) {
2533 // There may be no workspace screens (just hotseat items and an empty page).
2534 currScreen = PagedView.INVALID_RESTORE_PAGE;
2535 }
2536 currentScreen = currScreen;
Winson Chung9b9fb962013-11-15 15:39:34 -08002537 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002538 final boolean validFirstPage = currentScreen >= 0;
2539 final long currentScreenId =
2540 validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID;
Winson Chung9b9fb962013-11-15 15:39:34 -08002541
Winson Chung9b9fb962013-11-15 15:39:34 -08002542 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07002543 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
2544 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
2545 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
2546 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002547
Winson Chung9b9fb962013-11-15 15:39:34 -08002548 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002549 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002550 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002551 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002552 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2553 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2554
2555 // Tell the workspace that we're about to start binding items
2556 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002557 public void run() {
2558 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2559 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002560 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002561 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002562 }
2563 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002564 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002565 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002566
Adam Cohendcd297f2013-06-18 13:13:40 -07002567 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2568
Sunny Goyal527c7d32015-08-28 15:19:36 -07002569 Executor mainExecutor = new DeferredMainThreadExecutor();
2570 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07002571 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002572
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002573 // In case of validFirstPage, only bind the first screen, and defer binding the
2574 // remaining screens after first onDraw (and an optional the fade animation whichever
2575 // happens later).
2576 // This ensures that the first screen is immediately visible (eg. during rotation)
2577 // In case of !validFirstPage, bind all pages one after other.
2578 final Executor deferredExecutor =
2579 validFirstPage ? new ViewOnDrawExecutor(mHandler) : mainExecutor;
2580
2581 mainExecutor.execute(new Runnable() {
2582 @Override
2583 public void run() {
2584 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2585 if (callbacks != null) {
2586 callbacks.finishFirstPageBind(
2587 validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null);
2588 }
2589 }
2590 });
Sunny Goyal527c7d32015-08-28 15:19:36 -07002591
Sunny Goyal44c06432016-04-02 10:56:02 -07002592 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002593
2594 // Tell the workspace that we're done binding items
2595 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002596 public void run() {
2597 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2598 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002599 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002600 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002601
Sunny Goyal639e9062015-08-19 19:17:06 -07002602 mIsLoadingAndBindingWorkspace = false;
2603
2604 // Run all the bind complete runnables after workspace is bound.
2605 if (!mBindCompleteRunnables.isEmpty()) {
2606 synchronized (mBindCompleteRunnables) {
2607 for (final Runnable r : mBindCompleteRunnables) {
2608 runOnWorkerThread(r);
2609 }
2610 mBindCompleteRunnables.clear();
2611 }
2612 }
2613
Winson Chung98e030b2012-05-07 16:01:11 -07002614 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002615 if (DEBUG_LOADERS) {
2616 Log.d(TAG, "bound workspace in "
2617 + (SystemClock.uptimeMillis()-t) + "ms");
2618 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002619
Joe Onorato36115782010-06-17 13:28:48 -04002620 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002621 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002622 deferredExecutor.execute(r);
2623
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002624 if (validFirstPage) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002625 r = new Runnable() {
2626 public void run() {
2627 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2628 if (callbacks != null) {
2629 // We are loading synchronously, which means, some of the pages will be
2630 // bound after first draw. Inform the callbacks that page binding is
2631 // not complete, and schedule the remaining pages.
2632 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2633 callbacks.onPageBoundSynchronously(currentScreen);
2634 }
2635 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2636 }
2637 }
2638 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002639 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002640 }
Joe Onorato36115782010-06-17 13:28:48 -04002641 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002642
Joe Onorato36115782010-06-17 13:28:48 -04002643 private void loadAndBindAllApps() {
2644 if (DEBUG_LOADERS) {
2645 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2646 }
2647 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002648 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002649 synchronized (LoaderTask.this) {
2650 if (mStopped) {
2651 return;
2652 }
2653 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002654 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002655 synchronized (LoaderTask.this) {
2656 if (mStopped) {
2657 return;
2658 }
2659 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002660 }
Joe Onorato36115782010-06-17 13:28:48 -04002661 } else {
2662 onlyBindAllApps();
2663 }
2664 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002665
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002666 private void updateIconCache() {
2667 // Ignore packages which have a promise icon.
2668 HashSet<String> packagesToIgnore = new HashSet<>();
2669 synchronized (sBgLock) {
2670 for (ItemInfo info : sBgItemsIdMap) {
2671 if (info instanceof ShortcutInfo) {
2672 ShortcutInfo si = (ShortcutInfo) info;
2673 if (si.isPromise() && si.getTargetComponent() != null) {
2674 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2675 }
2676 } else if (info instanceof LauncherAppWidgetInfo) {
2677 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2678 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2679 packagesToIgnore.add(lawi.providerName.getPackageName());
2680 }
2681 }
2682 }
2683 }
2684 mIconCache.updateDbIcons(packagesToIgnore);
2685 }
2686
Joe Onorato36115782010-06-17 13:28:48 -04002687 private void onlyBindAllApps() {
2688 final Callbacks oldCallbacks = mCallbacks.get();
2689 if (oldCallbacks == null) {
2690 // This launcher has exited and nobody bothered to tell us. Just bail.
2691 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2692 return;
2693 }
2694
2695 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002696 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002697 final ArrayList<AppInfo> list
2698 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002699 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002700 public void run() {
2701 final long t = SystemClock.uptimeMillis();
2702 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2703 if (callbacks != null) {
2704 callbacks.bindAllApplications(list);
2705 }
2706 if (DEBUG_LOADERS) {
2707 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002708 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04002709 }
2710 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002711 };
Tony Wickham80f57872016-06-29 18:12:15 -07002712 runOnMainThread(r);
Joe Onorato36115782010-06-17 13:28:48 -04002713 }
2714
Winson Chung64359a52013-07-08 17:17:08 -07002715 private void loadAllApps() {
2716 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002717
Joe Onorato36115782010-06-17 13:28:48 -04002718 final Callbacks oldCallbacks = mCallbacks.get();
2719 if (oldCallbacks == null) {
2720 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002721 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002722 return;
2723 }
2724
Kenny Guyed131872014-04-30 03:02:21 +01002725 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2726
Winson Chung64359a52013-07-08 17:17:08 -07002727 // Clear the list of apps
2728 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002729 for (UserHandleCompat user : profiles) {
2730 // Query for the set of apps
2731 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002732 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002733 if (DEBUG_LOADERS) {
2734 Log.d(TAG, "getActivityList took "
2735 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2736 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2737 }
2738 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002739 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002740 if (apps == null || apps.isEmpty()) {
2741 return;
2742 }
Kenny Guyff05f432016-01-22 17:48:29 +00002743 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002744 // Create the ApplicationInfos
2745 for (int i = 0; i < apps.size(); i++) {
2746 LauncherActivityInfoCompat app = apps.get(i);
2747 // This builds the icon bitmaps.
Kenny Guyff05f432016-01-22 17:48:29 +00002748 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode));
Kenny Guyed131872014-04-30 03:02:21 +01002749 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002750
Sunny Goyal756a28a2015-04-23 17:07:55 -07002751 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2752 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002753 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002754
2755 @Override
2756 public void run() {
2757 heuristic.processUserApps(apps);
2758 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002759 };
2760 runOnMainThread(new Runnable() {
2761
2762 @Override
2763 public void run() {
2764 // Check isLoadingWorkspace on the UI thread, as it is updated on
2765 // the UI thread.
2766 if (mIsLoadingAndBindingWorkspace) {
2767 synchronized (mBindCompleteRunnables) {
2768 mBindCompleteRunnables.add(r);
2769 }
2770 } else {
2771 runOnWorkerThread(r);
2772 }
2773 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002774 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002775 }
Winson Chung64359a52013-07-08 17:17:08 -07002776 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002777 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002778 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2779 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002780
2781 // Post callback on main thread
2782 mHandler.post(new Runnable() {
2783 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002784
Winson Chung64359a52013-07-08 17:17:08 -07002785 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002786 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002787 if (callbacks != null) {
2788 callbacks.bindAllApplications(added);
2789 if (DEBUG_LOADERS) {
2790 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002791 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07002792 }
2793 } else {
2794 Log.i(TAG, "not binding apps: no Launcher activity");
2795 }
2796 }
2797 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002798 // Cleanup any data stored for a deleted user.
2799 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04002800 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002801 Log.d(TAG, "Icons processed in "
2802 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002803 }
2804 }
2805
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002806 private void loadAndBindDeepShortcuts() {
2807 if (DEBUG_LOADERS) {
2808 Log.d(TAG, "loadAndBindDeepShortcuts mDeepShortcutsLoaded=" + mDeepShortcutsLoaded);
2809 }
2810 if (!mDeepShortcutsLoaded) {
2811 mBgDeepShortcutMap.clear();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002812 mHasShortcutHostPermission = mDeepShortcutManager.hasHostPermission();
2813 if (mHasShortcutHostPermission) {
2814 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
2815 if (mUserManager.isUserUnlocked(user)) {
2816 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager
2817 .queryForAllShortcuts(user);
2818 updateDeepShortcutMap(null, user, shortcuts);
2819 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002820 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002821 }
2822 synchronized (LoaderTask.this) {
2823 if (mStopped) {
2824 return;
2825 }
2826 mDeepShortcutsLoaded = true;
2827 }
2828 }
Tony Wickham80f57872016-06-29 18:12:15 -07002829 bindDeepShortcuts();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002830 }
2831
Joe Onoratobe386092009-11-17 17:32:16 -08002832 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002833 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002834 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002835 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2836 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2837 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2838 }
Joe Onorato36115782010-06-17 13:28:48 -04002839 }
2840 }
2841
Sunny Goyal860538d2016-07-20 12:35:16 -07002842 /**
2843 * Clear all the shortcuts for the given package, and re-add the new shortcuts.
2844 */
2845 private void updateDeepShortcutMap(
2846 String packageName, UserHandleCompat user, List<ShortcutInfoCompat> shortcuts) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002847 if (packageName != null) {
2848 Iterator<ComponentKey> keysIter = mBgDeepShortcutMap.keySet().iterator();
2849 while (keysIter.hasNext()) {
Sunny Goyal860538d2016-07-20 12:35:16 -07002850 ComponentKey next = keysIter.next();
2851 if (next.componentName.getPackageName().equals(packageName)
2852 && next.user.equals(user)) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002853 keysIter.remove();
2854 }
2855 }
2856 }
2857
2858 // Now add the new shortcuts to the map.
2859 for (ShortcutInfoCompat shortcut : shortcuts) {
Tony Wickhamca258b32016-07-28 12:31:28 -07002860 boolean shouldShowInContainer = shortcut.isEnabled()
2861 && (shortcut.isDeclaredInManifest() || shortcut.isDynamic());
2862 if (shouldShowInContainer) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002863 ComponentKey targetComponent
2864 = new ComponentKey(shortcut.getActivity(), shortcut.getUserHandle());
2865 mBgDeepShortcutMap.addToList(targetComponent, shortcut.getId());
2866 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002867 }
2868 }
2869
Tony Wickham80f57872016-06-29 18:12:15 -07002870 public void bindDeepShortcuts() {
Sunny Goyal5f064012016-08-08 10:10:01 -07002871 final MultiHashMap<ComponentKey, String> shortcutMapCopy = mBgDeepShortcutMap.clone();
Tony Wickham80f57872016-06-29 18:12:15 -07002872 Runnable r = new Runnable() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002873 @Override
2874 public void run() {
2875 Callbacks callbacks = getCallback();
2876 if (callbacks != null) {
2877 callbacks.bindDeepShortcutMap(shortcutMapCopy);
2878 }
2879 }
Tony Wickham80f57872016-06-29 18:12:15 -07002880 };
2881 runOnMainThread(r);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002882 }
2883
Sunny Goyal75b0f552015-05-20 21:57:06 -07002884 /**
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002885 * Refreshes the cached shortcuts if the shortcut permission has changed.
2886 * Current implementation simply reloads the workspace, but it can be optimized to
2887 * use partial updates similar to {@link UserManagerCompat}
2888 */
2889 public void refreshShortcutsIfRequired() {
2890 if (Utilities.isNycMR1OrAbove()) {
2891 sWorker.removeCallbacks(mShortcutPermissionCheckRunnable);
2892 sWorker.post(mShortcutPermissionCheckRunnable);
2893 }
2894 }
2895
2896 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -07002897 * Called when the icons for packages have been updated in the icon cache.
2898 */
2899 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2900 final Callbacks callbacks = getCallback();
2901 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2902 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2903
2904 // If any package icon has changed (app was updated while launcher was dead),
2905 // update the corresponding shortcuts.
2906 synchronized (sBgLock) {
2907 for (ItemInfo info : sBgItemsIdMap) {
2908 if (info instanceof ShortcutInfo && user.equals(info.user)
2909 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2910 ShortcutInfo si = (ShortcutInfo) info;
2911 ComponentName cn = si.getTargetComponent();
2912 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2913 si.updateIcon(mIconCache);
2914 updatedShortcuts.add(si);
2915 }
2916 }
2917 }
2918 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2919 }
2920
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002921 bindUpdatedShortcuts(updatedShortcuts, user);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002922
2923 if (!updatedApps.isEmpty()) {
2924 mHandler.post(new Runnable() {
2925
2926 public void run() {
2927 Callbacks cb = getCallback();
2928 if (cb != null && callbacks == cb) {
2929 cb.bindAppsUpdated(updatedApps);
2930 }
2931 }
2932 });
2933 }
2934 }
2935
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002936 private void bindUpdatedShortcuts(
2937 ArrayList<ShortcutInfo> updatedShortcuts, UserHandleCompat user) {
2938 bindUpdatedShortcuts(updatedShortcuts, new ArrayList<ShortcutInfo>(), user);
2939 }
2940
2941 private void bindUpdatedShortcuts(
2942 final ArrayList<ShortcutInfo> updatedShortcuts,
2943 final ArrayList<ShortcutInfo> removedShortcuts,
2944 final UserHandleCompat user) {
2945 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002946 final Callbacks callbacks = getCallback();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002947 mHandler.post(new Runnable() {
2948
2949 public void run() {
2950 Callbacks cb = getCallback();
2951 if (cb != null && callbacks == cb) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002952 cb.bindShortcutsChanged(updatedShortcuts, removedShortcuts, user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002953 }
2954 }
2955 });
2956 }
2957 }
2958
2959 void enqueueItemUpdatedTask(Runnable task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002960 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002961 }
2962
Adam Cohen091440a2015-03-18 14:16:05 -07002963 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002964
2965 @Override
2966 public void onReceive(Context context, Intent intent) {
2967 synchronized (sBgLock) {
2968 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2969 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002970 final PackageManager manager = context.getPackageManager();
2971 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2972 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002973 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2974 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002975 packagesRemoved.clear();
2976 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002977 for (String pkg : entry.getValue()) {
2978 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07002979 if (PackageManagerHelper.isAppOnSdcard(manager, pkg)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002980 packagesUnavailable.add(pkg);
2981 } else {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002982 packagesRemoved.add(pkg);
2983 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002984 }
2985 }
2986 if (!packagesRemoved.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002987 enqueueItemUpdatedTask(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002988 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2989 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002990 if (!packagesUnavailable.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002991 enqueueItemUpdatedTask(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
Sunny Goyal1a745e82014-10-02 15:58:31 -07002992 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2993 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002994 }
Sunny Goyal34942622014-08-29 17:20:55 -07002995 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002996 }
2997 }
2998 }
2999
Joe Onorato36115782010-06-17 13:28:48 -04003000 private class PackageUpdatedTask implements Runnable {
3001 int mOp;
3002 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01003003 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04003004
3005 public static final int OP_NONE = 0;
3006 public static final int OP_ADD = 1;
3007 public static final int OP_UPDATE = 2;
3008 public static final int OP_REMOVE = 3; // uninstlled
3009 public static final int OP_UNAVAILABLE = 4; // external media unmounted
Kenny Guy44cba692016-01-21 19:50:02 +00003010 public static final int OP_SUSPEND = 5; // package suspended
3011 public static final int OP_UNSUSPEND = 6; // package unsuspended
Sunny Goyalda891c12016-03-18 18:29:24 -07003012 public static final int OP_USER_AVAILABILITY_CHANGE = 7; // user available/unavailable
Joe Onorato36115782010-06-17 13:28:48 -04003013
Kenny Guyed131872014-04-30 03:02:21 +01003014 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04003015 mOp = op;
3016 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01003017 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04003018 }
3019
3020 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07003021 if (!mHasLoaderCompletedOnce) {
3022 // Loader has not yet run.
3023 return;
3024 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04003025 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04003026
3027 final String[] packages = mPackages;
3028 final int N = packages.length;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003029 FlagOp flagOp = FlagOp.NO_OP;
Sunny Goyalda891c12016-03-18 18:29:24 -07003030 StringFilter pkgFilter = StringFilter.of(new HashSet<>(Arrays.asList(packages)));
Joe Onorato36115782010-06-17 13:28:48 -04003031 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003032 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04003033 for (int i=0; i<N; i++) {
3034 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003035 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003036 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003037 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003038
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003039 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3040 if (heuristic != null) {
3041 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003042 }
Joe Onorato36115782010-06-17 13:28:48 -04003043 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003044 }
Joe Onorato36115782010-06-17 13:28:48 -04003045 case OP_UPDATE:
3046 for (int i=0; i<N; i++) {
3047 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003048 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003049 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003050 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003051 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003052 // Since package was just updated, the target must be available now.
3053 flagOp = FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04003054 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003055 case OP_REMOVE: {
3056 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3057 if (heuristic != null) {
3058 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003059 }
Joe Onorato36115782010-06-17 13:28:48 -04003060 for (int i=0; i<N; i++) {
3061 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003062 mIconCache.removeIconsForPkg(packages[i], mUser);
3063 }
3064 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003065 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003066 case OP_UNAVAILABLE:
3067 for (int i=0; i<N; i++) {
3068 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3069 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003070 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003071 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003072 flagOp = FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04003073 break;
Kenny Guy44cba692016-01-21 19:50:02 +00003074 case OP_SUSPEND:
3075 case OP_UNSUSPEND:
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003076 flagOp = mOp == OP_SUSPEND ?
3077 FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED) :
3078 FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED);
Sunny Goyal17763cb2016-03-24 13:53:22 -07003079 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.(un)suspend " + N);
Sunny Goyalda891c12016-03-18 18:29:24 -07003080 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
3081 break;
3082 case OP_USER_AVAILABILITY_CHANGE:
3083 flagOp = UserManagerCompat.getInstance(context).isQuietModeEnabled(mUser)
3084 ? FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER)
3085 : FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER);
3086 // We want to update all packages for this user.
3087 pkgFilter = StringFilter.matchesAll();
3088 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
Kenny Guy44cba692016-01-21 19:50:02 +00003089 break;
Joe Onorato36115782010-06-17 13:28:48 -04003090 }
3091
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003092 ArrayList<AppInfo> added = null;
3093 ArrayList<AppInfo> modified = null;
3094 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003095
Adam Cohen487f7dd2012-06-28 18:12:10 -07003096 if (mBgAllAppsList.added.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003097 added = new ArrayList<>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003098 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003099 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003100 if (mBgAllAppsList.modified.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003101 modified = new ArrayList<>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003102 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003103 }
Winson Chung5d55f332012-07-16 20:45:03 -07003104 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003105 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003106 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003107 }
3108
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003109 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps = new HashMap<>();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003110
Joe Onorato36115782010-06-17 13:28:48 -04003111 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003112 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003113 for (AppInfo ai : added) {
3114 addedOrUpdatedApps.put(ai.componentName, ai);
3115 }
Joe Onorato36115782010-06-17 13:28:48 -04003116 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003117
Joe Onorato36115782010-06-17 13:28:48 -04003118 if (modified != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003119 final Callbacks callbacks = getCallback();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003120 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003121 for (AppInfo ai : modified) {
3122 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003123 }
3124
Joe Onorato36115782010-06-17 13:28:48 -04003125 mHandler.post(new Runnable() {
3126 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003127 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003128 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003129 callbacks.bindAppsUpdated(modifiedFinal);
3130 }
3131 }
3132 });
3133 }
Winson Chung83892cc2013-05-01 16:53:33 -07003134
Sunny Goyal4390ace2014-10-13 11:33:11 -07003135 // Update shortcut infos
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003136 if (mOp == OP_ADD || flagOp != FlagOp.NO_OP) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003137 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3138 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3139 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3140
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003141 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003142 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003143 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3144 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003145 boolean infoUpdated = false;
3146 boolean shortcutUpdated = false;
3147
3148 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003149 if ((si.iconResource != null)
Sunny Goyalda891c12016-03-18 18:29:24 -07003150 && pkgFilter.matches(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003151 Bitmap icon = Utilities.createIconBitmap(
3152 si.iconResource.packageName,
3153 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003154 if (icon != null) {
3155 si.setIcon(icon);
3156 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003157 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003158 }
3159 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003160
3161 ComponentName cn = si.getTargetComponent();
Sunny Goyalda891c12016-03-18 18:29:24 -07003162 if (cn != null && pkgFilter.matches(cn.getPackageName())) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003163 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3164
3165 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003166 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3167 // Auto install icon
3168 PackageManager pm = context.getPackageManager();
3169 ResolveInfo matched = pm.resolveActivity(
3170 new Intent(Intent.ACTION_MAIN)
3171 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3172 PackageManager.MATCH_DEFAULT_ONLY);
3173 if (matched == null) {
3174 // Try to find the best match activity.
3175 Intent intent = pm.getLaunchIntentForPackage(
3176 cn.getPackageName());
3177 if (intent != null) {
3178 cn = intent.getComponent();
3179 appInfo = addedOrUpdatedApps.get(cn);
3180 }
3181
3182 if ((intent == null) || (appInfo == null)) {
3183 removedShortcuts.add(si);
3184 continue;
3185 }
3186 si.promisedIntent = intent;
3187 }
3188 }
3189
3190 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003191 if (appInfo != null) {
3192 si.flags = appInfo.flags;
3193 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003194
Sunny Goyal756adbc2015-04-16 15:20:51 -07003195 si.intent = si.promisedIntent;
3196 si.promisedIntent = null;
3197 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003198 infoUpdated = true;
3199 si.updateIcon(mIconCache);
3200 }
3201
3202 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3203 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3204 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003205 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003206 si.contentDescription = appInfo.contentDescription;
3207 infoUpdated = true;
3208 }
3209
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003210 int oldDisabledFlags = si.isDisabled;
3211 si.isDisabled = flagOp.apply(si.isDisabled);
3212 if (si.isDisabled != oldDisabledFlags) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003213 shortcutUpdated = true;
3214 }
3215 }
3216
3217 if (infoUpdated || shortcutUpdated) {
3218 updatedShortcuts.add(si);
3219 }
3220 if (infoUpdated) {
3221 updateItemInDatabase(context, si);
3222 }
Sunny Goyalda891c12016-03-18 18:29:24 -07003223 } else if (info instanceof LauncherAppWidgetInfo && mOp == OP_ADD) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003224 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3225 if (mUser.equals(widgetInfo.user)
3226 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
Sunny Goyalda891c12016-03-18 18:29:24 -07003227 && pkgFilter.matches(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003228 widgetInfo.restoreStatus &=
3229 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3230 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003231
3232 // adding this flag ensures that launcher shows 'click to setup'
3233 // if the widget has a config activity. In case there is no config
3234 // activity, it will be marked as 'restored' during bind.
3235 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3236
Sunny Goyal4390ace2014-10-13 11:33:11 -07003237 widgets.add(widgetInfo);
3238 updateItemInDatabase(context, widgetInfo);
3239 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003240 }
3241 }
3242 }
3243
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003244 bindUpdatedShortcuts(updatedShortcuts, removedShortcuts, mUser);
3245 if (!removedShortcuts.isEmpty()) {
3246 deleteItemsFromDatabase(context, removedShortcuts);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003247 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003248
Sunny Goyal4390ace2014-10-13 11:33:11 -07003249 if (!widgets.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003250 final Callbacks callbacks = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003251 mHandler.post(new Runnable() {
3252 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003253 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003254 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003255 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003256 }
3257 }
3258 });
3259 }
3260 }
3261
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003262 final HashSet<String> removedPackages = new HashSet<>();
3263 final HashSet<ComponentName> removedComponents = new HashSet<>();
3264 if (mOp == OP_REMOVE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003265 // Mark all packages in the broadcast to be removed
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003266 Collections.addAll(removedPackages, packages);
3267
3268 // No need to update the removedComponents as
3269 // removedPackages is a super-set of removedComponents
Winson Chungdf95eb12013-10-16 14:57:07 -07003270 } else if (mOp == OP_UPDATE) {
3271 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003272 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003273 if (isPackageDisabled(context, packages[i], mUser)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003274 removedPackages.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003275 }
3276 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003277
3278 // Update removedComponents as some components can get removed during package update
3279 for (AppInfo info : removedApps) {
3280 removedComponents.add(info.componentName);
3281 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003282 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003283
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003284 if (!removedPackages.isEmpty() || !removedComponents.isEmpty()) {
3285 for (String pn : removedPackages) {
3286 deletePackageFromDatabase(context, pn, mUser);
3287 }
3288 for (ComponentName cn : removedComponents) {
3289 deleteItemsFromDatabase(context, getItemInfoForComponentName(cn, mUser));
Sunny Goyal1a745e82014-10-02 15:58:31 -07003290 }
3291
Winson Chungdf95eb12013-10-16 14:57:07 -07003292 // Remove any queued items from the install queue
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003293 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackages, mUser);
3294
Winson Chungdf95eb12013-10-16 14:57:07 -07003295 // Call the components-removed callback
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003296 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003297 mHandler.post(new Runnable() {
3298 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003299 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003300 if (callbacks == cb && cb != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003301 callbacks.bindWorkspaceComponentsRemoved(
3302 removedPackages, removedComponents, mUser);
3303 }
3304 }
3305 });
3306 }
3307
3308 if (!removedApps.isEmpty()) {
3309 // Remove corresponding apps from All-Apps
3310 final Callbacks callbacks = getCallback();
3311 mHandler.post(new Runnable() {
3312 public void run() {
3313 Callbacks cb = getCallback();
3314 if (callbacks == cb && cb != null) {
3315 callbacks.bindAppInfosRemoved(removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04003316 }
3317 }
3318 });
Joe Onoratobe386092009-11-17 17:32:16 -08003319 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003320
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003321 // Notify launcher of widget update. From marshmallow onwards we use AppWidgetHost to
3322 // get widget update signals.
3323 if (!Utilities.ATLEAST_MARSHMALLOW &&
3324 (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003325 final Callbacks callbacks = getCallback();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003326 mHandler.post(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003327 public void run() {
3328 Callbacks cb = getCallback();
3329 if (callbacks == cb && cb != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003330 callbacks.notifyWidgetProvidersChanged();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003331 }
3332 }
3333 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003334 }
3335 }
3336 }
3337
Sunny Goyal10923b32016-07-20 15:42:44 -07003338 /**
3339 * Repopulates the shortcut info, possibly updating any icon already on the workspace.
3340 */
3341 public void updateShortcutInfo(final ShortcutInfoCompat fullDetail, final ShortcutInfo info) {
3342 enqueueItemUpdatedTask(new Runnable() {
3343 @Override
3344 public void run() {
3345 info.updateFromDeepShortcutInfo(
3346 fullDetail, LauncherAppState.getInstance().getContext());
3347 ArrayList<ShortcutInfo> update = new ArrayList<ShortcutInfo>();
3348 update.add(info);
3349 bindUpdatedShortcuts(update, fullDetail.getUserHandle());
3350 }
3351 });
3352 }
3353
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003354 private class ShortcutsChangedTask implements Runnable {
Sunny Goyal50941fb2016-08-04 12:03:52 -07003355 private final String mPackageName;
3356 private final List<ShortcutInfoCompat> mShortcuts;
3357 private final UserHandleCompat mUser;
3358 private final boolean mUpdateIdMap;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003359
3360 public ShortcutsChangedTask(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal50941fb2016-08-04 12:03:52 -07003361 UserHandleCompat user, boolean updateIdMap) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003362 mPackageName = packageName;
3363 mShortcuts = shortcuts;
3364 mUser = user;
Sunny Goyal50941fb2016-08-04 12:03:52 -07003365 mUpdateIdMap = updateIdMap;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003366 }
3367
3368 @Override
3369 public void run() {
3370 mDeepShortcutManager.onShortcutsChanged(mShortcuts);
3371
3372 Map<String, ShortcutInfoCompat> idsToShortcuts = new HashMap<>();
3373 for (ShortcutInfoCompat shortcut : mShortcuts) {
3374 idsToShortcuts.put(shortcut.getId(), shortcut);
3375 }
3376
3377 // Find ShortcutInfo's that have changed on the workspace.
3378 MultiHashMap<String, ShortcutInfo> idsToWorkspaceShortcutInfos = new MultiHashMap<>();
3379 for (ItemInfo itemInfo : sBgItemsIdMap) {
3380 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
3381 ShortcutInfo si = (ShortcutInfo) itemInfo;
Tony Wickham672d07f2016-07-27 18:22:41 -07003382 if (si.getIntent().getPackage().equals(mPackageName) && si.user.equals(mUser)) {
3383 String shortcutId = si.getDeepShortcutId();
3384 if (idsToShortcuts.containsKey(shortcutId)) {
3385 idsToWorkspaceShortcutInfos.addToList(shortcutId, si);
3386 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003387 }
3388 }
3389 }
3390
3391 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3392 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager.queryForFullDetails(
3393 mPackageName, new ArrayList<>(idsToWorkspaceShortcutInfos.keySet()), mUser);
3394 ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3395 Context context = LauncherAppState.getInstance().getContext();
3396 for (ShortcutInfoCompat fullDetails : shortcuts) {
3397 List<ShortcutInfo> shortcutInfos = idsToWorkspaceShortcutInfos
3398 .get(fullDetails.getId());
3399 for (ShortcutInfo shortcutInfo : shortcutInfos) {
Sunny Goyal9994b2b2016-06-23 14:17:24 -07003400 shortcutInfo.updateFromDeepShortcutInfo(fullDetails, context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003401 updatedShortcutInfos.add(shortcutInfo);
3402 }
3403 }
3404 bindUpdatedShortcuts(updatedShortcutInfos, mUser);
3405
Sunny Goyal50941fb2016-08-04 12:03:52 -07003406 if (mUpdateIdMap) {
3407 // Update the deep shortcut map if the list of ids has changed for an activity.
3408 updateDeepShortcutMap(mPackageName, mUser, mShortcuts);
3409 bindDeepShortcuts();
3410 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003411 }
3412 }
3413
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003414 /**
3415 * Task to handle changing of lock state of the user
3416 */
3417 private class UserLockStateChangedTask implements Runnable {
3418
3419 private final UserHandleCompat mUser;
3420
3421 public UserLockStateChangedTask(UserHandleCompat user) {
3422 mUser = user;
3423 }
3424
3425 @Override
3426 public void run() {
3427 boolean isUserUnlocked = mUserManager.isUserUnlocked(mUser);
3428 Context context = mApp.getContext();
3429
3430 HashMap<ShortcutKey, ShortcutInfoCompat> pinnedShortcuts = new HashMap<>();
3431 if (isUserUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07003432 List<ShortcutInfoCompat> shortcuts =
3433 mDeepShortcutManager.queryForPinnedShortcuts(null, mUser);
3434 if (mDeepShortcutManager.wasLastCallSuccess()) {
3435 for (ShortcutInfoCompat shortcut : shortcuts) {
3436 pinnedShortcuts.put(ShortcutKey.fromInfo(shortcut), shortcut);
3437 }
3438 } else {
3439 // Shortcut manager can fail due to some race condition when the lock state
3440 // changes too frequently. For the purpose of the update,
3441 // consider it as still locked.
3442 isUserUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003443 }
3444 }
3445
3446 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3447 ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3448 ArrayList<ShortcutInfo> deletedShortcutInfos = new ArrayList<>();
3449 for (ItemInfo itemInfo : sBgItemsIdMap) {
3450 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT
3451 && mUser.equals(itemInfo.user)) {
3452 ShortcutInfo si = (ShortcutInfo) itemInfo;
3453 if (isUserUnlocked) {
3454 ShortcutInfoCompat shortcut =
3455 pinnedShortcuts.get(ShortcutKey.fromItemInfo(si));
3456 // We couldn't verify the shortcut during loader. If its no longer available
3457 // (probably due to clear data), delete the workspace item as well
3458 if (shortcut == null) {
3459 deletedShortcutInfos.add(si);
3460 continue;
3461 }
3462 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
3463 si.updateFromDeepShortcutInfo(shortcut, context);
3464 } else {
3465 si.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
3466 }
3467 updatedShortcutInfos.add(si);
3468 }
3469 }
3470 bindUpdatedShortcuts(updatedShortcutInfos, deletedShortcutInfos, mUser);
3471 if (!deletedShortcutInfos.isEmpty()) {
3472 deleteItemsFromDatabase(context, deletedShortcutInfos);
3473 }
3474
3475 // Remove shortcut id map for that user
3476 Iterator<ComponentKey> keysIter = mBgDeepShortcutMap.keySet().iterator();
3477 while (keysIter.hasNext()) {
3478 if (keysIter.next().user.equals(mUser)) {
3479 keysIter.remove();
3480 }
3481 }
3482
3483 if (isUserUnlocked) {
3484 updateDeepShortcutMap(null, mUser, mDeepShortcutManager.queryForAllShortcuts(mUser));
3485 }
3486 bindDeepShortcuts();
3487 }
3488 }
3489
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003490 private void bindWidgetsModel(final Callbacks callbacks, final WidgetsModel model) {
3491 mHandler.post(new Runnable() {
3492 @Override
3493 public void run() {
3494 Callbacks cb = getCallback();
3495 if (callbacks == cb && cb != null) {
3496 callbacks.bindWidgetsModel(model);
3497 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003498 }
3499 });
3500 }
3501
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003502 public void refreshAndBindWidgetsAndShortcuts(
3503 final Callbacks callbacks, final boolean bindFirst) {
3504 runOnWorkerThread(new Runnable() {
3505 @Override
3506 public void run() {
3507 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
3508 bindWidgetsModel(callbacks, mBgWidgetsModel.clone());
Sunny Goyal31860582015-09-18 08:38:57 -07003509 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003510 final WidgetsModel model = mBgWidgetsModel.updateAndClone(mApp.getContext());
3511 bindWidgetsModel(callbacks, model);
3512 // update the Widget entries inside DB on the worker thread.
3513 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3514 model.getRawList());
Sunny Goyal31860582015-09-18 08:38:57 -07003515 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003516 });
Michael Jurkac402cd92013-05-20 15:49:32 +02003517 }
3518
Adam Cohen091440a2015-03-18 14:16:05 -07003519 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003520 UserHandleCompat user) {
3521 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3522 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003523 }
Adam Cohen556f6132014-01-15 15:18:08 -08003524
Kenny Guyed131872014-04-30 03:02:21 +01003525 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3526 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003527 if (cn == null) {
3528 return false;
3529 }
Kenny Guyed131872014-04-30 03:02:21 +01003530 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3531 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003532 return false;
3533 }
Kenny Guyed131872014-04-30 03:02:21 +01003534 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003535 }
3536
Adam Cohena28b78e2014-05-20 17:03:04 -07003537 public static boolean isValidPackage(Context context, String packageName,
3538 UserHandleCompat user) {
3539 if (packageName == null) {
3540 return false;
3541 }
3542 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3543 return launcherApps.isPackageEnabledForProfile(packageName, user);
3544 }
3545
Joe Onorato9c1289c2009-08-17 11:03:03 -04003546 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003547 * Make an ShortcutInfo object for a restored application or shortcut item that points
3548 * to a package that is not yet installed on the system.
3549 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003550 public ShortcutInfo getRestoredItemInfo(Cursor c, Intent intent,
3551 int promiseType, int itemType, CursorIconInfo iconInfo) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003552 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003553 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003554
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003555 Bitmap icon = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003556 // the fallback icon
3557 if (icon == null) {
3558 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3559 } else {
3560 info.setIcon(icon);
3561 }
Sunny Goyal34942622014-08-29 17:20:55 -07003562
3563 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003564 String title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07003565 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003566 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003567 }
Sunny Goyal34942622014-08-29 17:20:55 -07003568 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3569 if (TextUtils.isEmpty(info.title)) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003570 info.title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07003571 }
Sunny Goyal34942622014-08-29 17:20:55 -07003572 } else {
3573 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3574 }
3575
Winson Chung82b016c2015-05-08 17:00:10 -07003576 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003577 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003578 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003579 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003580 return info;
3581 }
3582
3583 /**
3584 * Make an Intent object for a restored application or shortcut item that points
3585 * to the market page for the item.
3586 */
Adam Cohen091440a2015-03-18 14:16:05 -07003587 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003588 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003589 return getMarketIntent(componentName.getPackageName());
3590 }
3591
3592 static Intent getMarketIntent(String packageName) {
3593 return new Intent(Intent.ACTION_VIEW)
3594 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003595 .scheme("market")
3596 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003597 .appendQueryParameter("id", packageName)
3598 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003599 }
3600
3601 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003602 * Make an ShortcutInfo object for a shortcut that is an application.
3603 *
3604 * If c is not null, then it will be used to fill in missing data like the title and icon.
3605 */
Sunny Goyald09c3702016-04-06 16:18:20 -07003606 public ShortcutInfo getAppShortcutInfo(Intent intent,
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003607 UserHandleCompat user, Cursor c, CursorIconInfo iconInfo,
Sunny Goyal34b65272015-03-11 16:56:52 -07003608 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003609 if (user == null) {
3610 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003611 return null;
3612 }
3613
Kenny Guyed131872014-04-30 03:02:21 +01003614 ComponentName componentName = intent.getComponent();
3615 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08003616 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01003617 return null;
3618 }
3619
3620 Intent newIntent = new Intent(intent.getAction(), null);
3621 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3622 newIntent.setComponent(componentName);
3623 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003624 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003625 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3626 return null;
3627 }
3628
3629 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003630 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003631 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003632 Bitmap icon = iconInfo.loadIcon(c);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003633 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003634 }
3635
Sunny Goyald09c3702016-04-06 16:18:20 -07003636 if (lai != null && PackageManagerHelper.isAppSuspended(lai.getApplicationInfo())) {
3637 info.isDisabled = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
3638 }
3639
Joe Onorato56d82912010-03-07 14:32:10 -05003640 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003641 if (TextUtils.isEmpty(info.title) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003642 info.title = iconInfo.getTitle(c);
Joe Onorato56d82912010-03-07 14:32:10 -05003643 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003644
Joe Onorato56d82912010-03-07 14:32:10 -05003645 // fall back to the class name of the activity
3646 if (info.title == null) {
3647 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003648 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003649
Joe Onorato9c1289c2009-08-17 11:03:03 -04003650 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003651 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003652 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003653 if (lai != null) {
3654 info.flags = AppInfo.initFlags(lai);
3655 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003656 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003657 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003658
Sunny Goyale2df0622015-04-24 11:27:00 -07003659 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003660 ItemInfoFilter f) {
3661 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3662 for (ItemInfo i : infos) {
3663 if (i instanceof ShortcutInfo) {
3664 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003665 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003666 if (cn != null && f.filterItem(null, info, cn)) {
3667 filtered.add(info);
3668 }
3669 } else if (i instanceof FolderInfo) {
3670 FolderInfo info = (FolderInfo) i;
3671 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003672 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003673 if (cn != null && f.filterItem(info, s, cn)) {
3674 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003675 }
3676 }
Winson Chung64359a52013-07-08 17:17:08 -07003677 } else if (i instanceof LauncherAppWidgetInfo) {
3678 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3679 ComponentName cn = info.providerName;
3680 if (cn != null && f.filterItem(null, info, cn)) {
3681 filtered.add(info);
3682 }
Winson Chung8a435102012-08-30 17:16:53 -07003683 }
3684 }
Winson Chung64359a52013-07-08 17:17:08 -07003685 return new ArrayList<ItemInfo>(filtered);
3686 }
3687
Adam Cohen091440a2015-03-18 14:16:05 -07003688 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003689 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003690 ItemInfoFilter filter = new ItemInfoFilter() {
3691 @Override
3692 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003693 if (info.user == null) {
3694 return cn.equals(cname);
3695 } else {
3696 return cn.equals(cname) && info.user.equals(user);
3697 }
Winson Chung64359a52013-07-08 17:17:08 -07003698 }
3699 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003700 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003701 }
3702
Sunny Goyal1a745e82014-10-02 15:58:31 -07003703 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003704 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003705 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003706 @Thunk ShortcutInfo getShortcutInfo(Cursor c, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003707 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003708 // Non-app shortcuts are only supported for current user.
3709 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003710 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003711
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003712 // TODO: If there's an explicit component and we can't install that, delete it.
3713
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003714 loadInfoFromCursor(info, c, iconInfo);
3715 return info;
3716 }
Joe Onorato56d82912010-03-07 14:32:10 -05003717
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003718 /**
3719 * Make an ShortcutInfo object for a shortcut that isn't an application.
3720 */
3721 public void loadInfoFromCursor(ShortcutInfo info, Cursor c, CursorIconInfo iconInfo) {
3722 info.title = iconInfo.getTitle(c);
3723 Bitmap icon = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003724 // the fallback icon
3725 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003726 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003727 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003728 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003729 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003730 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003731
Sunny Goyal2350bc92014-10-14 16:42:54 -07003732 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003733 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3734 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3735 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3736
Adam Cohend9198822011-11-22 16:42:47 -08003737 if (intent == null) {
3738 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3739 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3740 return null;
3741 }
3742
Joe Onorato0589f0f2010-02-08 13:44:00 -08003743 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003744 boolean customIcon = false;
3745 ShortcutIconResource iconResource = null;
3746
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003747 if (bitmap instanceof Bitmap) {
3748 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003749 customIcon = true;
3750 } else {
3751 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003752 if (extra instanceof ShortcutIconResource) {
3753 iconResource = (ShortcutIconResource) extra;
3754 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003755 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003756 }
3757 }
3758
Michael Jurkac9d95c52011-08-29 14:03:34 -07003759 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003760
Kenny Guyed131872014-04-30 03:02:21 +01003761 // Only support intents for current user for now. Intents sent from other
3762 // users wouldn't get here without intent forwarding anyway.
3763 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003764 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003765 icon = mIconCache.getDefaultIcon(info.user);
3766 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003767 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003768 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003769
Winson Chung82b016c2015-05-08 17:00:10 -07003770 info.title = Utilities.trim(name);
3771 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003772 info.intent = intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003773 info.iconResource = iconResource;
3774
3775 return info;
3776 }
3777
Joe Onorato9c1289c2009-08-17 11:03:03 -04003778 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003779 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003780 * or make a new one.
3781 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003782 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003783 // See if a placeholder was created for us already
3784 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003785 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003786 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003787 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003788 folders.put(id, folderInfo);
3789 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003790 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003791 }
3792
Joe Onoratobe386092009-11-17 17:32:16 -08003793
Sunny Goyal651077b2014-06-30 14:15:31 -07003794 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3795 return (provider != null) && (provider.provider != null)
3796 && (provider.provider.getPackageName() != null);
3797 }
3798
Joe Onoratobe386092009-11-17 17:32:16 -08003799 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003800 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003801 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3802 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3803 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3804 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003805 if (mLoaderTask != null) {
3806 mLoaderTask.dumpState();
3807 } else {
3808 Log.d(TAG, "mLoaderTask=null");
3809 }
Joe Onoratobe386092009-11-17 17:32:16 -08003810 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003811
3812 public Callbacks getCallback() {
3813 return mCallbacks != null ? mCallbacks.get() : null;
3814 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003815
3816 /**
3817 * @return {@link FolderInfo} if its already loaded.
3818 */
3819 public FolderInfo findFolderById(Long folderId) {
3820 synchronized (sBgLock) {
3821 return sBgFolders.get(folderId);
3822 }
3823 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003824
Sunny Goyal527c7d32015-08-28 15:19:36 -07003825 @Thunk class DeferredMainThreadExecutor implements Executor {
3826
3827 @Override
3828 public void execute(Runnable command) {
3829 runOnMainThread(command);
3830 }
3831 }
3832
Sunny Goyal756adbc2015-04-16 15:20:51 -07003833 /**
3834 * @return the looper for the worker thread which can be used to start background tasks.
3835 */
3836 public static Looper getWorkerLooper() {
3837 return sWorkerThread.getLooper();
3838 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003839}