blob: d4223e1ff5af33b2d44ab1f47722477adbc1971d [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 Goyal86df1382016-08-10 15:03:22 -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 Goyal86df1382016-08-10 15:03:22 -07002178 if (appWidgetInfo.hasRestoreFlag(
2179 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG)) {
2180 intentDescription = c.getString(intentIndex);
2181 if (!TextUtils.isEmpty(intentDescription)) {
2182 appWidgetInfo.bindOptions =
2183 Intent.parseUri(intentDescription, 0);
2184 }
2185 }
Sunny Goyalff572272014-07-23 13:58:07 -07002186
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002187 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002188 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002189 appWidgetInfo.cellX = c.getInt(cellXIndex);
2190 appWidgetInfo.cellY = c.getInt(cellYIndex);
2191 appWidgetInfo.spanX = c.getInt(spanXIndex);
2192 appWidgetInfo.spanY = c.getInt(spanYIndex);
Sunny Goyalab7a4fe2015-07-15 17:20:54 -07002193 appWidgetInfo.user = user;
Joe Onorato36115782010-06-17 13:28:48 -04002194
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002195 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2196 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2197 Log.e(TAG, "Widget found where container != " +
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002198 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2199 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002200 continue;
2201 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002202
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002203 appWidgetInfo.container = container;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002204 // check & update map of what's occupied
Sunny Goyal41cdc8d2015-09-04 12:53:04 -07002205 if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) {
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002206 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002207 break;
2208 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002209
Adam Cohen59400422014-03-05 18:07:04 -08002210 if (!customWidget) {
2211 String providerName =
2212 appWidgetInfo.providerName.flattenToString();
2213 if (!providerName.equals(savedProvider) ||
2214 (appWidgetInfo.restoreStatus != restoreStatus)) {
2215 ContentValues values = new ContentValues();
2216 values.put(
2217 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2218 providerName);
2219 values.put(LauncherSettings.Favorites.RESTORED,
2220 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002221 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002222 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002223 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002224 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2225 sBgAppWidgets.add(appWidgetInfo);
2226 }
Joe Onorato36115782010-06-17 13:28:48 -04002227 break;
2228 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002229 } catch (Exception e) {
Sunny Goyala1365452015-10-01 15:46:24 -07002230 Log.e(TAG, "Desktop items loading interrupted", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002231 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002232 }
2233 } finally {
Sunny Goyal713edfc2016-05-06 09:58:34 -07002234 Utilities.closeSilently(c);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002235 }
2236
Winson Chungba9c37f2013-08-30 14:11:37 -07002237 // Break early if we've stopped loading
2238 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002239 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002240 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002241 }
2242
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002243 if (itemsToRemove.size() > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002244 // Remove dead items
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002245 contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI,
2246 Utilities.createDbSelectionQuery(
2247 LauncherSettings.Favorites._ID, itemsToRemove), null);
2248 if (DEBUG_LOADERS) {
2249 Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery(
2250 LauncherSettings.Favorites._ID, itemsToRemove));
2251 }
2252
2253 // Remove any empty folder
Sunny Goyald2497482015-09-22 18:24:19 -07002254 ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings
2255 .call(contentResolver,
2256 LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS)
2257 .getSerializable(LauncherSettings.Settings.EXTRA_VALUE);
2258 for (long folderId : deletedFolderIds) {
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002259 sBgWorkspaceItems.remove(sBgFolders.get(folderId));
2260 sBgFolders.remove(folderId);
2261 sBgItemsIdMap.remove(folderId);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002262 }
2263 }
2264
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002265 // Unpin shortcuts that don't exist on the workspace.
2266 for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) {
2267 MutableInt numTimesPinned = sBgPinnedShortcutCounts.get(key);
2268 if (numTimesPinned == null || numTimesPinned.value == 0) {
2269 // Shortcut is pinned but doesn't exist on the workspace; unpin it.
2270 mDeepShortcutManager.unpinShortcut(key);
2271 }
2272 }
2273
Sunny Goyal317698b2015-07-29 11:45:41 -07002274 // Sort all the folder items and make sure the first 3 items are high resolution.
2275 for (FolderInfo folder : sBgFolders) {
2276 Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR);
2277 int pos = 0;
2278 for (ShortcutInfo info : folder.contents) {
2279 if (info.usingLowResIcon) {
2280 info.updateIcon(mIconCache, false);
2281 }
2282 pos ++;
2283 if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) {
2284 break;
2285 }
2286 }
2287 }
2288
Chris Wrenf4d08112014-01-16 18:13:56 -05002289 if (restoredRows.size() > 0) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002290 // Update restored items that no longer require special handling
Sunny Goyalb1622cc2015-06-10 16:00:42 -07002291 ContentValues values = new ContentValues();
2292 values.put(LauncherSettings.Favorites.RESTORED, 0);
2293 contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values,
2294 Utilities.createDbSelectionQuery(
2295 LauncherSettings.Favorites._ID, restoredRows), null);
Chris Wrenf4d08112014-01-16 18:13:56 -05002296 }
2297
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002298 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2299 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal25aba0a2015-07-16 15:07:47 -07002300 new IntentFilter(Intent.ACTION_BOOT_COMPLETED),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002301 null, sWorker);
2302 }
2303
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002304 // Remove any empty screens
2305 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Sunny Goyale2df0622015-04-24 11:27:00 -07002306 for (ItemInfo item: sBgItemsIdMap) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002307 long screenId = item.screenId;
2308 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2309 unusedScreens.contains(screenId)) {
2310 unusedScreens.remove(screenId);
2311 }
2312 }
2313
2314 // If there are any empty screens remove them, and update.
2315 if (unusedScreens.size() != 0) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002316 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002317 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002318 }
2319
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002320 if (DEBUG_LOADERS) {
2321 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2322 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002323 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002324 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002325 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002326
Sunny Goyale2df0622015-04-24 11:27:00 -07002327 for (int i = 0; i < nScreens; i++) {
2328 long screenId = occupied.keyAt(i);
Winson Chungc9168342013-06-26 14:54:55 -07002329 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002330 line += " | ";
2331 }
Joe Onorato36115782010-06-17 13:28:48 -04002332 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002333 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002334 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002335 }
Joe Onorato36115782010-06-17 13:28:48 -04002336 }
Sunny Goyale26d1002016-06-20 14:52:14 -07002337 if (LauncherAppState.PROFILE_STARTUP) {
2338 Trace.endSection();
2339 }
Adam Cohene25af792013-06-06 23:08:25 -07002340 }
2341
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002342 /**
2343 * Partially updates the item without any notification. Must be called on the worker thread.
2344 */
2345 private void updateItem(long itemId, ContentValues update) {
2346 mContext.getContentResolver().update(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002347 LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002348 update,
2349 BaseColumns._ID + "= ?",
2350 new String[]{Long.toString(itemId)});
2351 }
2352
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002353 /** Filters the set of items who are directly or indirectly (via another container) on the
2354 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002355 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002356 ArrayList<ItemInfo> allWorkspaceItems,
2357 ArrayList<ItemInfo> currentScreenItems,
2358 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002359 // Purge any null ItemInfos
2360 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2361 while (iter.hasNext()) {
2362 ItemInfo i = iter.next();
2363 if (i == null) {
2364 iter.remove();
2365 }
2366 }
2367
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002368 // Order the set of items by their containers first, this allows use to walk through the
2369 // list sequentially, build up a list of containers that are in the specified screen,
2370 // as well as all items in those containers.
2371 Set<Long> itemsOnScreen = new HashSet<Long>();
2372 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2373 @Override
2374 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson12fb9fc2015-10-01 15:34:08 -07002375 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002376 }
2377 });
2378 for (ItemInfo info : allWorkspaceItems) {
2379 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002380 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002381 currentScreenItems.add(info);
2382 itemsOnScreen.add(info.id);
2383 } else {
2384 otherScreenItems.add(info);
2385 }
2386 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2387 currentScreenItems.add(info);
2388 itemsOnScreen.add(info.id);
2389 } else {
2390 if (itemsOnScreen.contains(info.container)) {
2391 currentScreenItems.add(info);
2392 itemsOnScreen.add(info.id);
2393 } else {
2394 otherScreenItems.add(info);
2395 }
2396 }
2397 }
2398 }
2399
2400 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002401 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002402 ArrayList<LauncherAppWidgetInfo> appWidgets,
2403 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2404 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002405
2406 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002407 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002408 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002409 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002410 currentScreenWidgets.add(widget);
2411 } else {
2412 otherScreenWidgets.add(widget);
2413 }
2414 }
2415 }
2416
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002417 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2418 * right) */
2419 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002420 final LauncherAppState app = LauncherAppState.getInstance();
Adam Cohen2e6da152015-05-06 11:42:25 -07002421 final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
Winson Chung882a52e2015-07-08 14:32:26 -07002422 final int screenCols = profile.numColumns;
2423 final int screenCellCount = profile.numColumns * profile.numRows;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002424 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002425 @Override
2426 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung882a52e2015-07-08 14:32:26 -07002427 if (lhs.container == rhs.container) {
2428 // Within containers, order by their spatial position in that container
2429 switch ((int) lhs.container) {
2430 case LauncherSettings.Favorites.CONTAINER_DESKTOP: {
2431 long lr = (lhs.screenId * screenCellCount +
2432 lhs.cellY * screenCols + lhs.cellX);
2433 long rr = (rhs.screenId * screenCellCount +
2434 rhs.cellY * screenCols + rhs.cellX);
Winson722e8562015-10-07 13:04:30 -07002435 return Utilities.longCompare(lr, rr);
Winson Chung882a52e2015-07-08 14:32:26 -07002436 }
2437 case LauncherSettings.Favorites.CONTAINER_HOTSEAT: {
2438 // We currently use the screen id as the rank
Winson722e8562015-10-07 13:04:30 -07002439 return Utilities.longCompare(lhs.screenId, rhs.screenId);
Winson Chung882a52e2015-07-08 14:32:26 -07002440 }
2441 default:
Sunny Goyal6c56c682015-07-16 14:09:05 -07002442 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Winson Chung882a52e2015-07-08 14:32:26 -07002443 throw new RuntimeException("Unexpected container type when " +
2444 "sorting workspace items.");
2445 }
2446 return 0;
2447 }
2448 } else {
2449 // Between containers, order by hotseat, desktop
Winson722e8562015-10-07 13:04:30 -07002450 return Utilities.longCompare(lhs.container, rhs.container);
Winson Chung882a52e2015-07-08 14:32:26 -07002451 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002452 }
2453 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002454 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002455
Adam Cohendcd297f2013-06-18 13:13:40 -07002456 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2457 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002458 final Runnable r = new Runnable() {
2459 @Override
2460 public void run() {
2461 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2462 if (callbacks != null) {
2463 callbacks.bindScreens(orderedScreens);
2464 }
2465 }
2466 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002467 runOnMainThread(r);
Adam Cohendcd297f2013-06-18 13:13:40 -07002468 }
2469
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002470 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2471 final ArrayList<ItemInfo> workspaceItems,
2472 final ArrayList<LauncherAppWidgetInfo> appWidgets,
Sunny Goyal527c7d32015-08-28 15:19:36 -07002473 final Executor executor) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002474
2475 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002476 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002477 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002478 final int start = i;
2479 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002480 final Runnable r = new Runnable() {
2481 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002482 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002483 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002484 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002485 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2486 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002487 }
2488 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002489 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002490 executor.execute(r);
Joe Onorato36115782010-06-17 13:28:48 -04002491 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002492
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002493 // Bind the widgets, one at a time
2494 N = appWidgets.size();
2495 for (int i = 0; i < N; i++) {
2496 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2497 final Runnable r = new Runnable() {
2498 public void run() {
2499 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2500 if (callbacks != null) {
2501 callbacks.bindAppWidget(widget);
2502 }
2503 }
2504 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002505 executor.execute(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002506 }
2507 }
2508
2509 /**
2510 * Binds all loaded data to actual views on the main thread.
2511 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002512 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002513 final long t = SystemClock.uptimeMillis();
2514 Runnable r;
2515
2516 // Don't use these two variables in any of the callback runnables.
2517 // Otherwise we hold a reference to them.
2518 final Callbacks oldCallbacks = mCallbacks.get();
2519 if (oldCallbacks == null) {
2520 // This launcher has exited and nobody bothered to tell us. Just bail.
2521 Log.w(TAG, "LoaderTask running with no launcher");
2522 return;
2523 }
2524
Winson Chung9b9fb962013-11-15 15:39:34 -08002525 // Save a copy of all the bg-thread collections
Sunny Goyal44c06432016-04-02 10:56:02 -07002526 ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
2527 ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
2528 ArrayList<Long> orderedScreenIds = new ArrayList<>();
Sunny Goyale2df0622015-04-24 11:27:00 -07002529
Winson Chung2abf94d2012-07-18 18:16:38 -07002530 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002531 workspaceItems.addAll(sBgWorkspaceItems);
2532 appWidgets.addAll(sBgAppWidgets);
Adam Cohendcd297f2013-06-18 13:13:40 -07002533 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002534 }
2535
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002536 final int currentScreen;
2537 {
2538 int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE
2539 ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen();
2540 if (currScreen >= orderedScreenIds.size()) {
2541 // There may be no workspace screens (just hotseat items and an empty page).
2542 currScreen = PagedView.INVALID_RESTORE_PAGE;
2543 }
2544 currentScreen = currScreen;
Winson Chung9b9fb962013-11-15 15:39:34 -08002545 }
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002546 final boolean validFirstPage = currentScreen >= 0;
2547 final long currentScreenId =
2548 validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID;
Winson Chung9b9fb962013-11-15 15:39:34 -08002549
Winson Chung9b9fb962013-11-15 15:39:34 -08002550 // Separate the items that are on the current screen, and all the other remaining items
Sunny Goyal44c06432016-04-02 10:56:02 -07002551 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
2552 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
2553 ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
2554 ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002555
Winson Chung9b9fb962013-11-15 15:39:34 -08002556 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002557 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002558 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002559 otherAppWidgets);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002560 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2561 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2562
2563 // Tell the workspace that we're about to start binding items
2564 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002565 public void run() {
2566 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2567 if (callbacks != null) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002568 callbacks.clearPendingBinds();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002569 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002570 }
2571 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002572 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002573 runOnMainThread(r);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002574
Adam Cohendcd297f2013-06-18 13:13:40 -07002575 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2576
Sunny Goyal527c7d32015-08-28 15:19:36 -07002577 Executor mainExecutor = new DeferredMainThreadExecutor();
2578 // Load items on the current page.
Sunny Goyal44c06432016-04-02 10:56:02 -07002579 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002580
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002581 // In case of validFirstPage, only bind the first screen, and defer binding the
2582 // remaining screens after first onDraw (and an optional the fade animation whichever
2583 // happens later).
2584 // This ensures that the first screen is immediately visible (eg. during rotation)
2585 // In case of !validFirstPage, bind all pages one after other.
2586 final Executor deferredExecutor =
2587 validFirstPage ? new ViewOnDrawExecutor(mHandler) : mainExecutor;
2588
2589 mainExecutor.execute(new Runnable() {
2590 @Override
2591 public void run() {
2592 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2593 if (callbacks != null) {
2594 callbacks.finishFirstPageBind(
2595 validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null);
2596 }
2597 }
2598 });
Sunny Goyal527c7d32015-08-28 15:19:36 -07002599
Sunny Goyal44c06432016-04-02 10:56:02 -07002600 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002601
2602 // Tell the workspace that we're done binding items
2603 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002604 public void run() {
2605 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2606 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002607 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002608 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002609
Sunny Goyal639e9062015-08-19 19:17:06 -07002610 mIsLoadingAndBindingWorkspace = false;
2611
2612 // Run all the bind complete runnables after workspace is bound.
2613 if (!mBindCompleteRunnables.isEmpty()) {
2614 synchronized (mBindCompleteRunnables) {
2615 for (final Runnable r : mBindCompleteRunnables) {
2616 runOnWorkerThread(r);
2617 }
2618 mBindCompleteRunnables.clear();
2619 }
2620 }
2621
Winson Chung98e030b2012-05-07 16:01:11 -07002622 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002623 if (DEBUG_LOADERS) {
2624 Log.d(TAG, "bound workspace in "
2625 + (SystemClock.uptimeMillis()-t) + "ms");
2626 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002627
Joe Onorato36115782010-06-17 13:28:48 -04002628 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002629 };
Sunny Goyal527c7d32015-08-28 15:19:36 -07002630 deferredExecutor.execute(r);
2631
Sunny Goyalb5b9ad62016-04-02 11:23:39 -07002632 if (validFirstPage) {
Sunny Goyal527c7d32015-08-28 15:19:36 -07002633 r = new Runnable() {
2634 public void run() {
2635 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2636 if (callbacks != null) {
2637 // We are loading synchronously, which means, some of the pages will be
2638 // bound after first draw. Inform the callbacks that page binding is
2639 // not complete, and schedule the remaining pages.
2640 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
2641 callbacks.onPageBoundSynchronously(currentScreen);
2642 }
2643 callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
2644 }
2645 }
2646 };
Sunny Goyald33860f2015-04-23 16:02:20 -07002647 runOnMainThread(r);
Winson Chung4a2afa32012-07-19 14:53:05 -07002648 }
Joe Onorato36115782010-06-17 13:28:48 -04002649 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002650
Joe Onorato36115782010-06-17 13:28:48 -04002651 private void loadAndBindAllApps() {
2652 if (DEBUG_LOADERS) {
2653 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2654 }
2655 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002656 loadAllApps();
Sunny Goyalf5cd9982015-05-18 15:19:29 -07002657 synchronized (LoaderTask.this) {
2658 if (mStopped) {
2659 return;
2660 }
2661 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002662 updateIconCache();
Reena Lee93f824a2011-09-23 17:20:28 -07002663 synchronized (LoaderTask.this) {
2664 if (mStopped) {
2665 return;
2666 }
2667 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002668 }
Joe Onorato36115782010-06-17 13:28:48 -04002669 } else {
2670 onlyBindAllApps();
2671 }
2672 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002673
Sunny Goyal4e5cc642015-06-25 16:37:44 -07002674 private void updateIconCache() {
2675 // Ignore packages which have a promise icon.
2676 HashSet<String> packagesToIgnore = new HashSet<>();
2677 synchronized (sBgLock) {
2678 for (ItemInfo info : sBgItemsIdMap) {
2679 if (info instanceof ShortcutInfo) {
2680 ShortcutInfo si = (ShortcutInfo) info;
2681 if (si.isPromise() && si.getTargetComponent() != null) {
2682 packagesToIgnore.add(si.getTargetComponent().getPackageName());
2683 }
2684 } else if (info instanceof LauncherAppWidgetInfo) {
2685 LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info;
2686 if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
2687 packagesToIgnore.add(lawi.providerName.getPackageName());
2688 }
2689 }
2690 }
2691 }
2692 mIconCache.updateDbIcons(packagesToIgnore);
2693 }
2694
Joe Onorato36115782010-06-17 13:28:48 -04002695 private void onlyBindAllApps() {
2696 final Callbacks oldCallbacks = mCallbacks.get();
2697 if (oldCallbacks == null) {
2698 // This launcher has exited and nobody bothered to tell us. Just bail.
2699 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2700 return;
2701 }
2702
2703 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002704 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002705 final ArrayList<AppInfo> list
2706 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002707 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002708 public void run() {
2709 final long t = SystemClock.uptimeMillis();
2710 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2711 if (callbacks != null) {
2712 callbacks.bindAllApplications(list);
2713 }
2714 if (DEBUG_LOADERS) {
2715 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
Hyunyoung Song747a5bc2016-02-08 11:31:33 -08002716 + (SystemClock.uptimeMillis() - t) + "ms");
Joe Onorato36115782010-06-17 13:28:48 -04002717 }
2718 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002719 };
Tony Wickham80f57872016-06-29 18:12:15 -07002720 runOnMainThread(r);
Joe Onorato36115782010-06-17 13:28:48 -04002721 }
2722
Winson Chung64359a52013-07-08 17:17:08 -07002723 private void loadAllApps() {
2724 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002725
Joe Onorato36115782010-06-17 13:28:48 -04002726 final Callbacks oldCallbacks = mCallbacks.get();
2727 if (oldCallbacks == null) {
2728 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002729 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002730 return;
2731 }
2732
Kenny Guyed131872014-04-30 03:02:21 +01002733 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2734
Winson Chung64359a52013-07-08 17:17:08 -07002735 // Clear the list of apps
2736 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002737 for (UserHandleCompat user : profiles) {
2738 // Query for the set of apps
2739 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Sunny Goyal756a28a2015-04-23 17:07:55 -07002740 final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
Kenny Guyed131872014-04-30 03:02:21 +01002741 if (DEBUG_LOADERS) {
2742 Log.d(TAG, "getActivityList took "
2743 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2744 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2745 }
2746 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002747 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002748 if (apps == null || apps.isEmpty()) {
2749 return;
2750 }
Kenny Guyff05f432016-01-22 17:48:29 +00002751 boolean quietMode = mUserManager.isQuietModeEnabled(user);
Kenny Guyed131872014-04-30 03:02:21 +01002752 // Create the ApplicationInfos
2753 for (int i = 0; i < apps.size(); i++) {
2754 LauncherActivityInfoCompat app = apps.get(i);
2755 // This builds the icon bitmaps.
Kenny Guyff05f432016-01-22 17:48:29 +00002756 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode));
Kenny Guyed131872014-04-30 03:02:21 +01002757 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002758
Sunny Goyal756a28a2015-04-23 17:07:55 -07002759 final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user);
2760 if (heuristic != null) {
Sunny Goyal639e9062015-08-19 19:17:06 -07002761 final Runnable r = new Runnable() {
Sunny Goyal756a28a2015-04-23 17:07:55 -07002762
2763 @Override
2764 public void run() {
2765 heuristic.processUserApps(apps);
2766 }
Sunny Goyal639e9062015-08-19 19:17:06 -07002767 };
2768 runOnMainThread(new Runnable() {
2769
2770 @Override
2771 public void run() {
2772 // Check isLoadingWorkspace on the UI thread, as it is updated on
2773 // the UI thread.
2774 if (mIsLoadingAndBindingWorkspace) {
2775 synchronized (mBindCompleteRunnables) {
2776 mBindCompleteRunnables.add(r);
2777 }
2778 } else {
2779 runOnWorkerThread(r);
2780 }
2781 }
Sunny Goyal756a28a2015-04-23 17:07:55 -07002782 });
Sunny Goyale0f58d72014-11-10 18:05:31 -08002783 }
Winson Chung64359a52013-07-08 17:17:08 -07002784 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002785 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002786 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2787 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002788
2789 // Post callback on main thread
2790 mHandler.post(new Runnable() {
2791 public void run() {
Hyunyoung Song9892e582015-05-05 10:07:23 -07002792
Winson Chung64359a52013-07-08 17:17:08 -07002793 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002794 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002795 if (callbacks != null) {
2796 callbacks.bindAllApplications(added);
2797 if (DEBUG_LOADERS) {
2798 Log.d(TAG, "bound " + added.size() + " apps in "
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002799 + (SystemClock.uptimeMillis() - bindTime) + "ms");
Winson Chung64359a52013-07-08 17:17:08 -07002800 }
2801 } else {
2802 Log.i(TAG, "not binding apps: no Launcher activity");
2803 }
2804 }
2805 });
Sunny Goyal18bf8e22015-04-08 18:13:46 -07002806 // Cleanup any data stored for a deleted user.
2807 ManagedProfileHeuristic.processAllUsers(profiles, mContext);
Joe Onorato36115782010-06-17 13:28:48 -04002808 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002809 Log.d(TAG, "Icons processed in "
2810 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002811 }
2812 }
2813
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002814 private void loadAndBindDeepShortcuts() {
2815 if (DEBUG_LOADERS) {
2816 Log.d(TAG, "loadAndBindDeepShortcuts mDeepShortcutsLoaded=" + mDeepShortcutsLoaded);
2817 }
2818 if (!mDeepShortcutsLoaded) {
2819 mBgDeepShortcutMap.clear();
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002820 mHasShortcutHostPermission = mDeepShortcutManager.hasHostPermission();
2821 if (mHasShortcutHostPermission) {
2822 for (UserHandleCompat user : mUserManager.getUserProfiles()) {
2823 if (mUserManager.isUserUnlocked(user)) {
2824 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager
2825 .queryForAllShortcuts(user);
2826 updateDeepShortcutMap(null, user, shortcuts);
2827 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002828 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002829 }
2830 synchronized (LoaderTask.this) {
2831 if (mStopped) {
2832 return;
2833 }
2834 mDeepShortcutsLoaded = true;
2835 }
2836 }
Tony Wickham80f57872016-06-29 18:12:15 -07002837 bindDeepShortcuts();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002838 }
2839
Joe Onoratobe386092009-11-17 17:32:16 -08002840 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002841 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002842 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002843 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2844 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2845 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2846 }
Joe Onorato36115782010-06-17 13:28:48 -04002847 }
2848 }
2849
Sunny Goyal860538d2016-07-20 12:35:16 -07002850 /**
2851 * Clear all the shortcuts for the given package, and re-add the new shortcuts.
2852 */
2853 private void updateDeepShortcutMap(
2854 String packageName, UserHandleCompat user, List<ShortcutInfoCompat> shortcuts) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002855 if (packageName != null) {
2856 Iterator<ComponentKey> keysIter = mBgDeepShortcutMap.keySet().iterator();
2857 while (keysIter.hasNext()) {
Sunny Goyal860538d2016-07-20 12:35:16 -07002858 ComponentKey next = keysIter.next();
2859 if (next.componentName.getPackageName().equals(packageName)
2860 && next.user.equals(user)) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002861 keysIter.remove();
2862 }
2863 }
2864 }
2865
2866 // Now add the new shortcuts to the map.
2867 for (ShortcutInfoCompat shortcut : shortcuts) {
Tony Wickhamca258b32016-07-28 12:31:28 -07002868 boolean shouldShowInContainer = shortcut.isEnabled()
2869 && (shortcut.isDeclaredInManifest() || shortcut.isDynamic());
2870 if (shouldShowInContainer) {
Sunny Goyal70a7c9b2016-07-28 10:07:32 -07002871 ComponentKey targetComponent
2872 = new ComponentKey(shortcut.getActivity(), shortcut.getUserHandle());
2873 mBgDeepShortcutMap.addToList(targetComponent, shortcut.getId());
2874 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002875 }
2876 }
2877
Tony Wickham80f57872016-06-29 18:12:15 -07002878 public void bindDeepShortcuts() {
Sunny Goyal5f064012016-08-08 10:10:01 -07002879 final MultiHashMap<ComponentKey, String> shortcutMapCopy = mBgDeepShortcutMap.clone();
Tony Wickham80f57872016-06-29 18:12:15 -07002880 Runnable r = new Runnable() {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002881 @Override
2882 public void run() {
2883 Callbacks callbacks = getCallback();
2884 if (callbacks != null) {
2885 callbacks.bindDeepShortcutMap(shortcutMapCopy);
2886 }
2887 }
Tony Wickham80f57872016-06-29 18:12:15 -07002888 };
2889 runOnMainThread(r);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002890 }
2891
Sunny Goyal75b0f552015-05-20 21:57:06 -07002892 /**
Sunny Goyal95f3d6b2016-08-10 16:09:29 -07002893 * Refreshes the cached shortcuts if the shortcut permission has changed.
2894 * Current implementation simply reloads the workspace, but it can be optimized to
2895 * use partial updates similar to {@link UserManagerCompat}
2896 */
2897 public void refreshShortcutsIfRequired() {
2898 if (Utilities.isNycMR1OrAbove()) {
2899 sWorker.removeCallbacks(mShortcutPermissionCheckRunnable);
2900 sWorker.post(mShortcutPermissionCheckRunnable);
2901 }
2902 }
2903
2904 /**
Sunny Goyal75b0f552015-05-20 21:57:06 -07002905 * Called when the icons for packages have been updated in the icon cache.
2906 */
2907 public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) {
2908 final Callbacks callbacks = getCallback();
2909 final ArrayList<AppInfo> updatedApps = new ArrayList<>();
2910 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>();
2911
2912 // If any package icon has changed (app was updated while launcher was dead),
2913 // update the corresponding shortcuts.
2914 synchronized (sBgLock) {
2915 for (ItemInfo info : sBgItemsIdMap) {
2916 if (info instanceof ShortcutInfo && user.equals(info.user)
2917 && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
2918 ShortcutInfo si = (ShortcutInfo) info;
2919 ComponentName cn = si.getTargetComponent();
2920 if (cn != null && updatedPackages.contains(cn.getPackageName())) {
2921 si.updateIcon(mIconCache);
2922 updatedShortcuts.add(si);
2923 }
2924 }
2925 }
2926 mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps);
2927 }
2928
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002929 bindUpdatedShortcuts(updatedShortcuts, user);
Sunny Goyal75b0f552015-05-20 21:57:06 -07002930
2931 if (!updatedApps.isEmpty()) {
2932 mHandler.post(new Runnable() {
2933
2934 public void run() {
2935 Callbacks cb = getCallback();
2936 if (cb != null && callbacks == cb) {
2937 cb.bindAppsUpdated(updatedApps);
2938 }
2939 }
2940 });
2941 }
2942 }
2943
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002944 private void bindUpdatedShortcuts(
2945 ArrayList<ShortcutInfo> updatedShortcuts, UserHandleCompat user) {
2946 bindUpdatedShortcuts(updatedShortcuts, new ArrayList<ShortcutInfo>(), user);
2947 }
2948
2949 private void bindUpdatedShortcuts(
2950 final ArrayList<ShortcutInfo> updatedShortcuts,
2951 final ArrayList<ShortcutInfo> removedShortcuts,
2952 final UserHandleCompat user) {
2953 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002954 final Callbacks callbacks = getCallback();
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002955 mHandler.post(new Runnable() {
2956
2957 public void run() {
2958 Callbacks cb = getCallback();
2959 if (cb != null && callbacks == cb) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07002960 cb.bindShortcutsChanged(updatedShortcuts, removedShortcuts, user);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002961 }
2962 }
2963 });
2964 }
2965 }
2966
2967 void enqueueItemUpdatedTask(Runnable task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002968 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002969 }
2970
Adam Cohen091440a2015-03-18 14:16:05 -07002971 @Thunk class AppsAvailabilityCheck extends BroadcastReceiver {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002972
2973 @Override
2974 public void onReceive(Context context, Intent intent) {
2975 synchronized (sBgLock) {
2976 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2977 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002978 final PackageManager manager = context.getPackageManager();
2979 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2980 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002981 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2982 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002983 packagesRemoved.clear();
2984 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002985 for (String pkg : entry.getValue()) {
2986 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyald09c3702016-04-06 16:18:20 -07002987 if (PackageManagerHelper.isAppOnSdcard(manager, pkg)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002988 packagesUnavailable.add(pkg);
2989 } else {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002990 packagesRemoved.add(pkg);
2991 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002992 }
2993 }
2994 if (!packagesRemoved.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002995 enqueueItemUpdatedTask(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002996 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2997 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002998 if (!packagesUnavailable.isEmpty()) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07002999 enqueueItemUpdatedTask(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
Sunny Goyal1a745e82014-10-02 15:58:31 -07003000 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
3001 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003002 }
Sunny Goyal34942622014-08-29 17:20:55 -07003003 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003004 }
3005 }
3006 }
3007
Joe Onorato36115782010-06-17 13:28:48 -04003008 private class PackageUpdatedTask implements Runnable {
3009 int mOp;
3010 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01003011 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04003012
3013 public static final int OP_NONE = 0;
3014 public static final int OP_ADD = 1;
3015 public static final int OP_UPDATE = 2;
3016 public static final int OP_REMOVE = 3; // uninstlled
3017 public static final int OP_UNAVAILABLE = 4; // external media unmounted
Kenny Guy44cba692016-01-21 19:50:02 +00003018 public static final int OP_SUSPEND = 5; // package suspended
3019 public static final int OP_UNSUSPEND = 6; // package unsuspended
Sunny Goyalda891c12016-03-18 18:29:24 -07003020 public static final int OP_USER_AVAILABILITY_CHANGE = 7; // user available/unavailable
Joe Onorato36115782010-06-17 13:28:48 -04003021
Kenny Guyed131872014-04-30 03:02:21 +01003022 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04003023 mOp = op;
3024 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01003025 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04003026 }
3027
3028 public void run() {
Sunny Goyalc905efc2015-05-06 09:54:53 -07003029 if (!mHasLoaderCompletedOnce) {
3030 // Loader has not yet run.
3031 return;
3032 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -04003033 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04003034
3035 final String[] packages = mPackages;
3036 final int N = packages.length;
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003037 FlagOp flagOp = FlagOp.NO_OP;
Sunny Goyalda891c12016-03-18 18:29:24 -07003038 StringFilter pkgFilter = StringFilter.of(new HashSet<>(Arrays.asList(packages)));
Joe Onorato36115782010-06-17 13:28:48 -04003039 switch (mOp) {
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003040 case OP_ADD: {
Joe Onorato36115782010-06-17 13:28:48 -04003041 for (int i=0; i<N; i++) {
3042 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003043 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003044 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003045 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003046
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003047 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3048 if (heuristic != null) {
3049 heuristic.processPackageAdd(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003050 }
Joe Onorato36115782010-06-17 13:28:48 -04003051 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003052 }
Joe Onorato36115782010-06-17 13:28:48 -04003053 case OP_UPDATE:
3054 for (int i=0; i<N; i++) {
3055 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003056 mIconCache.updateIconsForPkg(packages[i], mUser);
Kenny Guyed131872014-04-30 03:02:21 +01003057 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003058 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003059 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003060 // Since package was just updated, the target must be available now.
3061 flagOp = FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04003062 break;
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003063 case OP_REMOVE: {
3064 ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser);
3065 if (heuristic != null) {
3066 heuristic.processPackageRemoved(mPackages);
Sunny Goyale0f58d72014-11-10 18:05:31 -08003067 }
Joe Onorato36115782010-06-17 13:28:48 -04003068 for (int i=0; i<N; i++) {
3069 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003070 mIconCache.removeIconsForPkg(packages[i], mUser);
3071 }
3072 // Fall through
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003073 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003074 case OP_UNAVAILABLE:
3075 for (int i=0; i<N; i++) {
3076 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
3077 mBgAllAppsList.removePackage(packages[i], mUser);
Sunny Goyal5b0e6692015-03-19 14:31:19 -07003078 mApp.getWidgetCache().removePackage(packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04003079 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003080 flagOp = FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE);
Joe Onorato36115782010-06-17 13:28:48 -04003081 break;
Kenny Guy44cba692016-01-21 19:50:02 +00003082 case OP_SUSPEND:
3083 case OP_UNSUSPEND:
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003084 flagOp = mOp == OP_SUSPEND ?
3085 FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED) :
3086 FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED);
Sunny Goyal17763cb2016-03-24 13:53:22 -07003087 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.(un)suspend " + N);
Sunny Goyalda891c12016-03-18 18:29:24 -07003088 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
3089 break;
3090 case OP_USER_AVAILABILITY_CHANGE:
3091 flagOp = UserManagerCompat.getInstance(context).isQuietModeEnabled(mUser)
3092 ? FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER)
3093 : FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_QUIET_USER);
3094 // We want to update all packages for this user.
3095 pkgFilter = StringFilter.matchesAll();
3096 mBgAllAppsList.updatePackageFlags(pkgFilter, mUser, flagOp);
Kenny Guy44cba692016-01-21 19:50:02 +00003097 break;
Joe Onorato36115782010-06-17 13:28:48 -04003098 }
3099
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003100 ArrayList<AppInfo> added = null;
3101 ArrayList<AppInfo> modified = null;
3102 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003103
Adam Cohen487f7dd2012-06-28 18:12:10 -07003104 if (mBgAllAppsList.added.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003105 added = new ArrayList<>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003106 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003107 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003108 if (mBgAllAppsList.modified.size() > 0) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003109 modified = new ArrayList<>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003110 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003111 }
Winson Chung5d55f332012-07-16 20:45:03 -07003112 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003113 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003114 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003115 }
3116
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003117 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps = new HashMap<>();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003118
Joe Onorato36115782010-06-17 13:28:48 -04003119 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003120 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003121 for (AppInfo ai : added) {
3122 addedOrUpdatedApps.put(ai.componentName, ai);
3123 }
Joe Onorato36115782010-06-17 13:28:48 -04003124 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003125
Joe Onorato36115782010-06-17 13:28:48 -04003126 if (modified != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003127 final Callbacks callbacks = getCallback();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003128 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003129 for (AppInfo ai : modified) {
3130 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003131 }
3132
Joe Onorato36115782010-06-17 13:28:48 -04003133 mHandler.post(new Runnable() {
3134 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003135 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003136 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003137 callbacks.bindAppsUpdated(modifiedFinal);
3138 }
3139 }
3140 });
3141 }
Winson Chung83892cc2013-05-01 16:53:33 -07003142
Sunny Goyal4390ace2014-10-13 11:33:11 -07003143 // Update shortcut infos
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003144 if (mOp == OP_ADD || flagOp != FlagOp.NO_OP) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003145 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3146 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3147 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3148
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003149 synchronized (sBgLock) {
Sunny Goyale2df0622015-04-24 11:27:00 -07003150 for (ItemInfo info : sBgItemsIdMap) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003151 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3152 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003153 boolean infoUpdated = false;
3154 boolean shortcutUpdated = false;
3155
3156 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003157 if ((si.iconResource != null)
Sunny Goyalda891c12016-03-18 18:29:24 -07003158 && pkgFilter.matches(si.iconResource.packageName)) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003159 Bitmap icon = Utilities.createIconBitmap(
3160 si.iconResource.packageName,
3161 si.iconResource.resourceName, context);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003162 if (icon != null) {
3163 si.setIcon(icon);
3164 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003165 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003166 }
3167 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003168
3169 ComponentName cn = si.getTargetComponent();
Sunny Goyalda891c12016-03-18 18:29:24 -07003170 if (cn != null && pkgFilter.matches(cn.getPackageName())) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003171 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3172
3173 if (si.isPromise()) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003174 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3175 // Auto install icon
3176 PackageManager pm = context.getPackageManager();
3177 ResolveInfo matched = pm.resolveActivity(
3178 new Intent(Intent.ACTION_MAIN)
3179 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3180 PackageManager.MATCH_DEFAULT_ONLY);
3181 if (matched == null) {
3182 // Try to find the best match activity.
3183 Intent intent = pm.getLaunchIntentForPackage(
3184 cn.getPackageName());
3185 if (intent != null) {
3186 cn = intent.getComponent();
3187 appInfo = addedOrUpdatedApps.get(cn);
3188 }
3189
3190 if ((intent == null) || (appInfo == null)) {
3191 removedShortcuts.add(si);
3192 continue;
3193 }
3194 si.promisedIntent = intent;
3195 }
3196 }
3197
3198 // Restore the shortcut.
Sunny Goyalfa401a12015-04-10 13:45:42 -07003199 if (appInfo != null) {
3200 si.flags = appInfo.flags;
3201 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003202
Sunny Goyal756adbc2015-04-16 15:20:51 -07003203 si.intent = si.promisedIntent;
3204 si.promisedIntent = null;
3205 si.status = ShortcutInfo.DEFAULT;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003206 infoUpdated = true;
3207 si.updateIcon(mIconCache);
3208 }
3209
3210 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3211 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3212 si.updateIcon(mIconCache);
Winson Chung82b016c2015-05-08 17:00:10 -07003213 si.title = Utilities.trim(appInfo.title);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003214 si.contentDescription = appInfo.contentDescription;
3215 infoUpdated = true;
3216 }
3217
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003218 int oldDisabledFlags = si.isDisabled;
3219 si.isDisabled = flagOp.apply(si.isDisabled);
3220 if (si.isDisabled != oldDisabledFlags) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003221 shortcutUpdated = true;
3222 }
3223 }
3224
3225 if (infoUpdated || shortcutUpdated) {
3226 updatedShortcuts.add(si);
3227 }
3228 if (infoUpdated) {
3229 updateItemInDatabase(context, si);
3230 }
Sunny Goyalda891c12016-03-18 18:29:24 -07003231 } else if (info instanceof LauncherAppWidgetInfo && mOp == OP_ADD) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003232 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3233 if (mUser.equals(widgetInfo.user)
3234 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
Sunny Goyalda891c12016-03-18 18:29:24 -07003235 && pkgFilter.matches(widgetInfo.providerName.getPackageName())) {
Sunny Goyal53f96722015-07-13 19:54:53 -07003236 widgetInfo.restoreStatus &=
3237 ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY &
3238 ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003239
3240 // adding this flag ensures that launcher shows 'click to setup'
3241 // if the widget has a config activity. In case there is no config
3242 // activity, it will be marked as 'restored' during bind.
3243 widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3244
Sunny Goyal4390ace2014-10-13 11:33:11 -07003245 widgets.add(widgetInfo);
3246 updateItemInDatabase(context, widgetInfo);
3247 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003248 }
3249 }
3250 }
3251
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003252 bindUpdatedShortcuts(updatedShortcuts, removedShortcuts, mUser);
3253 if (!removedShortcuts.isEmpty()) {
3254 deleteItemsFromDatabase(context, removedShortcuts);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003255 }
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003256
Sunny Goyal4390ace2014-10-13 11:33:11 -07003257 if (!widgets.isEmpty()) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003258 final Callbacks callbacks = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003259 mHandler.post(new Runnable() {
3260 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003261 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003262 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003263 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003264 }
3265 }
3266 });
3267 }
3268 }
3269
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003270 final HashSet<String> removedPackages = new HashSet<>();
3271 final HashSet<ComponentName> removedComponents = new HashSet<>();
3272 if (mOp == OP_REMOVE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003273 // Mark all packages in the broadcast to be removed
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003274 Collections.addAll(removedPackages, packages);
3275
3276 // No need to update the removedComponents as
3277 // removedPackages is a super-set of removedComponents
Winson Chungdf95eb12013-10-16 14:57:07 -07003278 } else if (mOp == OP_UPDATE) {
3279 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003280 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003281 if (isPackageDisabled(context, packages[i], mUser)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003282 removedPackages.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003283 }
3284 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003285
3286 // Update removedComponents as some components can get removed during package update
3287 for (AppInfo info : removedApps) {
3288 removedComponents.add(info.componentName);
3289 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003290 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003291
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003292 if (!removedPackages.isEmpty() || !removedComponents.isEmpty()) {
3293 for (String pn : removedPackages) {
3294 deletePackageFromDatabase(context, pn, mUser);
3295 }
3296 for (ComponentName cn : removedComponents) {
3297 deleteItemsFromDatabase(context, getItemInfoForComponentName(cn, mUser));
Sunny Goyal1a745e82014-10-02 15:58:31 -07003298 }
3299
Winson Chungdf95eb12013-10-16 14:57:07 -07003300 // Remove any queued items from the install queue
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003301 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackages, mUser);
3302
Winson Chungdf95eb12013-10-16 14:57:07 -07003303 // Call the components-removed callback
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003304 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003305 mHandler.post(new Runnable() {
3306 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003307 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003308 if (callbacks == cb && cb != null) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003309 callbacks.bindWorkspaceComponentsRemoved(
3310 removedPackages, removedComponents, mUser);
3311 }
3312 }
3313 });
3314 }
3315
3316 if (!removedApps.isEmpty()) {
3317 // Remove corresponding apps from All-Apps
3318 final Callbacks callbacks = getCallback();
3319 mHandler.post(new Runnable() {
3320 public void run() {
3321 Callbacks cb = getCallback();
3322 if (callbacks == cb && cb != null) {
3323 callbacks.bindAppInfosRemoved(removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04003324 }
3325 }
3326 });
Joe Onoratobe386092009-11-17 17:32:16 -08003327 }
Hyunyoung Song227239e2015-05-04 18:17:35 -07003328
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003329 // Notify launcher of widget update. From marshmallow onwards we use AppWidgetHost to
3330 // get widget update signals.
3331 if (!Utilities.ATLEAST_MARSHMALLOW &&
3332 (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE)) {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07003333 final Callbacks callbacks = getCallback();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003334 mHandler.post(new Runnable() {
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003335 public void run() {
3336 Callbacks cb = getCallback();
3337 if (callbacks == cb && cb != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003338 callbacks.notifyWidgetProvidersChanged();
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003339 }
3340 }
3341 });
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003342 }
3343 }
3344 }
3345
Sunny Goyal10923b32016-07-20 15:42:44 -07003346 /**
3347 * Repopulates the shortcut info, possibly updating any icon already on the workspace.
3348 */
3349 public void updateShortcutInfo(final ShortcutInfoCompat fullDetail, final ShortcutInfo info) {
3350 enqueueItemUpdatedTask(new Runnable() {
3351 @Override
3352 public void run() {
3353 info.updateFromDeepShortcutInfo(
3354 fullDetail, LauncherAppState.getInstance().getContext());
3355 ArrayList<ShortcutInfo> update = new ArrayList<ShortcutInfo>();
3356 update.add(info);
3357 bindUpdatedShortcuts(update, fullDetail.getUserHandle());
3358 }
3359 });
3360 }
3361
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003362 private class ShortcutsChangedTask implements Runnable {
Sunny Goyal50941fb2016-08-04 12:03:52 -07003363 private final String mPackageName;
3364 private final List<ShortcutInfoCompat> mShortcuts;
3365 private final UserHandleCompat mUser;
3366 private final boolean mUpdateIdMap;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003367
3368 public ShortcutsChangedTask(String packageName, List<ShortcutInfoCompat> shortcuts,
Sunny Goyal50941fb2016-08-04 12:03:52 -07003369 UserHandleCompat user, boolean updateIdMap) {
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003370 mPackageName = packageName;
3371 mShortcuts = shortcuts;
3372 mUser = user;
Sunny Goyal50941fb2016-08-04 12:03:52 -07003373 mUpdateIdMap = updateIdMap;
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003374 }
3375
3376 @Override
3377 public void run() {
3378 mDeepShortcutManager.onShortcutsChanged(mShortcuts);
3379
3380 Map<String, ShortcutInfoCompat> idsToShortcuts = new HashMap<>();
3381 for (ShortcutInfoCompat shortcut : mShortcuts) {
3382 idsToShortcuts.put(shortcut.getId(), shortcut);
3383 }
3384
3385 // Find ShortcutInfo's that have changed on the workspace.
3386 MultiHashMap<String, ShortcutInfo> idsToWorkspaceShortcutInfos = new MultiHashMap<>();
3387 for (ItemInfo itemInfo : sBgItemsIdMap) {
3388 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
3389 ShortcutInfo si = (ShortcutInfo) itemInfo;
Tony Wickham672d07f2016-07-27 18:22:41 -07003390 if (si.getIntent().getPackage().equals(mPackageName) && si.user.equals(mUser)) {
3391 String shortcutId = si.getDeepShortcutId();
3392 if (idsToShortcuts.containsKey(shortcutId)) {
3393 idsToWorkspaceShortcutInfos.addToList(shortcutId, si);
3394 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003395 }
3396 }
3397 }
3398
3399 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3400 List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager.queryForFullDetails(
3401 mPackageName, new ArrayList<>(idsToWorkspaceShortcutInfos.keySet()), mUser);
3402 ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3403 Context context = LauncherAppState.getInstance().getContext();
3404 for (ShortcutInfoCompat fullDetails : shortcuts) {
3405 List<ShortcutInfo> shortcutInfos = idsToWorkspaceShortcutInfos
3406 .get(fullDetails.getId());
3407 for (ShortcutInfo shortcutInfo : shortcutInfos) {
Sunny Goyal9994b2b2016-06-23 14:17:24 -07003408 shortcutInfo.updateFromDeepShortcutInfo(fullDetails, context);
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003409 updatedShortcutInfos.add(shortcutInfo);
3410 }
3411 }
3412 bindUpdatedShortcuts(updatedShortcutInfos, mUser);
3413
Sunny Goyal50941fb2016-08-04 12:03:52 -07003414 if (mUpdateIdMap) {
3415 // Update the deep shortcut map if the list of ids has changed for an activity.
3416 updateDeepShortcutMap(mPackageName, mUser, mShortcuts);
3417 bindDeepShortcuts();
3418 }
Tony Wickhambfbf7f92016-05-19 11:19:39 -07003419 }
3420 }
3421
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003422 /**
3423 * Task to handle changing of lock state of the user
3424 */
3425 private class UserLockStateChangedTask implements Runnable {
3426
3427 private final UserHandleCompat mUser;
3428
3429 public UserLockStateChangedTask(UserHandleCompat user) {
3430 mUser = user;
3431 }
3432
3433 @Override
3434 public void run() {
3435 boolean isUserUnlocked = mUserManager.isUserUnlocked(mUser);
3436 Context context = mApp.getContext();
3437
3438 HashMap<ShortcutKey, ShortcutInfoCompat> pinnedShortcuts = new HashMap<>();
3439 if (isUserUnlocked) {
Sunny Goyal49f4f032016-08-01 15:45:49 -07003440 List<ShortcutInfoCompat> shortcuts =
3441 mDeepShortcutManager.queryForPinnedShortcuts(null, mUser);
3442 if (mDeepShortcutManager.wasLastCallSuccess()) {
3443 for (ShortcutInfoCompat shortcut : shortcuts) {
3444 pinnedShortcuts.put(ShortcutKey.fromInfo(shortcut), shortcut);
3445 }
3446 } else {
3447 // Shortcut manager can fail due to some race condition when the lock state
3448 // changes too frequently. For the purpose of the update,
3449 // consider it as still locked.
3450 isUserUnlocked = false;
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003451 }
3452 }
3453
3454 // Update the workspace to reflect the changes to updated shortcuts residing on it.
3455 ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
3456 ArrayList<ShortcutInfo> deletedShortcutInfos = new ArrayList<>();
3457 for (ItemInfo itemInfo : sBgItemsIdMap) {
3458 if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT
3459 && mUser.equals(itemInfo.user)) {
3460 ShortcutInfo si = (ShortcutInfo) itemInfo;
3461 if (isUserUnlocked) {
3462 ShortcutInfoCompat shortcut =
3463 pinnedShortcuts.get(ShortcutKey.fromItemInfo(si));
3464 // We couldn't verify the shortcut during loader. If its no longer available
3465 // (probably due to clear data), delete the workspace item as well
3466 if (shortcut == null) {
3467 deletedShortcutInfos.add(si);
3468 continue;
3469 }
3470 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
3471 si.updateFromDeepShortcutInfo(shortcut, context);
3472 } else {
3473 si.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER;
3474 }
3475 updatedShortcutInfos.add(si);
3476 }
3477 }
3478 bindUpdatedShortcuts(updatedShortcutInfos, deletedShortcutInfos, mUser);
3479 if (!deletedShortcutInfos.isEmpty()) {
3480 deleteItemsFromDatabase(context, deletedShortcutInfos);
3481 }
3482
3483 // Remove shortcut id map for that user
3484 Iterator<ComponentKey> keysIter = mBgDeepShortcutMap.keySet().iterator();
3485 while (keysIter.hasNext()) {
3486 if (keysIter.next().user.equals(mUser)) {
3487 keysIter.remove();
3488 }
3489 }
3490
3491 if (isUserUnlocked) {
3492 updateDeepShortcutMap(null, mUser, mDeepShortcutManager.queryForAllShortcuts(mUser));
3493 }
3494 bindDeepShortcuts();
3495 }
3496 }
3497
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003498 private void bindWidgetsModel(final Callbacks callbacks, final WidgetsModel model) {
3499 mHandler.post(new Runnable() {
3500 @Override
3501 public void run() {
3502 Callbacks cb = getCallback();
3503 if (callbacks == cb && cb != null) {
3504 callbacks.bindWidgetsModel(model);
3505 }
Hyunyoung Songd4af1482015-04-20 20:40:03 -07003506 }
3507 });
3508 }
3509
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003510 public void refreshAndBindWidgetsAndShortcuts(
3511 final Callbacks callbacks, final boolean bindFirst) {
3512 runOnWorkerThread(new Runnable() {
3513 @Override
3514 public void run() {
3515 if (bindFirst && !mBgWidgetsModel.isEmpty()) {
3516 bindWidgetsModel(callbacks, mBgWidgetsModel.clone());
Sunny Goyal31860582015-09-18 08:38:57 -07003517 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003518 final WidgetsModel model = mBgWidgetsModel.updateAndClone(mApp.getContext());
3519 bindWidgetsModel(callbacks, model);
3520 // update the Widget entries inside DB on the worker thread.
3521 LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(
3522 model.getRawList());
Sunny Goyal31860582015-09-18 08:38:57 -07003523 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003524 });
Michael Jurkac402cd92013-05-20 15:49:32 +02003525 }
3526
Adam Cohen091440a2015-03-18 14:16:05 -07003527 @Thunk static boolean isPackageDisabled(Context context, String packageName,
Kenny Guyed131872014-04-30 03:02:21 +01003528 UserHandleCompat user) {
3529 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3530 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003531 }
Adam Cohen556f6132014-01-15 15:18:08 -08003532
Kenny Guyed131872014-04-30 03:02:21 +01003533 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3534 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003535 if (cn == null) {
3536 return false;
3537 }
Kenny Guyed131872014-04-30 03:02:21 +01003538 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3539 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003540 return false;
3541 }
Kenny Guyed131872014-04-30 03:02:21 +01003542 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003543 }
3544
Adam Cohena28b78e2014-05-20 17:03:04 -07003545 public static boolean isValidPackage(Context context, String packageName,
3546 UserHandleCompat user) {
3547 if (packageName == null) {
3548 return false;
3549 }
3550 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3551 return launcherApps.isPackageEnabledForProfile(packageName, user);
3552 }
3553
Joe Onorato9c1289c2009-08-17 11:03:03 -04003554 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003555 * Make an ShortcutInfo object for a restored application or shortcut item that points
3556 * to a package that is not yet installed on the system.
3557 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003558 public ShortcutInfo getRestoredItemInfo(Cursor c, Intent intent,
3559 int promiseType, int itemType, CursorIconInfo iconInfo) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003560 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003561 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003562
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003563 Bitmap icon = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003564 // the fallback icon
3565 if (icon == null) {
3566 mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */);
3567 } else {
3568 info.setIcon(icon);
3569 }
Sunny Goyal34942622014-08-29 17:20:55 -07003570
3571 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003572 String title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07003573 if (!TextUtils.isEmpty(title)) {
Winson Chung82b016c2015-05-08 17:00:10 -07003574 info.title = Utilities.trim(title);
Sunny Goyal34942622014-08-29 17:20:55 -07003575 }
Sunny Goyal34942622014-08-29 17:20:55 -07003576 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3577 if (TextUtils.isEmpty(info.title)) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003578 info.title = iconInfo.getTitle(c);
Sunny Goyal34942622014-08-29 17:20:55 -07003579 }
Sunny Goyal34942622014-08-29 17:20:55 -07003580 } else {
3581 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3582 }
3583
Winson Chung82b016c2015-05-08 17:00:10 -07003584 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalc22841b2015-07-13 19:59:50 -07003585 info.itemType = itemType;
Sunny Goyal34942622014-08-29 17:20:55 -07003586 info.promisedIntent = intent;
Sunny Goyal756adbc2015-04-16 15:20:51 -07003587 info.status = promiseType;
Chris Wrenf4d08112014-01-16 18:13:56 -05003588 return info;
3589 }
3590
3591 /**
3592 * Make an Intent object for a restored application or shortcut item that points
3593 * to the market page for the item.
3594 */
Adam Cohen091440a2015-03-18 14:16:05 -07003595 @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003596 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003597 return getMarketIntent(componentName.getPackageName());
3598 }
3599
3600 static Intent getMarketIntent(String packageName) {
3601 return new Intent(Intent.ACTION_VIEW)
3602 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003603 .scheme("market")
3604 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003605 .appendQueryParameter("id", packageName)
3606 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003607 }
3608
3609 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003610 * Make an ShortcutInfo object for a shortcut that is an application.
3611 *
3612 * If c is not null, then it will be used to fill in missing data like the title and icon.
3613 */
Sunny Goyald09c3702016-04-06 16:18:20 -07003614 public ShortcutInfo getAppShortcutInfo(Intent intent,
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003615 UserHandleCompat user, Cursor c, CursorIconInfo iconInfo,
Sunny Goyal34b65272015-03-11 16:56:52 -07003616 boolean allowMissingTarget, boolean useLowResIcon) {
Kenny Guyed131872014-04-30 03:02:21 +01003617 if (user == null) {
3618 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003619 return null;
3620 }
3621
Kenny Guyed131872014-04-30 03:02:21 +01003622 ComponentName componentName = intent.getComponent();
3623 if (componentName == null) {
Sunny Goyalb740f592015-12-17 23:22:42 -08003624 Log.d(TAG, "Missing component found in getShortcutInfo");
Kenny Guyed131872014-04-30 03:02:21 +01003625 return null;
3626 }
3627
3628 Intent newIntent = new Intent(intent.getAction(), null);
3629 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3630 newIntent.setComponent(componentName);
3631 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003632 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003633 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3634 return null;
3635 }
3636
3637 final ShortcutInfo info = new ShortcutInfo();
Sunny Goyal34b65272015-03-11 16:56:52 -07003638 mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003639 if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003640 Bitmap icon = iconInfo.loadIcon(c);
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003641 info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon);
Kenny Guyed131872014-04-30 03:02:21 +01003642 }
3643
Sunny Goyald09c3702016-04-06 16:18:20 -07003644 if (lai != null && PackageManagerHelper.isAppSuspended(lai.getApplicationInfo())) {
3645 info.isDisabled = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
3646 }
3647
Joe Onorato56d82912010-03-07 14:32:10 -05003648 // from the db
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003649 if (TextUtils.isEmpty(info.title) && c != null) {
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003650 info.title = iconInfo.getTitle(c);
Joe Onorato56d82912010-03-07 14:32:10 -05003651 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003652
Joe Onorato56d82912010-03-07 14:32:10 -05003653 // fall back to the class name of the activity
3654 if (info.title == null) {
3655 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003656 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08003657
Joe Onorato9c1289c2009-08-17 11:03:03 -04003658 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003659 info.user = user;
Winson Chung82b016c2015-05-08 17:00:10 -07003660 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Sunny Goyalfa401a12015-04-10 13:45:42 -07003661 if (lai != null) {
3662 info.flags = AppInfo.initFlags(lai);
3663 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003664 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003665 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003666
Sunny Goyale2df0622015-04-24 11:27:00 -07003667 static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos,
Winson Chung64359a52013-07-08 17:17:08 -07003668 ItemInfoFilter f) {
3669 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3670 for (ItemInfo i : infos) {
3671 if (i instanceof ShortcutInfo) {
3672 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003673 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003674 if (cn != null && f.filterItem(null, info, cn)) {
3675 filtered.add(info);
3676 }
3677 } else if (i instanceof FolderInfo) {
3678 FolderInfo info = (FolderInfo) i;
3679 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003680 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003681 if (cn != null && f.filterItem(info, s, cn)) {
3682 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003683 }
3684 }
Winson Chung64359a52013-07-08 17:17:08 -07003685 } else if (i instanceof LauncherAppWidgetInfo) {
3686 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3687 ComponentName cn = info.providerName;
3688 if (cn != null && f.filterItem(null, info, cn)) {
3689 filtered.add(info);
3690 }
Winson Chung8a435102012-08-30 17:16:53 -07003691 }
3692 }
Winson Chung64359a52013-07-08 17:17:08 -07003693 return new ArrayList<ItemInfo>(filtered);
3694 }
3695
Adam Cohen091440a2015-03-18 14:16:05 -07003696 @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
Kenny Guyed131872014-04-30 03:02:21 +01003697 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003698 ItemInfoFilter filter = new ItemInfoFilter() {
3699 @Override
3700 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003701 if (info.user == null) {
3702 return cn.equals(cname);
3703 } else {
3704 return cn.equals(cname) && info.user.equals(user);
3705 }
Winson Chung64359a52013-07-08 17:17:08 -07003706 }
3707 };
Sunny Goyale2df0622015-04-24 11:27:00 -07003708 return filterItemInfos(sBgItemsIdMap, filter);
Winson Chung64359a52013-07-08 17:17:08 -07003709 }
3710
Sunny Goyal1a745e82014-10-02 15:58:31 -07003711 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003712 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003713 */
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003714 @Thunk ShortcutInfo getShortcutInfo(Cursor c, CursorIconInfo iconInfo) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07003715 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003716 // Non-app shortcuts are only supported for current user.
3717 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003718 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003719
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003720 // TODO: If there's an explicit component and we can't install that, delete it.
3721
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003722 loadInfoFromCursor(info, c, iconInfo);
3723 return info;
3724 }
Joe Onorato56d82912010-03-07 14:32:10 -05003725
Sunny Goyald3b87ef2016-07-28 12:11:54 -07003726 /**
3727 * Make an ShortcutInfo object for a shortcut that isn't an application.
3728 */
3729 public void loadInfoFromCursor(ShortcutInfo info, Cursor c, CursorIconInfo iconInfo) {
3730 info.title = iconInfo.getTitle(c);
3731 Bitmap icon = iconInfo.loadIcon(c, info);
Sunny Goyal4e5cc642015-06-25 16:37:44 -07003732 // the fallback icon
3733 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003734 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003735 info.usingFallbackIcon = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003736 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003737 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003738 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003739
Sunny Goyal2350bc92014-10-14 16:42:54 -07003740 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003741 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3742 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3743 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3744
Adam Cohend9198822011-11-22 16:42:47 -08003745 if (intent == null) {
3746 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3747 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3748 return null;
3749 }
3750
Joe Onorato0589f0f2010-02-08 13:44:00 -08003751 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003752 boolean customIcon = false;
3753 ShortcutIconResource iconResource = null;
3754
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003755 if (bitmap instanceof Bitmap) {
3756 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003757 customIcon = true;
3758 } else {
3759 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003760 if (extra instanceof ShortcutIconResource) {
3761 iconResource = (ShortcutIconResource) extra;
3762 icon = Utilities.createIconBitmap(iconResource.packageName,
Sunny Goyal53d7ee42015-05-22 12:25:45 -07003763 iconResource.resourceName, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003764 }
3765 }
3766
Michael Jurkac9d95c52011-08-29 14:03:34 -07003767 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003768
Kenny Guyed131872014-04-30 03:02:21 +01003769 // Only support intents for current user for now. Intents sent from other
3770 // users wouldn't get here without intent forwarding anyway.
3771 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003772 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003773 icon = mIconCache.getDefaultIcon(info.user);
3774 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003775 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003776 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003777
Winson Chung82b016c2015-05-08 17:00:10 -07003778 info.title = Utilities.trim(name);
3779 info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003780 info.intent = intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003781 info.iconResource = iconResource;
3782
3783 return info;
3784 }
3785
Joe Onorato9c1289c2009-08-17 11:03:03 -04003786 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003787 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003788 * or make a new one.
3789 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003790 @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003791 // See if a placeholder was created for us already
3792 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003793 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003794 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003795 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003796 folders.put(id, folderInfo);
3797 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003798 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003799 }
3800
Joe Onoratobe386092009-11-17 17:32:16 -08003801
Sunny Goyal651077b2014-06-30 14:15:31 -07003802 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3803 return (provider != null) && (provider.provider != null)
3804 && (provider.provider.getPackageName() != null);
3805 }
3806
Joe Onoratobe386092009-11-17 17:32:16 -08003807 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003808 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003809 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3810 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3811 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3812 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003813 if (mLoaderTask != null) {
3814 mLoaderTask.dumpState();
3815 } else {
3816 Log.d(TAG, "mLoaderTask=null");
3817 }
Joe Onoratobe386092009-11-17 17:32:16 -08003818 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003819
3820 public Callbacks getCallback() {
3821 return mCallbacks != null ? mCallbacks.get() : null;
3822 }
Sunny Goyal18bf8e22015-04-08 18:13:46 -07003823
3824 /**
3825 * @return {@link FolderInfo} if its already loaded.
3826 */
3827 public FolderInfo findFolderById(Long folderId) {
3828 synchronized (sBgLock) {
3829 return sBgFolders.get(folderId);
3830 }
3831 }
Sunny Goyal756adbc2015-04-16 15:20:51 -07003832
Sunny Goyal527c7d32015-08-28 15:19:36 -07003833 @Thunk class DeferredMainThreadExecutor implements Executor {
3834
3835 @Override
3836 public void execute(Runnable command) {
3837 runOnMainThread(command);
3838 }
3839 }
3840
Sunny Goyal756adbc2015-04-16 15:20:51 -07003841 /**
3842 * @return the looper for the worker thread which can be used to start background tasks.
3843 */
3844 public static Looper getWorkerLooper() {
3845 return sWorkerThread.getLooper();
3846 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003847}